Files
chitai/frontend/src/lib/server/api.ts
T
patrick 3a29294f96 chore: clear the mechanical lint and type findings
Dead imports and locals removed, each blocks keyed, `any` narrowed to unknown.
Two context setters kept their calls and lost only the unused binding; the
settings redirect no longer awaits a parent whose data it discards. A leading
underscore now marks a binding that only holds a position.
2026-08-17 17:55:21 -04:00

84 lines
1.8 KiB
TypeScript

import { BACKEND_API_URL } from '$lib/server/config';
export class ApiClient {
private token: string;
constructor(token: string) {
this.token = token;
}
private async request(endpoint: string, options: RequestInit = {}): Promise<Response> {
return fetch(`${BACKEND_API_URL}${endpoint}`, {
...options,
headers: {
Authorization: `Bearer ${this.token}`,
...options.headers
}
});
}
async get(endpoint: string): Promise<Response> {
return this.request(endpoint);
}
async post(endpoint: string, data: unknown): Promise<Response> {
return this.request(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
}
async postForm(endpoint: string, data: Record<string, string>): Promise<Response> {
return this.request(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams(data).toString()
});
}
async postMultipart(endpoint: string, formData: FormData): Promise<Response> {
return this.request(endpoint, {
method: 'POST',
body: formData
});
}
async put(endpoint: string, data: unknown): Promise<Response> {
return this.request(endpoint, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
}
async putMultipart(endpoint: string, formData: FormData) {
return this.request(endpoint, {
method: 'PUT',
body: formData
});
}
async delete(endpoint: string): Promise<Response> {
return this.request(endpoint, {
method: 'DELETE'
});
}
async patch(endpoint: string, data: unknown): Promise<Response> {
return this.request(endpoint, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
}
}