npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-api-response-js

v1.0.7

Published

JSON API Standard compliant http response library.

Downloads

5

Readme

json-api-response-js

JSON API compliant http response library.

npm version Build Status Coverage Status

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('json-api-response-js');

// Using axios
...
  axios.get(process.env.SOME_URL, {
    method: 'POST',
    headers: {
      ...
      'Content-Type': 'application/json',
      ...
    },
  }).then((response) => {
    callback(null, JSONApiResponse.ok({
      message: '...',
      data: { ... },
    }));
  }).catch((error) => {
    callback(null, JSONAPIResponse.internalServerError({
      message: '...',
      data: error,
    }));
  });
...

Sample Output

// Handling Object Nested Payload
const payload = {
    id: 1,
    order_id: 10,
    name: 'Gwen Hester',
    info: {
      mobile: '+63 919 123 4567',
      age: 25,
      status: 'married'
    },
};

const response = JSONApiResponse.created(payload);
// Output
{ status: 201,
      headers:
       { 'Content-Type': 'application/vnd.api+json',
         responseHeaders:
          { 'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Credentials': true } },
      body: '{"data":{"type":"serializes","id":"1","attributes":{"id":1,"order-id":10,"name":"Gwen Hester","info":{"mobile":"+63 919 123 1234","age":25,"status":"married"}}}}' }

// Handling Array Nested Payload
const arrayNestedPayload = [{
    id: 1,
    order_id: 10,
    name: 'JL Raymundo',
    info: {
      mobile: '+63 918 123 4567',
      age: 25,
      status: 'single'
    },
    country: {
      code: 'PH',
      name: 'Philippines'
    }
  },
  {
    id: 2,
    order_id: 11,
    name: 'Eugene Santos',
    info: {
      mobile: '+63 917 123 4567',
      age: 35,
      status: 'married'
    },
    country: {
      code: 'PH',
      name: 'Philippines'
    }
  }
];

const response = JSONApiResponse.accepted(arrayNestedPayload);
// Output
{ status: 201,
      headers:
       { 'Content-Type': 'application/vnd.api+json',
         responseHeaders:
          { 'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Credentials': true } },
      body: '{"data":[{"type":"serializes","id":"1","attributes":{"id":1,"order-id":10,"name":"JL Raymundo","info":{"mobile":"+63 918 123 4567","age":25,"status":"single"},"country":{"code":"PH","name":"Philippines"}}},{"type":"serializes","id":"2","attributes":{"id":2,"order-id":11,"name":"Eugene Santos","info":{"mobile":"+63 917 123 4567","age":35,"status":"married"},"country":{"code":"PH","name":"Philippines"}}}]}' }

References

const JSONApiResponse = require('json-api-response-js');

// Status Code 200, OK
// @param Object data - The object payload. (Default value: {})
// @param string type - The JSON API data type. (Default value: 'response')
// @param string contentType - The HTTP response content type. (Default value: 'application/vnd.api+json')
// @param Object responseHeaders - The HTTP response headers. (Default value: {
//    'Access-Control-Allow-Origin':
//    '*','Access-Control-Allow-Credentials': true}
//    )
JSONApiResponse.ok({...});

// Status Code 201, Created
// @param Object data - The object payload. (Default value: {})
// @param string type - The JSON API data type. (Default value: 'response')
// @param string contentType - The HTTP response content type. (Default value: 'application/vnd.api+json')
// @param Object responseHeaders - The HTTP response headers. (Default value: {
//    'Access-Control-Allow-Origin':
//    '*','Access-Control-Allow-Credentials': true}
//    )
JSONApiResponse.created({...});

// Status Code 202, Accepted
// @param Object data - The object payload. (Default value: {})
// @param string type - The JSON API data type. (Default value: 'response')
// @param string contentType - The HTTP response content type. (Default value: 'application/vnd.api+json')
// @param Object responseHeaders - The HTTP response headers. (Default value: {
//    'Access-Control-Allow-Origin':
//    '*','Access-Control-Allow-Credentials': true}
//    )
JSONApiResponse.accepted({...});

