api-toggler

Alias your app API with other API

Usage no npm install needed!

<script type="module">
  import apiToggler from 'https://cdn.skypack.dev/api-toggler';
</script>

README

api-toggler

Commitizen friendly XO code style

NPM version Build Status Coveralls Status Dependency Status DevDependency Status

Alias your app API with other API

Install

npm install --save api-toggler

Usage

const ApiToggler = require('api-toggler');

const someApiToggler = new ApiToggler({
  name: 'first_name',
  surname: 'last_name'
});

String

someApiToggler.toggle('name');
//=> 'first_name'

someApiToggler.toggle('first_name');
//=> 'name'

Object

someApiToggler.toggle({first_name: 'Vladimir', last_name: 'Rodkin'});
//=> {name: 'Vladimir', surname: 'Rodkin'}

someApiToggler.toggle({name: 'Vladimir', surname: 'Rodkin'});
//=> {first_name: 'Vladimir', last_name: 'Rodkin'}

Array

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'}]

Example

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', . . . }

License

MIT © Vladimir Rodkin