Skip to content

Instantly share code, notes, and snippets.

@Hoyasumii
Last active August 23, 2024 14:48
Show Gist options
  • Save Hoyasumii/7b1474c947459de21908359e171606ba to your computer and use it in GitHub Desktop.
Save Hoyasumii/7b1474c947459de21908359e171606ba to your computer and use it in GitHub Desktop.
HttpRequester for Bun
class HttpRequest {
public baseUrl: string;
constructor(baseUrl: string) {
this.baseUrl = baseUrl;
}
private headerBuilder = (data: object): Headers => {
return new Headers(Object.entries(data));
};
async get(path: string, headers: object) {
return await fetch(`${this.baseUrl}/${path}`, {
method: "get",
headers: this.headerBuilder(headers),
});
}
async post<T>(path: string, body: T, headers: object) {
return await fetch(`${this.baseUrl}/${path}`, {
method: "post",
headers: this.headerBuilder(headers),
body: body as any,
});
}
async put<T>(path: string, body: T, headers: object) {
return await fetch(`${this.baseUrl}/${path}`, {
method: "put",
headers: this.headerBuilder(headers),
body: body as any,
});
}
async patch<T>(path: string, body: Partial<T>, headers: object) {
return await fetch(`${this.baseUrl}/${path}`, {
method: "patch",
headers: this.headerBuilder(headers),
body: body as any,
});
}
async delete(path: string, headers: object) {
return await fetch(`${this.baseUrl}/${path}`, {
method: "delete",
headers: this.headerBuilder(headers),
});
}
}
export const Requester = (baseUrl: string) => new HttpRequest(baseUrl);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment