Skip to content

Commit 46555cb

Browse files
feat(client): add support for endpoint-specific base URLs
1 parent d540a0b commit 46555cb

5 files changed

Lines changed: 43 additions & 8 deletions

File tree

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
@@ -211,6 +211,13 @@ export class Grid {
211211
});
212212
}
213213

214+
/**
215+
* Check whether the base URL is set to its default.
216+
*/
217+
#baseURLOverridden(): boolean {
218+
return this.baseURL !== 'https://api.grid.is';
219+
}
220+
214221
protected defaultQuery(): Record<string, string | undefined> | undefined {
215222
return this._options.defaultQuery;
216223
}
@@ -260,11 +267,16 @@ export class Grid {
260267
return Errors.APIError.generate(status, error, message, headers);
261268
}
262269

263-
buildURL(path: string, query: Record<string, unknown> | null | undefined): string {
270+
buildURL(
271+
path: string,
272+
query: Record<string, unknown> | null | undefined,
273+
defaultBaseURL?: string | undefined,
274+
): string {
275+
const baseURL = (!this.#baseURLOverridden() && defaultBaseURL) || this.baseURL;
264276
const url =
265277
isAbsoluteURL(path) ?
266278
new URL(path)
267-
: new URL(this.baseURL + (this.baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));
279+
: new URL(baseURL + (baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));
268280

269281
const defaultQuery = this.defaultQuery();
270282
if (!isEmptyObj(defaultQuery)) {
@@ -624,9 +636,9 @@ export class Grid {
624636
{ retryCount = 0 }: { retryCount?: number } = {},
625637
): { req: FinalizedRequestInit; url: string; timeout: number } {
626638
const options = { ...inputOptions };
627-
const { method, path, query } = options;
639+
const { method, path, query, defaultBaseURL } = options;
628640

629-
const url = this.buildURL(path!, query as Record<string, unknown>);
641+
const url = this.buildURL(path!, query as Record<string, unknown>, defaultBaseURL);
630642
if ('timeout' in options) validatePositiveInteger('timeout', options.timeout);
631643
options.timeout = options.timeout ?? this.timeout;
632644
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
@@ -306,6 +306,28 @@ describe('instantiate client', () => {
306306
const client = new Grid({ apiKey: 'My API Key' });
307307
expect(client.baseURL).toEqual('https://api.grid.is');
308308
});
309+
310+
test('in request options', () => {
311+
const client = new Grid({ apiKey: 'My API Key' });
312+
expect(client.buildURL('/foo', null, 'http://localhost:5000/option')).toEqual(
313+
'http://localhost:5000/option/foo',
314+
);
315+
});
316+
317+
test('in request options overridden by client options', () => {
318+
const client = new Grid({ apiKey: 'My API Key', baseURL: 'http://localhost:5000/client' });
319+
expect(client.buildURL('/foo', null, 'http://localhost:5000/option')).toEqual(
320+
'http://localhost:5000/client/foo',
321+
);
322+
});
323+
324+
test('in request options overridden by env variable', () => {
325+
process.env['GRID_BASE_URL'] = 'http://localhost:5000/env';
326+
const client = new Grid({ apiKey: 'My API Key' });
327+
expect(client.buildURL('/foo', null, 'http://localhost:5000/option')).toEqual(
328+
'http://localhost:5000/env/foo',
329+
);
330+
});
309331
});
310332

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