Skip to content

Commit 899a786

Browse files
committed
feat: Added emv config
1 parent bef150f commit 899a786

File tree

5 files changed

+42
-8
lines changed

5 files changed

+42
-8
lines changed

src/Client.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
1-
import { STUDIO_BASE_URL } from "./Constants";
21
import * as Core from "./Core";
2+
import { AI21EnvConfig } from "./EnvConfig";
3+
import { MissingAPIKeyError } from "./errors";
34
import { Chat } from "./resources/chat";
45

56
export interface AI21Options {
67
apiKey: string;
78
baseURL: string;
89
via: string | null;
10+
timeout: number;
11+
maxRetries: number;
912
}
1013
export class AI21 extends Core.APIClient {
14+
private apiKey: string;
1115
private _options: AI21Options;
1216

1317
constructor(
14-
apiKey: string = process.env.AI21_API_KEY ?? '',
15-
baseURL: string = process.env.AI21_BASE_URL ?? STUDIO_BASE_URL,
18+
apiKey: string = AI21EnvConfig.API_KEY,
19+
baseURL: string = AI21EnvConfig.BASE_URL,
20+
timeout: number = AI21EnvConfig.TIMEOUT_SECONDS,
21+
maxRetries: number = AI21EnvConfig.MAX_RETRIES,
1622
via: string | null = null,
1723
) {
1824
const options: AI21Options = {
1925
apiKey,
2026
baseURL,
2127
via,
28+
timeout,
29+
maxRetries,
2230
};
2331

2432
super({
2533
baseURL,
26-
timeout: 60000,
34+
timeout,
2735
apiKey,
36+
maxRetries,
2837
options: {}
2938
});
30-
this.apiKey = apiKey
39+
40+
if (!apiKey) {
41+
throw new MissingAPIKeyError();
42+
}
43+
44+
this.apiKey = apiKey;
3145
this._options = options;
3246
}
3347

src/Constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export const STUDIO_BASE_URL = 'https://api.ai21.com/studio/v1';
1+
export const STUDIO_BASE_URL = 'https://api.ai21.com/studio/v1';
2+
export const DEFAULT_TIMEOUT = 60000;

src/Core.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ export class APIPromise<T> extends Promise<T> {
143143

144144
export abstract class APIClient {
145145
protected options: ClientOptions;
146-
protected apiKey: string;
147146
protected baseURL: string;
148147
protected maxRetries: number;
149148
protected timeout: number;
@@ -165,7 +164,6 @@ export abstract class APIClient {
165164
this.maxRetries = validatePositiveInteger('maxRetries', maxRetries);
166165
this.timeout = validatePositiveInteger('timeout', timeout);
167166

168-
this.apiKey = apiKey;
169167
this.options = options;
170168
}
171169
get<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {

src/EnvConfig.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { DEFAULT_TIMEOUT } from "./Constants";
2+
3+
import { STUDIO_BASE_URL } from "./Constants";
4+
5+
export class AI21EnvConfig {
6+
static readonly API_KEY = process.env.AI21_API_KEY ?? '';
7+
static readonly BASE_URL = process.env.AI21_BASE_URL ?? STUDIO_BASE_URL;
8+
static readonly TIMEOUT_SECONDS = process.env.AI21_TIMEOUT_SECONDS ?
9+
Number(process.env.AI21_TIMEOUT_SECONDS) :
10+
DEFAULT_TIMEOUT;
11+
static readonly MAX_RETRIES = process.env.AI21_MAX_RETRIES ?
12+
Number(process.env.AI21_MAX_RETRIES) :
13+
3;
14+
static readonly LOG_LEVEL = process.env.AI21_LOG_LEVEL ?? 'info';
15+
}

src/errors.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
export class AI21Error extends Error {}
2+
3+
export class MissingAPIKeyError extends AI21Error {
4+
constructor() {
5+
super("API key is required");
6+
}
7+
}

0 commit comments

Comments
 (0)