UNPKG

964 BJavaScriptView Raw
1'use strict';
2var isFunction = require('lodash/isFunction');
3var DeserializerUtils = require('./deserializer-utils');
4
5module.exports = function (opts) {
6 if (!opts) { opts = {}; }
7
8 this.deserialize = function (jsonapi, callback) {
9 function collection() {
10 return Promise
11 .all(jsonapi.data.map(function (d) {
12 return new DeserializerUtils(jsonapi, d, opts).perform();
13 }))
14 .then(function (result) {
15 if (isFunction(callback)) {
16 callback(null, result);
17 }
18
19 return result
20 });
21 }
22
23 function resource() {
24 return new DeserializerUtils(jsonapi, jsonapi.data, opts)
25 .perform()
26 .then(function (result) {
27 if (isFunction(callback)) {
28 callback(null, result);
29 }
30
31 return result
32 });
33 }
34
35 if (Array.isArray(jsonapi.data)) {
36 return collection();
37 } else {
38 return resource();
39 }
40 };
41};