Skip to content

Data adapter useful in data serialization when talking with external services.

Notifications You must be signed in to change notification settings

mgechev/data-adapter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

Library for data adaptation using decorators. You can declaratively state how your data should look when being sent 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

About

Data adapter useful in data serialization when talking with external services.

Topics

Resources

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published