Created
December 20, 2021 08:55
-
-
Save victory-sokolov/d48fadaaec1ed3e9d14a554f0ee2dfff to your computer and use it in GitHub Desktop.
Fetch wrapper
This file contains hidden or 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
export class HttpMethod { | |
static GET = "GET"; | |
static POST = "POST"; | |
static PUT = "PUT"; | |
static DELETE = "DELETE"; | |
} | |
const request = async ({endpoint, params, method = HttpMethod.GET}) { | |
const url = new URL(`${this.baseUrl}/${endpoint}`); | |
if(method === HttpMethod.GET && params) { | |
url.search = new URLSearchParams(params); | |
} | |
const response = await fetch(url, { | |
method, | |
headers: this.headers, | |
...(method !== HttpMethod.GET ? { body: JSON.stringify(params) } : {}), | |
}) | |
return response.json(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment