Skip to content

Instantly share code, notes, and snippets.

@jojojojojoj5564656465465
Forked from Fredkiss3/jsonfetch.ts
Created June 29, 2022 00:26
Show Gist options
  • Save jojojojojoj5564656465465/fe70faf57d090c3c726db6620295a723 to your computer and use it in GitHub Desktop.
Save jojojojojoj5564656465465/fe70faf57d090c3c726db6620295a723 to your computer and use it in GitHub Desktop.
JSON FETCH
export async function jsonFetch<T>(
url: string,
options: RequestInit = {}
): Promise<T> {
// Set the default headers correctly
const headers: HeadersInit = new Headers(options.headers);
headers.set('Accept', 'application/json');
headers.set('Content-Type', 'application/json');
return fetch(url, {
...options,
headers,
credentials: 'include',
})
.then((response) => response.json())
.catch((error) => {
console.error('There was an error ?', error);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment