From 7680630e597810565e1bc26ceca51860d8e01b33 Mon Sep 17 00:00:00 2001 From: Joaquin Colacci Date: Thu, 7 Dec 2023 20:40:15 -0500 Subject: [PATCH] improvement: clean endpoints for the cloud endpoint (#168) --- src/context/asyncContext.ts | 2 +- src/context/config.ts | 28 +++++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/context/asyncContext.ts b/src/context/asyncContext.ts index 321d295..05e70df 100644 --- a/src/context/asyncContext.ts +++ b/src/context/asyncContext.ts @@ -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); diff --git a/src/context/config.ts b/src/context/config.ts index 9f8de58..0ab18a8 100644 --- a/src/context/config.ts +++ b/src/context/config.ts @@ -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();