# jsonapi-converter

> converting data for json api

Latest version **0.0.6** (published 2019-08-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install jsonapi-converter
pnpm add jsonapi-converter
yarn add jsonapi-converter
bun add jsonapi-converter
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2019-08-04 |
| First published | 2019-05-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 11.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Kirill Kvashonin |
| Maintainers | etozhkirill |
| Keywords | json-api, jsonapi, converter, serializer, javascript |

## Links

- npm: https://www.npmjs.com/package/jsonapi-converter
- Repository: https://github.com/kirillurgant/jsonapi-converter
- Homepage: https://github.com/kirillurgant/jsonapi-converter#readme
- Issues: https://github.com/kirillurgant/jsonapi-converter/issues
- npm.io page: https://npm.io/package/jsonapi-converter

## Dependencies (1)

- [ramda](https://npm.io/package/ramda.md) ^0.26.1

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.0.6 (latest) — 2019-08-04
- 0.0.5 — 2019-05-11
- 0.0.4 — 2019-05-10
- 0.0.3 — 2019-05-10

## README

# JSON API Converter

A javascript library for converting your data to [JSON
API](http://jsonapi.org) (1.0 compliant).

## Installation
`yarn add jsonapi-converter`

## Documentation

### Converting data to JSON API
```javascript
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

```javascript
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:

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

Converting with relationships:
```javascript
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:

```javascript
{
  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](https://github.com/kirillurgant/jsonapi-converter/blob/master/tests/index.test.js)

---
_Source: https://npm.io/package/jsonapi-converter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
