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

0.2.3 • Public • Published

Introduction

Library for data adaptation using decorators. You can declaratively state how your data should look when being send to the server.

How to use?

npm install data-adapter

Example

Using class adapter

import {AdaptClass, denormalize, normalize} from 'data-adapter';
 
@AdaptClass({
  name: snakeCase
})
class Human {
  firstName: string = 'foo';
  lastName: string = 'bar';
  age: number = 42;
}
 
// { first_name: 'foo', last_name: 'bar', age: 42 }
denormalize(new Human());
 
// { firstName: 'foo', lastName: 'bar', age: 42 }
normalize({ first_name: 'foo', last_name: 'bar', age: 42 }, Human);
 

Using property adapters

import {Adapt, denormalize, normalize} from 'data-adapter';
 
const denormalize = (obj, field) => {
  return obj[field] === 'male' ? 0 : 1;
};
 
const normalize = (obj, field) => {
  return obj[field] === 0 ? 'male' : 'female';
};
 
class Kid {
  @Adapt({ normalize, denormalize })
  gender;
}
 
class Parent {
  @Adapt({ name: 'first_name' }) firstName;
  @Adapt({ name: 'last_name' }) lastName;
  @Adapt({ type: Kid })
  kids;
}
 
let parent = new Parent();
parent.firstName = 'John';
parent.lastName = 'Doe';
 
let kid1 = new Kid();
kid1.gender = 'male';
let kid2 = new Kid();
kid2.gender = 'female';
 
// { first_name: 'John', last_name: 'Doe', kids: [{ gender: 0 }, { gender: 1 }] }
const denormalized = denormalize(new Person());
 
// { firstName: 'John', lastName: 'Doe', kids: [{ gender: 'male' }, { gender: 'female' }] }
const normalized = normalize(denormalized);

License

MIT

Dependents (0)

Package Sidebar

Install

npm i data-adapter

Weekly Downloads

2

Version

0.2.3

License

MIT

Last publish

Collaborators

  • mgechev