// Status Code 206, Partial Content
// @param Object data - The object payload. (Default value: {})
// @param string type - The JSON API data type. (Default value: 'response')
// @param string contentType - The HTTP response content type. (Default value: 'application/vnd.api+json')
// @param Object responseHeaders - The HTTP response headers. (Default value: {
//    'Access-Control-Allow-Origin':
//    '*','Access-Control-Allow-Credentials': true}
//    )
JSONApiResponse.partialContent({...});

// Status Code 301, Moved Permanently
// @param Object data - The object payload. (Default value: {})
// @param string type - The JSON API data type. (Default value: 'response')
// @param string contentType - The HTTP response content type. (Default value: 'application/vnd.api+json')
// @param Object responseHeaders - The HTTP response headers. (Default value: {
//    'Access-Control-Allow-Origin':
//    '*','Access-Control-Allow-Credentials': true}
//    )
JSONApiResponse.movedPermanently({...});

// Status Code 304, Not Modified
// @param Object data - The object payload. (Default value: {})
// @param string type - The JSON API data type. (Default value: 'response')
// @param string contentType - The HTTP response content type. (Default value: 'application/vnd.api+json')
// @param Object responseHeaders - The HTTP response headers. (Default value: {
//    'Access-Control-Allow-Origin':
//    '*','Access-Control-Allow-Credentials': true}
//    )
JSONApiResponse.notModified({...});

// Status Code 400, Bad Request
// @param Object data - The object payload. (Default value: {})
// @param string type - The JSON API data type. (Default value: 'response')
// @param string contentType - The HTTP response content type. (Default value: 'application/vnd.api+json')
// @param Object responseHeaders - The HTTP response headers. (Default value: {
//    'Access-Control-Allow-Origin':
//    '*','Access-Control-Allow-Credentials': true}
//    )
JSONApiResponse.badRequest({...});

// Status Code 401, Unauthorized
// @param Object data - The object payload. (Default value: {})
// @param string type - The JSON API data type. (Default value: 'response')
// @param string contentType - The HTTP response content type. (Default value: 'application/vnd.api+json')
// @param Object responseHeaders - The HTTP response headers. (Default value: {
//    'Access-Control-Allow-Origin':
//    '*','Access-Control-Allow-Credentials': true}
//    )
JSONApiResponse.unauthorized({...});

// Status Code 403, Forbidden
// @param Object data - The object payload. (Default value: {})
// @param string type - The JSON API data type. (Default value: 'response')
// @param string contentType - The HTTP response content type. (Default value: 'application/vnd.api+json')
// @param Object responseHeaders - The HTTP response headers. (Default value: {
//    'Access-Control-Allow-Origin':
//    '*','Access-Control-Allow-Credentials': true}
//    )
JSONApiResponse.forbidden({...});

// Status Code 404, Not Found
// @param Object data - The object payload. (Default value: {})
// @param string type - The JSON API data type. (Default value: 'response')
// @param string contentType - The HTTP response content type. (Default value: 'application/vnd.api+json')
// @param Object responseHeaders - The HTTP response headers. (Default value: {
//    'Access-Control-Allow-Origin':
//    '*','Access-Control-Allow-Credentials': true}
//    )
JSONApiResponse.notFound({...});

// Status Code 405, Method Not Allowed
// @param Object data - The object payload. (Default value: {})
// @param string type - The JSON API data type. (Default value: 'response')
// @param string contentType - The HTTP response content type. (Default value: 'application/vnd.api+json')
// @param Object responseHeaders - The HTTP response headers. (Default value: {
//    'Access-Control-Allow-Origin':
//    '*','Access-Control-Allow-Credentials': true}
//    )
JSONApiResponse.methodNotAllowed({...});

// Status Code 500, Internal Server Error
// @param Object data - The object payload. (Default value: {})
// @param string type - The JSON API data type. (Default value: 'response')
// @param string contentType - The HTTP response content type. (Default value: 'application/vnd.api+json')
// @param Object responseHeaders - The HTTP response headers. (Default value: {
//    'Access-Control-Allow-Origin':
//    '*','Access-Control-Allow-Credentials': true}
//    )
JSONApiResponse.internalServerError({...});

// Custom. Statue 302, Found
// @param Object data - The object payload. (Default value: {})
// @param string type - The JSON API data type. (Default value: 'response')
// @param integer status - The HTTP response status. (Default value: 200)
// @param string contentType - The HTTP response content type. (Default value: 'application/vnd.api+json')
// @param Object responseHeaders - The HTTP response headers. (Default value: {
//    'Access-Control-Allow-Origin':
//    '*','Access-Control-Allow-Credentials': true}
//    )
JSONApiResponse.custom({...}, 'custom-response-type', 302, 'application/json', {
    'CUSTOM-HEADER': 'VALUE'
}});

Tests

 ✘ jay@ThinkPad  /usr/share/nginx/html/json-api-response-js  npm test

> [email protected] test /usr/share/nginx/html/json-api-response-js
> jest --coverage

 PASS  __tests__/index.test.js
  testing index.js
    ✓ it should invoke index.ok function with array and return JSON API standard response (5ms)
    ✓ it should invoke index.ok function with object and return JSON API standard response
    ✓ it should invoke index.ok function with object (nested) and return JSON API standard response (1ms)
    ✓ it should invoke index.created function with array and return JSON API standard response (1ms)
    ✓ it should invoke index.created function with object and return JSON API standard response
    ✓ it should invoke index.created function with object (nested) and return JSON API standard response (1ms)
    ✓ it should invoke index.accepted function with array and return JSON API standard response
    ✓ it should invoke index.accepted function with object and return JSON API standard response
    ✓ it should invoke index.accepted function with object (nested) and return JSON API standard response (1ms)
    ✓ it should invoke index.partialContent function with array and return JSON API standard response (1ms)
    ✓ it should invoke index.partialContent function with object and return JSON API standard response
    ✓ it should invoke index.partialContent function with object (nested) and return JSON API standard response
    ✓ it should invoke index.movedPermanently function with array and return JSON API standard response (1ms)
    ✓ it should invoke index.movedPermanently function with object and return JSON API standard response
    ✓ it should invoke index.movedPermanently function with object (nested) and return JSON API standard response
    ✓ it should invoke index.notModified function with array and return JSON API standard response
    ✓ it should invoke index.notModified function with object and return JSON API standard response
    ✓ it should invoke index.notModified function with object (nested) and return JSON API standard response
    ✓ it should invoke index.badRequest function with array and return JSON API standard response (1ms)
    ✓ it should invoke index.badRequest function with object and return JSON API standard response
    ✓ it should invoke index.badRequest function with object (nested) and return JSON API standard response
    ✓ it should invoke index.unauthorized function with array and return JSON API standard response (1ms)
    ✓ it should invoke index.unauthorized function with object and return JSON API standard response
    ✓ it should invoke index.unauthorized function with object (nested) and return JSON API standard response
    ✓ it should invoke index.forbidden function with array and return JSON API standard response (1ms)
    ✓ it should invoke index.forbidden function with object and return JSON API standard response
    ✓ it should invoke index.forbidden function with object (nested) and return JSON API standard response
    ✓ it should invoke index.notFound function with array and return JSON API standard response
    ✓ it should invoke index.notFound function with object and return JSON API standard response (1ms)
    ✓ it should invoke index.notFound function with object (nested) and return JSON API standard response
    ✓ it should invoke index.methodNotAllowed function with array and return JSON API standard response
    ✓ it should invoke index.methodNotAllowed function with object and return JSON API standard response (1ms)
    ✓ it should invoke index.methodNotAllowed function with object (nested) and return JSON API standard response
    ✓ it should invoke index.internalServerError function with array and return JSON API standard response
    ✓ it should invoke index.internalServerError function with object and return JSON API standard response (1ms)
    ✓ it should invoke index.internalServerError function with object (nested) and return JSON API standard response
    ✓ it should invoke index.custom function with array and return JSON API standard response
    ✓ it should invoke index.custom function with object and return JSON API standard response
    ✓ it should invoke index.custom function with object (nested) and return JSON API standard response (1ms)

----------|----------|----------|----------|----------|-------------------|
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 total
Tests:       39 passed, 39 total
Snapshots:   0 total
Time:        1.094s
Ran all test suites.