Files
ao3-mirror-ssr/src/composables/apiRequest.js
UnknownMp eca669d62a
All checks were successful
Build / build-and-test (push) Successful in 28s
[Base] 更新 SSR 机制, 支持流式渲染注入 head 标签
损失的 TTFB 可以忽略不计
2025-06-05 18:02:03 +08:00

60 lines
1.5 KiB
JavaScript

import { useAxios } from '@vueuse/integrations/useAxios'
import axios from 'axios'
import { objectToQueryString } from '../utils.js'
function getEndpoint() {
const apiMapping = {
'': ['http://localhost:28001/', '/api/'],
}
let host = import.meta.env.SSR ? 'ssr' : window.location.host
let entry = apiMapping[host] ?? apiMapping['']
return import.meta.env.SSR ? entry[0] : replaceUrl(entry[1])
}
function replaceUrl(url) {
return url
.replace('{{hostname}}', window.location.hostname)
.replace('{{port}}', window.location.port)
.replace('{{protocol}}', window.location.protocol)
}
export function useApiRequest(method, url, data, config = {}) {
const baseURL = getEndpoint()
const fullURL = method === 'GET' && data
? `${baseURL}${url}?${objectToQueryString(data)}`
: `${baseURL}${url}`
const {
response,
error,
isFinished,
isLoading,
execute,
} = useAxios(
fullURL,
{
method,
...(method === 'POST' ? { data } : {}),
...(config || {})
},
{
immediate: false,
axios,
}
)
const exec = async () => {
const start = Date.now()
try { await execute() }
catch (e) {}
const stop = Date.now()
return {
status: response.value?.status || (error.value?.response?.status ?? -1),
data: response.value?.data || error.value?.response?.data || null,
duration: stop - start,
error: error.value,
isSSR: import.meta.env.SSR,
}
}
return { execute: exec, isFinished, isLoading }
}