@straw-hat/fetcher
Version:
Simple HTTP Client
34 lines • 903 B
JavaScript
function isString(val) {
return typeof val === 'string';
}
export function getRequestBody(body) {
if (body === undefined) {
return undefined;
}
if (body instanceof FormData) {
return body;
}
if (body instanceof Blob) {
return body;
}
if (body instanceof URLSearchParams) {
return body;
}
if (isString(body)) {
return body;
}
return JSON.stringify(body);
}
const JSON_CONTENT_TYPE_REGEX = /^application\/(.+\+)?json/;
export function getResponseBody(response) {
if (response.status === 204) {
return null;
}
const contentType = response.headers.get('Content-Type') ?? '';
const isJSON = response.status !== 204 && JSON_CONTENT_TYPE_REGEX.test(contentType.toLowerCase());
if (isJSON) {
return response.json();
}
return response.text();
}
//# sourceMappingURL=helpers.js.map