1.0.4 • Published 1 year ago

decoverto v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

npm version Build Status Code coverage License

Decoverto

Convert data into instances of first and third party classes and back using decorators. Enjoy not having to write data-to-class setup code.

Test and tinker on the Decoverto playground

Features

A model looks like this:

import {model, map, MapShape, property} from 'decoverto';

@model()
class User {

    @property()
    createdAt: Date;

    @property()
    givenName: string;

    @property(() => String)
    referralToken: string | null;

    @property(map(() => String, () => Boolean, {shape: MapShape.Object}))
    permissions: Map<string, boolean>;
}

Quickstart

  1. Install decoverto

    yarn add decoverto

    or

    npm install decoverto
  2. When using TypeScript, enable experimentalDecorators in tsconfig.json

  3. Define a model

    import {model, map, MapShape, property} from 'decoverto';
    
    @model()
    class User {
    
        @property(() => Date)
        createdAt: Date;
    
        @property(() => String)
        givenName: string;
    
        @property(() => String)
        referralToken: string | null;
    
        @property(map(() => String, () => Boolean, {shape: MapShape.Object}))
        permissions: Map<string, boolean>;
    }
  4. Create a Decoverto instance const decoverto = new Decoverto()

  5. Convert some data

    // Convert raw data using the default JSON parser
    user = decoverto.type(User).rawToInstance(`{
        "createdAt": "2021-03-31T14:08:42.009Z",
        "givenName": "Mark",
        "referralToken": null,
        "permissions": {
            "canManageUsers": true,
            "canCreateProducts": true
        }
    }`);
    
    // Convert an object literal
    user = decoverto.type(User).plainToInstance({
        createdAt: new Date(),
        givenName: 'Mark',
        referralToken: null,
        permissions: {
            canManageUsers: true,
            canCreateProducts: true,
        }
    });

For more information, see links in the features list or the docs directory.

Examples/Playground

Decoverto has playgrounds where you can view examples and tinker with them. It allows you to see what is possible and experiment.

2.0.0

1 year ago

1.0.4

2 years ago

1.0.2

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.2.4

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago