From ae535c607ea485d0b5829b2b53352e66da6347d8 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Tue, 19 May 2020 16:33:31 -0400 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=A4=96=20setup=20API=20concerns?= =?UTF-8?q?=20for=20project=20resource?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/api/addon/adapters/application.js | 14 ++++ addons/api/addon/adapters/project.js | 14 ++++ addons/api/addon/generated/models/project.js | 36 ++++++++++ addons/api/addon/models/base.js | 3 + addons/api/addon/models/project.js | 3 + addons/api/addon/serializers/application.js | 66 +++++++++++++++++ addons/api/app/adapters/application.js | 1 + addons/api/app/adapters/project.js | 1 + addons/api/app/models/base.js | 1 + addons/api/app/models/project.js | 1 + addons/api/app/serializers/application.js | 1 + addons/api/package.json | 1 + .../api/tests/dummy/app/routes/application.js | 4 +- addons/api/tests/dummy/config/environment.js | 4 ++ .../tests/unit/adapters/application-test.js | 20 ++++++ .../api/tests/unit/adapters/project-test.js | 16 +++++ addons/api/tests/unit/models/base-test.js | 13 ++++ addons/api/tests/unit/models/project-test.js | 12 ++++ .../unit/serializers/application-test.js | 70 +++++++++++++++++++ 19 files changed, 279 insertions(+), 2 deletions(-) create mode 100644 addons/api/addon/adapters/application.js create mode 100644 addons/api/addon/adapters/project.js create mode 100644 addons/api/addon/generated/models/project.js create mode 100644 addons/api/addon/models/base.js create mode 100644 addons/api/addon/models/project.js create mode 100644 addons/api/addon/serializers/application.js create mode 100644 addons/api/app/adapters/application.js create mode 100644 addons/api/app/adapters/project.js create mode 100644 addons/api/app/models/base.js create mode 100644 addons/api/app/models/project.js create mode 100644 addons/api/app/serializers/application.js create mode 100644 addons/api/tests/unit/adapters/application-test.js create mode 100644 addons/api/tests/unit/adapters/project-test.js create mode 100644 addons/api/tests/unit/models/base-test.js create mode 100644 addons/api/tests/unit/models/project-test.js create mode 100644 addons/api/tests/unit/serializers/application-test.js diff --git a/addons/api/addon/adapters/application.js b/addons/api/addon/adapters/application.js new file mode 100644 index 000000000..a64f443bc --- /dev/null +++ b/addons/api/addon/adapters/application.js @@ -0,0 +1,14 @@ +import RESTAdapter from '@ember-data/adapter/rest'; +import config from 'ember-get-config'; +import { get } from '@ember/object'; + +export default class ApplicationAdapter extends RESTAdapter { + // =attributes + + /** + * Sets namespace to the `api.namespace` string in the application's config, + * if set. + * @type {string} + */ + namespace = get(config, 'api.namespace'); +} diff --git a/addons/api/addon/adapters/project.js b/addons/api/addon/adapters/project.js new file mode 100644 index 000000000..a3a02d3e7 --- /dev/null +++ b/addons/api/addon/adapters/project.js @@ -0,0 +1,14 @@ +import ApplicationAdapter from './application'; +import ScopeAdapterMixin from '../mixins/scope-adapter'; + +export default class ProjectAdapter extends ApplicationAdapter.extend( + ScopeAdapterMixin +) { + // =attributes + + /** + * Project is scoped to org only, technically. + * @type {boolean} + */ + includeProject = false; +} diff --git a/addons/api/addon/generated/models/project.js b/addons/api/addon/generated/models/project.js new file mode 100644 index 000000000..abfe583d4 --- /dev/null +++ b/addons/api/addon/generated/models/project.js @@ -0,0 +1,36 @@ +import BaseModel from '../../models/base'; +import { attr } from '@ember-data/model'; + +/** + * Project contains all fields related to a Project resource + */ +export default class GeneratedProjectModel extends BaseModel { + // =attributes + + @attr('string', { + description: 'Optional name for identification purposes', + }) + name; + + @attr('string', { + description: 'Optional user-set description for identification purposes', + }) + description; + + @attr('date', { + description: 'The time this resource was created\nOutput only.', + readOnly: true, + }) + created_time; + + @attr('date', { + description: 'The time this resource was last updated\nOutput only.', + readOnly: true, + }) + updated_time; + + @attr('boolean', { + description: 'Whether the resource is disabled', + }) + disabled; +} diff --git a/addons/api/addon/models/base.js b/addons/api/addon/models/base.js new file mode 100644 index 000000000..d8d0d004d --- /dev/null +++ b/addons/api/addon/models/base.js @@ -0,0 +1,3 @@ +import Model from '@ember-data/model'; + +export default class BaseModel extends Model {} diff --git a/addons/api/addon/models/project.js b/addons/api/addon/models/project.js new file mode 100644 index 000000000..6799bf779 --- /dev/null +++ b/addons/api/addon/models/project.js @@ -0,0 +1,3 @@ +import GeneratedProjectModel from '../generated/models/project'; + +export default class OrgModel extends GeneratedProjectModel {} diff --git a/addons/api/addon/serializers/application.js b/addons/api/addon/serializers/application.js new file mode 100644 index 000000000..a17ee2db3 --- /dev/null +++ b/addons/api/addon/serializers/application.js @@ -0,0 +1,66 @@ +import RESTSerializer from '@ember-data/serializer/rest'; +import { underscore } from '@ember/string'; +import { copy } from '@ember/object/internals'; + +/** + * Manages serialization/normalization of data to/from the API. + */ +export default class ApplicationSerializer extends RESTSerializer { + // =methods + + /** + * Generates an underscored key for the attribute. + * @param {string} attr + * @return {string} + */ + keyForAttribute(attr) { + return underscore(attr); + } + + /** + * If an attribute is annotated as readOnly in the model, don't serialize it. + * Otherwise delegate to default attribute serializer. + * @method serializeAttribute + * @param {Snapshot} snapshot + * @param {Object} json + * @param {String} key + * @param {Object} attribute + */ + serializeAttribute(snapshot, json, key, { options }) { + return options.readOnly ? null : super.serializeAttribute(...arguments); + } + + /** + * 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, + * e.g. `{hostCatalogs: [...]}`. This method makes this transformation to + * accommodate Ember Data. + * + * @override + * @param {object} store + * @param {object} primaryModelClass + * @param {object} payload + * @param {?string} id + * @param {string} requestType + * @return {object} + */ + normalizeArrayResponse(store, primaryModelClass, payload, id, requestType) { + // Setup a new payload data structure. + const transformedPayload = {}; + // Find the Ember-data-expected root key name. + const payloadKey = this.payloadKeyFromModelName( + primaryModelClass.modelName + ); + // Copy the data rooted under `items` in the existing payload into the new + // payload under the expected root key name. + transformedPayload[payloadKey] = copy(payload.items, true); + // Return the result of normalizing the transformed payload. + return super.normalizeArrayResponse( + store, + primaryModelClass, + transformedPayload, + id, + requestType + ); + } +} diff --git a/addons/api/app/adapters/application.js b/addons/api/app/adapters/application.js new file mode 100644 index 000000000..1e462f594 --- /dev/null +++ b/addons/api/app/adapters/application.js @@ -0,0 +1 @@ +export { default } from 'api/adapters/application'; diff --git a/addons/api/app/adapters/project.js b/addons/api/app/adapters/project.js new file mode 100644 index 000000000..0908e069c --- /dev/null +++ b/addons/api/app/adapters/project.js @@ -0,0 +1 @@ +export { default } from 'api/adapters/project'; diff --git a/addons/api/app/models/base.js b/addons/api/app/models/base.js new file mode 100644 index 000000000..56eb805f8 --- /dev/null +++ b/addons/api/app/models/base.js @@ -0,0 +1 @@ +export { default } from 'api/models/base'; diff --git a/addons/api/app/models/project.js b/addons/api/app/models/project.js new file mode 100644 index 000000000..1fc732fe1 --- /dev/null +++ b/addons/api/app/models/project.js @@ -0,0 +1 @@ +export { default } from 'api/models/project'; diff --git a/addons/api/app/serializers/application.js b/addons/api/app/serializers/application.js new file mode 100644 index 000000000..568fa69b6 --- /dev/null +++ b/addons/api/app/serializers/application.js @@ -0,0 +1 @@ +export { default } from 'api/serializers/application'; diff --git a/addons/api/package.json b/addons/api/package.json index 46be73f6e..d5253ab79 100644 --- a/addons/api/package.json +++ b/addons/api/package.json @@ -41,6 +41,7 @@ "ember-data": "^3.16.0", "ember-disable-prototype-extensions": "^1.1.3", "ember-export-application-global": "^2.0.1", + "ember-get-config": "^0.2.4", "ember-load-initializers": "^2.1.1", "ember-maybe-import-regenerator": "^0.1.6", "ember-qunit": "^4.6.0", diff --git a/addons/api/tests/dummy/app/routes/application.js b/addons/api/tests/dummy/app/routes/application.js index 62946d66d..036973710 100644 --- a/addons/api/tests/dummy/app/routes/application.js +++ b/addons/api/tests/dummy/app/routes/application.js @@ -4,7 +4,7 @@ import { inject as service } from '@ember/service'; export default class ApplicationRoute extends Route { @service scope; - afterModel() { - //console.log(this.scope); + model() { + this.store.findAll('project'); } } diff --git a/addons/api/tests/dummy/config/environment.js b/addons/api/tests/dummy/config/environment.js index 1b05a0196..b073014e3 100644 --- a/addons/api/tests/dummy/config/environment.js +++ b/addons/api/tests/dummy/config/environment.js @@ -21,6 +21,10 @@ module.exports = function (environment) { // Here you can pass flags/options to your application instance // when it is created }, + + api: { + namespace: '/api/addon/v1', + }, }; if (environment === 'development') { diff --git a/addons/api/tests/unit/adapters/application-test.js b/addons/api/tests/unit/adapters/application-test.js new file mode 100644 index 000000000..16d34c9b1 --- /dev/null +++ b/addons/api/tests/unit/adapters/application-test.js @@ -0,0 +1,20 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; +import config from 'ember-get-config'; + +module('Unit | Adapter | application', function (hooks) { + setupTest(hooks); + + test('its namespace is equal to the configured namespace', function (assert) { + assert.expect(2); + const adapter = this.owner.lookup('adapter:application'); + assert.ok(adapter.namespace); + assert.equal(adapter.namespace, config.api.namespace); + }); + + test('it generates correct URL prefixes', function (assert) { + assert.expect(1); + const adapter = this.owner.lookup('adapter:application'); + assert.equal(adapter.urlPrefix(), config.api.namespace); + }); +}); diff --git a/addons/api/tests/unit/adapters/project-test.js b/addons/api/tests/unit/adapters/project-test.js new file mode 100644 index 000000000..90576b2bd --- /dev/null +++ b/addons/api/tests/unit/adapters/project-test.js @@ -0,0 +1,16 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; +import config from 'ember-get-config'; + +module('Unit | Adapter | project', function (hooks) { + setupTest(hooks); + + test('it generates URL prefixes without project scope', function (assert) { + assert.expect(1); + const service = this.owner.lookup('service:scope'); + service.org = { id: 1 }; + service.project = { id: 2 }; + const adapter = this.owner.lookup('adapter:project'); + assert.equal(adapter.urlPrefix(), `${config.api.namespace}/orgs/1`); + }); +}); diff --git a/addons/api/tests/unit/models/base-test.js b/addons/api/tests/unit/models/base-test.js new file mode 100644 index 000000000..d2cb2f2b4 --- /dev/null +++ b/addons/api/tests/unit/models/base-test.js @@ -0,0 +1,13 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Model | base', function (hooks) { + setupTest(hooks); + + // Replace this with your real tests. + test('it exists', function (assert) { + let store = this.owner.lookup('service:store'); + let model = store.createRecord('base', {}); + assert.ok(model); + }); +}); diff --git a/addons/api/tests/unit/models/project-test.js b/addons/api/tests/unit/models/project-test.js new file mode 100644 index 000000000..9b2a93d73 --- /dev/null +++ b/addons/api/tests/unit/models/project-test.js @@ -0,0 +1,12 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Model | project', function (hooks) { + setupTest(hooks); + + test('it exists', function (assert) { + let store = this.owner.lookup('service:store'); + let model = store.createRecord('project', {}); + assert.ok(model); + }); +}); diff --git a/addons/api/tests/unit/serializers/application-test.js b/addons/api/tests/unit/serializers/application-test.js new file mode 100644 index 000000000..281799a40 --- /dev/null +++ b/addons/api/tests/unit/serializers/application-test.js @@ -0,0 +1,70 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Serializer | application', function (hooks) { + setupTest(hooks); + + test('it exists', function (assert) { + assert.expect(1); + const store = this.owner.lookup('service:store'); + const serializer = store.serializerFor('application'); + assert.ok(serializer); + }); + + test('it serializes records', function (assert) { + assert.expect(1); + const store = this.owner.lookup('service:store'); + const record = store.createRecord('project', { + name: 'Project', + description: 'Description', + }); + const serializedRecord = record.serialize(); + assert.deepEqual(serializedRecord, { + name: 'Project', + description: 'Description', + disabled: false, + }); + }); + + test('it normalizes array records from an `items` root key', function (assert) { + assert.expect(1); + const store = this.owner.lookup('service:store'); + const serializer = store.serializerFor('project'); + const projectModelClass = store.createRecord('project').constructor; + const payload = { + items: [ + { id: 1, name: 'Project 1' }, + { id: 2, name: 'Project 2' }, + { id: 3, name: 'Project 3' }, + ], + }; + const normalizedArray = serializer.normalizeArrayResponse( + store, + projectModelClass, + payload + ); + assert.deepEqual(normalizedArray, { + included: [], + data: [ + { + id: '1', + type: 'project', + attributes: { name: 'Project 1' }, + relationships: {}, + }, + { + id: '2', + type: 'project', + attributes: { name: 'Project 2' }, + relationships: {}, + }, + { + id: '3', + type: 'project', + attributes: { name: 'Project 3' }, + relationships: {}, + }, + ], + }); + }); +});