Introduction
Simple package to serialize data to response to client for Node.js (e.g. Express.js).
Installation
$ npm install jserializer
Usage
// product.ts // productSerializer.ts; // productsController.ts;;; app.get'/', // will get response// {// products: [// {// id: '001',// name: 'Fire Emblem',// stock: 20// }// ]// } // or serialize one object only app.get'/', // will get response// {// product: {// id: '001',// name: 'Fire Emblem',// stock: 20// }// }
Decorators
@attr
Defines serialier's attribute. Example:
@attr
has some usage:
Without parameter
Will get static attribute's name as serialized data attribute's name and get data from attribute of resource with the same name.
With string parameter
Will get static attribute's name as serialized data attribute's name and get data from attribute of resource with the same name as given string.
With function parameter
Will get static attribute's name as serialized data attribute's name and pass the resource into function parameter to get data.
With object parameter
Sometime, the static attribute's name is the same as default attribute of the class. You must change the static attribute's name or else javascript will raise error. But you still want to response the data with key's name like that one (e.g. name), you can use this option.
// you can change into this
This option has some options:
name
: the name of response attributeinverseColumn
: the name of resource's attribute to get dataop
: the function to convert data from resource to response data (you do not need to useinverseColumn
if you use this option)
If inverseColumn
is not provided, the data will get from the resource's attribute with the same name as static attribute's name.