chore: add app meta header to request headers

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei 2024-07-08 17:51:00 +08:00
parent 335412534a
commit 6fab907ed3
No known key found for this signature in database
GPG Key ID: 0F62D33977F021F7
5 changed files with 24 additions and 3 deletions

View File

@ -20,6 +20,7 @@ export default defineConfig({
alias: {
"@renderer": resolve("src/renderer/src"),
"@shared": resolve("src/shared/src"),
"@pkg": resolve("./package.json"),
},
},
plugins: [react()],

View File

@ -32,8 +32,11 @@ export function SiteIcon({
className={cn("mr-2 size-5 shrink-0 rounded-sm", className)}
loading="lazy"
onError={(e) => {
if (fallback) {
if (fallback && e.currentTarget.src !== fallback) {
e.currentTarget.src = fallback
} else {
// Empty svg
e.currentTarget.src = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3C/svg%3E"
}
}}
style={style}

View File

@ -1,11 +1,11 @@
import { getCsrfToken } from "@hono/auth-js/react"
import PKG from "@pkg"
import { setLoginModalShow } from "@renderer/atoms/user"
import type { AppType } from "@renderer/hono"
import { hc } from "hono/client"
import { FetchError, ofetch } from "ofetch"
let csrfTokenPromise: Promise<string> | null = null
export const apiFetch = ofetch.create({
baseURL: import.meta.env.VITE_API_URL,
credentials: "include",
@ -28,6 +28,15 @@ export const apiFetch = ofetch.create({
} else {
(options.body as Record<string, unknown>).csrfToken = csrfToken
}
const header = new Headers(options.headers)
header.set("x-app-version", PKG.version)
header.set(
"X-App-Dev",
process.env.NODE_ENV === "development" ? "1" : "0",
)
options.headers = header
}
},
onResponseError(context) {
@ -51,6 +60,12 @@ export const apiFetch = ofetch.create({
export const apiClient = hc<AppType>("", {
fetch: async (input, options = {}) => apiFetch(input.toString(), options),
headers() {
return {
"X-App-Version": PKG.version,
"X-App-Dev": process.env.NODE_ENV === "development" ? "1" : "0",
}
},
})
export const getFetchErrorMessage = (error: Error) => {

View File

@ -24,7 +24,8 @@
],
"@shared/*": [
"src/shared/src/*"
]
],
"@pkg": ["./package.json"]
}
}
}

View File

@ -17,6 +17,7 @@ export default defineConfig({
alias: {
"@renderer": resolve("src/renderer/src"),
"@shared": resolve("src/shared/src"),
"@pkg": resolve("./package.json"),
},
},
base: "/",