-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
jsonapi-fixture-converter.js
121 lines (104 loc) · 3.2 KB
/
jsonapi-fixture-converter.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import Ember from 'ember';
import Converter from './fixture-converter';
const { pluralize, dasherize } = Ember.String;
/**
* Using `serializeMode` to create a payload the way ember-data would serialize types
* when returning a payload to a server that accepts JSON-API wherin the types are
* pluralized
*
*/
class JSONAPIFixtureConverter extends Converter {
constructor(store, options = { transformKeys: true, serializeMode: false }) {
super(store, options);
this.typeTransformFn = this.serializeMode ? pluralize : this.noTransformFn;
this.defaultKeyTransformFn = dasherize;
this.polymorphicTypeTransformFn = dasherize;
this.included = [];
}
emptyResponse(_, options={}) {
return {data: options.useValue || null};
}
/**
* JSONAPIerializer does not use modelName for payload key,
* and just has 'data' as the top level key.
*
* @param modelName
* @param fixture
* @returns {*}
*/
createPayload(modelName, fixture) {
return { data: fixture };
}
/**
* Add the included data
*
* @param payload
*/
addIncludedArray(payload) {
if (!Ember.isEmpty(this.included)) {
payload.included = this.included;
}
}
/**
In order to conform to the way ember data expects to handle relationships
in a json api payload ( during deserialization ), convert a record ( model instance )
into an object with type and id. Types are pluralized.
@param {Object or DS.Model instance} record
@param {Object} relationship
*/
normalizeAssociation(record, relationship) {
if (Ember.typeOf(record) === 'object') {
if (relationship.options.polymorphic) {
return { type: this.typeTransformFn(dasherize(record.type)), id: record.id };
} else {
return { type: this.typeTransformFn(record.type), id: record.id };
}
} else {
return { type: this.typeTransformFn(record.constructor.modelName), id: record.id };
}
}
isEmbeddedRelationship(/*modelName, attr*/) {
return false;
}
/**
Recursively descend into the fixture json, looking for relationships that are
either record instances or other fixture objects that need to be normalized
and/or included in the 'included' hash
@param modelName
@param fixture
@param included
@returns {{type: *, id: *, attributes}}
*/
convertSingle(modelName, fixture) {
let data = {
type: this.typeTransformFn(modelName),
attributes: this.extractAttributes(modelName, fixture),
};
this.addPrimaryKey(modelName, data, fixture);
let relationships = this.extractRelationships(modelName, fixture);
if (Object.getOwnPropertyNames(relationships).length > 0) {
data.relationships = relationships;
}
return data;
}
/*
Add the model to included array unless it's already there.
*/
addToIncluded(data) {
let found = Ember.A(this.included).find((model)=> {
return model.id === data.id && model.type === data.type;
});
if (!found) {
this.included.push(data);
}
}
addToIncludedFromProxy(proxy) {
proxy.includes().forEach((data)=> {
this.addToIncluded(data);
});
}
assignRelationship(object) {
return { data: object };
}
}
export default JSONAPIFixtureConverter;