We have agreed on a set of standard interface structure specifications and provided default interface parsing and error handling capabilities, which are integrated in the request through the ResponseParser middleware
Standard interface structure
export interface response {
success: boolean; // if request is success
data?: any; // response data
errorCode?: string; // code for errorType
errorMessage?: string; // message display to user
showType?: number; // error display type: 0 silent; 1 message; 4 notification; 9 page
traceId?: string; // Convenient for back-end Troubleshooting: unique request ID
host?: string; // onvenient for backend Troubleshooting: host of current access server
}
If the interface structure of the project is inconsistent with the standard interface specification, but you want to use the following interface resolution and error handling capabilities, you can use request.responseparsers []. Adapter to adapt:
// Assume your project interface structure is: "{ok: true, result: { name: 'litou' }, error: { code: '000', msg: 'xxx' } }"
// The configuration is as follows:
export const request = {
errorConfig: {
adaptor: data => {
return {
success: data.ok,
data: data.result,
errorCode: data.error.code,
errorMessage: data.error.msg,
};
},
},
};
Interface Resolution and Error Handling
When the response status code is not 200, the errorhandler will still be triggered. Developers can override the default errorhandler through configuration
When the response status code is 200 and body.success is true, no processing will be performed
When the response status code is 200 and ‘body.success’ is false, different error handling will be done according to the value of ‘body.showtype’ (default is 4)
@umijs/plugin-request
Integrate umi-request deeply to umi.
Install
Usage
Getting started in 3 steps.
1. Configure in
.umirc.js2 Use request in your project.
3 Add config in ‘src/app.ts’
Add common options
ResponseParser middleware
We have agreed on a set of standard interface structure specifications and provided default interface parsing and error handling capabilities, which are integrated in the request through the ResponseParser middleware
Standard interface structure
If the interface structure of the project is inconsistent with the standard interface specification, but you want to use the following interface resolution and error handling capabilities, you can use request.responseparsers []. Adapter to adapt:
Interface Resolution and Error Handling
If you want to override all or part of the default error handling, just configure the handler:
If you want to skip errorHander on some request, we extend a request option
skipErrorHandler, you can set it ture for skip default error handler.Middleware
The responseParser middleware is built in. You can expand middlewares through ‘middlewares’ config:
Full example can find in ./example.
Contribute
npm i && npm i umi@latest
npm run build – -w
cd example
npx umi dev
LICENSE
MIT