Negligible Psychological Misery

    vicis
    TypeScript icon, indicating that this package has built-in type declarations

    2.2.0 • Public • Published

    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

    Install

    npm i vicis

    Homepage

    vicis.js.org

    DownloadsWeekly Downloads

    154

    Version

    2.2.0

    License

    MIT

    Unpacked Size

    101 kB

    Total Files

    10

    Last publish

    Collaborators

    • r37r0m0d3l