restify-jsonapi

2.0.0 • Public • Published

restify-jsonapi

Package comprise JSON:API plugins and formatter for the restify server.

Plugins

JSONAPIRequest

The plugin sends response with '415 Unsupported Media Type' status code if the request specifies the header Content-Type: application/vnd.api+json with any media type parameters, or sends response '406 Not Acceptable' status code if a request’s Accept header contains the JSON:API media type and all instances of that media type are modified with media type parameters, otherwise calls the next().

const restify = require('restify');
const JSONAPIRequest = require('restify-jsonapi').plugins.JSONAPIRequest;
 
const server = restify.createServer();
server.pre(JSONAPIRequest);

JSONAPIResponse

The plugin attaches 'application/vnd.api+json' header to every response.

const restify = require('restify');
const JSONAPIResponse = require('restify-jsonapi').plugins.JSONAPIResponse;
 
const server = restify.createServer();
server.pre(JSONAPIResponse);

Formatter

The formatter converts given data to a JSON string.

const restify = require('restify');
const restifyJSONAPI = require('restify-jsonapi');
 
const formatJSONAPI = restifyJSONAPI.formatJSONAPI;
const mediaType = restifyJSONAPI.JSONAPIMediaType;
 
const server = restify.createServer({
  formatters: {
    mediaType: formatJSONAPI
  }
});
server.pre(JSONAPIResponse);

If the data is an instance of the Error, the formatter converts it to appropriate 'application/vnd.api+json' string. Result will be like that

{
  errors: [
    {
      status: data.statusCode,
      code: data.name,
      title: data.message,
    },
  ],
}

If the data includes 'errors' field, every element of the 'errors' will be converted into the object like above, and the data will be converted into JSON string. Otherwise the data will be converted into JSON string.

Readme

Keywords

none

Package Sidebar

Install

npm i restify-jsonapi

Weekly Downloads

0

Version

2.0.0

License

MIT

Unpacked Size

14.1 kB

Total Files

10

Last publish

Collaborators

  • m4toro