Files
vtsuru.live/src/api/query.ts
Megghy 1f47703a8b feat: 更新配置和文件上传逻辑, 迁移数据库结构(前端也得改
- 移除不再使用的 vite-plugin-monaco-editor
- 更新 package.json 和 vite.config.mts 文件
- 修改用户配置 API 逻辑,支持上传和下载配置
- 添加对文件上传的支持,优化文件处理逻辑
- 更新多个组件以支持新文件上传功能
- 删除不必要的 VTsuruTypes.ts 文件,整合到 VTsuruConfigTypes.ts 中
2025-05-03 06:18:32 +08:00

173 lines
4.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* eslint-disable indent */
import { apiFail } from '@/data/constants'
import { useLocalStorage } from '@vueuse/core'
import { APIRoot, PaginationResponse } from './api-models'
import { cookie } from './account';
export async function QueryPostAPI<T>(
urlString: string,
body?: unknown,
headers?: [string, string][],
contentType?: string
): Promise<APIRoot<T>> {
return await QueryPostAPIWithParams<T>(
urlString,
undefined,
body,
contentType || 'application/json',
headers
)
}
export async function QueryPostAPIWithParams<T>(
urlString: string,
params?: any,
body?: any,
contentType?: string,
headers?: [string, string][]
): Promise<APIRoot<T>> {
// @ts-expect-error 忽略
return await QueryPostAPIWithParamsInternal<APIRoot<T>>(
urlString,
params,
body,
contentType,
headers
)
}
async function QueryPostAPIWithParamsInternal<T>(
urlString: string,
params?: any,
body?: any,
contentType: string = 'application/json',
headers: [string, string][] = []
) {
let url: URL
try {
url = new URL(urlString.toString())
} catch (e) {
console.error('尝试解析API地址失败: ' + urlString, e)
return {
code: 400,
message: '无效的API地址: ' + urlString,
data: {} as T
}
}
url.search = getParams(params)
headers ??= []
let h = {} as { [key: string]: string }
headers.forEach((header) => {
h[header[0]] = header[1]
})
if (cookie.value.cookie) h['Authorization'] = `Bearer ${cookie.value.cookie}`
// 当使用FormData时不手动设置Content-Type让浏览器自动添加boundary
if (!(body instanceof FormData)) {
h['Content-Type'] = contentType
}
return await QueryAPIInternal<T>(url, {
method: 'post',
headers: h,
body: body instanceof FormData ? body : typeof body === 'string' ? body : JSON.stringify(body)
})
}
async function QueryAPIInternal<T>(url: URL, init: RequestInit) {
try {
const data = await fetch(url, init)
const result = (await data.json()) as T
return result
} catch (e) {
console.error(`[${init.method}] API调用失败: ${e}`)
if (!apiFail.value) {
apiFail.value = true
console.log('默认API异常, 切换至故障转移节点')
}
throw e
}
}
export async function QueryGetAPI<T>(
urlString: string,
params?: any,
headers?: [string, string][]
): Promise<APIRoot<T>> {
// @ts-expect-error 忽略
return await QueryGetAPIInternal<APIRoot<T>>(urlString, params, headers)
}
async function QueryGetAPIInternal<T>(
urlString: string,
params?: any,
headers?: [string, string][]
) {
try {
let url: URL
try {
url = new URL(urlString.toString())
} catch (e) {
console.error('尝试解析API地址失败: ' + urlString, e)
return {
code: 400,
message: '无效的API地址: ' + urlString,
data: {} as T
}
}
url.search = getParams(params)
headers ??= []
let h = {} as { [key: string]: string }
headers.forEach((header) => {
h[header[0]] = header[1]
})
if (cookie.value.cookie) {
h['Authorization'] = `Bearer ${cookie.value.cookie}`
}
return await QueryAPIInternal<T>(url, { method: 'get', headers: h })
} catch (err) {
console.log(`url:${urlString}, error:${err}`)
throw err
}
}
function getParams(params: any) {
const urlParams = new URLSearchParams(window.location.search)
if (params) {
const keys = Object.keys(params)
if (keys.length > 0) {
keys.forEach((k) => {
if (params[k] == undefined) {
delete params[k]
}
})
}
}
const resultParams = new URLSearchParams(params)
if (urlParams.has('as')) {
resultParams.set('as', urlParams.get('as') || '')
}
if (urlParams.has('token')) {
resultParams.set('token', urlParams.get('token') || '')
}
return resultParams.toString()
}
export async function QueryPostPaginationAPI<T>(
url: string,
body?: unknown
): Promise<PaginationResponse<T>> {
// @ts-expect-error 忽略
return await QueryPostAPIWithParamsInternal<PaginationResponse<T>>(
url,
undefined,
body
)
}
export async function QueryGetPaginationAPI<T>(
urlString: string,
params?: unknown
): Promise<PaginationResponse<T>> {
// @ts-expect-error 忽略
return await QueryGetAPIInternal<PaginationResponse<T>>(urlString, params)
}
export function GetHeaders(): [string, string][] {
return [['Authorization', `Bearer ${cookie.value?.cookie}`]]
}