Skip to content

Commit bdfd113

Browse files
feat(client): add support for endpoint-specific base URLs
1 parent 353a5a6 commit bdfd113

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
@@ -41,7 +41,7 @@
4141
"publint": "^0.2.12",
4242
"ts-jest": "^29.1.0",
4343
"ts-node": "^10.5.0",
44-
"tsc-multi": "https://github.com/stainless-api/tsc-multi/releases/download/v1.1.7/tsc-multi.tgz",
44+
"tsc-multi": "https://github.com/stainless-api/tsc-multi/releases/download/v1.1.8/tsc-multi.tgz",
4545
"tsconfig-paths": "^4.0.0",
4646
"typescript": "5.8.3",
4747
"typescript-eslint": "8.31.1",

src/client.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,13 @@ export class Writer {
273273
});
274274
}
275275

276+
/**
277+
* Check whether the base URL is set to its default.
278+
*/
279+
#baseURLOverridden(): boolean {
280+
return this.baseURL !== 'https://api.writer.com';
281+
}
282+
276283
protected defaultQuery(): Record<string, string | undefined> | undefined {
277284
return this._options.defaultQuery;
278285
}
@@ -322,11 +329,16 @@ export class Writer {
322329
return Errors.APIError.generate(status, error, message, headers);
323330
}
324331

325-
buildURL(path: string, query: Record<string, unknown> | null | undefined): string {
332+
buildURL(
333+
path: string,
334+
query: Record<string, unknown> | null | undefined,
335+
defaultBaseURL?: string | undefined,
336+
): string {
337+
const baseURL = (!this.#baseURLOverridden() && defaultBaseURL) || this.baseURL;
326338
const url =
327339
isAbsoluteURL(path) ?
328340
new URL(path)
329-
: new URL(this.baseURL + (this.baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));
341+
: new URL(baseURL + (baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));
330342

331343
const defaultQuery = this.defaultQuery();
332344
if (!isEmptyObj(defaultQuery)) {
@@ -686,9 +698,9 @@ export class Writer {
686698
{ retryCount = 0 }: { retryCount?: number } = {},
687699
): { req: FinalizedRequestInit; url: string; timeout: number } {
688700
const options = { ...inputOptions };
689-
const { method, path, query } = options;
701+
const { method, path, query, defaultBaseURL } = options;
690702

691-
const url = this.buildURL(path!, query as Record<string, unknown>);
703+
const url = this.buildURL(path!, query as Record<string, unknown>, defaultBaseURL);
692704
if ('timeout' in options) validatePositiveInteger('timeout', options.timeout);
693705
options.timeout = options.timeout ?? this.timeout;
694706
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
@@ -310,6 +310,28 @@ describe('instantiate client', () => {
310310
const client = new Writer({ apiKey: 'My API Key' });
311311
expect(client.baseURL).toEqual('https://api.writer.com');
312312
});
313+
314+
test('in request options', () => {
315+
const client = new Writer({ apiKey: 'My API Key' });
316+
expect(client.buildURL('/foo', null, 'http://localhost:5000/option')).toEqual(
317+
'http://localhost:5000/option/foo',
318+
);
319+
});
320+
321+
test('in request options overridden by client options', () => {
322+
const client = new Writer({ apiKey: 'My API Key', baseURL: 'http://localhost:5000/client' });
323+
expect(client.buildURL('/foo', null, 'http://localhost:5000/option')).toEqual(
324+
'http://localhost:5000/client/foo',
325+
);
326+
});
327+
328+
test('in request options overridden by env variable', () => {
329+
process.env['WRITER_BASE_URL'] = 'http://localhost:5000/env';
330+
const client = new Writer({ apiKey: 'My API Key' });
331+
expect(client.buildURL('/foo', null, 'http://localhost:5000/option')).toEqual(
332+
'http://localhost:5000/env/foo',
333+
);
334+
});
313335
});
314336

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

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3300,9 +3300,9 @@ ts-node@^10.5.0:
33003300
v8-compile-cache-lib "^3.0.0"
33013301
yn "3.1.1"
33023302

3303-
"tsc-multi@https://github.com/stainless-api/tsc-multi/releases/download/v1.1.7/tsc-multi.tgz":
3304-
version "1.1.7"
3305-
resolved "https://github.com/stainless-api/tsc-multi/releases/download/v1.1.7/tsc-multi.tgz#52f40adf8b808bd0b633346d11cc4a8aeea465cd"
3303+
"tsc-multi@https://github.com/stainless-api/tsc-multi/releases/download/v1.1.8/tsc-multi.tgz":
3304+
version "1.1.8"
3305+
resolved "https://github.com/stainless-api/tsc-multi/releases/download/v1.1.8/tsc-multi.tgz#f544b359b8f05e607771ffacc280e58201476b04"
33063306
dependencies:
33073307
debug "^4.3.7"
33083308
fast-glob "^3.3.2"

0 commit comments

Comments
 (0)