@ant-design/x-sdk
Version:
placeholder for @ant-design/x-sdk
49 lines (42 loc) • 1.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
const XFetch = async (baseURL, options) => {
const {
fetch: fetchFn = globalThis.fetch,
middlewares = {},
...requestInit
} = options;
if (typeof fetchFn !== 'function') {
throw new Error('The options.fetch must be a typeof fetch function!');
}
/** ---------------------- request init ---------------------- */
let fetchArgs = [baseURL, requestInit];
/** ---------------------- request middleware ---------------------- */
if (typeof middlewares.onRequest === 'function') {
const modifiedFetchArgs = await middlewares.onRequest(...fetchArgs);
fetchArgs = modifiedFetchArgs;
}
/** ---------------------- fetch ---------------------- */
let response = await fetchFn(...fetchArgs);
/** ---------------------- response middleware ---------------------- */
if (typeof middlewares.onResponse === 'function') {
const modifiedResponse = await middlewares.onResponse(response);
if (!(modifiedResponse instanceof Response)) {
throw new Error('The options.onResponse must return a Response instance!');
}
response = modifiedResponse;
}
/** ---------------------- response check ---------------------- */
if (!response.ok) {
throw new Error(`Fetch failed with status ${response.status}`);
}
if (!response.body) {
throw new Error('The response body is empty.');
}
/** ---------------------- return ---------------------- */
return response;
};
var _default = exports.default = XFetch;