fix: try to adaptor vercel upstream env

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2024-10-21 23:59:44 +08:00
parent 6eb4b2d260
commit 528c5ca62a
5 changed files with 51 additions and 40 deletions
-37
View File
@@ -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<string> | 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<AppType>(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",
}
},
})
+6
View File
@@ -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()
})
+9 -1
View File
@@ -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,
+30
View File
@@ -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()
}
+6 -2
View File
@@ -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,