egg-serializer
serializer any data to response
Install
$ npm i egg-serializer --save
Usage
// {app_root}/config/plugin.jsexportsserializer = enable: true package: 'egg-serializer';
Example
ctx.serialize
in Controller
ctxbody = ctx; // with optionsctxbody = ctx;
Serializers
Easy way, exports fields
moduleexports = 'name' 'age';
Simple way, only provider fields
// {app_root}/app/serializer/user.jsmodule { return Serializer { return 'name' 'age' // ... and so on ; } };
Custom way, override format method
// {app_root}/app/serializer/api/search.jsmodule { return Serializer { super; thisfields = 'count' 'time' // ... and so on ; } { const data = this; databook = app; datauser = app; datalast_editor = app; dataserver_time = Date; if optionsfoo === 'bar' datafoo = 'bar'; return data; } };
Configuration
// {app_root}/config/config.default.jsexportsserializer = // show `_serializer` property on data showSerializerType: false;
see config/config.default.js for more detail.
Questions & Suggestions
Please open an issue here.