55 lines
2.0 KiB
TypeScript
55 lines
2.0 KiB
TypeScript
import type {
|
|
BaseRetData, PerformanceGetListData, PerformanceGetListResponse,
|
|
} from "./models"
|
|
import {sleep} from "~/utils/utils.ts";
|
|
|
|
// export const apiEndpoint = import.meta.env.VITE_API_ENDPOINT
|
|
|
|
|
|
export async function fetchAPI(path: string, method: string, body?: any, headers?: any, contentType: string | null = "application/json"): Promise<Response> {
|
|
const baseHeaders: { [key: string]: any } = {
|
|
"password": `${localStorage.getItem("password")}`,
|
|
}
|
|
if (contentType) {
|
|
baseHeaders["Content-Type"] = contentType
|
|
}
|
|
let reqHeaders: any
|
|
if (headers) {
|
|
reqHeaders = {...baseHeaders, ...headers}
|
|
}
|
|
else {
|
|
reqHeaders = baseHeaders
|
|
}
|
|
return new Promise((resolve, reject) => {
|
|
fetch(`${localStorage.getItem("testServerAddress")}/${path}`, {
|
|
method,
|
|
credentials: "include",
|
|
headers: reqHeaders,
|
|
body: contentType === "application/json" ? (body ? JSON.stringify(body) : undefined) : body,
|
|
})
|
|
.then((v) => resolve(v))
|
|
.catch((e) => reject(e))
|
|
})
|
|
}
|
|
|
|
export async function apiPing(): Promise<BaseRetData> {
|
|
const resp = await fetchAPI("ping", "GET")
|
|
return resp.json()
|
|
}
|
|
|
|
export async function apiPingWithPassword(serverAddress: string, password: string): Promise<BaseRetData> {
|
|
localStorage.setItem("testServerAddress", serverAddress)
|
|
localStorage.setItem("password", password)
|
|
return apiPing()
|
|
}
|
|
|
|
export async function apiPerformanceGetList(count: number, example_data_limit: number, idx: number): Promise<PerformanceGetListResponse> {
|
|
const resp = await fetchAPI(`performance_test_get_list?count=${count}&example_data_limit=${example_data_limit}&idx=${idx}`, "GET")
|
|
return resp.json()
|
|
}
|
|
|
|
export async function apiPerformanceUploadData(count: number, idx: number): Promise<PerformanceGetListResponse> {
|
|
const resp = await fetchAPI(`performance_test_add_books?count=${count}&idx=${idx}`, "GET")
|
|
return resp.json()
|
|
}
|