Skip to content

Commit 6823eb4

Browse files
committed
fix: CR
1 parent 3d6878f commit 6823eb4

File tree

3 files changed

+35
-42
lines changed

3 files changed

+35
-42
lines changed

src/AI21.ts

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,54 @@ import { AI21EnvConfig } from './EnvConfig';
33
import { MissingAPIKeyError } from './errors';
44
import { Chat } from './resources/chat';
55
import { APIClient } from './APIClient';
6+
import { DefaultQuery, Headers } from './types';
7+
8+
export type ClientOptions = {
9+
baseURL?: string;
10+
apiKey?: string;
11+
maxRetries?: number;
12+
timeout?: number;
13+
via?: string | null;
14+
defaultQuery?: DefaultQuery;
15+
defaultHeaders?: Headers;
16+
dangerouslyAllowBrowser?: boolean;
17+
};
618

7-
export interface AI21Options {
8-
apiKey: string;
9-
baseURL: string;
10-
via: string | null;
11-
timeout: number;
12-
maxRetries: number;
13-
}
1419
export class AI21 extends APIClient {
20+
protected options: ClientOptions;
1521
private apiKey: string;
16-
private _options: AI21Options;
22+
private via: string | null;
1723

18-
constructor(
19-
apiKey: string = AI21EnvConfig.API_KEY,
20-
baseURL: string = AI21EnvConfig.BASE_URL,
21-
timeout: number = AI21EnvConfig.TIMEOUT_SECONDS,
22-
maxRetries: number = AI21EnvConfig.MAX_RETRIES,
23-
via: string | null = null,
24-
) {
25-
const options: AI21Options = {
24+
constructor({
25+
apiKey = AI21EnvConfig.API_KEY,
26+
baseURL = AI21EnvConfig.BASE_URL,
27+
timeout = AI21EnvConfig.TIMEOUT_SECONDS,
28+
maxRetries,
29+
via,
30+
...opts
31+
}: ClientOptions) {
32+
const options: ClientOptions = {
2633
apiKey,
2734
baseURL,
28-
via,
2935
timeout,
3036
maxRetries,
37+
via,
38+
...opts,
3139
};
3240

3341
super({
3442
baseURL,
3543
timeout,
3644
maxRetries,
37-
options: {},
3845
});
3946

4047
if (!apiKey) {
4148
throw new MissingAPIKeyError();
4249
}
4350

4451
this.apiKey = apiKey;
45-
this._options = options;
52+
this.via = via ?? null;
53+
this.options = options;
4654
}
4755

4856
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -59,11 +67,15 @@ export class AI21 extends APIClient {
5967
};
6068
}
6169

70+
protected defaultQuery(): DefaultQuery | undefined {
71+
return this.options.defaultQuery;
72+
}
73+
6274
protected override getUserAgent(): string {
6375
let userAgent = super.getUserAgent();
6476

65-
if (this._options.via) {
66-
userAgent += ` via ${this._options.via}`;
77+
if (this.via) {
78+
userAgent += ` via ${this.via}`;
6779
}
6880
return userAgent;
6981
}

src/APIClient.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,7 @@ const validatePositiveInteger = (name: string, n: unknown): number => {
2525
return n;
2626
};
2727

28-
type ClientOptions = {
29-
apiKey?: string;
30-
organization?: string | null;
31-
project?: string | null;
32-
baseURL?: string;
33-
maxRetries?: number;
34-
timeout?: number;
35-
defaultQuery?: DefaultQuery;
36-
defaultHeaders?: Headers;
37-
dangerouslyAllowBrowser?: boolean;
38-
};
39-
4028
export abstract class APIClient {
41-
protected options: ClientOptions;
4229
protected baseURL: string;
4330
protected maxRetries: number;
4431
protected timeout: number;
@@ -47,18 +34,14 @@ export abstract class APIClient {
4734
baseURL,
4835
maxRetries = AI21EnvConfig.MAX_RETRIES,
4936
timeout = AI21EnvConfig.TIMEOUT_SECONDS,
50-
options,
5137
}: {
5238
baseURL: string;
5339
maxRetries?: number | undefined;
5440
timeout: number | undefined;
55-
options: ClientOptions;
5641
}) {
5742
this.baseURL = baseURL;
5843
this.maxRetries = validatePositiveInteger('maxRetries', maxRetries);
5944
this.timeout = validatePositiveInteger('timeout', timeout);
60-
61-
this.options = options;
6245
}
6346
get<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): Promise<Rsp> {
6447
return this.makeRequest('get', path, opts);
@@ -98,9 +81,7 @@ export abstract class APIClient {
9881
return {};
9982
}
10083

101-
protected defaultQuery(): DefaultQuery | undefined {
102-
return this.options.defaultQuery;
103-
}
84+
protected abstract defaultQuery(): DefaultQuery | undefined;
10485

10586
private makeRequest<Req, Rsp>(
10687
method: HTTPMethod,

src/example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { AI21 } from './ai21';
44
This is a temporary example to test the API streaming/non-streaming functionality.
55
*/
66
async function main() {
7-
const client = new AI21('L40MQGXxfbtQVnCRqNTTKaojD8Snt7nQ');
7+
const client = new AI21({ apiKey: process.env.AI21_API_KEY });
88

99
try {
1010
const response = await client.chat.completions.create({

0 commit comments

Comments
 (0)