Last active
August 23, 2024 14:48
-
-
Save Hoyasumii/7b1474c947459de21908359e171606ba to your computer and use it in GitHub Desktop.
HttpRequester for Bun
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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