@waiting/fetch
Version:
HTTP fetch API for browser and Node.js. Handle 302/303 redirect correctly on Node.js
28 lines • 1.04 kB
JavaScript
import assert from 'node:assert';
import { FetchMsg, FnKeys, } from './types.js';
export async function handleResponseError(resp, bare = false) {
/* istanbul ignore else */
if (resp.ok || bare) {
return resp;
}
const { status, statusText } = resp;
const ret = resp.text()
.catch((err) => JSON.stringify(err))
.then((txt) => {
const str = `${FetchMsg.httpErrorMsgPrefix}${status.toString()}\nstatusText: ${statusText}\nResponse: ${txt}`;
const ex = new Error(str);
return Promise.reject(ex);
});
return ret;
}
export async function processResponseType(response, dataType) {
if (['raw', 'bare'].includes(dataType)) {
return response;
}
assert(dataType in FnKeys, `dataType: ${dataType} is not in ${Object.keys(FnKeys).join(', ')}`);
assert(typeof response[dataType] === 'function', `response.${dataType} is not a function`);
// @ts-expect-error
const ret = await response[dataType]();
return ret;
}
//# sourceMappingURL=response.js.map