feat: 项目初始化
This commit is contained in:
212
src/lib/api/api-client.ts
Normal file
212
src/lib/api/api-client.ts
Normal file
@@ -0,0 +1,212 @@
|
||||
import ky, { HTTPError, type Options, TimeoutError } from 'ky'
|
||||
import {
|
||||
API_ERROR_MESSAGES,
|
||||
DEFAULT_REQUEST_ACCEPT_HEADER,
|
||||
DEFAULT_REQUEST_TIMEOUT_MS,
|
||||
} from '@/constants'
|
||||
import {
|
||||
handleUnauthorizedSession,
|
||||
tryRefreshAuthSession,
|
||||
} from '@/lib/auth/auth-session'
|
||||
import { useAuthStore } from '@/store/auth-store'
|
||||
|
||||
import { ApiError } from './api-error'
|
||||
import type { ApiResponse } from './types'
|
||||
|
||||
type RequestOptions = Omit<Options, 'json'>
|
||||
type JsonRequestOptions<TBody> = RequestOptions & {
|
||||
json?: TBody
|
||||
}
|
||||
|
||||
const AUTH_REFRESH_ATTEMPTED_CONTEXT_KEY = 'authRefreshAttempted'
|
||||
const AUTH_SKIP_REFRESH_CONTEXT_KEY = 'skipAuthRefresh'
|
||||
const appEnv = import.meta.env.VITE_APP_ENV
|
||||
const shouldLogRequests = import.meta.env.VITE_ENABLE_REQUEST_LOG === 'true'
|
||||
|
||||
function normalizeApiBaseUrl(baseUrl: string | undefined) {
|
||||
const candidate = baseUrl?.trim()
|
||||
|
||||
if (!candidate) {
|
||||
throw new Error('VITE_API_BASE_URL 未配置')
|
||||
}
|
||||
|
||||
if (/^https?:\/\//.test(candidate)) {
|
||||
return candidate.replace(/\/+$/, '')
|
||||
}
|
||||
|
||||
return candidate.replace(/^\/+/, '').replace(/\/+$/, '')
|
||||
}
|
||||
|
||||
async function parseResponseBody(response: Response) {
|
||||
if (response.status === 204) {
|
||||
return null
|
||||
}
|
||||
|
||||
const contentType = response.headers.get('content-type') ?? ''
|
||||
|
||||
if (contentType.includes('application/json')) {
|
||||
return response.json()
|
||||
}
|
||||
|
||||
return response.text()
|
||||
}
|
||||
|
||||
function getErrorMessage(response: Response, data: unknown) {
|
||||
if (data && typeof data === 'object') {
|
||||
const message =
|
||||
'message' in data ? data.message : 'msg' in data ? data.msg : null
|
||||
|
||||
if (typeof message === 'string' && message.length > 0) {
|
||||
return message
|
||||
}
|
||||
}
|
||||
|
||||
return `Request failed with status ${response.status}`
|
||||
}
|
||||
|
||||
async function toApiError(error: unknown) {
|
||||
if (error instanceof HTTPError) {
|
||||
const data = error.data
|
||||
|
||||
return new ApiError({
|
||||
message: getErrorMessage(error.response, data),
|
||||
status: error.response.status,
|
||||
data,
|
||||
url: error.response.url,
|
||||
})
|
||||
}
|
||||
|
||||
if (error instanceof TimeoutError) {
|
||||
return new ApiError({
|
||||
message: API_ERROR_MESSAGES.timeout,
|
||||
status: 408,
|
||||
})
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
return new ApiError({
|
||||
message: error.message,
|
||||
})
|
||||
}
|
||||
|
||||
return new ApiError({
|
||||
message: API_ERROR_MESSAGES.unexpected,
|
||||
})
|
||||
}
|
||||
|
||||
export const apiBaseUrl = normalizeApiBaseUrl(import.meta.env.VITE_API_BASE_URL)
|
||||
|
||||
const apiClient = ky.create({
|
||||
prefix: apiBaseUrl,
|
||||
retry: 0,
|
||||
timeout: DEFAULT_REQUEST_TIMEOUT_MS,
|
||||
hooks: {
|
||||
beforeRequest: [
|
||||
({ request }) => {
|
||||
request.headers.set('Accept', DEFAULT_REQUEST_ACCEPT_HEADER)
|
||||
|
||||
const token = useAuthStore.getState().accessToken
|
||||
|
||||
if (token) {
|
||||
request.headers.set('Authorization', `Bearer ${token}`)
|
||||
}
|
||||
|
||||
if (shouldLogRequests) {
|
||||
console.info(`[api:${appEnv}] ${request.method} ${request.url}`)
|
||||
}
|
||||
},
|
||||
],
|
||||
afterResponse: [
|
||||
({ request, response }) => {
|
||||
if (shouldLogRequests) {
|
||||
console.info(
|
||||
`[api:${appEnv}] ${request.method} ${response.url} -> ${response.status}`,
|
||||
)
|
||||
}
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
|
||||
async function request<TResponse>(input: string, options?: Options) {
|
||||
try {
|
||||
const response = await apiClient(input, options)
|
||||
const data = await parseResponseBody(response)
|
||||
|
||||
return data as TResponse
|
||||
} catch (error) {
|
||||
if (
|
||||
error instanceof HTTPError &&
|
||||
error.response.status === 401 &&
|
||||
options?.context?.[AUTH_SKIP_REFRESH_CONTEXT_KEY] !== true &&
|
||||
options?.context?.[AUTH_REFRESH_ATTEMPTED_CONTEXT_KEY] !== true
|
||||
) {
|
||||
const refreshed = await tryRefreshAuthSession()
|
||||
|
||||
if (refreshed) {
|
||||
return request<TResponse>(input, {
|
||||
...options,
|
||||
context: {
|
||||
...options?.context,
|
||||
[AUTH_REFRESH_ATTEMPTED_CONTEXT_KEY]: true,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if (error instanceof HTTPError && error.response.status === 401) {
|
||||
handleUnauthorizedSession()
|
||||
}
|
||||
|
||||
throw await toApiError(error)
|
||||
}
|
||||
}
|
||||
|
||||
async function requestRest<TData>(input: string, options?: Options) {
|
||||
return request<ApiResponse<TData>>(input, options)
|
||||
}
|
||||
|
||||
export const api = {
|
||||
get<TData>(input: string, options?: RequestOptions) {
|
||||
return requestRest<TData>(input, {
|
||||
...options,
|
||||
method: 'get',
|
||||
})
|
||||
},
|
||||
post<TData, TBody = unknown>(
|
||||
input: string,
|
||||
{ json, ...options }: JsonRequestOptions<TBody> = {},
|
||||
) {
|
||||
return requestRest<TData>(input, {
|
||||
...options,
|
||||
json,
|
||||
method: 'post',
|
||||
})
|
||||
},
|
||||
put<TData, TBody = unknown>(
|
||||
input: string,
|
||||
{ json, ...options }: JsonRequestOptions<TBody> = {},
|
||||
) {
|
||||
return requestRest<TData>(input, {
|
||||
...options,
|
||||
json,
|
||||
method: 'put',
|
||||
})
|
||||
},
|
||||
patch<TData, TBody = unknown>(
|
||||
input: string,
|
||||
{ json, ...options }: JsonRequestOptions<TBody> = {},
|
||||
) {
|
||||
return requestRest<TData>(input, {
|
||||
...options,
|
||||
json,
|
||||
method: 'patch',
|
||||
})
|
||||
},
|
||||
delete<TData>(input: string, options?: RequestOptions) {
|
||||
return requestRest<TData>(input, {
|
||||
...options,
|
||||
method: 'delete',
|
||||
})
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user