From 9112a694d9380d9bbc964b843e61ad5335164a5e Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Mon, 17 Mar 2025 09:28:22 +1100 Subject: [PATCH] Strips trailing slashes from API endpoint. If you provide the API endpoint with a trailing slash, then the URL that is formed has 2 slashes in. This causes a 500 error when making requests to the Data API. The 500 error is not clear about the cause, it's a very generic openresty error, so stripping trailing slashes saves time in debugging a non-obvious issue. --- src/db/db.ts | 2 ++ tests/unit/db/db.test.ts | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/src/db/db.ts b/src/db/db.ts index 039077bc..1ae6c61b 100644 --- a/src/db/db.ts +++ b/src/db/db.ts @@ -150,6 +150,8 @@ export class Db extends HierarchicalEmitter { : this.#defaultOpts.dbOptions.keyspace ?? undefined, }; + endpoint = endpoint.endsWith('/') ? endpoint.replace(/\/+$/, "") : endpoint; + this.#httpClient = new DataAPIHttpClient({ baseUrl: endpoint, tokenProvider: this.#defaultOpts.dbOptions.token, diff --git a/tests/unit/db/db.test.ts b/tests/unit/db/db.test.ts index ed1ed0e0..e49bb3d0 100644 --- a/tests/unit/db/db.test.ts +++ b/tests/unit/db/db.test.ts @@ -43,6 +43,11 @@ describe('unit.db.db', () => { const client = new DataAPIClient(); assert.doesNotThrow(() => client.db(TEST_APPLICATION_URI)); }); + + it("should strip trailing slashes from the endpoint", () => { + const db = new Db(internalOps(), "https://id-region.apps.astra.datastax.com/", DbOptsHandler.empty); + assert.strictEqual(db["_httpClient"].baseUrl, `https://id-region.apps.astra.datastax.com/${DEFAULT_DATA_API_PATHS["astra"]}`)); + }); }); describe('new Db tests', () => {