vicis

Presentation and transformation layer for data output in RESTful APIs.

Usage no npm install needed!

<script type="module">
  import vicis from 'https://cdn.skypack.dev/vicis';
</script>

README

Vicis

« Vicis »

Presentation and transformation layer for data output in RESTful APIs.

•• Vicis Documentation •• JavaScript Repository •• Deno Repository ••

npm downloads stars types build lgtm

This is Node.js analogue to these libraries:


💬 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: "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:

{
  "id": 54759309034942800,
  "login": "johnwick@gmail.com",
  "confirmed": false,
  "registered": true,
  "url": null
}

🗺️ My other projects

Full list here