vicis
Version:
Presentation and transformation layer for data output in RESTful APIs.
105 lines (83 loc) • 3.25 kB
Markdown

Presentation and transformation layer for data output in RESTful APIs.
•• [Vicis Documentation](https://vicis.js.org) •• [JavaScript Repository](https://github.com/vicisjs/vicis) •• [Deno Repository](https://github.com/r37r0m0d3l/deno-vicis) ••
[](https://www.npmjs.com/package/vicis)
[](https://www.npmjs.com/package/vicis)
[](https://github.com/vicisjs/vicis)
[](https://github.com/vicisjs/vicis/blob/master/dist/index.d.ts)
[](https://travis-ci.com/vicisjs/vicis)
[](https://lgtm.com/projects/g/vicisjs/vicis/alerts/)
This is Node.js analogue to these libraries:
- 🐘 [Fractal](https://fractal.thephpleague.com/) for PHP
- 💎 [Roar](https://github.com/trailblazer/roar) for Ruby
- 🍢 [Marshmallow](https://marshmallow.readthedocs.io/en/stable/) for Python
- ⚡ [FastAPI - Response Model](https://fastapi.tiangolo.com/tutorial/response-model/) for Python FastAPI framework.
---
Code:
```js
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: "johnwick@gmail.com",
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:
```json
{
"id": 54759309034942800,
"login": "johnwick@gmail.com",
"confirmed": false,
"registered": true,
"url": null
}
```
[](https://r37r0m0d3l.icu/open_source_map)
<img src="https://raw.githubusercontent.com/r37r0m0d3l/r37r0m0d3l/master/osmap.svg" width="960" height="520" style="display:block;height:auto;margin-left:auto;margin-right:auto;min-height:520px;min-width:960px;width:100%;">
<!-- Badges -->
[]: https://buymeacoffee.com/r37r0m0d3l
[]: https://img.shields.io/badge/support-buymeacoffee-1E90FF.svg?&logo=buy-me-a-coffee&label=support