Skip to content

Commit 89f4f61

Browse files
committed
fix: Removed unnecesary code
1 parent ccf224e commit 89f4f61

File tree

8 files changed

+125
-95
lines changed

8 files changed

+125
-95
lines changed

.eslintrc.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

eslint.config.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import globals from "globals";
2+
import pluginJs from "@eslint/js";
3+
import tseslint from "typescript-eslint";
4+
5+
6+
/** @type {import('eslint').Linter.Config[]} */
7+
export default [
8+
{files: ["**/*.{js,mjs,cjs,ts}"]},
9+
{languageOptions: { globals: globals.browser }},
10+
{rules: {
11+
"semi": "off",
12+
"no-unreachable": "error", // Already in ts, but perhaps it catches more cases
13+
"@typescript-eslint/explicit-function-return-type": "off",
14+
"@typescript-eslint/strict-boolean-expressions": "off", // Often works incorrectly
15+
"@typescript-eslint/no-floating-promises": "off",
16+
},
17+
},
18+
pluginJs.configs.recommended,
19+
...tseslint.configs.recommended,
20+
];

package-lock.json

Lines changed: 64 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"scripts": {
1616
"build": "tsc",
1717
"test": "jest",
18-
"lint": "eslint src -c .eslintrc.js",
18+
"lint": "eslint . .ts,.tsx",
1919
"format": "prettier --write \"src/**/*.ts\"",
2020
"prepare": "npm run build",
2121
"example": "npx tsx src/script.ts"
@@ -39,17 +39,20 @@
3939
"node-fetch": "^2.7.0"
4040
},
4141
"devDependencies": {
42+
"@eslint/js": "^9.14.0",
4243
"@types/node-fetch": "^2.6.11",
4344
"@typescript-eslint/eslint-plugin": "^8.13.0",
4445
"@typescript-eslint/parser": "^8.13.0",
4546
"eslint": "^9.14.0",
4647
"eslint-config-prettier": "^9.1.0",
4748
"eslint-plugin-prettier": "^5.2.1",
49+
"globals": "^15.12.0",
4850
"prettier": "^3.3.3",
4951
"ts-node": "^10.5.0",
5052
"tsc-multi": "^1.1.0",
5153
"tsconfig-paths": "^4.0.0",
5254
"tsx": "^4.19.2",
53-
"typescript": "^4.8.2"
55+
"typescript": "^4.8.2",
56+
"typescript-eslint": "^8.13.0"
5457
}
5558
}

src/APIPromise.ts

Lines changed: 0 additions & 64 deletions
This file was deleted.

src/Core.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import {
1111
PromiseOrValue,
1212
DefaultQuery,
1313
} from './models';
14-
import { APIPromise } from './APIPromise';
1514
import { AI21EnvConfig } from './EnvConfig.js';
15+
import { handleAPIResponse } from './ResponseHandler.js';
1616

1717
export type Headers = Record<string, string | null | undefined>;
1818

@@ -61,19 +61,19 @@ export abstract class APIClient {
6161

6262
this.options = options;
6363
}
64-
get<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
64+
get<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): Promise<Rsp> {
6565
return this.makeRequest('get', path, opts);
6666
}
6767

68-
post<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
68+
post<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): Promise<Rsp> {
6969
return this.makeRequest('post', path, opts);
7070
}
7171

72-
put<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
72+
put<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): Promise<Rsp> {
7373
return this.makeRequest('put', path, opts);
7474
}
7575

76-
delete<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
76+
delete<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): Promise<Rsp> {
7777
return this.makeRequest('delete', path, opts);
7878
}
7979

@@ -116,14 +116,14 @@ export abstract class APIClient {
116116
method: HTTPMethod,
117117
path: string,
118118
opts?: PromiseOrValue<RequestOptions<Req>>,
119-
): APIPromise<Rsp> {
119+
): Promise<Rsp> {
120120
const options = {
121121
method,
122122
path,
123123
...opts,
124124
};
125125

126-
return new APIPromise(this.performRequest(options as FinalRequestOptions));
126+
return this.performRequest(options as FinalRequestOptions).then(handleAPIResponse);
127127
}
128128

129129
private async performRequest(options: FinalRequestOptions): Promise<APIResponseProps> {
@@ -155,3 +155,4 @@ export abstract class APIClient {
155155
);
156156
}
157157
}
158+

src/ResponseHandler.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { APIResponseProps } from './models';
2+
import { AI21Error } from './errors';
3+
import { Stream } from './Streaming';
4+
5+
6+
export async function handleAPIResponse({response, options }: APIResponseProps): Promise<any> {
7+
if (options.stream) {
8+
if (!response.body) {
9+
throw new AI21Error('Response body is null');
10+
}
11+
return new Stream(response);
12+
}
13+
14+
const contentType = response.headers.get('content-type');
15+
const data = contentType?.includes('application/json')
16+
? await response.json()
17+
: await response.text();
18+
19+
return {
20+
data,
21+
response,
22+
};
23+
}
24+

src/resources/chat/completions.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { APIPromise } from '../../APIPromise';
21
import * as Models from '../../models';
32
import { APIResource } from '../../APIResource';
43
import { Stream } from '../../Streaming';
@@ -7,17 +6,17 @@ export class Completions extends APIResource {
76
create(
87
body: Models.ChatCompletionCreateParamsNonStreaming,
98
options?: Models.RequestOptions,
10-
): APIPromise<Models.ChatCompletionResponse>;
9+
): Promise<Models.ChatCompletionResponse>;
1110

1211
create(
1312
body: Models.ChatCompletionCreateParamsStreaming,
1413
options?: Models.RequestOptions,
15-
): APIPromise<Stream<Models.ChatCompletionChunk>>;
14+
): Promise<Stream<Models.ChatCompletionChunk>>;
1615

1716
create(
1817
body: Models.ChatCompletionCreateParams,
1918
options?: Models.RequestOptions,
20-
): APIPromise<Stream<Models.ChatCompletionChunk> | Models.ChatCompletionResponse>;
19+
): Promise<Stream<Models.ChatCompletionChunk> | Models.ChatCompletionResponse>;
2120

2221
create(body: Models.ChatCompletionCreateParams, options?: Models.RequestOptions) {
2322
return this._client.post<Models.ChatCompletionCreateParams, Models.ChatCompletionResponse>(
@@ -27,6 +26,6 @@ export class Completions extends APIResource {
2726
...options,
2827
stream: body.stream ?? false,
2928
} as Models.RequestOptions<Models.ChatCompletionCreateParams>,
30-
) as APIPromise<Models.ChatCompletionResponse> | APIPromise<Stream<Models.ChatCompletionChunk>>;
29+
) as Promise<Models.ChatCompletionResponse> | Promise<Stream<Models.ChatCompletionChunk>>;
3130
}
3231
}

0 commit comments

Comments
 (0)