Skip to content

Commit 950b6e6

Browse files
feat(client): add support for endpoint-specific base URLs
1 parent ae245a4 commit 950b6e6

File tree

5 files changed

+43
-8
lines changed

5 files changed

+43
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"publint": "^0.2.12",
4343
"ts-jest": "^29.1.0",
4444
"ts-node": "^10.5.0",
45-
"tsc-multi": "https://github.com/stainless-api/tsc-multi/releases/download/v1.1.7/tsc-multi.tgz",
45+
"tsc-multi": "https://github.com/stainless-api/tsc-multi/releases/download/v1.1.8/tsc-multi.tgz",
4646
"tsconfig-paths": "^4.0.0",
4747
"typescript": "5.8.3"
4848
},

src/client.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ export class Supermemory {
218218
});
219219
}
220220

221+
/**
222+
* Check whether the base URL is set to its default.
223+
*/
224+
#baseURLOverridden(): boolean {
225+
return this.baseURL !== 'https://api.supermemory.ai';
226+
}
227+
221228
protected defaultQuery(): Record<string, string | undefined> | undefined {
222229
return this._options.defaultQuery;
223230
}
@@ -267,11 +274,16 @@ export class Supermemory {
267274
return Errors.APIError.generate(status, error, message, headers);
268275
}
269276

270-
buildURL(path: string, query: Record<string, unknown> | null | undefined): string {
277+
buildURL(
278+
path: string,
279+
query: Record<string, unknown> | null | undefined,
280+
defaultBaseURL?: string | undefined,
281+
): string {
282+
const baseURL = (!this.#baseURLOverridden() && defaultBaseURL) || this.baseURL;
271283
const url =
272284
isAbsoluteURL(path) ?
273285
new URL(path)
274-
: new URL(this.baseURL + (this.baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));
286+
: new URL(baseURL + (baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));
275287

276288
const defaultQuery = this.defaultQuery();
277289
if (!isEmptyObj(defaultQuery)) {
@@ -612,9 +624,9 @@ export class Supermemory {
612624
{ retryCount = 0 }: { retryCount?: number } = {},
613625
): { req: FinalizedRequestInit; url: string; timeout: number } {
614626
const options = { ...inputOptions };
615-
const { method, path, query } = options;
627+
const { method, path, query, defaultBaseURL } = options;
616628

617-
const url = this.buildURL(path!, query as Record<string, unknown>);
629+
const url = this.buildURL(path!, query as Record<string, unknown>, defaultBaseURL);
618630
if ('timeout' in options) validatePositiveInteger('timeout', options.timeout);
619631
options.timeout = options.timeout ?? this.timeout;
620632
const { bodyHeaders, body } = this.buildBody({ options });

src/internal/request-options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export type RequestOptions = {
2020
fetchOptions?: MergedRequestInit;
2121
signal?: AbortSignal | undefined | null;
2222
idempotencyKey?: string;
23+
defaultBaseURL?: string | undefined;
2324

2425
__binaryResponse?: boolean | undefined;
2526
};

tests/index.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,28 @@ describe('instantiate client', () => {
314314
const client = new Supermemory({ apiKey: 'My API Key' });
315315
expect(client.baseURL).toEqual('https://api.supermemory.ai');
316316
});
317+
318+
test('in request options', () => {
319+
const client = new Supermemory({ apiKey: 'My API Key' });
320+
expect(client.buildURL('/foo', null, 'http://localhost:5000/option')).toEqual(
321+
'http://localhost:5000/option/foo',
322+
);
323+
});
324+
325+
test('in request options overridden by client options', () => {
326+
const client = new Supermemory({ apiKey: 'My API Key', baseURL: 'http://localhost:5000/client' });
327+
expect(client.buildURL('/foo', null, 'http://localhost:5000/option')).toEqual(
328+
'http://localhost:5000/client/foo',
329+
);
330+
});
331+
332+
test('in request options overridden by env variable', () => {
333+
process.env['SUPERMEMORY_BASE_URL'] = 'http://localhost:5000/env';
334+
const client = new Supermemory({ apiKey: 'My API Key' });
335+
expect(client.buildURL('/foo', null, 'http://localhost:5000/option')).toEqual(
336+
'http://localhost:5000/env/foo',
337+
);
338+
});
317339
});
318340

319341
test('maxRetries option is correctly set', () => {

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3283,9 +3283,9 @@ ts-node@^10.5.0:
32833283
v8-compile-cache-lib "^3.0.0"
32843284
yn "3.1.1"
32853285

3286-
"tsc-multi@https://github.com/stainless-api/tsc-multi/releases/download/v1.1.7/tsc-multi.tgz":
3287-
version "1.1.7"
3288-
resolved "https://github.com/stainless-api/tsc-multi/releases/download/v1.1.7/tsc-multi.tgz#52f40adf8b808bd0b633346d11cc4a8aeea465cd"
3286+
"tsc-multi@https://github.com/stainless-api/tsc-multi/releases/download/v1.1.8/tsc-multi.tgz":
3287+
version "1.1.8"
3288+
resolved "https://github.com/stainless-api/tsc-multi/releases/download/v1.1.8/tsc-multi.tgz#f544b359b8f05e607771ffacc280e58201476b04"
32893289
dependencies:
32903290
debug "^4.3.7"
32913291
fast-glob "^3.3.2"

0 commit comments

Comments
 (0)