Skip to content

Commit 5818db2

Browse files
committed
fix: CR
1 parent 60b602d commit 5818db2

File tree

7 files changed

+13
-20
lines changed

7 files changed

+13
-20
lines changed

src/APIClient.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
FinalRequestOptions,
99
APIResponseProps,
1010
HTTPMethod,
11-
PromiseOrValue,
1211
DefaultQuery,
1312
Headers,
1413
} from './types/index.js';
@@ -43,19 +42,19 @@ export abstract class APIClient {
4342
this.maxRetries = validatePositiveInteger('maxRetries', maxRetries);
4443
this.timeout = validatePositiveInteger('timeout', timeout);
4544
}
46-
get<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): Promise<Rsp> {
45+
get<Req, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
4746
return this.makeRequest('get', path, opts);
4847
}
4948

50-
post<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): Promise<Rsp> {
49+
post<Req, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
5150
return this.makeRequest('post', path, opts);
5251
}
5352

54-
put<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): Promise<Rsp> {
53+
put<Req, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
5554
return this.makeRequest('put', path, opts);
5655
}
5756

58-
delete<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): Promise<Rsp> {
57+
delete<Req, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
5958
return this.makeRequest('delete', path, opts);
6059
}
6160

@@ -83,11 +82,7 @@ export abstract class APIClient {
8382

8483
protected abstract defaultQuery(): DefaultQuery | undefined;
8584

86-
private makeRequest<Req, Rsp>(
87-
method: HTTPMethod,
88-
path: string,
89-
opts?: PromiseOrValue<RequestOptions<Req>>,
90-
): Promise<Rsp> {
85+
private makeRequest<Req, Rsp>(method: HTTPMethod, path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
9186
const options = {
9287
method,
9388
path,
@@ -116,7 +111,7 @@ export abstract class APIClient {
116111
});
117112

118113
if (!response.ok) {
119-
throw new AI21Error(`Request failed with status ${response.status}`);
114+
throw new AI21Error(`Request failed with status ${response.status}. ${await response.text()}`);
120115
}
121116

122117
return { response, options, controller };

src/APIResource.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { APIClient } from './APIClient';
22

33
export class APIResource {
4-
protected _client: APIClient;
4+
protected client: APIClient;
55

66
constructor(client: APIClient) {
7-
this._client = client;
7+
this.client = client;
88
}
99
}

src/ResponseHandler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { AI21Error } from './errors';
33
import { Stream } from './Streaming';
44
import { Response } from 'node-fetch';
55

6-
type APIResponse<T = unknown> = {
7-
data: T;
6+
type APIResponse<T> = {
7+
data?: T;
88
response: Response;
99
};
1010

@@ -20,7 +20,7 @@ export async function handleAPIResponse<T>({
2020
}
2121

2222
const contentType = response.headers.get('content-type');
23-
const data = contentType?.includes('application/json') ? await response.json() : await response.text();
23+
const data = contentType?.includes('application/json') ? await response.json() : null;
2424

2525
return {
2626
data,

src/resources/chat/chat.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import { APIResource } from '../../APIResource';
22
import { Completions } from './completions';
33

44
export class Chat extends APIResource {
5-
completions: Completions = new Completions(this._client);
5+
completions: Completions = new Completions(this.client);
66
}

src/resources/chat/completions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class Completions extends APIResource {
1919
): Promise<Stream<Models.ChatCompletionChunk> | Models.ChatCompletionResponse>;
2020

2121
create(body: Models.ChatCompletionCreateParams, options?: Models.RequestOptions) {
22-
return this._client.post<Models.ChatCompletionCreateParams, Models.ChatCompletionResponse>(
22+
return this.client.post<Models.ChatCompletionCreateParams, Models.ChatCompletionResponse>(
2323
'/chat/completions',
2424
{
2525
body,

src/types/API.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,4 @@ export type FinalRequestOptions = RequestOptions & {
3030
};
3131

3232
export type DefaultQuery = Record<string, unknown>;
33-
export type PromiseOrValue<T> = T | Promise<T>;
3433
export type Headers = Record<string, string | null | undefined>;

src/types/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,5 @@ export {
3333
type FinalRequestOptions,
3434
type HTTPMethod,
3535
type DefaultQuery,
36-
type PromiseOrValue,
3736
type Headers,
3837
} from './API';

0 commit comments

Comments
 (0)