json-api-response-js
JSON API compliant http response library.
Tech
json-api-response-js uses a number of open source projects to work properly:
- jsonapi-serializer - A Node.js framework agnostic library for (de)serializing your data to JSON API (1.0 compliant).
- typcheckjs - JS data type checking.
Setup
npm install --save json-api-response-js
Usage
const JSONApiResponse = require; // Using axios... axios.get.then((response) => { callback; }).catch((error) => { callback; });...
Sample Output
// Handling Object Nested Payloadconst payload = ; const response = JSONApiResponse.created;// Output
References
const JSONApiResponse = require; // Status Code 200, OK// @param Object data - The object payload. // @param string type - The JSON API data type. // @param string contentType - The HTTP response content type. // @param Object responseHeaders - The HTTP response headers.
Tests
✘ jay@ThinkPad /usr/share/nginx/html/json-api-response-js npm test > json-api-response-js@1.0.0 test /usr/share/nginx/html/json-api-response-js> jest --coverage PASS __tests__/index.test.js testing index.js ✓ it should invoke index.ok ✓ it should invoke index.ok ✓ it should invoke index.ok ✓ it should invoke index.created ✓ it should invoke index.created ✓ it should invoke index.created ✓ it should invoke index.accepted ✓ it should invoke index.accepted ✓ it should invoke index.accepted ✓ it should invoke index.partialContent ✓ it should invoke index.partialContent ✓ it should invoke index.partialContent ✓ it should invoke index.movedPermanently ✓ it should invoke index.movedPermanently ✓ it should invoke index.movedPermanently ✓ it should invoke index.notModified ✓ it should invoke index.notModified ✓ it should invoke index.notModified ✓ it should invoke index.badRequest ✓ it should invoke index.badRequest ✓ it should invoke index.badRequest ✓ it should invoke index.unauthorized ✓ it should invoke index.unauthorized ✓ it should invoke index.unauthorized ✓ it should invoke index.forbidden ✓ it should invoke index.forbidden ✓ it should invoke index.forbidden ✓ it should invoke index.notFound ✓ it should invoke index.notFound ✓ it should invoke index.notFound ✓ it should invoke index.methodNotAllowed ✓ it should invoke index.methodNotAllowed ✓ it should invoke index.methodNotAllowed ✓ it should invoke index.internalServerError ✓ it should invoke index.internalServerError ✓ it should invoke index.internalServerError ✓ it should invoke index.custom ✓ it should invoke index.custom ✓ it should invoke index.custom ----------|----------|----------|----------|----------|-------------------|File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | ----------|----------|----------|----------|----------|-------------------|All files | 100 | 63.27 | 100 | 100 | | index.js | 100 | 63.27 | 100 | 100 |... 28,138,148,158 |----------|----------|----------|----------|----------|-------------------|Test Suites: 1 passed, 1 totalTests: 39 passed, 39 totalSnapshots: 0 totalTime: 1.094sRan all test suites.