Skip to content

Instantly share code, notes, and snippets.

@victory-sokolov
Created December 20, 2021 08:55
Show Gist options
  • Save victory-sokolov/d48fadaaec1ed3e9d14a554f0ee2dfff to your computer and use it in GitHub Desktop.
Save victory-sokolov/d48fadaaec1ed3e9d14a554f0ee2dfff to your computer and use it in GitHub Desktop.
Fetch wrapper
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