2022-03-08 14:32:01 +03:00
|
|
|
import {Collection, RestSerializer} from 'miragejs';
|
|
|
|
import {pluralize} from 'ember-inflector';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {underscore} from '@ember/string';
|
2017-01-02 21:49:44 +03:00
|
|
|
|
|
|
|
export default RestSerializer.extend({
|
2018-10-18 02:18:29 +03:00
|
|
|
keyForCollection(collection) {
|
|
|
|
return underscore(pluralize(collection));
|
|
|
|
},
|
|
|
|
|
2017-01-02 21:49:44 +03:00
|
|
|
keyForAttribute(attr) {
|
|
|
|
return underscore(attr);
|
|
|
|
},
|
|
|
|
|
2018-10-18 02:18:29 +03:00
|
|
|
keyForRelationship(relationship) {
|
|
|
|
return underscore(relationship);
|
|
|
|
},
|
|
|
|
|
|
|
|
keyForEmbeddedRelationship(relationship) {
|
|
|
|
return underscore(relationship);
|
|
|
|
},
|
|
|
|
|
2020-01-08 18:36:17 +03:00
|
|
|
keyForForeignKey(relationshipName) {
|
|
|
|
return `${underscore(relationshipName)}_id`;
|
|
|
|
},
|
|
|
|
|
2017-01-02 21:49:44 +03:00
|
|
|
serialize(object, request) {
|
2022-04-13 21:34:48 +03:00
|
|
|
if (this.isModel(object)) {
|
2017-01-02 21:49:44 +03:00
|
|
|
object = new Collection(object.modelName, [object]);
|
|
|
|
}
|
|
|
|
|
|
|
|
let json = RestSerializer.prototype.serialize.call(this, object, request);
|
|
|
|
|
|
|
|
if (this.isCollection(object) && object.meta) {
|
|
|
|
json.meta = object.meta;
|
|
|
|
}
|
|
|
|
|
|
|
|
return json;
|
|
|
|
},
|
|
|
|
|
|
|
|
// POST and PUT request send data in pluralized attributes for all models,
|
|
|
|
// so we extract it here - this allows #normalizedRequestAttrs to work
|
|
|
|
// in route functions
|
|
|
|
normalize(body, modelName) {
|
|
|
|
// sometimes mirage doesn't include a modelName, so we extrapolate it from
|
|
|
|
// the first element of Object.keys
|
|
|
|
modelName = pluralize(modelName) || Object.keys(body)[0];
|
|
|
|
let [attributes] = body[modelName] || [{}];
|
|
|
|
return {data: {attributes}};
|
|
|
|
}
|
|
|
|
});
|