diff --git a/apps/server/client/lib/api-fetch.ts b/apps/server/client/lib/api-fetch.ts deleted file mode 100644 index 64ea7793d..000000000 --- a/apps/server/client/lib/api-fetch.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { env } from "@follow/shared/env" -import type { AppType } from "@follow/shared/hono" -import { getCsrfToken } from "@hono/auth-js/react" -import { hc } from "hono/client" -import { ofetch } from "ofetch" - -let csrfTokenPromise: Promise | null = null -export const apiFetch = ofetch.create({ - baseURL: env.VITE_EXTERNAL_API_URL || env.VITE_API_URL, - credentials: "include", - retry: false, - onRequest: async ({ options }) => { - if (!csrfTokenPromise) { - csrfTokenPromise = getCsrfToken() - } - - const csrfToken = await csrfTokenPromise - - const header = new Headers(options.headers) - - header.set("x-app-version", "Web External") - header.set("X-Csrf-Token", csrfToken) - options.headers = header - }, -}) - -export const apiClient = hc(env.VITE_EXTERNAL_API_URL || env.VITE_API_URL, { - fetch: async (input: any, options = {}) => - apiFetch(input.toString(), options).catch((err) => { - throw err - }), - headers() { - return { - "X-App-Version": "Web External", - } - }, -}) diff --git a/apps/server/index.ts b/apps/server/index.ts index ecbee4988..8f8bd095a 100644 --- a/apps/server/index.ts +++ b/apps/server/index.ts @@ -15,6 +15,8 @@ const isVercel = process.env.VERCEL === "1" declare module "@fastify/request-context" { interface RequestContextData { req: FastifyRequest + + upstreamEnv: "prod" | "dev" } } @@ -28,6 +30,10 @@ export const createApp = async () => { app.addHook("onRequest", (req, reply, done) => { req.requestContext.set("req", req) + + const { host } = req.headers + + if (!isDev) req.requestContext.set("upstreamEnv", host?.includes("dev") ? "dev" : "prod") done() }) diff --git a/apps/server/src/lib/api-client.ts b/apps/server/src/lib/api-client.ts index 5297b8975..7db3de93a 100644 --- a/apps/server/src/lib/api-client.ts +++ b/apps/server/src/lib/api-client.ts @@ -19,8 +19,16 @@ export const createApiClient = (authSessionToken: string) => { if (env.VITE_EXTERNAL_API_URL?.startsWith("/")) { baseURL = `http://${host}${env.VITE_EXTERNAL_API_URL}` } + + const upstreamEnv = req.requestContext.get("upstreamEnv") + if (upstreamEnv === "dev" && env.VITE_EXTERNAL_DEV_API_URL) { + baseURL = env.VITE_EXTERNAL_DEV_API_URL + } + if (upstreamEnv === "prod" && env.VITE_EXTERNAL_PROD_API_URL) { + baseURL = env.VITE_EXTERNAL_PROD_API_URL + } + const apiFetch = ofetch.create({ - // baseURL, credentials: "include", retry: false, diff --git a/apps/server/src/router/global.ts b/apps/server/src/router/global.ts index 505ecb092..54017514e 100644 --- a/apps/server/src/router/global.ts +++ b/apps/server/src/router/global.ts @@ -2,6 +2,7 @@ import { readFileSync } from "node:fs" import path, { resolve } from "node:path" +import { env } from "@follow/shared/env" import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify" import { parseHTML } from "linkedom" import { FetchError } from "ofetch" @@ -51,6 +52,35 @@ const prodHandler = (app: FastifyInstance) => { for (const link of document.querySelectorAll("link")) { link.href = `${prefix}${link.href}` } + + const upstreamEnv = req.requestContext.get("upstreamEnv") + + if (upstreamEnv) { + document.head.prepend(document.createComment(`upstreamEnv: ${upstreamEnv}`)) + + // override client side api url + + const injectScript = (apiUrl: string) => { + const template = `function injectEnv(env2) { + for (const key in env2) { + if (env2[key] === void 0) continue; + globalThis["__followEnv"] ??= {}; + globalThis["__followEnv"][key] = env2[key]; + } + } +injectEnv({"VITE_API_URL":"${apiUrl}"})` + const $script = document.createElement("script") + $script.innerHTML = template + document.head.prepend($script) + } + if (upstreamEnv === "dev" && env.VITE_EXTERNAL_DEV_API_URL) { + injectScript(env.VITE_EXTERNAL_DEV_API_URL) + } + if (upstreamEnv === "prod" && env.VITE_EXTERNAL_PROD_API_URL) { + injectScript(env.VITE_EXTERNAL_PROD_API_URL) + } + } + template = document.toString() } diff --git a/packages/shared/src/env.ts b/packages/shared/src/env.ts index effc347b5..704906de1 100644 --- a/packages/shared/src/env.ts +++ b/packages/shared/src/env.ts @@ -11,10 +11,14 @@ export const env = createEnv({ VITE_SENTRY_DSN: z.string().optional(), VITE_INBOXES_EMAIL: z.string().default("@follow.re"), VITE_FIREBASE_CONFIG: z.string().optional(), - // For external, use api_url if you don't want to fill it in. - VITE_EXTERNAL_API_URL: z.string().optional(), + VITE_OPENPANEL_CLIENT_ID: z.string().optional(), VITE_OPENPANEL_API_URL: z.string().url().optional(), + + // For external, use api_url if you don't want to fill it in. + VITE_EXTERNAL_PROD_API_URL: z.string().optional(), + VITE_EXTERNAL_DEV_API_URL: z.string().optional(), + VITE_EXTERNAL_API_URL: z.string().optional(), }, emptyStringAsUndefined: true,