-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathnode.js
48 lines (40 loc) · 1.21 KB
/
node.js
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
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import RESTSerializer, { EmbeddedRecordsMixin } from '@ember-data/serializer/rest';
import { assign } from '@ember/polyfills';
import { decamelize } from '@ember/string';
export default RESTSerializer.extend(EmbeddedRecordsMixin, {
keyForAttribute: function (attr) {
return decamelize(attr);
},
pushPayload(store, payload) {
const transformedPayload = this.normalizeResponse(
store,
store.modelFor('node'),
payload,
null,
'findAll'
);
return store.push(transformedPayload);
},
nodeFromObject(name, payload) {
const nodeObj = payload.nodes[name];
return assign(nodeObj, {
name,
id: name,
});
},
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
const nodes = payload.nodes
? Object.keys(payload.nodes).map((name) => this.nodeFromObject(name, payload))
: [assign(payload, { id: '1' })];
const transformedPayload = { nodes: nodes };
return this._super(store, primaryModelClass, transformedPayload, id, requestType);
},
normalize(model, hash, prop) {
hash.id = '1';
return this._super(model, hash, prop);
},
});