2020-01-16 20:01:12 +03:00
|
|
|
import RESTSerializer from '@ember-data/serializer/rest';
|
2018-10-18 20:23:19 +03:00
|
|
|
import {camelize, decamelize, underscore} from '@ember/string';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {pluralize} from 'ember-inflector';
|
2016-01-23 21:12:22 +03:00
|
|
|
|
2022-02-02 21:27:03 +03:00
|
|
|
export default class Application extends RESTSerializer {
|
2019-12-17 23:42:42 +03:00
|
|
|
// hacky method for getting access to meta data for single-resource responses
|
|
|
|
// https://github.com/emberjs/data/pull/4077#issuecomment-200780097
|
|
|
|
// TODO: review once the record links and meta RFC lands
|
|
|
|
// https://github.com/emberjs/rfcs/blob/master/text/0332-ember-data-record-links-and-meta.md
|
|
|
|
extractMeta(store, typeClass) {
|
2022-02-02 21:27:03 +03:00
|
|
|
let meta = super.extractMeta(...arguments);
|
2019-12-17 23:42:42 +03:00
|
|
|
typeClass.___meta = meta;
|
|
|
|
return meta;
|
2022-02-02 21:27:03 +03:00
|
|
|
}
|
2019-12-17 23:42:42 +03:00
|
|
|
|
2022-02-02 21:27:03 +03:00
|
|
|
serialize() {
|
|
|
|
let json = super.serialize(...arguments);
|
2018-04-05 13:54:36 +03:00
|
|
|
|
|
|
|
// don't send attributes that are updated automatically on the server
|
|
|
|
delete json.created_by;
|
|
|
|
delete json.updated_by;
|
|
|
|
|
|
|
|
return json;
|
2022-02-02 21:27:03 +03:00
|
|
|
}
|
2018-04-05 13:54:36 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
serializeIntoHash(hash, type, record, options) {
|
2014-05-09 09:00:10 +04:00
|
|
|
// Our API expects an id on the posted object
|
|
|
|
options = options || {};
|
|
|
|
options.includeId = true;
|
|
|
|
|
|
|
|
// We have a plural root in the API
|
2016-06-11 19:52:36 +03:00
|
|
|
let root = pluralize(type.modelName);
|
2015-10-28 14:36:45 +03:00
|
|
|
let data = this.serialize(record, options);
|
2014-05-09 09:00:10 +04:00
|
|
|
|
|
|
|
hash[root] = [data];
|
2022-02-02 21:27:03 +03:00
|
|
|
}
|
2016-01-23 21:12:22 +03:00
|
|
|
|
|
|
|
keyForAttribute(attr) {
|
|
|
|
return decamelize(attr);
|
2022-02-02 21:27:03 +03:00
|
|
|
}
|
2018-10-18 20:23:19 +03:00
|
|
|
|
|
|
|
keyForRelationship(key, typeClass, method) {
|
|
|
|
let transform = method === 'serialize' ? underscore : camelize;
|
|
|
|
|
|
|
|
if (typeClass === 'belongsTo' && !key.match(/(Id|By)$/)) {
|
|
|
|
let transformed = `${transform(key)}_id`;
|
|
|
|
return transformed;
|
|
|
|
}
|
|
|
|
|
|
|
|
return transform(key);
|
2014-05-09 09:00:10 +04:00
|
|
|
}
|
2022-02-10 17:50:58 +03:00
|
|
|
|
|
|
|
normalizeQueryRecordResponse(store, primaryModelClass, payload) {
|
|
|
|
const root = this.keyForAttribute(primaryModelClass.modelName);
|
|
|
|
const pluralizedRoot = pluralize(root);
|
|
|
|
|
|
|
|
if (payload[pluralizedRoot] && root !== 'setting') {
|
|
|
|
payload[root] = payload[pluralizedRoot][0];
|
|
|
|
delete payload[pluralizedRoot];
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.normalizeQueryRecordResponse(...arguments);
|
|
|
|
}
|
2022-02-02 21:27:03 +03:00
|
|
|
}
|