diff --git a/ui/app/adapters/generated-item-list.js b/ui/app/adapters/generated-item-list.js index 9dac19bef9..5c5d3f2e91 100644 --- a/ui/app/adapters/generated-item-list.js +++ b/ui/app/adapters/generated-item-list.js @@ -6,22 +6,21 @@ import ApplicationAdapter from './application'; import { task } from 'ember-concurrency'; import { service } from '@ember/service'; -import { tracked } from '@glimmer/tracking'; -export default class GeneratedItemAdapter extends ApplicationAdapter { - @service store; - namespace = 'v1'; - @tracked dynamicApiPath = ''; +export default ApplicationAdapter.extend({ + store: service(), + namespace: 'v1', + urlForItem() {}, + dynamicApiPath: '', - @task - *getDynamicApiPath(id) { + getDynamicApiPath: task(function* (id) { + // TODO: remove yield at some point. const result = yield this.store.peekRecord('auth-method', id); this.dynamicApiPath = result.apiPath; return; - } + }), - @task - *fetchByQuery(store, query, isList) { + fetchByQuery: task(function* (store, query, isList) { const { id } = query; const data = {}; if (isList) { @@ -36,13 +35,13 @@ export default class GeneratedItemAdapter extends ApplicationAdapter { }; return { ...resp, ...data }; }); - } + }), query(store, type, query) { return this.fetchByQuery.perform(store, query, true); - } + }, queryRecord(store, type, query) { return this.fetchByQuery.perform(store, query); - } -} + }, +}); diff --git a/ui/app/services/path-help.js b/ui/app/services/path-help.js index c686ce583d..03b8284a77 100644 --- a/ui/app/services/path-help.js +++ b/ui/app/services/path-help.js @@ -9,18 +9,18 @@ has less (or no) information about. */ import Model from '@ember-data/model'; -import Service, { service } from '@ember/service'; +import Service from '@ember/service'; import { encodePath } from 'vault/utils/path-encoding-helpers'; import { getOwner } from '@ember/application'; import { expandOpenApiProps, combineAttributes } from 'vault/utils/openapi-to-attrs'; import fieldToAttrs from 'vault/utils/field-to-attrs'; import { resolve, reject } from 'rsvp'; -import { assert, debug } from '@ember/debug'; +import { debug } from '@ember/debug'; import { capitalize } from '@ember/string'; import { computed } from '@ember/object'; // eslint-disable-line import { withModelValidations } from 'vault/decorators/model-validations'; -import GeneratedItemAdapter from 'vault/adapters/generated-item-list'; +import generatedItemAdapter from 'vault/adapters/generated-item-list'; import { sanitizePath } from 'core/utils/sanitize-path'; import { filterPathsByItemType, @@ -29,16 +29,16 @@ import { } from 'vault/utils/openapi-helpers'; import { isPresent } from '@ember/utils'; -export default class PathHelpService extends Service { - @service store; - +export default Service.extend({ + attrs: null, + dynamicApiPath: '', ajax(url, options = {}) { const appAdapter = getOwner(this).lookup(`adapter:application`); const { data } = options; return appAdapter.ajax(url, 'GET', { data, }); - } + }, /** * getNewModel instantiates models which use OpenAPI fully or partially @@ -50,7 +50,9 @@ export default class PathHelpService extends Service { */ getNewModel(modelType, backend, apiPath, itemType) { const owner = getOwner(this); - const modelFactory = owner.factoryFor(`model:${modelType}`); + const modelName = `model:${modelType}`; + + const modelFactory = owner.factoryFor(modelName); let newModel, helpUrl; // if we have a factory, we need to take the existing model into account if (modelFactory) { @@ -62,7 +64,7 @@ export default class PathHelpService extends Service { } helpUrl = modelProto.getHelpUrl(backend); - return this.registerNewModelWithProps(helpUrl, backend, newModel, modelType); + return this.registerNewModelWithProps(helpUrl, backend, newModel, modelName); } else { debug(`Creating new Model for ${modelType}`); newModel = Model.extend({}); @@ -72,7 +74,7 @@ export default class PathHelpService extends Service { // and we don't need paths for them yet if (!apiPath) { helpUrl = newModel.proto().getHelpUrl(backend); - return this.registerNewModelWithProps(helpUrl, backend, newModel, modelType); + return this.registerNewModelWithProps(helpUrl, backend, newModel, modelName); } // use paths to dynamically create our openapi help url @@ -99,13 +101,13 @@ export default class PathHelpService extends Service { helpUrl = `/v1/${apiPath}${path.slice(1)}?help=true` || newModel.proto().getHelpUrl(backend); pathInfo.paths = paths; newModel = newModel.extend({ paths: pathInfo }); - return this.registerNewModelWithProps(helpUrl, backend, newModel, modelType); + return this.registerNewModelWithProps(helpUrl, backend, newModel, modelName); }) .catch((err) => { // TODO: we should handle the error better here console.error(err); // eslint-disable-line }); - } + }, /** * getPaths is used to fetch all the openAPI paths available for an auth method, @@ -134,7 +136,7 @@ export default class PathHelpService extends Service { itemID, }); }); - } + }, // Makes a call to grab the OpenAPI document. // Returns relevant information from OpenAPI @@ -184,7 +186,7 @@ export default class PathHelpService extends Service { const newProps = { ...paramProp, ...props }; return expandOpenApiProps(newProps); }); - } + }, getNewAdapter(pathInfo, itemType) { // we need list and create paths to set the correct urls for actions @@ -198,7 +200,7 @@ export default class PathHelpService extends Service { const createPath = paths.find((path) => path.action === 'Create' || path.operations.includes('post')); const deletePath = paths.find((path) => path.operations.includes('delete')); - return class NewAdapter extends GeneratedItemAdapter { + return generatedItemAdapter.extend({ urlForItem(id, isList, dynamicApiPath) { const itemType = sanitizePath(getPath.path); let url; @@ -220,30 +222,30 @@ export default class PathHelpService extends Service { } return url; - } + }, urlForQueryRecord(id, modelName) { return this.urlForItem(id, modelName); - } + }, urlForUpdateRecord(id) { const itemType = createPath.path.slice(1, createPath.path.indexOf('{') - 1); return `${this.buildURL()}/${apiPath}${itemType}/${id}`; - } + }, urlForCreateRecord(modelType, snapshot) { const id = snapshot.record.mutableId; // computed property that returns either id or private settable _id value const path = createPath.path.slice(1, createPath.path.indexOf('{') - 1); return `${this.buildURL()}/${apiPath}${path}/${id}`; - } + }, urlForDeleteRecord(id) { const path = deletePath.path.slice(1, deletePath.path.indexOf('{') - 1); return `${this.buildURL()}/${apiPath}${path}/${id}`; - } + }, createRecord(store, type, snapshot) { - return super.createRecord(...arguments).then((response) => { + return this._super(...arguments).then((response) => { // if the server does not return an id and one has not been set on the model we need to set it manually from the mutableId value if (!response?.id && !snapshot.record.id) { snapshot.record.id = snapshot.record.mutableId; @@ -251,12 +253,11 @@ export default class PathHelpService extends Service { } return response; }); - } - }; - } + }, + }); + }, registerNewModelWithProps(helpUrl, backend, newModel, modelName) { - assert('modelName should not include the type prefix', modelName.includes(':') === false); return this.getProps(helpUrl, backend).then((props) => { const { attrs, newFields } = combineAttributes(newModel.attributes, props); const owner = getOwner(this); @@ -286,7 +287,9 @@ export default class PathHelpService extends Service { }, ], }; - newModel = withModelValidations(validations)(class GeneratedItemModel extends newModel {}); + @withModelValidations(validations) + class GeneratedItemModel extends newModel {} + newModel = GeneratedItemModel; } } } catch (err) { @@ -305,10 +308,10 @@ export default class PathHelpService extends Service { }), }); newModel.reopenClass({ merged: true }); - owner.unregister(`model:${modelName}`); - owner.register(`model:${modelName}`, newModel); + owner.unregister(modelName); + owner.register(modelName, newModel); }); - } + }, getFieldGroups(newModel) { const groups = { default: [], @@ -332,5 +335,5 @@ export default class PathHelpService extends Service { fieldGroups.push({ [group]: groups[group] }); } return fieldToAttrs(newModel, fieldGroups); - } -} + }, +});