Fix API calls to hit emulated Firebase (#424)
This commit is contained in:
parent
33ddd86add
commit
069b88d6fd
|
@ -1,7 +1,7 @@
|
||||||
import { NextApiRequest, NextApiResponse } from 'next'
|
import { NextApiRequest, NextApiResponse } from 'next'
|
||||||
import { FIREBASE_CONFIG } from 'common/envs/constants'
|
|
||||||
import { promisify } from 'util'
|
import { promisify } from 'util'
|
||||||
import { pipeline } from 'stream'
|
import { pipeline } from 'stream'
|
||||||
|
import { getFunctionUrl } from 'web/lib/firebase/api-call'
|
||||||
import fetch, { Headers, Response } from 'node-fetch'
|
import fetch, { Headers, Response } from 'node-fetch'
|
||||||
|
|
||||||
function getProxiedRequestHeaders(req: NextApiRequest, whitelist: string[]) {
|
function getProxiedRequestHeaders(req: NextApiRequest, whitelist: string[]) {
|
||||||
|
@ -33,8 +33,7 @@ function getProxiedResponseHeaders(res: Response, whitelist: string[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const fetchBackend = (req: NextApiRequest, endpoint: string) => {
|
export const fetchBackend = (req: NextApiRequest, endpoint: string) => {
|
||||||
const { projectId, region } = FIREBASE_CONFIG
|
const url = getFunctionUrl(endpoint)
|
||||||
const url = `https://${region}-${projectId}.cloudfunctions.net/${endpoint}`
|
|
||||||
const headers = getProxiedRequestHeaders(req, [
|
const headers = getProxiedRequestHeaders(req, [
|
||||||
'Authorization',
|
'Authorization',
|
||||||
'Content-Length',
|
'Content-Length',
|
||||||
|
|
|
@ -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
|
// app just hit the cloud functions directly -- there's no difference and it's
|
||||||
// one less hop
|
// one less hop
|
||||||
|
|
||||||
function getFunctionUrl(name: string) {
|
export function getFunctionUrl(name: string) {
|
||||||
const { projectId, region } = FIREBASE_CONFIG
|
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) {
|
export function createContract(params: any) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user