Alias your app API with other API
npm install --save api-toggler
const ApiToggler = require('api-toggler');
const someApiToggler = new ApiToggler({
name: 'first_name',
surname: 'last_name'
});
someApiToggler.toggle('name');
//=> 'first_name'
someApiToggler.toggle('first_name');
//=> 'name'
someApiToggler.toggle({first_name: 'Vladimir', last_name: 'Rodkin'});
//=> {name: 'Vladimir', surname: 'Rodkin'}
someApiToggler.toggle({name: 'Vladimir', surname: 'Rodkin'});
//=> {first_name: 'Vladimir', last_name: 'Rodkin'}
someApiToggler.toggle([{
first_name: 'Vladimir',
last_name: 'Rodkin'
}, {
first_name: 'Foo',
last_name: 'Bar'
}]);
//=> [{name: 'Vladimir', surname: 'Rodkin'}, {name: 'Foo', surname: 'Bar'}]
someApiToggler.toggle([{
name: 'Vladimir',
surname: 'Rodkin'
}, {
name: 'Foo',
surname: 'Bar'
}]);
//=> [{first_name: 'Vladimir', last_name: 'Rodkin'}, {first_name: 'Foo', last_name: 'Bar'}]
My API | GitHub API |
---|---|
pk |
id |
username |
login |
fullname |
name |
I need to convert GitHub API to my and back
Initialize schema toggler:
const ApiToggler = require('api-toggler');
const ghApiToggler = new ApiToggler({
username: 'login',
fullname: 'name',
pk: 'id'
});
Toggle GitHub API to my API:
fetch('https://api.github.com/users/vovanr')
.then(x => x.json())
//=> {id: 1, login: 'VovanR', name: 'Vladimir Rodkin', . . . }
.then(x => ghApiToggler.toggle(x))
//=> {pk: 1, username: 'VovanR', fullname: 'Vladimir Rodkin', . . . }
Toggle my API to GitHub API:
x
//=> {pk: 1, username: 'VovanR', fullname: 'Vladimir Rodkin', . . . }
apiToggler.toggle(x)
//=> {id: 1, login: 'VovanR', name: 'Vladimir Rodkin', . . . }
MIT © Vladimir Rodkin