JSPM

jsonapi-converter

0.0.6
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 6
  • Score
    100M100P100Q29313F
  • License MIT

converting data for json api

Package Exports

  • jsonapi-converter

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (jsonapi-converter) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

JSON API Converter

A javascript library for converting your data to JSON API (1.0 compliant).

Installation

yarn add jsonapi-converter

Documentation

Converting data to JSON API

const { Converter } = require('jsonapi-converter');
const userConverter = new Converter('users', {
  attributes: ['name', 'email']
});

The constructor Converter takes two arguments:

  • type: The resource type.
  • options: The converting options.

Calling the convert method on the returned object will convert your data (object or array) to a compliant JSONAPI document.

Available options

  • attributes: An array of attributes to show in your resource
  • idKey: key of 'id' in your resource (by default it 'id' or '_id')
  • meta: document meta object
  • another resources (structure is fractaling)

Examples

const { Converter } = require('jsonapi-converter');
const userConverter = new Converter('users', {
  attributes: ['name', 'email']
});

const data = {
  id: 1,
  name: 'John',
  email: 'john@mail.com',
  password: 'qwe123'
};

const result = userConverter.convert(data);

The result will be something like:

{
  data: {
    type: 'users',
    id: 1,
    attributes: {
      name: 'John',
      email: 'john@mail.com'
    }
  }
}

Converting with relationships:

const { Converter } = require('jsonapi-converter');
const userConverter = new Converter('users', {
  meta: {
    count: 2
  }
  attributes: ['name', 'email'],
  address: {
    idKey: 'key'
    attributes: ['city']
  }
});

const data = [
  {
    id: 1,
    name: 'John',
    email: 'john@mail.com',
    password: 'qwe123',
    address: {
      key: 23,
      city: 'Moscow',
      street: 'Lenina'
    }
  },
  {
    id: 2,
    name: 'Kate',
    email: 'kate@mail.com',
    password: 'asd123',
    address: {
      key: 54,
      city: 'London',
      street: 'Baker st'
    }
  }
];

const result = userConverter.convert(data);

The result will be something like:

{
  meta: {
    count: 2
  },
  data: [
    {
      type: 'users',
      id: 1,
      attributes: {
        name: 'John',
        email: 'john@mail.com'
      },
      relationships: {
        address: {
          data: {
            type: 'address',
            id: 23,
            attributes: {
              city: 'Moscow'
            }
          }
        }
      }
    },
    {
      type: 'users',
      id: 2,
      attributes: {
        name: 'Kate',
        email: 'kate@mail.com'
      },
      relationships: {
        address: {
          data: {
            type: 'address',
            id: 54,
            attributes: {
              city: 'London'
            }
          }
        }
      }
    }
  ]
}

See more examples in tests