adaptr
v1.1.4
Published
A small helper that allows the transformation of data received from a server to a more JS friendly format. It works in both direction.
Downloads
12
Maintainers
Readme
Adaptr
Adaptr is a small helper that allows the transformation of data received from a server to a more JS friendly format. It works in both direction..
Usage
import Adaptr from 'adaptr';
// user object received from server
const user = {
'id': 133,
'is_logged_in': true
};
// we define an adapter with the desired schema
// on the left it's the expected server format
// on the right it's the desired local format
const userAdapter = new Adaptr('user', {
'id': 'userId',
'is_logged_in': 'isLoggedIn'
});
const localFormat = userAdapter.unserialize(user);
const serverFormat = userAdapter.serialize(localFormat);
Output
"localFormat": {
"user": {
"userId": 133,
"isLoggedIn": true
}
}
"serverFormat": {
"user": {
"id": 133,
"is_logged_in": true
}
}
Nested objects
import Adaptr from 'adaptr';
// user object received from server
const ;
const comment = {
'comment_id': 23,
'text': 'this is a comment',
'user'= {
'id': 133,
'is_logged_in': true
}
};
// we define an adapter with the desired schema
// on the left it's the expected server format
// on the right it's the desired local format
const userAdapter = new Adaptr('user', {
'id': 'userId',
'is_logged_in': 'isLoggedIn'
});
const commentAdapter = new Adaptr('comment', {
'comment_id': 'id',
'text': 'text',
'user': userAdapter
});
const localFormat = commentAdapter.unserialize(user);
const serverFormat = commentAdapter.serialize(localFormat);
Output
"localFormat": {
"comment": {
"id": 23,
"text": "this is a comment",
"user": {
"userId": 133,
"isLoggedIn": true
}
}
}
"serverFormat": {
"comment": {
"comment_id": 23,
"text": "this is a comment",
"user": {
"id": 133,
"is_logged_in": true
}
}
}
Installation
Adaptr is dependencie free.
$ npm install adaptr
License
MIT
Free Software, Hell Yeah!