Skip to content

Commit c98acfc

Browse files
committed
fix: support connect base url override
1 parent e4c50ef commit c98acfc

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

src/client.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ export interface ClientOptions {
112112
*/
113113
apiKey?: string | undefined;
114114

115+
/**
116+
* Base URL for Smithery Connect REST methods.
117+
*/
118+
connectBaseURL?: string | null | undefined;
119+
115120
/**
116121
* Override the default base URL for the API, e.g., "https://api.example.com/v2/"
117122
*
@@ -186,6 +191,7 @@ export interface ClientOptions {
186191
*/
187192
export class Smithery {
188193
apiKey: string;
194+
connectBaseURL: string | null;
189195

190196
baseURL: string;
191197
maxRetries: number;
@@ -203,6 +209,7 @@ export class Smithery {
203209
* API Client for interfacing with the Smithery API.
204210
*
205211
* @param {string | undefined} [opts.apiKey=process.env['SMITHERY_API_KEY'] ?? undefined]
212+
* @param {string | null | undefined} [opts.connectBaseURL=process.env['SMITHERY_CONNECT_BASE_URL'] ?? https://smithery.run]
206213
* @param {string} [opts.baseURL=process.env['SMITHERY_BASE_URL'] ?? https://api.smithery.ai] - Override the default base URL for the API.
207214
* @param {number} [opts.timeout=1 minute] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.
208215
* @param {MergedRequestInit} [opts.fetchOptions] - Additional `RequestInit` options to be passed to `fetch` calls.
@@ -214,6 +221,7 @@ export class Smithery {
214221
constructor({
215222
baseURL = readEnv('SMITHERY_BASE_URL'),
216223
apiKey = readEnv('SMITHERY_API_KEY'),
224+
connectBaseURL = readEnv('SMITHERY_CONNECT_BASE_URL') ?? 'https://smithery.run',
217225
...opts
218226
}: ClientOptions = {}) {
219227
if (apiKey === undefined) {
@@ -224,6 +232,7 @@ export class Smithery {
224232

225233
const options: ClientOptions = {
226234
apiKey,
235+
connectBaseURL,
227236
...opts,
228237
baseURL: baseURL || `https://api.smithery.ai`,
229238
};
@@ -258,6 +267,7 @@ export class Smithery {
258267
this._options = options;
259268

260269
this.apiKey = apiKey;
270+
this.connectBaseURL = connectBaseURL;
261271
}
262272

263273
/**
@@ -274,6 +284,7 @@ export class Smithery {
274284
fetch: this.fetch,
275285
fetchOptions: this.fetchOptions,
276286
apiKey: this.apiKey,
287+
connectBaseURL: this.connectBaseURL,
277288
...options,
278289
});
279290
return client;
@@ -324,7 +335,12 @@ export class Smithery {
324335
query: Record<string, unknown> | null | undefined,
325336
defaultBaseURL?: string | undefined,
326337
): string {
327-
const baseURL = (!this.#baseURLOverridden() && defaultBaseURL) || this.baseURL;
338+
const resolvedDefaultBaseURL =
339+
defaultBaseURL === 'https://smithery.run' ? this.connectBaseURL || defaultBaseURL : defaultBaseURL;
340+
const baseURL =
341+
(defaultBaseURL === 'https://smithery.run' && resolvedDefaultBaseURL) ||
342+
(!this.#baseURLOverridden() && resolvedDefaultBaseURL) ||
343+
this.baseURL;
328344
const url =
329345
isAbsoluteURL(path) ?
330346
new URL(path)

tests/index.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ describe('instantiate client', () => {
306306

307307
afterEach(() => {
308308
process.env['SMITHERY_BASE_URL'] = undefined;
309+
process.env['SMITHERY_CONNECT_BASE_URL'] = undefined;
309310
});
310311

311312
test('explicit option', () => {
@@ -352,6 +353,30 @@ describe('instantiate client', () => {
352353
'http://localhost:5000/env/foo',
353354
);
354355
});
356+
357+
test('connect base URL overrides Smithery Connect REST methods when API base URL is overridden', () => {
358+
const client = new Smithery({
359+
apiKey: 'My API Key',
360+
baseURL: 'http://localhost:5000/api',
361+
connectBaseURL: 'http://localhost:5000/connect',
362+
});
363+
364+
expect(client.buildURL('/namespace/connection', null, 'https://smithery.run')).toEqual(
365+
'http://localhost:5000/connect/namespace/connection',
366+
);
367+
});
368+
369+
test('connect base URL can be read from the environment', () => {
370+
process.env['SMITHERY_CONNECT_BASE_URL'] = 'http://localhost:5000/connect-env';
371+
const client = new Smithery({
372+
apiKey: 'My API Key',
373+
baseURL: 'http://localhost:5000/api',
374+
});
375+
376+
expect(client.buildURL('/namespace/connection', null, 'https://smithery.run')).toEqual(
377+
'http://localhost:5000/connect-env/namespace/connection',
378+
);
379+
});
355380
});
356381

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

0 commit comments

Comments
 (0)