Initial commit
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
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: any): 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: any): 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: any): Promise<Response> {
|
||||
return this.request(endpoint, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user