diff --git a/common/api.ts b/common/api.ts new file mode 100644 index 00000000..02dba409 --- /dev/null +++ b/common/api.ts @@ -0,0 +1,22 @@ +import { ENV_CONFIG } from 'common/envs/constants' + +export class APIError extends Error { + code: number + details?: unknown + constructor(code: number, message: string, details?: unknown) { + super(message) + this.code = code + this.name = 'APIError' + this.details = details + } +} + +export function getFunctionUrl(name: string) { + if (process.env.NEXT_PUBLIC_FIREBASE_EMULATE) { + const { projectId, region } = ENV_CONFIG.firebaseConfig + return `http://localhost:5001/${projectId}/${region}/${name}` + } else { + const { cloudRunId, cloudRunRegion } = ENV_CONFIG + return `https://${name}-${cloudRunId}-${cloudRunRegion}.a.run.app` + } +} diff --git a/functions/src/api.ts b/functions/src/api.ts index 6ebffc24..8c01ea05 100644 --- a/functions/src/api.ts +++ b/functions/src/api.ts @@ -3,13 +3,14 @@ import { logger } from 'firebase-functions/v2' import { HttpsOptions, onRequest, Request } from 'firebase-functions/v2/https' import { log } from './utils' import { z } from 'zod' - +import { APIError } from '../../common/api' import { PrivateUser } from '../../common/user' import { CORS_ORIGIN_MANIFOLD, CORS_ORIGIN_LOCALHOST, CORS_ORIGIN_VERCEL, } from '../../common/envs/constants' +export { APIError } from '../../common/api' type Output = Record type AuthedUser = { @@ -21,17 +22,6 @@ type JwtCredentials = { kind: 'jwt'; data: admin.auth.DecodedIdToken } type KeyCredentials = { kind: 'key'; data: string } type Credentials = JwtCredentials | KeyCredentials -export class APIError { - code: number - msg: string - details: unknown - constructor(code: number, msg: string, details?: unknown) { - this.code = code - this.msg = msg - this.details = details - } -} - const auth = admin.auth() const firestore = admin.firestore() const privateUsers = firestore.collection( @@ -136,7 +126,7 @@ export const newEndpoint = (endpointOpts: EndpointOptions, fn: Handler) => { res.status(200).json(await fn(req, authedUser)) } catch (e) { if (e instanceof APIError) { - const output: { [k: string]: unknown } = { message: e.msg } + const output: { [k: string]: unknown } = { message: e.message } if (e.details != null) { output.details = e.details } diff --git a/functions/src/email-templates/market-close.html b/functions/src/email-templates/market-close.html index 150987cc..711f7ccb 100644 --- a/functions/src/email-templates/market-close.html +++ b/functions/src/email-templates/market-close.html @@ -613,7 +613,7 @@ >our Discord! Or,