Skip to content

Commit

Permalink
improvement: clean endpoints for the cloud endpoint (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
joacoc authored Dec 8, 2023
1 parent 97f3588 commit 7680630
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/context/asyncContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export default class AsyncContext extends Context {
this.clients = {
...this.clients,
admin: adminClient,
cloud: new CloudClient(adminClient, profile["cloud-endpoint"])
cloud: new CloudClient(adminClient, this.config.getCloudEndpoint())
};

await this.loadEnvironment(init);
Expand Down
28 changes: 25 additions & 3 deletions src/context/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,16 +354,38 @@ export class Config {
}
}

/**
* @returns a clean profile cloud endpoint.
*/
getCloudEndpoint(): string | undefined {
return this.profile && this.cleanEndpoint(this.profile["cloud-endpoint"]);
}

/**
* Removes unwanted characters from the endpoint URLs.
* @param endpoint
* @returns the clean endpoint.
*/
cleanEndpoint(endpoint: string | undefined): string | undefined {
if (endpoint) {
const trimmedEndpoint = endpoint.trim();
return trimmedEndpoint.endsWith("/") ? trimmedEndpoint.substring(0, trimmedEndpoint.length - 1) : trimmedEndpoint
}
}

/**
* @returns the admin endpoint of the current profile
*/
getAdminEndpoint(): string | undefined {
if (this.profile) {
const cloudEndpoint = this.getCloudEndpoint();
if (this.profile["admin-endpoint"]) {
return this.profile["admin-endpoint"];
} else if (this.profile["cloud-endpoint"]) {
const cloudUrl = new URL(this.profile["cloud-endpoint"]);
return this.cleanEndpoint(this.profile["admin-endpoint"]);
} else if (cloudEndpoint) {
const cloudUrl = new URL(cloudEndpoint);
console.log("[Config]", "After endpoint: ", cloudUrl.toString());
const { hostname } = cloudUrl;

if (hostname.startsWith("api.")) {
cloudUrl.hostname = "admin." + hostname.slice(4);
return cloudUrl.toString();
Expand Down

0 comments on commit 7680630

Please sign in to comment.