Skip to content

Commit 4f6bb37

Browse files
feat(api): update via SDK Studio
1 parent b885fff commit 4f6bb37

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

.stats.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
configured_endpoints: 4
22
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/kernel%2Fkernel-d168b58fcf39dbd0458d132091793d3e2d0930070b7dda2d5f7f1baff20dd31b.yml
33
openapi_spec_hash: b7e0fd7ee1656d7dbad57209d1584d92
4-
config_hash: c2bc5253d8afd6d67e031f73353c9b22
4+
config_hash: 2d282609080a6011e3f6222451f72237

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import Kernel from '@onkernel/sdk';
2424

2525
const client = new Kernel({
2626
apiKey: process.env['KERNEL_API_KEY'], // This is the default and can be omitted
27+
environment: 'development', // defaults to 'production'
2728
});
2829

2930
async function main() {
@@ -49,6 +50,7 @@ import Kernel from '@onkernel/sdk';
4950

5051
const client = new Kernel({
5152
apiKey: process.env['KERNEL_API_KEY'], // This is the default and can be omitted
53+
environment: 'development', // defaults to 'production'
5254
});
5355

5456
async function main() {

src/client.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,27 @@ import { formatRequestDetails, loggerFor } from './internal/utils/log';
3434
import { isEmptyObj } from './internal/utils/values';
3535
import { KernelApp, appRegistry } from './core/app-framework';
3636

37+
const environments = {
38+
production: 'https://api.onkernel.com/',
39+
development: 'https://localhost:3001/',
40+
};
41+
type Environment = keyof typeof environments;
42+
3743
export interface ClientOptions {
3844
/**
3945
* Defaults to process.env['KERNEL_API_KEY'].
4046
*/
4147
apiKey?: string | undefined;
4248

49+
/**
50+
* Specifies the environment to use for the API.
51+
*
52+
* Each environment maps to a different base URL:
53+
* - `production` corresponds to `https://api.onkernel.com/`
54+
* - `development` corresponds to `https://localhost:3001/`
55+
*/
56+
environment?: Environment | undefined;
57+
4358
/**
4459
* Override the default base URL for the API, e.g., "https://api.example.com/v2/"
4560
*
@@ -129,7 +144,8 @@ export class Kernel {
129144
* API Client for interfacing with the Kernel API.
130145
*
131146
* @param {string | undefined} [opts.apiKey=process.env['KERNEL_API_KEY'] ?? undefined]
132-
* @param {string} [opts.baseURL=process.env['KERNEL_BASE_URL'] ?? http://localhost:3001] - Override the default base URL for the API.
147+
* @param {Environment} [opts.environment=production] - Specifies the environment URL to use for the API.
148+
* @param {string} [opts.baseURL=process.env['KERNEL_BASE_URL'] ?? https://api.onkernel.com/] - Override the default base URL for the API.
133149
* @param {number} [opts.timeout=1 minute] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.
134150
* @param {MergedRequestInit} [opts.fetchOptions] - Additional `RequestInit` options to be passed to `fetch` calls.
135151
* @param {Fetch} [opts.fetch] - Specify a custom `fetch` function implementation.
@@ -151,10 +167,17 @@ export class Kernel {
151167
const options: ClientOptions = {
152168
apiKey,
153169
...opts,
154-
baseURL: baseURL || `http://localhost:3001`,
170+
baseURL,
171+
environment: opts.environment ?? 'production',
155172
};
156173

157-
this.baseURL = options.baseURL!;
174+
if (baseURL && opts.environment) {
175+
throw new Errors.KernelError(
176+
'Ambiguous URL; The `baseURL` option (or KERNEL_BASE_URL env var) and the `environment` option are given. If you want to use the environment you must pass baseURL: null',
177+
);
178+
}
179+
180+
this.baseURL = options.baseURL || environments[options.environment || 'production'];
158181
this.timeout = options.timeout ?? Kernel.DEFAULT_TIMEOUT /* 1 minute */;
159182
this.logger = options.logger ?? console;
160183
const defaultLogLevel = 'warn';
@@ -180,7 +203,8 @@ export class Kernel {
180203
withOptions(options: Partial<ClientOptions>): this {
181204
return new (this.constructor as any as new (props: ClientOptions) => typeof this)({
182205
...this._options,
183-
baseURL: this.baseURL,
206+
environment: options.environment ? options.environment : undefined,
207+
baseURL: options.environment ? undefined : this.baseURL,
184208
maxRetries: this.maxRetries,
185209
timeout: this.timeout,
186210
logger: this.logger,

tests/index.test.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,26 @@ describe('instantiate client', () => {
302302
test('empty env variable', () => {
303303
process.env['KERNEL_BASE_URL'] = ''; // empty
304304
const client = new Kernel({ apiKey: 'My API Key' });
305-
expect(client.baseURL).toEqual('http://localhost:3001');
305+
expect(client.baseURL).toEqual('https://api.onkernel.com/');
306306
});
307307

308308
test('blank env variable', () => {
309309
process.env['KERNEL_BASE_URL'] = ' '; // blank
310310
const client = new Kernel({ apiKey: 'My API Key' });
311-
expect(client.baseURL).toEqual('http://localhost:3001');
311+
expect(client.baseURL).toEqual('https://api.onkernel.com/');
312+
});
313+
314+
test('env variable with environment', () => {
315+
process.env['KERNEL_BASE_URL'] = 'https://example.com/from_env';
316+
317+
expect(
318+
() => new Kernel({ apiKey: 'My API Key', environment: 'production' }),
319+
).toThrowErrorMatchingInlineSnapshot(
320+
`"Ambiguous URL; The \`baseURL\` option (or KERNEL_BASE_URL env var) and the \`environment\` option are given. If you want to use the environment you must pass baseURL: null"`,
321+
);
322+
323+
const client = new Kernel({ apiKey: 'My API Key', baseURL: null, environment: 'production' });
324+
expect(client.baseURL).toEqual('https://api.onkernel.com/');
312325
});
313326
});
314327

0 commit comments

Comments
 (0)