Skip to content

Commit 6b92fcd

Browse files
committed
fix: Typing of both fetches
1 parent cb2c875 commit 6b92fcd

File tree

7 files changed

+34
-22
lines changed

7 files changed

+34
-22
lines changed

src/APIClient.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AI21Error } from './errors';
22
import { VERSION } from './version';
33

4-
import { RequestOptions, FinalRequestOptions, APIResponseProps, HTTPMethod, Headers } from './types/index.js';
4+
import { RequestOptions, FinalRequestOptions, APIResponseProps, HTTPMethod, Headers, UnifiedResponse } from './types';
55
import { AI21EnvConfig } from './EnvConfig';
66
import { handleAPIResponse } from './ResponseHandler';
77
import { createFetchInstance } from 'envFetch';
@@ -90,25 +90,19 @@ export abstract class APIClient {
9090
}
9191

9292
private async performRequest(options: FinalRequestOptions): Promise<APIResponseProps> {
93-
const controller = new AbortController();
9493
const url = `${this.baseURL}${options.path}`;
9594

9695
const headers = {
9796
...this.defaultHeaders(options),
9897
...options.headers,
9998
};
100-
const response = await this.fetch.call(url, {
101-
method: options.method,
102-
headers: headers as any,
103-
signal: controller.signal,
104-
body: options.body ? JSON.stringify(options.body) : undefined,
105-
});
99+
const response = await this.fetch.call(url, { ...options, headers });
106100

107101
if (!response.ok) {
108102
throw new AI21Error(`Request failed with status ${response.status}. ${await response.text()}`);
109103
}
110104

111-
return { response, options, controller };
105+
return { response: response as UnifiedResponse, options };
112106
}
113107

114108
protected isRunningInBrowser(): boolean {

src/Streaming/Stream.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { DefaultSSEDecoder } from './SSEDecoder';
22
import { SSEDecoder } from './SSEDecoder';
33
import { SSE_DONE_MSG } from './Consts';
44
import { StreamingDecodeError } from '../errors';
5+
import { UnifiedResponse } from '../types';
56

67
function getStreamMessage<T>(chunk: string): T {
78
try {
@@ -16,15 +17,15 @@ export class Stream<T> implements AsyncIterableIterator<T> {
1617
private iterator: AsyncIterableIterator<T>;
1718

1819
constructor(
19-
private response: Response,
20+
private response: UnifiedResponse,
2021
decoder?: SSEDecoder,
2122
) {
2223
this.decoder = decoder || new DefaultSSEDecoder();
2324
this.iterator = this.stream();
2425
}
2526

2627
private async *stream(): AsyncIterableIterator<T> {
27-
for await (const chunk of this.decoder.iterLines(this.response)) {
28+
for await (const chunk of this.decoder.iterLines(this.response as Response)) {
2829
if (chunk === SSE_DONE_MSG) break;
2930
yield getStreamMessage(chunk);
3031
}

src/fetch/BaseFetch.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { FinalRequestOptions, UnifiedResponse } from "types";
2+
3+
14
export abstract class Fetch {
2-
abstract call(url: string, options?: RequestInit): Promise<Response>;
5+
abstract call(url: string, options: FinalRequestOptions): Promise<UnifiedResponse>;
36
}

src/fetch/BrowserFetch.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
import { FinalRequestOptions, UnifiedResponse } from 'types';
12
import { Fetch } from './BaseFetch';
23

34
export class BrowserFetch extends Fetch {
4-
call(url: string, options?: RequestInit): Promise<Response> {
5-
return fetch(url, options);
5+
call(url: string, options: FinalRequestOptions): Promise<UnifiedResponse> {
6+
const controller = new AbortController();
7+
8+
return fetch(url, {
9+
method: options?.method,
10+
headers: options?.headers as HeadersInit,
11+
body: options.body ? JSON.stringify(options.body) : undefined,
12+
signal: controller.signal,
13+
});
614
}
715
}

src/fetch/NodeFetch.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
// import { RequestInit as NodeRequestInit } from 'node-fetch';
2-
1+
import { FinalRequestOptions, UnifiedResponse } from "types";
32
import { Fetch } from "./BaseFetch";
43

54
export class NodeFetch extends Fetch {
6-
async call(url: string, options?: RequestInit): Promise<Response> {
7-
const nodeFetch = (await import("node-fetch")).default;
8-
// @ts-ignore
9-
return nodeFetch(url, options);
5+
async call(url: string, options: FinalRequestOptions): Promise<UnifiedResponse> {
6+
const nodeFetchModule = await import("node-fetch");
7+
const nodeFetch = nodeFetchModule.default;
8+
9+
return nodeFetch(url, {
10+
method: options?.method,
11+
headers: options.headers as Record<string, string>,
12+
body: options?.body ? JSON.stringify(options.body) : undefined,
13+
});
1014
}
1115
}

src/types/API.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export type HTTPMethod = 'get' | 'post' | 'put' | 'patch' | 'delete';
22

33
export type APIResponseProps = {
4-
response: Response;
4+
response: UnifiedResponse;
55
options: FinalRequestOptions;
6-
controller: AbortController;
6+
controller?: AbortController;
77
};
88

99
export type RequestOptions<
@@ -27,3 +27,4 @@ export type FinalRequestOptions = RequestOptions & {
2727

2828
export type DefaultQuery = Record<string, unknown>;
2929
export type Headers = Record<string, string | null | undefined>;
30+
export type UnifiedResponse = Response | import('node-fetch').Response;

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ export {
3434
type HTTPMethod,
3535
type DefaultQuery,
3636
type Headers,
37+
type UnifiedResponse,
3738
} from './API';

0 commit comments

Comments
 (0)