egg-fetch-middleware
Install
$ npm i egg-fetch-middleware --save
Usage
// {app_root}/config/plugin.jsexportsfetchMiddleware = enable: true package: "egg-fetch-middleware";
Configuration
// {app_root}/config/config.default.jsexportsfetchMiddleware = // The local and unittest environments are forced to be enabled. Other environments use this configuration to specify whether to display Error stack information when an error is thrown showStack: false;
see config/config.default.js for more detail.
Example
fetchMiddleware is a plugin for formatting and getting data interactions.
Once enabled in plugin.js, it can be handled directly in the controller or service via methods such as ctx.ok(data, {})
. The following is test/fixtures/apps/fetch-middleware-test/app/controller/home.js
(return ctx.ok(data, {})
in service)
'use strict'; const Controller = Controller; async { const id = ctxhelper; ctxbody = ctx; } async { ctxbody = ctx; } async { const err = 'Custom 5xx server internal error'; ctxbody = ctx; } async { ctx; } // Recommended to use a value greater than or equal to 10000 to use as a business error code async { // Note: If throwError is on the server side, ctx.AcceptJSON must be true. // can refer to https://eggjs.org/api/Request.html#acceptJSON ctx; } // Not recommended, but some business scenario is written like this. Handle the front end by business error async { // Note: If throwError is on the server side, ctx.AcceptJSON must be true. // can refer to https://eggjs.org/api/Request.html#acceptJSON ctx; } moduleexports = HomeController;
Questions & Suggestions
Please open an issue here.