1
1
import * as Types from './types' ;
2
2
import { AI21EnvConfig } from './EnvConfig' ;
3
- import { MissingAPIKeyError } from './errors' ;
3
+ import { AI21Error , MissingAPIKeyError } from './errors' ;
4
4
import { Chat } from './resources/chat' ;
5
5
import { APIClient } from './APIClient' ;
6
6
import { Headers } from './types' ;
7
+ import * as Runtime from './runtime' ;
7
8
import { ConversationalRag } from './resources/rag/conversationalRag' ;
8
9
9
- export type ClientOptions = {
10
+ export interface ClientOptions {
10
11
baseURL ?: string ;
11
12
apiKey ?: string ;
12
13
maxRetries ?: number ;
13
14
timeout ?: number ;
14
15
via ?: string | null ;
15
16
defaultHeaders ?: Headers ;
17
+ /**
18
+ * By default, using this library on the client side is prohibited to prevent exposing your secret API credentials to potential attackers.
19
+ * Only enable this option by setting it to `true` if you fully understand the risks and have implemented appropriate security measures.
20
+ */
16
21
dangerouslyAllowBrowser ?: boolean ;
17
- } ;
22
+ }
18
23
19
24
export class AI21 extends APIClient {
20
25
protected options : ClientOptions ;
@@ -28,7 +33,7 @@ export class AI21 extends APIClient {
28
33
maxRetries,
29
34
via,
30
35
...opts
31
- } : ClientOptions ) {
36
+ } : ClientOptions = { } ) {
32
37
const options : ClientOptions = {
33
38
apiKey,
34
39
baseURL,
@@ -38,6 +43,12 @@ export class AI21 extends APIClient {
38
43
...opts ,
39
44
} ;
40
45
46
+ if ( ! options . dangerouslyAllowBrowser && Runtime . isBrowser ) {
47
+ throw new AI21Error (
48
+ 'AI21 client is not supported in the browser by default due to potential API key exposure. Use `dangerouslyAllowBrowser` option to `true` to override it.' ,
49
+ ) ;
50
+ }
51
+
41
52
super ( {
42
53
baseURL,
43
54
timeout,
0 commit comments