UNPKG

4.42 kBMarkdownView Raw
1# JSON API Serializer
2[![Build Status](https://travis-ci.org/SeyZ/jsonapi-serializer.svg?branch=master)](https://travis-ci.org/SeyZ/jsonapi-serializer)
3
4A Node.js framework agnostic library for serializing your data to [JSON
5API](http://jsonapi.org) (1.0 compliant).
6
7## Installation
8`$ npm install jsonapi-serializer`
9
10## Documentation
11
12**JSONAPISerializer(type, data, opts)** serializes the *data* (can be an object or an array) following the rules defined in *opts*.
13
14- type: The resource type.
15- data: An object to serialize.
16- opts
17 - *attributes*: An array of attributes to show. You can define an attribute as an option if you want to define some relationships (included or not).
18 - *ref*: If present, it's considered as a [compount document](http://jsonapi.org/format/#document-compound-documents).
19 - *attributes*: An array of attributes to show.
20 - *links*: An object that describes the links. Values can be *string* or a *function* (see examples below)
21
22
23## Example
24
25### Simple usage
26
27```
28var JSONAPISerializer = require('jsonapi-serializer');
29
30new JSONAPISerializer('users', data, {
31 links: { self: 'http://localhost:3000/api/users' },
32 attributes: ['firstName', 'lastName']
33}).then(function (users) {
34 // `users` here are JSON API compliant.
35});
36```
37
38The result will be something like:
39
40```
41{
42 "links": {
43 "self": "http://localhost:3000/api/users"
44 },
45 "data": [{
46 "type": "users",
47 "id": "1",
48 "attributes": {
49 "first-name": "Sandro",
50 "last-name": "Munda"
51 }
52 }, {
53 "type": "users",
54 "id": "2",
55 "attributes": {
56 "first-name": "John",
57 "last-name": "Doe"
58 },
59 }]
60}
61```
62
63### Nested resource
64```
65var JSONAPISerializer = require('jsonapi-serializer');
66
67new JSONAPISerializer('users', data, {
68 links: { self: 'http://localhost:3000/api/users' },
69 attributes: ['firstName', 'lastName', 'address'],
70 address: {
71 attributes: ['addressLine1', 'zipCode', 'city']
72 }
73}).then(function (users) {
74 // `users` here are JSON API compliant.
75});
76```
77
78The result will be something like:
79
80```
81{
82 "links": {
83 "self": "http://localhost:3000/api/users"
84 },
85 "data": [{
86 "type": "users",
87 "id": "1",
88 "attributes": {
89 "first-name": "Sandro",
90 "last-name": "Munda",
91 "address": {
92 "address-line1": "630 Central Avenue",
93 "zip-code": 24012,
94 "city": "Roanoke"
95 }
96 }
97 }, {
98 "type": "users",
99 "id": "2",
100 "attributes": {
101 "first-name": "John",
102 "last-name": "Doe",
103 "address": {
104 "address-line1": "400 State Street",
105 "zip-code": 33702,
106 "city": "Saint Petersburg"
107 }
108 }
109 }]
110}
111```
112
113### Compount document
114
115```
116var JSONAPISerializer = require('jsonapi-serializer');
117
118new JSONAPISerializer('users', data, {
119 links: { self: 'http://localhost:3000/api/users' },
120 attributes: ['firstName', 'lastName', 'books'],
121 books: {
122 ref: '_id',
123 attributes: ['title', 'isbn'],
124 links: {
125 self: 'http://example.com/books/1/relationships/author',
126 related: function (book) {
127 return 'http://example.com/posts/' + post.id + '/author';
128 }
129 }
130 }
131}).then(function (users) {
132 // `users` here are JSON API compliant.
133});
134```
135
136The result will be something like:
137
138```
139{
140 "links": {
141 "self": "http://localhost:3000/api/users"
142 },
143 "data": [{
144 "type": "users",
145 "id": "1",
146 "attributes": {
147 "first-name": "Sandro",
148 "last-name": "Munda"
149 },
150 "relationships": {
151 "books": {
152 "data": [
153 { "type": "books", "id": "1" },
154 { "type": "books", "id": "2" }
155 ]
156 }
157 }
158 }, {
159 "type": "users",
160 "id": "2",
161 "attributes": {
162 "first-name": "John",
163 "last-name": "Doe"
164 },
165 "relationships": {
166 "books": {
167 "data": [
168 { "type": "books", "id": "1" },
169 { "type": "books", "id": "2" }
170 ]
171 }
172 }
173 }],
174 "included": [{
175 "type": "books",
176 "id": "1",
177 "attributes": {
178 "title": "La Vida Estilista",
179 "isbn": "9992266589"
180 }
181 }, {
182 "type": "books",
183 "id": "2",
184 "attributes": {
185 "title": "La Maria Cebra",
186 "isbn": "9992264446"
187 }
188 }, {
189 "type": "books",
190 "id": "3",
191 "attributes": {
192 "title": "El Salero Cangrejo",
193 "isbn": "9992209739"
194 }
195 }]
196}
197```
198
199
200# License
201
202[MIT](https://github.com/SeyZ/jsonapi-serializer/blob/master/LICENSE)