diff --git a/src/lib/request.ts b/src/lib/request.ts index 7759861..53b7056 100644 --- a/src/lib/request.ts +++ b/src/lib/request.ts @@ -1,12 +1,51 @@ import ky, {type Options} from 'ky' +type ForwardHeaderSource = Headers | Record | undefined | null + const api = ky.create({ - baseUrl: import.meta.env.VITE_API_BASE_URL + baseUrl: import.meta.env.VITE_API_BASE_URL }); -// ApiPageResponse -export const http = { - get: async (url: string, options?: Options): Promise => { - return api.get(url, options).json() - } -}; \ No newline at end of file +const pickHeader = (source: ForwardHeaderSource, name: string): string | undefined => { + if (!source) return undefined + if (source instanceof Headers) return source.get(name) ?? undefined + return source[name] ?? source[name.toLowerCase()] +} + +const buildForwardHeaders = (source: ForwardHeaderSource): Record => { + const xff = pickHeader(source, 'x-forwarded-for') + const realIp = pickHeader(source, 'x-real-ip') + const cfIp = pickHeader(source, 'cf-connecting-ip') + const ua = pickHeader(source, 'user-agent') + const referer = pickHeader(source, 'referer') + const host = pickHeader(source, 'host') + const proto = pickHeader(source, 'x-forwarded-proto') + + return { + ...(xff ? {'x-forwarded-for': xff} : {}), + ...(realIp ? {'x-real-ip': realIp} : {}), + ...(cfIp ? {'cf-connecting-ip': cfIp} : {}), + ...(ua ? {'user-agent': ua} : {}), + ...(referer ? {referer} : {}), + ...(host ? {host} : {}), + ...(proto ? {'x-forwarded-proto': proto} : {}) + } +} + +export const createHttp = (forwardHeadersSource?: ForwardHeaderSource) => { + const forwardedHeaders = buildForwardHeaders(forwardHeadersSource) + + return { + get: async (url: string, options?: Options): Promise => { + return api.get(url, { + ...options, + headers: { + ...forwardedHeaders, + ...(options?.headers ?? {}) + } + }).json() + } + }; +} + +export const http = createHttp(); diff --git a/src/pages/index.astro b/src/pages/index.astro index 25675a4..2c222c5 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -2,10 +2,12 @@ import Layout from "@/layouts/Layout.astro"; import PostList from "@/components/PostList.astro"; import type {Post} from "@/types/post"; -import {http} from "@/lib/request"; -import { metadata } from "@/consts.ts"; +import {createHttp} from "@/lib/request"; +import {metadata} from "@/consts.ts"; -const {list,total} = await http.get>("post", { +const http = createHttp(Astro.request.headers) + +const {list, total} = await http.get>("post", { searchParams: { page: 1, page_size: metadata.postPageSize diff --git a/src/pages/page/[...page].astro b/src/pages/page/[...page].astro index c044ef9..3c2b87e 100644 --- a/src/pages/page/[...page].astro +++ b/src/pages/page/[...page].astro @@ -2,7 +2,7 @@ import Layout from "@/layouts/Layout.astro"; import PostList from "@/components/PostList.astro"; import type {Post} from "@/types/post"; -import {http} from "@/lib/request"; +import {createHttp} from "@/lib/request"; import {metadata} from "@/consts.ts"; const {page} = Astro.params @@ -11,6 +11,8 @@ if (isNaN(Number(page))) { return Astro.redirect('/') } +const http = createHttp(Astro.request.headers) + const {list, total} = await http.get>("post", { searchParams: { page, diff --git a/src/pages/posts/[...slug].astro b/src/pages/posts/[...slug].astro index ccbce23..1260885 100644 --- a/src/pages/posts/[...slug].astro +++ b/src/pages/posts/[...slug].astro @@ -3,7 +3,7 @@ import "github-markdown-css/github-markdown-light.css" import "@/styles/github-markdown.css" import Layout from "@/layouts/Layout.astro"; -import {http} from "@/lib/request.ts"; +import {createHttp} from "@/lib/request.ts"; import {highlightHtmlWithJsdom} from "@/lib/highlight.js"; import {formatDatetime} from "@/lib/format.ts"; import type {Post} from "@/types"; @@ -12,6 +12,8 @@ const {slug} = Astro.params; let data: Post; +const http = createHttp(Astro.request.headers) + try { const res = await http.get>(`post/${slug}`); data = res.data