-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathres-deserialize.js
More file actions
51 lines (44 loc) · 1.37 KB
/
res-deserialize.js
File metadata and controls
51 lines (44 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const deserialize = require('./_deserialize')
const _isArray = require('lodash/isArray')
function needsDeserialization (method) {
return ['GET', 'PATCH', 'POST'].indexOf(method) !== -1
}
function isCollection (responseData) {
return _isArray(responseData)
}
module.exports = {
name: 'DESERIALIZE',
res: function (payload) {
/*
* Note: The axios ajax response attaches the actual response data to
* `res.data`. JSON API Resources also passes back the response with
* a `data` attribute. This means we have `res.data.data`.
*/
let jsonApi = payload.jsonApi
let status = payload.res.status
let req = payload.req
let res = payload.res.data
let errors = res.errors
let meta = res.meta
let links = res.links
let included = res.included
let data = null
if (status !== 204 && needsDeserialization(req.method)) {
if (isCollection(res.data)) {
data = deserialize.collection.call(jsonApi, res.data, included)
} else if (res.data) {
data = deserialize.resource.call(jsonApi, res.data, included)
}
deserialize.cache.clear()
}
if (res.data && data) {
var params = ['meta', 'links']
params.forEach(function (param) {
if (res.data[param]) {
data[param] = res.data[param]
}
})
}
return { data, errors, meta, links }
}
}