vicis
v2.2.0
Published
Presentation and transformation layer for data output in RESTful APIs.
Downloads
691
Maintainers
Readme
« Vicis »
Presentation and transformation layer for data output in RESTful APIs.
•• Vicis Documentation •• JavaScript Repository •• Deno Repository ••
This is Node.js analogue to these libraries:
🐘 Fractal for PHP
💎 Roar for Ruby
🍢 Marshmallow for Python
⚡ FastAPI - Response Model for Python FastAPI framework.
💬 Tl;dr
Code:
import { Vicis } from "vicis";
const configuration = {
cast: {
// convert `_id` to integer
_id: Vicis.INTEGER,
// convert `registered` to boolean
registered: Vicis.FLAG,
},
nullish: {
// if not set `confirmed` set to `false`
confirmed: false,
},
exclude: [
// exclude fields with names like `password`
/(?:password)/gi, /^(?:_)(?:_)?/,
],
omit: [
// remove fields that may be personal
"createdAt", "updatedAt", "deletedAt",
],
rename: {
// rename `_id` to `id`
_id: "id",
// rename `email` to `login`
email: "login",
},
replace: {
// always replace field value with `null`
url: null,
},
order: [
// `id` and `login` goes first, then everyone else
"id", "login",
],
};
const model = {
_id: "54759309034942804",
email: "[email protected]",
userPassword: "36e80092ff7f1ed72903cda9409b9d2c",
registered: "1",
url: "example.com",
createdAt: "2020-01-01 01:23:45",
__v: 1
};
const serializer = new Vicis(configuration);
serializer.data(model);
console.log(serializer.getData());
Output:
{
"id": 54759309034942800,
"login": "[email protected]",
"confirmed": false,
"registered": true,
"url": null
}