Ghost/ghost/admin/mirage/serializers/application.js
Kevin Ansfield c4c48d4104 Resolved ember-cli-mirage import deprecations
refs https://github.com/TryGhost/Admin/pull/2209

- `miragejs` has been extracted to a framework-independent library, the re-exports of `miragejs` elements in `ember-cli-mirage` have been deprecated making our test logs very noisy
- added `miragejs` as a top-level dependency
- updated all relevant imports to pull from `miragejs` instead of `ember-cli-mirage`
2022-03-08 11:32:01 +00:00

53 lines
1.7 KiB
JavaScript

import {Collection, RestSerializer} from 'miragejs';
import {pluralize} from 'ember-inflector';
import {underscore} from '@ember/string';
export default RestSerializer.extend({
keyForCollection(collection) {
return underscore(pluralize(collection));
},
keyForAttribute(attr) {
return underscore(attr);
},
keyForRelationship(relationship) {
return underscore(relationship);
},
keyForEmbeddedRelationship(relationship) {
return underscore(relationship);
},
keyForForeignKey(relationshipName) {
return `${underscore(relationshipName)}_id`;
},
serialize(object, request) {
// Ember expects pluralized responses for the post, user, and invite models,
// and this shortcut will ensure that those models are pluralized
if (this.isModel(object) && ['post', 'user', 'invite'].includes(object.modelName)) {
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}};
}
});