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

2.1.0 • Public • Published
Logo

A lightweight typescript library to handle the mapping of entities to REST APIs

Installation

The library is available as a package on npm.

npm install --save trem

Usage

First, configure your entity/entities using the default annotations/hydrators, or create your own.

import { Entity, Number, String, BasicEntityHydrator } from 'trem';

@Entity({'potato$': BasicEntityHydrator})
export class Potato  {
    @Number('id', false) public id: number;
    @String('variety', true) public variety: string;
    @String('color', true) public color: string;
    @String('locations.0.country', true) public mainCountry: string;

    public isCommon(): boolean {
        return this.color === 'yellow';
    }

    // Add your own logic here
}

Then, use the responseHandler to do the mapping for you.

import { ResponseHandler } from 'trem';
import { Potato } from './potato';

class MyApiCaller {
  protected entityMapper: ResponseHandler;

  constructor() {
    this.entityMapper = new ResponseHandler([Potato /* Add your other entities here */])
  }

  public getData<T>(): T[] {
    // Your api call logic here
    return this.entityMapper.handle(url, responseData);
  }
}

Package Sidebar

Install

npm i trem

Weekly Downloads

1

Version

2.1.0

License

MIT

Unpacked Size

27.7 kB

Total Files

14

Last publish

Collaborators

  • jtabet