refactor: 💡 update to unprefixed target pathing

This commit is contained in:
Randall Morey committed 2020-09-03 14:35:05 -04:00
1 parent be06009e05
commit bce27df1f4
9 files changed
+81 -12

No files matched your search

+8 -1
View File
@@ -41,6 +41,7 @@ function prenormalizeArrayResponse(response) {
export default class ApplicationAdapter extends RESTAdapter.extend(
AdapterBuildURLMixin
) {
// =attributes
/**
@@ -57,6 +58,12 @@ export default class ApplicationAdapter extends RESTAdapter.extend(
*/
namespace = get(config, 'api.namespace');
/**
* Whether or not to prefix the URL with the resource scope.
* @type {boolean}
*/
hasScopePrefix = true;
// =methods
/**
@@ -80,7 +87,7 @@ export default class ApplicationAdapter extends RESTAdapter.extend(
const isScope = modelName === 'scope';
let scopePath = '';
let scopeID = '';
if (snapshot) {
if (snapshot && this.hasScopePrefix) {
// Not all snapshots have `attr` (such as array snapshots),
// so we do this sort of ugly check.
if (snapshot.attr) {
+13
View File
@@ -0,0 +1,13 @@
import ApplicationAdapter from './application';
export default class TargetAdapter extends ApplicationAdapter {
// =attributes
/**
* Targets use the new, unprefixed pathing style.
* @override
*/
hasScopePrefix = false;
}
@@ -14,6 +14,12 @@ export default class ApplicationSerializer extends RESTSerializer {
scope: { serialize: false }
};
/**
* Whether or not to serialize the scope ID into scope_id.
* @type {boolean}
*/
serializeScopeID = false;
// =methods
/**
@@ -67,6 +73,21 @@ export default class ApplicationSerializer extends RESTSerializer {
Object.assign(hash, serialized);
}
/**
* Optionally serializes the scope ID into `scope_id`.
* @override
* @param {Snapshot} snapshot
* @return {object}
*/
serialize(snapshot) {
const serialized = super.serialize(...arguments);
if (this.serializeScopeID) {
const scope_id = snapshot?.attr('scope')?.attr('scope_id');
if (scope_id) serialized.scope_id = scope_id;
}
return serialized;
}
/**
* In our API, array payloads are always rooted under the same key `items`.
* Ember Data normally expects them to be rooted under their model name,
+8
View File
@@ -2,6 +2,14 @@ import ApplicationSerializer from './application';
export default class TargetSerializer extends ApplicationSerializer {
// =attributes
/**
* Serialized the scope ID into the `scope_id` field.
* @type {boolean}
*/
serializeScopeID = true;
// =methods
/**
+1
View File
@@ -0,0 +1 @@
export { default } from 'api/adapters/target';
@@ -0,0 +1,12 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Adapter | target', function(hooks) {
setupTest(hooks);
// Replace this with your real tests.
test('it exists', function(assert) {
let adapter = this.owner.lookup('adapter:target');
assert.ok(adapter);
});
});
+3 -3
View File
@@ -59,7 +59,7 @@ module('Unit | Model | target', function (hooks) {
test('it has an `addHostSets` method that targets a specific POST API endpoint and serialization', async function (assert) {
assert.expect(1);
this.server.post('/v1/scopes/o_1/targets/123abc:add-host-sets', (schema, request) => {
this.server.post('/v1/targets/123abc:add-host-sets', (schema, request) => {
const body = JSON.parse(request.requestBody);
assert.deepEqual(body, {
host_set_ids: ['123_abc', 'foobar'],
@@ -92,7 +92,7 @@ module('Unit | Model | target', function (hooks) {
test('it has a `removeHostSets` method that targets a specific POST API endpoint and serialization', async function (assert) {
assert.expect(1);
this.server.post('/v1/scopes/o_1/targets/123abc:remove-host-sets', (schema, request) => {
this.server.post('/v1/targets/123abc:remove-host-sets', (schema, request) => {
const body = JSON.parse(request.requestBody);
assert.deepEqual(body, {
host_set_ids: ['1', '3'],
@@ -125,7 +125,7 @@ module('Unit | Model | target', function (hooks) {
test('it has a `removeHostSet` method that deletes a single host set using `removeHostSets` method', async function (assert) {
assert.expect(1);
this.server.post('/v1/scopes/o_1/targets/123abc:remove-host-sets', (schema, request) => {
this.server.post('/v1/targets/123abc:remove-host-sets', (schema, request) => {
const body = JSON.parse(request.requestBody);
assert.deepEqual(body, {
host_set_ids: ['3'],
@@ -16,8 +16,8 @@ export default class ScopesScopeProjectsProjectTargetsRoute extends Route {
* @return {Promise{[TargetModel]}}
*/
async model() {
const scopeID = this.modelFor('scopes.scope.projects.project').id;
return this.store.findAll('target', { adapterOptions: { scopeID } });
const { id: scope_id } = this.modelFor('scopes.scope.projects.project');
return this.store.query('target', { scope_id });
}
// =actions
+13 -6
View File
@@ -182,12 +182,19 @@ export default function() {
// target
this.get('/scopes/:scope_id/targets');
this.post('/scopes/:scope_id/targets');
this.get('/scopes/:scope_id/targets/:id');
this.patch('/scopes/:scope_id/targets/:id');
this.del('/scopes/:scope_id/targets/:id');
this.post('/scopes/:scope_id/targets/:idMethod', function ({ targets }, { params: { idMethod } }) {
this.get('/targets', function ({ targets }, { queryParams: { scope_id } }) {
return targets.where(target => target.scope?.id === scope_id);
});
this.post('/targets', function ({ targets, scopes }) {
const attrs = this.normalizedRequestAttrs();
const scope = scopes.findBy(scope => scope.id === attrs.scopeId);
attrs.scope = { id: scope.id, type: scope.type };
return targets.create(attrs);
});
this.get('/targets/:id');
this.patch('/targets/:id');
this.del('/targets/:id');
this.post('/targets/:idMethod', function ({ targets }, { params: { idMethod } }) {
const attrs = this.normalizedRequestAttrs();
const id = idMethod.split(':')[0];
const method = idMethod.split(':')[1];