Skip to content
This repository was archived by the owner on Aug 14, 2025. It is now read-only.

Commit 4c942da

Browse files
feat(client): add support for endpoint-specific base URLs
1 parent b2ab744 commit 4c942da

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
@@ -307,6 +307,13 @@ export class LlamaStackClient {
307307
});
308308
}
309309

310+
/**
311+
* Check whether the base URL is set to its default.
312+
*/
313+
#baseURLOverridden(): boolean {
314+
return this.baseURL !== 'http://any-hosted-llama-stack.com';
315+
}
316+
310317
protected defaultQuery(): Record<string, string | undefined> | undefined {
311318
return this._options.defaultQuery;
312319
}
@@ -352,11 +359,16 @@ export class LlamaStackClient {
352359
return Errors.APIError.generate(status, error, message, headers);
353360
}
354361

355-
buildURL(path: string, query: Record<string, unknown> | null | undefined): string {
362+
buildURL(
363+
path: string,
364+
query: Record<string, unknown> | null | undefined,
365+
defaultBaseURL?: string | undefined,
366+
): string {
367+
const baseURL = (!this.#baseURLOverridden() && defaultBaseURL) || this.baseURL;
356368
const url =
357369
isAbsoluteURL(path) ?
358370
new URL(path)
359-
: new URL(this.baseURL + (this.baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));
371+
: new URL(baseURL + (baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));
360372

361373
const defaultQuery = this.defaultQuery();
362374
if (!isEmptyObj(defaultQuery)) {
@@ -697,9 +709,9 @@ export class LlamaStackClient {
697709
{ retryCount = 0 }: { retryCount?: number } = {},
698710
): { req: FinalizedRequestInit; url: string; timeout: number } {
699711
const options = { ...inputOptions };
700-
const { method, path, query } = options;
712+
const { method, path, query, defaultBaseURL } = options;
701713

702-
const url = this.buildURL(path!, query as Record<string, unknown>);
714+
const url = this.buildURL(path!, query as Record<string, unknown>, defaultBaseURL);
703715
if ('timeout' in options) validatePositiveInteger('timeout', options.timeout);
704716
options.timeout = options.timeout ?? this.timeout;
705717
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
@@ -320,6 +320,28 @@ describe('instantiate client', () => {
320320
const client = new LlamaStackClient({ apiKey: 'My API Key' });
321321
expect(client.baseURL).toEqual('http://any-hosted-llama-stack.com');
322322
});
323+
324+
test('in request options', () => {
325+
const client = new LlamaStackClient({ apiKey: 'My API Key' });
326+
expect(client.buildURL('/foo', null, 'http://localhost:5000/option')).toEqual(
327+
'http://localhost:5000/option/foo',
328+
);
329+
});
330+
331+
test('in request options overridden by client options', () => {
332+
const client = new LlamaStackClient({ apiKey: 'My API Key', baseURL: 'http://localhost:5000/client' });
333+
expect(client.buildURL('/foo', null, 'http://localhost:5000/option')).toEqual(
334+
'http://localhost:5000/client/foo',
335+
);
336+
});
337+
338+
test('in request options overridden by env variable', () => {
339+
process.env['LLAMA_STACK_CLIENT_BASE_URL'] = 'http://localhost:5000/env';
340+
const client = new LlamaStackClient({ apiKey: 'My API Key' });
341+
expect(client.buildURL('/foo', null, 'http://localhost:5000/option')).toEqual(
342+
'http://localhost:5000/env/foo',
343+
);
344+
});
323345
});
324346

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