Skip to content

A json api formatter that works as a omnirouter middleware to format responses according to http://jsonapi.org

License

Notifications You must be signed in to change notification settings

FreeAllMedia/jsonapi-formatter

Repository files navigation

Jsonapi-formatter.js npm version license type npm downloads ECMAScript 6

ES6 Component that works as an omnirouter middleware to provide jsonapi responses.

import JsonApiFormatter from "jsonapi-formatter";
import Router from "omnirouter";

router = new Router(options);
router.get("/user", (request, response) => {
	response.internalServerError(new Error("Oh no!"));
});
//inject middleware
router.use(JsonApiFormatter);
router.listen(portNumber, () => {
	//now your responses will return an envelope according to the [jsonapi format](http://jsonapi.org/format) (notice that the entities you may return inside of it are not automatically converted, currently)
}));

Quality and Compatibility

Build Status Coverage Status Code Climate Dependency Status Dev Dependency Status

Every build and release is automatically tested on the following platforms:

node 0.12.x node 0.11.x node 0.10.x iojs 2.x.x iojs 1.x.x

If your platform is not listed above, you can test your local environment for compatibility by copying and pasting the following commands into your terminal:

npm install jsonapi-formatter
cd node_modules/jsonapi-formatter
gulp test-local

Installation

Copy and paste the following command into your terminal to install Jsonapi-formatter:

npm install jsonapi-formatter --save

Import / Require

// ES6
import jsonapi-formatter from "jsonapi-formatter";
// ES5
var jsonapi-formatter = require("jsonapi-formatter");
// Require.js
define(["require"] , function (require) {
    var jsonapi-formatter = require("jsonapi-formatter");
});

Getting Started

Json Api Formatter formats your response into a format according to the jsonapi format Currently it works as an omnirouter middleware, or manually by calling the methods

  • formatResponse(response) Receives a response object that has at least a set function by now.
  • format(body) Receives a body object to add the envelop to it.

Using it with omnirouter

Example usage with omnirouter

import JsonApiFormatter from "jsonapi-formatter";
import Router from "omnirouter";

router = new Router(options);
router.get("/user", (request, response) => {
	response.internalServerError(new Error("Oh no!"));
});
//inject middleware
router.use(JsonApiFormatter);
router.listen(portNumber, () => {
	//now your responses will return an envelope according to the [jsonapi format](http://jsonapi.org/format) (notice that the entities you may return inside of it are not automatically converted, currently)
}));

How to Contribute

See something that could use improvement? Have a great feature idea? We listen!

You can submit your ideas through our issues system, or make the modifications yourself and submit them to us in the form of a GitHub pull request.

We always aim to be friendly and helpful.

Running Tests

It's easy to run the test suite locally, and highly recommended if you're using Jsonapi-formatter.js on a platform we aren't automatically testing for.

npm test

About

A json api formatter that works as a omnirouter middleware to format responses according to http://jsonapi.org

Resources

License

Stars

Watchers

Forks

Packages

No packages published