Skip to content

Commit aa975e7

Browse files
philnashtoptobes
authored andcommitted
Strips trailing slashes from API endpoint. (#107)
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.
1 parent b91d6bc commit aa975e7

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

src/db/db.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ export class Db extends HierarchicalEmitter<CommandEventMap> {
150150
: this.#defaultOpts.dbOptions.keyspace ?? undefined,
151151
};
152152

153+
endpoint = endpoint.endsWith('/') ? endpoint.replace(/\/+$/, "") : endpoint;
154+
153155
this.#httpClient = new DataAPIHttpClient({
154156
baseUrl: endpoint,
155157
tokenProvider: this.#defaultOpts.dbOptions.token,

tests/unit/db/db.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ describe('unit.db.db', () => {
4343
const client = new DataAPIClient();
4444
assert.doesNotThrow(() => client.db(TEST_APPLICATION_URI));
4545
});
46+
47+
it("should strip trailing slashes from the endpoint", () => {
48+
const db = new Db(internalOps(), "https://id-region.apps.astra.datastax.com/", DbOptsHandler.empty);
49+
assert.strictEqual(db["_httpClient"].baseUrl, `https://id-region.apps.astra.datastax.com/${DEFAULT_DATA_API_PATHS["astra"]}`));
50+
});
4651
});
4752

4853
describe('new Db tests', () => {

0 commit comments

Comments
 (0)