@tkrotoff/fetch
Version:
Fetch wrapper
103 lines (102 loc) • 3.63 kB
JavaScript
import { entriesToObject } from './utils/entriesToObject';
import { HttpError } from './HttpError';
const arrayBufferMimeType = '*/*';
const blobMimeType = '*/*';
const formDataMimeType = 'multipart/form-data';
export const jsonMimeType = 'application/json';
const textMimeType = 'text/*';
export function isJSONResponse(response) {
var _a;
const contentType = (_a = response.headers.get('content-type')) !== null && _a !== void 0 ? _a : '';
return contentType.includes(jsonMimeType);
}
function extendResponsePromiseWithBodyMethods(responsePromise, headers) {
function setAcceptHeader(mimeType) {
var _a;
headers.set('accept', (_a = headers.get('accept')) !== null && _a !== void 0 ? _a : mimeType);
}
responsePromise.arrayBuffer = async () => {
setAcceptHeader(arrayBufferMimeType);
const response = await responsePromise;
return response.arrayBuffer();
};
responsePromise.blob = async () => {
setAcceptHeader(blobMimeType);
const response = await responsePromise;
return response.blob();
};
responsePromise.formData = async () => {
setAcceptHeader(formDataMimeType);
const response = await responsePromise;
return response.formData();
};
responsePromise.json = async () => {
setAcceptHeader(jsonMimeType);
const response = await responsePromise;
if (isJSONResponse(response)) {
return response.json();
}
return response.text();
};
responsePromise.text = async () => {
setAcceptHeader(textMimeType);
const response = await responsePromise;
return response.text();
};
}
export const defaults = {
init: {}
};
function request(input, headers, init, method, body) {
async function _fetch() {
await Promise.resolve();
const req = new Request(input, {
...defaults.init,
...init,
headers,
method,
body
});
const res = await fetch(req);
if (!res.ok)
throw new HttpError(req, res);
return res;
}
const responsePromise = _fetch();
extendResponsePromiseWithBodyMethods(responsePromise, headers);
return responsePromise;
}
function getHeaders(init) {
const defaultInitHeaders = entriesToObject(new Headers(defaults.init.headers));
const initHeaders = entriesToObject(new Headers(init === null || init === void 0 ? void 0 : init.headers));
return new Headers({ ...defaultInitHeaders, ...initHeaders });
}
function getJSONHeaders(init) {
const headers = getHeaders(init);
headers.set('content-type', jsonMimeType);
return headers;
}
export function get(input, init) {
return request(input, getHeaders(init), init, 'GET');
}
export function post(input, body, init) {
return request(input, getHeaders(init), init, 'POST', body);
}
export function postJSON(input, body, init) {
return request(input, getJSONHeaders(init), init, 'POST', JSON.stringify(body));
}
export function put(input, body, init) {
return request(input, getHeaders(init), init, 'PUT', body);
}
export function putJSON(input, body, init) {
return request(input, getJSONHeaders(init), init, 'PUT', JSON.stringify(body));
}
export function patch(input, body, init) {
return request(input, getHeaders(init), init, 'PATCH', body);
}
export function patchJSON(input, body, init) {
return request(input, getJSONHeaders(init), init, 'PATCH', JSON.stringify(body));
}
export function del(input, init) {
return request(input, getHeaders(init), init, 'DELETE');
}