JSON:API Serializer inspired by Fractal (PHP)
yarn add jsonapi-fractal
OR
npm install jsonapi-fractal --save
import { serialize } from '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: 'kebabCase'
})
import { deserialize } from 'jsonapi-fractal'
const serializedData = ...
const entity = deserialize(serializedData, { changeCase: 'camelCase' })
import { Transformer, DefaultTransformer, transform, whitelist } from 'jsonapi-fractal'
class UserTransformer extends Transformer {
constructor() {
super()
this.type = 'users'
this.relationships = ['images']
}
transform (user, options) {
return whitelist(user, ['_id', 'firstName', 'lastName'])
}
images (user, options) {
return transform()
.withInput(user.images)
.withTransformer(new DefaultTransformer('images'))
.withIncluded(true)
}
}
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()