Files
amaoke.app/src/lib/client.ts
T
2025-11-25 10:36:22 +08:00

41 lines
1.4 KiB
TypeScript

import type { ResultDocument, UserData } from "./types";
export async function post(endpoint: string, data: any) {
return await fetch(endpoint, {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
}
}).then(async res => {
if (res.status >= 400) throw new Error(`${res.status}: ${await res.json().then(json => json['message'])}`)
return res.json()
}).catch(e => {
console.error(e)
throw e
})
}
export const API = {
post,
saveUserData: async (data: Partial<UserData>) => await post('/api/user', data),
saveResult: async (data: Omit<ResultDocument, "_id" | "createdAt">) => await post('/api/result', data),
netease: {
startImport: async (link: string) => await post('/api/import/netease/start', { link }),
checkProgress: async (id: string) => await post('/api/import/netease/progress', { id }),
checkLogin: async (pwd?: string) => await post('/admin/netease-login', { pwd })
},
user: {
createSyncCode: async () => await post('/api/user/sync-code', {}),
loginWithSyncCode: async (code: string) => await post('/api/auth/login', { code })
},
song: {
prepare: async (id: number) => await post(`/api/song/${id}/prepare`, {}),
status: async (id: number) => await fetch(`/api/song/${id}/prepare`).then(res => res.json())
}
}