71 lines
1.9 KiB
JavaScript
71 lines
1.9 KiB
JavaScript
/**
|
|
* Copyright IBM Corp. 2021, 2026
|
|
* SPDX-License-Identifier: BUSL-1.1
|
|
*/
|
|
|
|
import Service from '@ember/service';
|
|
import { assert } from '@ember/debug';
|
|
import { service } from '@ember/service';
|
|
import { notifyError } from 'core/decorators/notify';
|
|
|
|
export default class ClusterUrlService extends Service {
|
|
// =services
|
|
|
|
@service store;
|
|
|
|
// =attributes
|
|
|
|
/**
|
|
* @type {ApplicationAdapter}
|
|
*/
|
|
get adapter() {
|
|
return this.store.adapterFor('application');
|
|
}
|
|
|
|
// =methods
|
|
|
|
async getClusterUrl() {
|
|
return window.desktop.cluster.getClusterUrl();
|
|
}
|
|
|
|
/**
|
|
* Validates that the specified clusterUrl is a Boundary API. If so, the app is
|
|
* updated to use the clusterUrl. If it is not a Boundary API, or an clusterUrl
|
|
* wasn't specified, throws an error.
|
|
* @param {string} clusterUrl - protocol://host:port
|
|
*/
|
|
async setClusterUrl(clusterUrl) {
|
|
const originalHost = this.adapter.host;
|
|
assert(
|
|
`setClusterUrl expects a string, you passed ${clusterUrl}`,
|
|
typeof clusterUrl === 'string',
|
|
);
|
|
try {
|
|
// Trim whitespaces and silently drop trailing slashes if present.
|
|
// This is important since API paths that will be appended to the clusterUrl
|
|
// will include preceeding slashes already.
|
|
clusterUrl = clusterUrl.trim();
|
|
clusterUrl = clusterUrl.replace(/\/*$/, '');
|
|
this.adapter.host = clusterUrl;
|
|
if (clusterUrl !== (await this.getClusterUrl())) {
|
|
await window.desktop.cluster.setClusterUrl(clusterUrl);
|
|
}
|
|
} catch (e) {
|
|
this.adapter.host = originalHost;
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Resets the clusterUrl in both the electron store and the adapter host.
|
|
*/
|
|
@notifyError(({ message }) => message, {
|
|
catch: true,
|
|
log: { origin: 'resetClusterUrl' },
|
|
})
|
|
async resetClusterUrl() {
|
|
this.adapter.host = undefined;
|
|
return window.desktop.cluster.resetClusterUrl();
|
|
}
|
|
}
|