npm.io
2.0.0 • Published 8 years ago

api-meady-easy

Licence
MIT
Version
2.0.0
Deps
1
Size
11 kB
Vulns
0
Weekly
0

Api Utilities

Node.js http-utility functions commonly used in API development.

Supported Language Features

This project uses JavaScript ES6

Install

> npm install --save api-made-easy 
Run tests
> npm run test 
Run example
> node request/body/example
Usage
Body intersection

Creates a new object from a given object by inclusion.

const Body = require('api-made-easy');
const difference = require('./actions').difference;

const bodyRequest = {
  name: 'Peter Parker',
  grades: ['a', 'c'],
  age: null,
  dob: undefined,
  roles: [],
};

const bodyIntersection = Body(intersection(bodyRequest));
const nameNGrades = bodyIntersection(['name', 'grades']);
const ageNdob = bodyIntersection(['age', 'dob']);
const address = bodyIntersection(['address']);
console.log(nameNGrades); // { name: 'Peter Parker', grades: [ 'a', 'c' ] }
console.log(ageNdob); // { age: null, dob: undefined }
console.log(address); // { address: undefined }
Body Difference

Creates a new object from a given object by exclusion.

const Body = require('api-made-easy').Body;
const difference = require('api-made-easy').difference;

const bodyDifference = Body(difference(bodyRequest));
const bodyWithNoRoles = bodyDifference(['roles']);
console.log(bodyWithNoRoles);
/*
{   name: 'Peter Parker',
    grades: [ 'a', 'c' ],
    age: null,
    dob: undefined
}
*/
StandardResponse

StandardResponse wraps all http-responses into successful 200-http-responses. So, the caller-client decides the next step. Also, StandardResponse could return the original request.

Upon receiving 400's or 500's http-response-codes, some frameworks will re send the request automatically again and again . With the StandardResponse the caller-client can modify this behaviour.

const StandardResponse = require('api-made-easy').StandardResponse;

const happyResponse = new StandardResponse(
    true,
    'All good',
    {id: 1},
    {id:1, name: 'Peter Parker'}
).getResponse();

Keywords