Skip to content

Commit 7b4d7a2

Browse files
feat(client): add support for endpoint-specific base URLs
1 parent 801fb38 commit 7b4d7a2

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
@@ -228,6 +228,13 @@ export class Kernel {
228228
});
229229
}
230230

231+
/**
232+
* Check whether the base URL is set to its default.
233+
*/
234+
#baseURLOverridden(): boolean {
235+
return this.baseURL !== environments[this._options.environment || 'production'];
236+
}
237+
231238
protected defaultQuery(): Record<string, string | undefined> | undefined {
232239
return this._options.defaultQuery;
233240
}
@@ -277,11 +284,16 @@ export class Kernel {
277284
return Errors.APIError.generate(status, error, message, headers);
278285
}
279286

280-
buildURL(path: string, query: Record<string, unknown> | null | undefined): string {
287+
buildURL(
288+
path: string,
289+
query: Record<string, unknown> | null | undefined,
290+
defaultBaseURL?: string | undefined,
291+
): string {
292+
const baseURL = (!this.#baseURLOverridden() && defaultBaseURL) || this.baseURL;
281293
const url =
282294
isAbsoluteURL(path) ?
283295
new URL(path)
284-
: new URL(this.baseURL + (this.baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));
296+
: new URL(baseURL + (baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));
285297

286298
const defaultQuery = this.defaultQuery();
287299
if (!isEmptyObj(defaultQuery)) {
@@ -622,9 +634,9 @@ export class Kernel {
622634
{ retryCount = 0 }: { retryCount?: number } = {},
623635
): { req: FinalizedRequestInit; url: string; timeout: number } {
624636
const options = { ...inputOptions };
625-
const { method, path, query } = options;
637+
const { method, path, query, defaultBaseURL } = options;
626638

627-
const url = this.buildURL(path!, query as Record<string, unknown>);
639+
const url = this.buildURL(path!, query as Record<string, unknown>, defaultBaseURL);
628640
if ('timeout' in options) validatePositiveInteger('timeout', options.timeout);
629641
options.timeout = options.timeout ?? this.timeout;
630642
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
@@ -21,6 +21,7 @@ export type RequestOptions = {
2121
fetchOptions?: MergedRequestInit;
2222
signal?: AbortSignal | undefined | null;
2323
idempotencyKey?: string;
24+
defaultBaseURL?: string | undefined;
2425

2526
__binaryResponse?: boolean | undefined;
2627
__streamClass?: typeof Stream;

tests/index.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,28 @@ describe('instantiate client', () => {
323323
const client = new Kernel({ apiKey: 'My API Key', baseURL: null, environment: 'production' });
324324
expect(client.baseURL).toEqual('https://api.onkernel.com/');
325325
});
326+
327+
test('in request options', () => {
328+
const client = new Kernel({ apiKey: 'My API Key' });
329+
expect(client.buildURL('/foo', null, 'http://localhost:5000/option')).toEqual(
330+
'http://localhost:5000/option/foo',
331+
);
332+
});
333+
334+
test('in request options overridden by client options', () => {
335+
const client = new Kernel({ apiKey: 'My API Key', baseURL: 'http://localhost:5000/client' });
336+
expect(client.buildURL('/foo', null, 'http://localhost:5000/option')).toEqual(
337+
'http://localhost:5000/client/foo',
338+
);
339+
});
340+
341+
test('in request options overridden by env variable', () => {
342+
process.env['KERNEL_BASE_URL'] = 'http://localhost:5000/env';
343+
const client = new Kernel({ apiKey: 'My API Key' });
344+
expect(client.buildURL('/foo', null, 'http://localhost:5000/option')).toEqual(
345+
'http://localhost:5000/env/foo',
346+
);
347+
});
326348
});
327349

328350
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)