Files
web/src/lib/api.ts
2026-03-31 16:09:43 +08:00

47 lines
1.2 KiB
TypeScript

// 定义后端服务URL和OAuth2配置
const _api_base_url = process.env.NEXT_PUBLIC_API_BASE_URL
if (!_api_base_url) throw new Error('NEXT_PUBLIC_API_BASE_URL is not set')
const API_BASE_URL = _api_base_url
const _client_id = process.env.CLIENT_ID
if (!_client_id) throw new Error('CLIENT_ID is not set')
const CLIENT_ID = _client_id
const _client_secret = process.env.CLIENT_SECRET
if (!_client_secret) throw new Error('CLIENT_SECRET is not set')
const CLIENT_SECRET = _client_secret
// 统一的API响应类型
type ApiResponse<T = undefined> = {
success: false
status: number
message: string
} | {
success: true
data: T
}
type PageRecord<T = unknown> = {
total: number
page: number
size: number
list: T[]
}
type ExtraReq<T extends (...args: never) => unknown> = T extends (...args: infer P) => unknown ? P[0] : never
type ExtraResp<T extends (...args: never) => unknown> = Awaited<ReturnType<T>> extends ApiResponse<infer D> ? D : never
// 预定义错误
const UnauthorizedError = new Error('未授权访问')
export {
API_BASE_URL,
CLIENT_ID,
CLIENT_SECRET,
type ApiResponse,
type PageRecord,
type ExtraReq,
type ExtraResp,
UnauthorizedError,
}