/* eslint-disable indent */ import { useLocalStorage } from '@vueuse/core' import { APIRoot, PaginationResponse } from './api-models' import { Cookies20Regular } from '@vicons/fluent' const cookie = useLocalStorage('JWT_Token', '') export async function QueryPostAPI(url: string, body?: unknown, headers?: [string, string][]): Promise> { headers ??= [] if (cookie.value) headers?.push(['Authorization', `Bearer ${cookie.value}`]) headers?.push(['Content-Type', 'application/json']) const data = await fetch(url, { method: 'post', headers: headers, body: typeof body === 'string' ? body : JSON.stringify(body), }) // 不处理异常, 在页面处理 return (await data.json()) as APIRoot } export async function QueryPostAPIWithParams(urlString: string, params?: any, body?: any, contentType?: string, headers?: [string, string][]): Promise> { const url = new URL(urlString) url.search = new URLSearchParams(params).toString() headers ??= [] headers?.push(['Authorization', `Bearer ${cookie.value}`]) if (contentType) headers?.push(['Content-Type', contentType]) const data = await fetch(url, { method: 'post', headers: headers, body: body, }) // 不处理异常, 在页面处理 return (await data.json()) as APIRoot } export async function QueryGetAPI(urlString: string, params?: any, headers?: [string, string][]): Promise> { const url = new URL(urlString) url.search = new URLSearchParams(params).toString() if (cookie.value) { headers ??= [] headers?.push(['Authorization', `Bearer ${cookie.value}`]) } const data = await fetch(url.toString(), { method: 'get', headers: headers, }) // 不处理异常, 在页面处理 return (await data.json()) as APIRoot } export async function QueryPostPaginationAPI(url: string, body?: unknown): Promise>> { return await QueryPostAPI>(url, body) } export async function QueryGetPaginationAPI(urlString: string, params?: unknown): Promise>> { return await QueryGetAPI>(urlString, params) }