mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-21 09:52:06 +03:00
bc09a7c4d3
no issue
- adjusts mirage config so that we're correctly serializing foreign keys
- our API outputs (and our app code expects) foreign keys for non-embedded resources to be in the format `{relationship_name}_id` but mirage's default REST serializer does not include the `_id` suffix
- tests started failing because c46d04f612
introduced a direct access of the invite->role relationship which Ember Data was not correctly creating when mirage was outputting `role` rather than `role_id`
53 lines
1.7 KiB
JavaScript
53 lines
1.7 KiB
JavaScript
import {Collection, RestSerializer} from 'ember-cli-mirage';
|
|
import {pluralize} from 'ember-cli-mirage/utils/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}};
|
|
}
|
|
});
|