1.6.0 • Published 5 years ago

json-api-response-converter v1.6.0

Weekly downloads
124
License
MIT
Repository
github
Last release
5 years ago

JSON:API Response Converter

Donate npm.io npm.io npm.io

Features

  • Very small
  • No dependencies
  • Easy to use
  • Compatible with Vuex ORM

Getting started

Install

$ npm install json-api-response-converter --save

# or

$ yarn add json-api-response-converter

Usage

import JsonApiResponseConverter from 'json-api-response-converter'

const response = {
  data: [
    {
      id: '1',
      type: 'articles',
      attributes: {
        title: 'This project is awesome'
      },
      relationships: {
        author: {
          data: { id: '1', type: 'author' }
        },

        comments: {
          data: [
            { id: '1', type: 'comment' },
            { id: '2', type: 'comment' }
          ]
        }
      }
    }
  ],

  included: [
    {
      id: '1',
      type: 'author',
      attributes: {
        name: 'Anakin'
      }
    },
    {
      id: '1',
      type: 'comment',
      attributes: {
        body: 'First!'
      }
    },
    {
      id: '2',
      type: 'comment',
      attributes: {
        body: 'Second!'
      }
    }
  ]
}

const data = new JsonApiResponseConverter(response).formattedResponse

console.log(data)
/**
[
  {
    id: 1,
    title: 'This project is awesome',

    author: {
      id: 1,
      name: 'Anakin'
    },

    comments: [
      {
        id: 1,
        body: 'First!'
      },
      {
        id: 2,
        body: 'Second!'
      }
    ]
  }
]
*/

Many edges cases are tested in the tests folder

Development

$ git clone https://github.com/guillaumebriday/json-api-response-converter
$ cd json-api-response-converter
$ yarn # or npm install

You can run tests with Jest:

$ yarn test # or npm run test

Check the syntax with ESLint:

$ yarn lint

Contributing

Do not hesitate to contribute to the project by adapting or adding features ! Bug reports or pull requests are welcome.

Credits

Inspired by:

License

The gem is available as open source under the terms of the MIT License.

1.6.0

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago