Skip to content

Commit

Permalink
fix: 优化 rawRequest (#226)
Browse files Browse the repository at this point in the history
* fix: 优化 rawRequest

* fix: dataHandler 支持 promise
  • Loading branch information
winixt authored Jan 18, 2024
1 parent ffb3417 commit 91c9ad8
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions packages/fes-plugin-request/src/template/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,30 @@ function getCustomerHandler(ctx, options = {}) {
};
}

export const request = (url, data, options = {}) => {
function formatOptions(options = {}) {
if (typeof options === 'string') {
options = {
method: options,
};
}
return options;
}

function getReqInstance() {
if (!currentRequestInstance) {
currentRequestInstance = getRequestInstance();
}
return currentRequestInstance;
}

const _request = (url, data, options, onSuccess) => {
options = formatOptions(options);
const reqInstance = getReqInstance();
const userConfig = userConfigHandler(url, data, options);
const context = createContext(userConfig);
const { dataHandler, errorHandler } = getCustomerHandler(context, options);

return currentRequestInstance.request(context).then(async () => {
return reqInstance.request(context).then(async () => {
if (context.config.skipErrorHandler) {
console.warn('3.x 已移除 skipErrorHandler 参数,请改用 dataHandler 处理');
if (
Expand All @@ -130,14 +140,24 @@ export const request = (url, data, options = {}) => {
}
}
if (!context.error) {
if (onSuccess) {
return onSuccess(await dataHandler(context.response.data, context.response), context);
}
return dataHandler(context.response.data, context.response);
}
errorHandler && errorHandler(context.error);
return Promise.reject(context.error);
});
};

export const rawRequest = request;
export const request = _request;

export const rawRequest = (url, data, options) =>
_request(url, data, options, (d, ctx) => {
ctx.response.rawData = ctx.response.data;
ctx.response.data = d;
return ctx.response;
});

function isPromiseLike(obj) {
return !!obj && typeof obj === 'object' && typeof obj.then === 'function';
Expand Down

0 comments on commit 91c9ad8

Please sign in to comment.