1.1.9 • Published 7 years ago

node-jsonapi v1.1.9

Weekly downloads
14
License
MIT
Repository
github
Last release
7 years ago

node-jsonapi

This library helps you build and parse JSON response using JSON API format.


Installation

$ yarn add node-jsonapi

Initialization

const JSONApi = require('node-jsonapi');
const API = new JSONApi(options);

Options

keyrequiredtypedescription
versionnostringJSON API version. Currently only support JSON API v1.0

Initialization without options

const JSONApi = require('node-jsonapi');
const API = new JSONApi();

Initialization with options

const JSONApi = require('node-jsonapi');
const API = new JSONApi({
  version: '1.0',
});

Methods

  • build()
  • error()
  • parse()

build()

Build valid JSON API response. Return a promise.

const JSONApi = require('node-jsonapi');
const API = new JSONApi();

// Assuming this is the data from SQL
let data = [{
  id: 1,
  title: 'FOO',
  userId: 10,
  userEmail: 'foo@gmail.com',
},
{
  id: 2,
  title: 'BAR',
  userId: 12,
  userEmail: 'bar@gmail.com',
},
{
  id: 3,
  title: 'FOOBAR',
  userId: 10,
  userEmail: 'foo@gmail.com',
}];

let options = {
  type: 'post',
  data: data,
  relationships: {
    author: {
      type: 'user',
      attributes: [ 'userId', 'userEmail' ],
      id: 'userId',
      singular: true,
    },
  },
};

API.build(options)
  .then(res.json)
  .catch((err) => {
    res.status(400);
    res.json(err);
  });

Resolved object

{
  "jsonapi": {
    "version": "1.0"
  },
  "data": [
    {
      "type": "post",
      "relationships": {
        "author": {
          "data": {
            "id": 10,
            "type": "user"
          },
          "meta": {
            "singular": true
          }
        }
      },
      "id": 1,
      "attributes": {
        "title": "FOO"
      }
    },
    {
      "type": "post",
      "relationships": {
        "author": {
          "data": {
            "id": 12,
            "type": "user"
          },
          "meta": {
            "singular": true
          }
        }
      },
      "id": 2,
      "attributes": {
        "title": "BAR"
      }
    },
    {
      "type": "post",
      "relationships": {
        "author": {
          "data": {
            "id": 10,
            "type": "user"
          },
          "meta": {
            "singular": true
          }
        }
      },
      "id": 3,
      "attributes": {
        "title": "FOOBAR"
      }
    }
  ],
  "included": [
    {
      "type": "user",
      "id": 10,
      "attributes": {
        "userEmail": "foo@gmail.com"
      }
    },
    {
      "type": "user",
      "id": 12,
      "attributes": {
        "userEmail": "bar@gmail.com"
      }
    }
  ]
}

Options

keyrequiredtypedescription
typeyesstringResponse data type
idnostringKey for data id when id key doesn't exist
datayesobjectResponse data. See data structure below
relationshipsnoobjectData relationship scheme. See relationships structure below
singularnobooleanWhether it's a single or multiple data

Data structure

keyrequiredtypedescription
idyesstring or numberData id
any keysnoanyAny data keys

Example of single data structure

let options = {
  //...
  data: {
    id: 1,
    title: 'foo',
  },
  //...
};

Example of multiple data structure

let options = {
  //...
  data = [
    { id: 1, title: 'foo' },
    { id: 2, title: 'bar' },
  ],
  //...
};

Relationships structure (optional)

This is the relationship scheme

keyrequiredtypedescription
nameyesstringRelationship name
/typeyesstringRelationship type
/idyesstringRelationship id
/attributesyesarrayRelationship attributes
/singularnobooleanWhether the relationship data is singular

Example

let options = {
  //...
  relationships: {
    author: {
      type: 'user',
      attributes: [ 'userId', 'userEmail' ],
      id: 'userId',
      singular: true,
    },
  },
  //...
};

error()

Build valid JSON API error response. Return a promise.

const JSONApi = require('node-jsonapi');
const API = new JSONApi();

let errors = [{
  status: 400,
  code: 'ERR_API_SET_PROFILE',
  title: 'Failed to set author profile',
  detail: 'Missing author name',
  source: 'profiles.create'
}];

API.error(errors)
  .then(res.json)
  .catch((err) => {
    res.status(400);
    res.json(err);
  });

Resolved object

{
  "jsonapi": {
    "version": "1.0"
  },
  "errors": [
    {
      "status": 400,
      "code": "ERR_API_SET_PROFILE",
      "title": "Failed to set author profile",
      "detail": "Missing author name",
      "source": "profiles.create"
    }
  ]
}

Errors structure

Errors is an Array of error.

Error

keyrequiredtypedescription
statusyesnumberHTTP status
codeyesstring or numberError code
titlenostringError title
detailnostringError detail
sourcenostringError source

parse()

Parse valid JSON API response to flat plain object. Return a promise.

const JSONApi = require('node-jsonapi');
const API = new JSONApi();

const jsonData = {
  "jsonapi": {
    "version": "1.0"
  },
  "data": [
    {
      "type": "post",
      "relationships": {
        "author": {
          "data": {
            "id": 10,
            "type": "user"
          },
          "meta": {
            "singular": true
          }
        }
      },
      "id": 1,
      "attributes": {
        "title": "FOO"
      }
    },
    {
      "type": "post",
      "relationships": {
        "author": {
          "data": {
            "id": 12,
            "type": "user"
          },
          "meta": {
            "singular": true
          }
        }
      },
      "id": 2,
      "attributes": {
        "title": "BAR"
      }
    },
    {
      "type": "post",
      "relationships": {
        "author": {
          "data": {
            "id": 10,
            "type": "user"
          },
          "meta": {
            "singular": true
          }
        }
      },
      "id": 3,
      "attributes": {
        "title": "FOOBAR"
      }
    }
  ],
  "included": [
    {
      "type": "user",
      "id": 10,
      "attributes": {
        "userEmail": "foo@gmail.com"
      }
    },
    {
      "type": "user",
      "id": 12,
      "attributes": {
        "userEmail": "bar@gmail.com"
      }
    }
  ]
};

API.parse(jsonData)
  .then(res.json)
  .catch((err) => {
    res.status(400);
    res.json(err);
  });

Resolved object

{
  "data": [
    {
      "author": {
        "userId": 10,
        "userEmail": "foo@gmail.com"
      },
      "postId": 1,
      "title": "FOO"
    },
    {
      "author": {
        "userId": 12,
        "userEmail": "bar@gmail.com"
      },
      "postId": 2,
      "title": "BAR"
    },
    {
      "author": {
        "userId": 10,
        "userEmail": "foo@gmail.com"
      },
      "postId": 3,
      "title": "FOOBAR"
    }
  ]
}
1.1.9

7 years ago

1.1.8

7 years ago

1.1.7

7 years ago

1.1.6

7 years ago

1.1.5

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago