jsonapi-fractal
TypeScript icon, indicating that this package has built-in type declarations

2.3.1 • Public • Published

jsonapi-fractal

npm version Test Test Coverage Maintainability License

JSON:API Serializer inspired by Fractal (PHP)

Installation

yarn add jsonapi-fractal
OR
npm install jsonapi-fractal --save

Simple Serialize

// examples/simple-serialize.js

const { serialize, CaseType } = require('jsonapi-fractal')

const entity = {
  id: 1,
  firstName: 'Joe',
  lastName: 'Doe',
  address: {
    id: 1,
  },
  images: [{ id: 1 }, { id: 2 }],
}

const serialized = serialize(entity, 'users', {
  relationships: ['address', 'images'],
  changeCase: CaseType.kebabCase,
})

console.log(JSON.stringify(serialized))

/**
 * OUTPUT:
 *
 * {
 *   "data": {
 *     "id": 1,
 *     "type": "users",
 *     "attributes": {
 *       "first-name": "Joe",
 *       "last-name": "Doe"
 *     },
 *     "relationships": {
 *       "address": {
 *         "data": {
 *           "id": 1,
 *           "type": "address"
 *         }
 *       },
 *       "images": {
 *         "data": [
 *           {
 *             "id": 1,
 *             "type": "images"
 *           },
 *           {
 *             "id": 2,
 *             "type": "images"
 *           }
 *         ]
 *       }
 *     }
 *   }
 * }
 */

Deserialize

// examples/deserialize.js

const { deserialize, CaseType } = require('jsonapi-fractal')

const serializedData = {
  data: {
    id: 'myuserid',
    type: 'users',
    attributes: {
      name: 'Joe',
      'last-name': 'Doe',
    },
  },
}
const entity = deserialize(serializedData, { changeCase: CaseType.camelCase })

console.log(JSON.stringify(entity))

/**
 * OUTPUT:
 *
 * {
 *   "id": "myuserid",
 *   "name": "Joe",
 *   "lastName": "Doe"
 * }
 */

Serialize with transformers

// examples/serialize-with-transformers.js

const { Transformer, DefaultTransformer, transform, whitelist } = require('jsonapi-fractal')

class UserTransformer extends Transformer {
  constructor() {
    super()
    this.type = 'users'
    this.relationships = {
      images: this.images,
    }
  }

  transform(user, options) {
    return whitelist(user, ['_id', 'firstName', 'lastName'])
  }

  images(user, options) {
    return transform()
      .withInput(user.images)
      .withTransformer(new DefaultTransformer('images'))
      .withIncluded(true)
      .toContext()
  }
}

const user = {
  _id: 1,
  firstName: 'Joe',
  lastName: 'Doe',
  images: [{ _id: 5, url: 'http://' }],
}

const serialized = transform()
  .withInput(user)
  .withTransformer(new UserTransformer())
  .withOptions({ idKey: '_id' })
  .serialize()

console.log(JSON.stringify(serialized))

/**
 * OUTPUT:
 *
 * {
 *   "data": {
 *     "id": 1,
 *     "type": "users",
 *     "attributes": {
 *       "firstName": "Joe",
 *       "lastName": "Doe"
 *     },
 *     "relationships": {
 *       "images": {
 *         "data": [
 *           {
 *             "id": 5,
 *             "type": "images"
 *           }
 *         ]
 *       }
 *     }
 *   },
 *   "included": [
 *     {
 *       "id": 5,
 *       "type": "images",
 *       "attributes": {
 *         "url": "http://"
 *       }
 *     }
 *   ]
 * }
 */

Links

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
2.3.12,814latest

Version History

VersionDownloads (Last 7 Days)Published
2.3.12,814
2.3.00
2.2.050
2.1.00
2.0.10
2.0.00
1.1.022
1.0.1976
1.0.180
1.0.170
1.0.160
1.0.150
1.0.140
1.0.130
1.0.120
1.0.110
1.0.100
1.0.90
1.0.80
1.0.70
1.0.60
1.0.50
1.0.40
1.0.30
1.0.20
1.0.10
1.0.00

Package Sidebar

Install

npm i jsonapi-fractal

Weekly Downloads

2,962

Version

2.3.1

License

MIT

Unpacked Size

33.3 kB

Total Files

22

Last publish

Collaborators

  • andersondanilo