
This commit adds adapters and serializers for v3, but also a fallback serializer for v2, which allows to handle v2 and v3 payloads at the same time. This is needed, because when we use v3 endpoint for one of the models (in this case repo), we can also get embedded records of other types (like branch or build).
42 lines
1.2 KiB
JavaScript
42 lines
1.2 KiB
JavaScript
import Ember from 'ember';
|
|
import V3Serializer from 'travis/serializers/v3';
|
|
|
|
export default V3Serializer.extend({
|
|
isNewSerializerAPI: true,
|
|
|
|
extractRelationships(modelClass, resourceHash) {
|
|
if(resourceHash['@type']) {
|
|
return this._super(...arguments);
|
|
} else {
|
|
let relationships = {};
|
|
|
|
modelClass.eachRelationship((key, relationshipMeta) => {
|
|
// V2 API payload
|
|
let relationship = null;
|
|
let relationshipKey = this.keyForV2Relationship(key, relationshipMeta.kind, 'deserialize');
|
|
|
|
if (resourceHash.hasOwnProperty(relationshipKey)) {
|
|
let data = null;
|
|
let relationshipHash = resourceHash[relationshipKey];
|
|
if (relationshipMeta.kind === 'belongsTo') {
|
|
data = this.extractRelationship(relationshipMeta.type, relationshipHash);
|
|
} else if (relationshipMeta.kind === 'hasMany') {
|
|
data = relationshipHash.map((item) => this.extractRelationship(relationshipMeta.type, item));
|
|
}
|
|
relationship = { data };
|
|
}
|
|
|
|
if (relationship) {
|
|
relationships[key] = relationship;
|
|
}
|
|
});
|
|
|
|
return relationships;
|
|
}
|
|
},
|
|
|
|
keyForV2Relationship(key, typeClass, method) {
|
|
return key.underscore() + '_id';
|
|
}
|
|
});
|