2022-04-07 19:51:41 +00:00
|
|
|
import { camelize, capitalize } from '@ember/string';
|
2017-09-19 14:47:10 +00:00
|
|
|
import { RestSerializer } from 'ember-cli-mirage';
|
|
|
|
|
2022-04-07 19:51:41 +00:00
|
|
|
const keyCase = (str) =>
|
|
|
|
str === 'id' ? 'ID' : capitalize(camelize(str)).replace(/Id/g, 'ID');
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
export default RestSerializer.extend({
|
|
|
|
serialize() {
|
|
|
|
const json = RestSerializer.prototype.serialize.apply(this, arguments);
|
|
|
|
const keys = Object.keys(json);
|
|
|
|
if (keys.length === 1) {
|
|
|
|
return json[keys[0]];
|
|
|
|
} else {
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-08-28 00:58:24 +00:00
|
|
|
keyForModel: keyCase,
|
2022-04-07 19:51:41 +00:00
|
|
|
keyForForeignKey: (str) => `${keyCase(str)}ID`,
|
2018-03-13 21:41:54 +00:00
|
|
|
keyForCollection: keyCase,
|
2017-09-19 14:47:10 +00:00
|
|
|
keyForAttribute: keyCase,
|
|
|
|
keyForRelationship: keyCase,
|
2018-03-13 21:41:54 +00:00
|
|
|
keyForRelationshipIds: keyCase,
|
2017-09-19 14:47:10 +00:00
|
|
|
keyForEmbeddedRelationship: keyCase,
|
|
|
|
});
|