chore: 🤖 add adapter to generate URLs for host sets
This commit is contained in:
4 files changed
+93
-12
No files matched your search
@@ -0,0 +1,38 @@
|
||||
import ApplicationAdapter from './application';
|
||||
import { inject as service } from '@ember/service';
|
||||
|
||||
export default class HostSetAdapter extends ApplicationAdapter {
|
||||
|
||||
// =services
|
||||
|
||||
@service store;
|
||||
|
||||
// =methods
|
||||
|
||||
/**
|
||||
* Append a host catalog URL ahead of the host sets path if `hostCatalogID` is
|
||||
* passed via adapter options. Otherwise, generate the prefix as normal.
|
||||
*
|
||||
* TODO: generalize the interface to resource-prefixed URLs by accepting
|
||||
* a modelName and an ID, rather than looking for a known key.
|
||||
*
|
||||
* @override
|
||||
* @param {string} path
|
||||
* @param {string} parentURL
|
||||
* @param {string} modelName
|
||||
* @param {string} id
|
||||
* @param {object} snapshot
|
||||
* @return {string} urlPrefix
|
||||
*/
|
||||
urlPrefix(path, parentURL, modelName, id, snapshot) {
|
||||
const hostCatalogID = snapshot?.adapterOptions?.hostCatalogID;
|
||||
if (hostCatalogID) {
|
||||
const hostCatalogAdapter = this.store.adapterFor('host-catalog');
|
||||
return hostCatalogAdapter
|
||||
._buildPrefixedURL('host-catalog', hostCatalogID, snapshot);
|
||||
} else {
|
||||
return super.urlPrefix(...arguments);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -46,6 +46,31 @@ export default Mixin.create({
|
||||
return '';
|
||||
},
|
||||
|
||||
/**
|
||||
* Builds the (mostly full) URL with a prefix, but not the suffix.
|
||||
* The `_buildURL` method adds a suffix.
|
||||
* Do not call directly.
|
||||
* @override
|
||||
* @param {String} modelName
|
||||
* @param {String} id
|
||||
* @return {String} url
|
||||
*/
|
||||
_buildPrefixedURL(modelName, id, snapshot) {
|
||||
let path;
|
||||
let url = [];
|
||||
const host = get(this, 'host');
|
||||
const prefix = this.urlPrefix(null, null, modelName, id, snapshot);
|
||||
if (modelName) {
|
||||
path = this.pathForType(modelName);
|
||||
if (path) url.push(path);
|
||||
}
|
||||
if (id) url.push(encodeURIComponent(id));
|
||||
if (prefix) url.unshift(prefix);
|
||||
url = url.join('/');
|
||||
if (!host && url && url.charAt(0) !== '/') url = '/' + url;
|
||||
return `${url}`;
|
||||
},
|
||||
|
||||
/**
|
||||
* Overrides the default `_buildURL` with a nearly identical version.
|
||||
* This method simply passes arguments into the `urlPrefix` method for
|
||||
@@ -58,19 +83,8 @@ export default Mixin.create({
|
||||
* @return {String} url
|
||||
*/
|
||||
_buildURL(modelName, id, snapshot) {
|
||||
let path;
|
||||
let url = [];
|
||||
const host = get(this, 'host');
|
||||
const prefix = this.urlPrefix(null, null, modelName, id, snapshot);
|
||||
const url = this._buildPrefixedURL(modelName, id, snapshot);
|
||||
const suffix = this.urlSuffix(modelName, id, snapshot);
|
||||
if (modelName) {
|
||||
path = this.pathForType(modelName);
|
||||
if (path) url.push(path);
|
||||
}
|
||||
if (id) url.push(encodeURIComponent(id));
|
||||
if (prefix) url.unshift(prefix);
|
||||
url = url.join('/');
|
||||
if (!host && url && url.charAt(0) !== '/') url = '/' + url;
|
||||
return `${url}${suffix}`;
|
||||
},
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
export { default } from 'api/adapters/host-set';
|
||||
@@ -0,0 +1,28 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupTest } from 'ember-qunit';
|
||||
|
||||
module('Unit | Adapter | host set', function(hooks) {
|
||||
setupTest(hooks);
|
||||
|
||||
test('it generates correct complete URLs', function (assert) {
|
||||
assert.expect(2);
|
||||
const scopeID = 'o_1';
|
||||
const hostCatalogID = 'hc_098';
|
||||
const method = 'my-custom-method';
|
||||
const mockSnapshot = {
|
||||
adapterOptions: {
|
||||
scopeID,
|
||||
method
|
||||
}
|
||||
};
|
||||
const adapter = this.owner.lookup('adapter:host-set');
|
||||
// Without hostCatalogID...
|
||||
let url = adapter.buildURL('host-set', null, mockSnapshot, 'findAll');
|
||||
assert.equal(url, '/v1/scopes/o_1/host-sets:my-custom-method');
|
||||
// With hostCatalogID...
|
||||
mockSnapshot.adapterOptions.hostCatalogID = hostCatalogID;
|
||||
url = adapter.buildURL('host-set', null, mockSnapshot, 'findAll');
|
||||
assert.equal(url, '/v1/scopes/o_1/host-catalogs/hc_098/host-sets:my-custom-method');
|
||||
});
|
||||
|
||||
});
|
||||
Reference in new issue
Block a user