Fix API calls to hit emulated Firebase (#424)

This commit is contained in:
Marshall Polaris 2022-06-04 15:19:46 -07:00 committed by GitHub
parent 33ddd86add
commit 069b88d6fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 5 deletions

View File

@ -1,7 +1,7 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { FIREBASE_CONFIG } from 'common/envs/constants'
import { promisify } from 'util'
import { pipeline } from 'stream'
import { getFunctionUrl } from 'web/lib/firebase/api-call'
import fetch, { Headers, Response } from 'node-fetch'
function getProxiedRequestHeaders(req: NextApiRequest, whitelist: string[]) {
@ -33,8 +33,7 @@ function getProxiedResponseHeaders(res: Response, whitelist: string[]) {
}
export const fetchBackend = (req: NextApiRequest, endpoint: string) => {
const { projectId, region } = FIREBASE_CONFIG
const url = `https://${region}-${projectId}.cloudfunctions.net/${endpoint}`
const url = getFunctionUrl(endpoint)
const headers = getProxiedRequestHeaders(req, [
'Authorization',
'Content-Length',

View File

@ -38,9 +38,11 @@ export async function call(url: string, method: string, params: any) {
// app just hit the cloud functions directly -- there's no difference and it's
// one less hop
function getFunctionUrl(name: string) {
export function getFunctionUrl(name: string) {
const { projectId, region } = FIREBASE_CONFIG
return `https://${region}-${projectId}.cloudfunctions.net/${name}`
return process.env.NEXT_PUBLIC_FIREBASE_EMULATE
? `http://localhost:5001/${projectId}/${region}/${name}`
: `https://${region}-${projectId}.cloudfunctions.net/${name}`
}
export function createContract(params: any) {