Skip to content

Instantly share code, notes, and snippets.

@jberger
Created September 2, 2020 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jberger/32be2d92a822f7bdb334431bb51bdf03 to your computer and use it in GitHub Desktop.
Save jberger/32be2d92a822f7bdb334431bb51bdf03 to your computer and use it in GitHub Desktop.
export function Fetcher (options) {
if (options.base) this.base = options.base;
};
Fetcher.prototype.fetch = function(input, init) {
if (input instanceof Request) {
return window.fetch(input, init);
}
let url = this.base ? new URL(input, this.base) : new URL(input);
if (! init) init = {};
if (! init.headers) {
init.headers = new Headers();
} else if ( !( init.headers instanceof Headers) ) {
init.headers = new Headers(init.headers);
}
let is_get = ! init.method || init.method == 'GET' || init.method == 'HEAD';
// shortcut for query or form data
if (init.form) {
let form;
if (is_get) {
form = url.searchParams;
} else {
if (init.body) {
return Promise.reject(new Error('The form shortcut should not be used when a body is specified'));
}
form = new FormData();
init.body = form;
if (! init.headers.has('Content-Type')) {
init.headers.set('Content-Type', 'application/x-www-form-urlencoded');
}
}
// if not iterable, try to make it iterable with Object.entries
let items = typeof init.form[Symbol.iterator] === 'function' ? init.form : Object.entries(init.form);
items.forEach(([key, value]) => form.append(key, value));
delete init.form;
// shortcut for JSON
} else if (init.json) {
if (init.body) {
return Promise.reject(new Error('The json shortcut should not be used when a body is specified'));
}
init.body = JSON.stringify(init.json);
delete init.json;
if (! init.headers.has('Content-Type')) {
init.headers.set('Content-Type', 'application/json');
}
}
return window.fetch(url.toString(), init);
};
export let fetcher = (new Fetcher()).fetch;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment