https://github.com/rogermori/api-made-easy
Node.js utility functions commonly used in API development.
https://github.com/rogermori/api-made-easy
node node-js node-module nodejs nodejs-modules
Last synced: about 2 months ago
JSON representation
Node.js utility functions commonly used in API development.
- Host: GitHub
- URL: https://github.com/rogermori/api-made-easy
- Owner: rogermori
- License: mit
- Created: 2018-09-25T18:00:35.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-12-26T20:03:05.000Z (over 7 years ago)
- Last Synced: 2025-10-25T11:12:48.864Z (8 months ago)
- Topics: node, node-js, node-module, nodejs, nodejs-modules
- Language: JavaScript
- Homepage:
- Size: 89.8 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Api Utilities
Node.js utility functions commonly used in API development.
## Supported Language Features
This project uses JavaScript [ES6](https://github.com/lukehoban/es6features)
#### Install
```
> npm install --save api-made-easy
```
#### Run tests
```
> npm run test
```
#### Run examples
```
> node request/body/example
```
### Usage
#### Body intersection
Creates a new object from a given object by inclusion.
````
const Body = require('api-made-easy');
const intersection = require('api-made-easy').intersection;
const bodyRequest = {
name: 'Peter Parker',
grades: ['a', 'c'],
age: null,
dob: undefined,
roles: [],
};
const bodyIntersection = Body(intersection(bodyRequest));
const nameNGrades = bodyIntersection(['name', {grades: 'scores']);
const ageNdob = bodyIntersection(['age', 'dob']);
const address = bodyIntersection(['address']);
console.log(nameNGrades); // { name: 'Peter Parker', scores: [ '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,createSuccessResponse,
createErrorResponse} = require('api-made-easy');
const happyResponse = new StandardResponse(
true, //success
'All good', //sucess message option
{id: 1}, //response
{id:1, name: 'Peter Parker'}, //Context Info
true //Include orignal request in response
).getResponse();
const successResponse = createSuccessResponse(
{id: 1}, //response
{id:1, name: 'Peter Parker'}, //Context Info
'success message" //Optional
)
const errorResponse = createErrorResponse(
'error Message',
{id:1, name: 'Peter Parker'} //Context Info
)
````
### Header
Utility functions for handling headers
### Json Web Token Header
````
const JWt = require('api-made-easy').Headers.JWt;
console.log(JWt('tk12345'));
/*
{ 'Content-Type': application/json,
'Accept': application/json,
'Authorization': 'Bearer tk12345'
}
*/
console.log(JWt('tk12345',false));
/*
{ 'Content-Type': application/json,
'Accept': application/json,
'Authorization': 'tk12345'
}
*/
````