diff --git a/app/adapters/kv.js b/app/adapters/kv.js index ec2da18f764c..3133f8c4aa22 100644 --- a/app/adapters/kv.js +++ b/app/adapters/kv.js @@ -12,6 +12,9 @@ const makeAttrable = function(obj) { const keyToArray = function(key) { return (key === '/' ? '' : key).split('/'); }; +const PRIMARY_KEY = 'Key'; +const DATACENTER_KEY = 'Datacenter'; + const stringify = function(obj) { if (typeOf(obj) === 'string') { return obj; @@ -52,7 +55,7 @@ export default Adapter.extend({ }, urlForDeleteRecord: function(id, modelName, snapshot) { const query = { - dc: snapshot.attr('Datacenter'), + dc: snapshot.attr(DATACENTER_KEY), }; if (isFolder(id)) { query.recurse = null; @@ -61,7 +64,7 @@ export default Adapter.extend({ }, urlForCreateRecord: function(modelName, snapshot) { return this.appendURL('kv', keyToArray(snapshot.attr('Key')), { - dc: snapshot.attr('Datacenter'), + dc: snapshot.attr(DATACENTER_KEY), }); }, urlForUpdateRecord: function(id, modelName, snapshot) { @@ -86,11 +89,11 @@ export default Adapter.extend({ // isBoolean? should error on false const url = requestData.url.split('?')[0]; const kv = { - Key: url + [PRIMARY_KEY]: url .split('/') .splice(3) .join('/'), - Datacenter: '', + [DATACENTER_KEY]: '', }; // TODO: separator? // safest way to check this is a create? if (this.urlForCreateRecord(null, makeAttrable(kv)).split('?')[0] === url) { diff --git a/app/adapters/node.js b/app/adapters/node.js index 70f799ca86be..3f71734e0d7e 100644 --- a/app/adapters/node.js +++ b/app/adapters/node.js @@ -8,7 +8,7 @@ export default Adapter.extend({ delete query.id; return this.appendURL('internal/ui/node', [id]); }, - handleResponse: function(status, headers, payload, requestData) { - return this._super(status, headers, { nodes: payload }, requestData); - }, + // handleResponse: function(status, headers, payload, requestData) { + // return this._super(status, headers, { nodes: payload }, requestData); + // }, }); diff --git a/app/adapters/service.js b/app/adapters/service.js index fbf42df446b0..2e4c2855d81e 100644 --- a/app/adapters/service.js +++ b/app/adapters/service.js @@ -1,5 +1,6 @@ import Adapter from './application'; import { assign } from '@ember/polyfills'; +const PRIMARY_KEY = 'Id'; export default Adapter.extend({ urlForQuery: function(query, modelName) { return this.appendURL('internal/ui/services'); @@ -21,17 +22,18 @@ export default Adapter.extend({ const parts = requestData.url.split('/'); if (this.isQueryRecord(parts)) { response = { - Id: parts.pop(), + [PRIMARY_KEY]: parts.pop(), Nodes: response, }; } else { // isQuery response = response.map(function(item, i, arr) { return assign({}, item, { - Id: item.Name, + [PRIMARY_KEY]: item.Name, }); }); } - return this._super(status, headers, { services: response }, requestData); + return this._super(status, headers, response, requestData); + // return this._super(status, headers, {services: response}, requestData); }, }); diff --git a/app/serializers/acl.js b/app/serializers/acl.js index bbd997a21d15..62de4ac76624 100644 --- a/app/serializers/acl.js +++ b/app/serializers/acl.js @@ -2,49 +2,13 @@ import Serializer from './application'; import { typeOf } from '@ember/utils'; export default Serializer.extend({ primaryKey: 'ID', - normalizeQueryResponse: function(store, primaryModelClass, payload, id, requestType) { - return this._super( - store, - primaryModelClass, - { - [primaryModelClass.modelName]: payload, - }, - id, - requestType - ); - }, - normalizeQueryRecordResponse: function(store, primaryModelClass, payload, id, requestType) { - return this._super( - store, - primaryModelClass, - { - [primaryModelClass.modelName]: typeOf(payload) === 'array' ? payload[0] : payload, - }, - id, - requestType - ); - }, - normalizeUpdateRecordResponse: function(store, primaryModelClass, payload, id, requestType) { - return this.normalizeQueryResponse(...arguments); - }, - normalizeDeleteRecordResponse: function(store, primaryModelClass, payload, id, requestType) { - return this.normalizeQueryResponse( - store, - primaryModelClass, - { [this.get('primaryKey')]: id }, - id, - requestType - ); - }, - normalizeCreateRecordResponse: function(store, primaryModelClass, payload, id, requestType) { - return this._super( - store, - primaryModelClass, - { - [primaryModelClass.modelName]: payload, - }, - id, - requestType - ); + normalizePayload: function(payload, id, requestType) { + switch (requestType) { + case 'deleteRecord': + return { [this.get('primaryKey')]: id }; + case 'queryRecord': + return typeOf(payload) === 'array' ? payload[0] : payload; + } + return payload; }, }); diff --git a/app/serializers/application.js b/app/serializers/application.js index 73ccc8454e84..94653e78dd1a 100644 --- a/app/serializers/application.js +++ b/app/serializers/application.js @@ -1,3 +1,22 @@ import Serializer from 'ember-data/serializers/rest'; -export default Serializer.extend({}); +export default Serializer.extend({ + // this could get confusing if you tried to override + // say `normalizeQueryResponse` + // TODO: consider creating a method for each one of the `normalize...Response` family + normalizeResponse: function(store, primaryModelClass, payload, id, requestType) { + return this._super( + store, + primaryModelClass, + { + [primaryModelClass.modelName]: this.normalizePayload(payload, id, requestType), + }, + id, + requestType + ); + }, + // TODO: decide on whether this is a 'hook' or an 'overridable method' + normalizePayload: function(payload, id, requestType) { + return payload; + }, +}); diff --git a/app/serializers/dc.js b/app/serializers/dc.js index c3e29df299f1..28cd44eb9c44 100644 --- a/app/serializers/dc.js +++ b/app/serializers/dc.js @@ -1,19 +1,15 @@ import Serializer from './application'; export default Serializer.extend({ primaryKey: 'Name', - normalizeFindAllResponse: function(store, primaryModelClass, payload, id, requestType) { - return this._super( - store, - primaryModelClass, - { - [primaryModelClass.modelName]: payload.map(item => { + normalizePayload: function(payload, id, requestType) { + switch (requestType) { + case 'findAll': + return payload.map(item => { return { [this.get('primaryKey')]: item, }; - }), - }, - id, - requestType - ); + }); + } + return payload; }, }); diff --git a/app/serializers/kv.js b/app/serializers/kv.js index 4c54600a4efd..fce5184eb674 100644 --- a/app/serializers/kv.js +++ b/app/serializers/kv.js @@ -1,48 +1,17 @@ import Serializer from './application'; - export default Serializer.extend({ primaryKey: 'Key', - normalizeQueryResponse: function(store, primaryModelClass, payload, id, requestType) { - return this._super( - store, - primaryModelClass, - { - [primaryModelClass.modelName]: payload.map(function(item, i, arr) { + normalizePayload: function(payload, id, requestType) { + switch (requestType) { + case 'query': + return payload.map(item => { return { Key: item, }; - }), - }, - id, - requestType - ); - }, - normalizeCreateRecordResponse: function(store, primaryModelClass, payload, id, requestType) { - return this._super( - store, - primaryModelClass, - { - [primaryModelClass.modelName]: payload, - }, - id, - requestType - ); - }, - normalizeUpdateRecordResponse: function(store, primaryModelClass, payload, id, requestType) { - return this.normalizeCreateRecordResponse(...arguments); - }, - normalizeDeleteRecordResponse: function(store, primaryModelClass, payload, id, requestType) { - return this.normalizeCreateRecordResponse(...arguments); - }, - normalizeQueryRecordResponse: function(store, primaryModelClass, payload, id, requestType) { - return this._super( - store, - primaryModelClass, - { - [primaryModelClass.modelName]: payload[0], - }, - id, - requestType - ); + }); + case 'queryRecord': + return payload[0]; + } + return payload; }, }); diff --git a/app/serializers/session.js b/app/serializers/session.js index 60c873cbabd2..6db937efe41f 100644 --- a/app/serializers/session.js +++ b/app/serializers/session.js @@ -2,26 +2,11 @@ import Serializer from './application'; export default Serializer.extend({ primaryKey: 'ID', - normalizeQueryResponse: function(store, primaryModelClass, payload, id, requestType) { - return this._super( - store, - primaryModelClass, - { - [primaryModelClass.modelName]: payload, - }, - id, - requestType - ); - }, - normalizeQueryRecordResponse: function(store, primaryModelClass, payload, id, requestType) { - return this._super( - store, - primaryModelClass, - { - [primaryModelClass.modelName]: payload[0], - }, - id, - requestType - ); + normalizePayload: function(payload, id, requestType) { + switch (requestType) { + case 'queryRecord': + return payload[0]; + } + return payload; }, }); diff --git a/tests/unit/adapters/kv-test.js b/tests/unit/adapters/kv-test.js index 8d5f38b412cf..ccdcda8b2332 100644 --- a/tests/unit/adapters/kv-test.js +++ b/tests/unit/adapters/kv-test.js @@ -6,7 +6,21 @@ module('Unit | Adapter | kv', function(hooks) { // Replace this with your real tests. test('it exists', function(assert) { - let adapter = this.owner.lookup('adapter:kv'); + const adapter = this.owner.lookup('adapter:kv'); assert.ok(adapter); }); + test('handleResponse returns a Kv-like object when the request is a createRecord', function(assert) { + const adapter = this.owner.lookup('adapter:kv'); + // unflake, this is also going through _super and + // using urlForCreateRecord and makeAttrable so not the unit + const expected = 'key/name'; + const actual = adapter.handleResponse(200, {}, true, { + method: 'GET', + url: `/v1/kv/${expected}`, + }); + assert.deepEqual(actual, { + Key: expected, + Datacenter: '', + }); + }); });