* fix: update package.json paths and enable composite in tsconfig * fix: add tsconfig.tsbuildinfo to .gitignore * fix: enable composite in main * fix: update AppType import path * chore: clean tsup
45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import type { AppType } from "@follow/shared"
|
|
import { env } from "@follow/shared/env"
|
|
import PKG from "@pkg"
|
|
import { hc } from "hono/client"
|
|
import { ofetch } from "ofetch"
|
|
|
|
import { logger } from "../logger"
|
|
import { getAuthSessionToken, getUser } from "./user"
|
|
|
|
const abortController = new AbortController()
|
|
export const apiFetch = ofetch.create({
|
|
baseURL: env.VITE_API_URL,
|
|
credentials: "include",
|
|
signal: abortController.signal,
|
|
retry: false,
|
|
onRequest({ request }) {
|
|
const authSessionToken = getAuthSessionToken()
|
|
if (!authSessionToken) {
|
|
abortController.abort()
|
|
return
|
|
}
|
|
|
|
logger.info(`API Request: ${request.toString()}`)
|
|
},
|
|
onRequestError(context) {
|
|
if (context.error.name === "AbortError") {
|
|
return
|
|
}
|
|
},
|
|
})
|
|
|
|
export const apiClient = hc<AppType>("", {
|
|
fetch: async (input, options = {}) => apiFetch(input.toString(), options),
|
|
headers() {
|
|
const authSessionToken = getAuthSessionToken()
|
|
const user = getUser()
|
|
return {
|
|
"X-App-Version": PKG.version,
|
|
"X-App-Dev": process.env.NODE_ENV === "development" ? "1" : "0",
|
|
Cookie: authSessionToken ? `authjs.session-token=${authSessionToken}` : "",
|
|
"User-Agent": `Follow/${PKG.version}${user?.id ? ` uid: ${user.id}` : ""}`,
|
|
}
|
|
},
|
|
})
|