Skip to content

Commit 72ceaa6

Browse files
committed
fix: Quality fixes
1 parent 2f750fe commit 72ceaa6

File tree

9 files changed

+104
-127
lines changed

9 files changed

+104
-127
lines changed

src/APIClient.ts

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

4-
import { RequestOptions, FinalRequestOptions, APIResponseProps, HTTPMethod, Headers, UnifiedResponse } from './types';
4+
import {
5+
RequestOptions,
6+
FinalRequestOptions,
7+
APIResponseProps,
8+
HTTPMethod,
9+
Headers,
10+
UnifiedResponse,
11+
} from './types';
512
import { AI21EnvConfig } from './EnvConfig';
6-
import { handleAPIResponse } from './ResponseHandler';
713
import { createFetchInstance } from 'envFetch';
814
import { Fetch } from 'fetch';
915

src/ResponseHandler.ts

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

src/Streaming/SSEDecoder.ts

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,39 @@ abstract class BaseSSEDecoder implements SSEDecoder {
2121
abstract iterLines(response: UnifiedResponse): AsyncIterableIterator<string>;
2222

2323
async *_iterLines(reader: ReadableStreamDefaultReader<Uint8Array>): AsyncIterableIterator<string> {
24-
25-
let buffer = '';
26-
27-
try {
28-
while (true) {
29-
const { done, value } = await reader.read();
30-
31-
if (done) {
32-
if (buffer.length > 0) {
33-
const decoded = this.decode(buffer.trim());
34-
if (decoded) yield decoded;
35-
}
36-
break;
37-
}
38-
39-
buffer += new TextDecoder().decode(value);
40-
const lines = buffer.split('\n');
41-
buffer = lines.pop() || '';
42-
43-
for (const line of lines) {
44-
const decoded = this.decode(line.trim());
24+
let buffer = '';
25+
26+
try {
27+
while (true) {
28+
const { done, value } = await reader.read();
29+
30+
if (done) {
31+
if (buffer.length > 0) {
32+
const decoded = this.decode(buffer.trim());
4533
if (decoded) yield decoded;
4634
}
35+
break;
36+
}
37+
38+
buffer += new TextDecoder().decode(value);
39+
const lines = buffer.split('\n');
40+
buffer = lines.pop() || '';
41+
42+
for (const line of lines) {
43+
const decoded = this.decode(line.trim());
44+
if (decoded) yield decoded;
4745
}
48-
} finally {
49-
reader.releaseLock();
5046
}
47+
} finally {
48+
reader.releaseLock();
49+
}
5150
}
5251
}
5352

5453
export class BrowserSSEDecoder extends BaseSSEDecoder {
5554
async *iterLines(response: UnifiedResponse): AsyncIterableIterator<string> {
5655
if (!response.body) {
57-
throw new Error('Response body is null');
56+
throw new Error('Response body is null');
5857
}
5958

6059
const body = response.body as ReadableStream<Uint8Array>;
@@ -63,9 +62,10 @@ export class BrowserSSEDecoder extends BaseSSEDecoder {
6362
}
6463

6564
export class NodeSSEDecoder extends BaseSSEDecoder {
66-
async *iterLines(response: UnifiedResponse): AsyncIterableIterator<string> {
67-
const readerStream = (await import("stream/web")).ReadableStream as any;
68-
const reader = readerStream.from(response.body).getReader();
69-
yield* this._iterLines(reader);
70-
}
65+
async *iterLines(response: UnifiedResponse): AsyncIterableIterator<string> {
66+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
67+
const readerStream = (await import('stream/web')).ReadableStream as any;
68+
const reader = readerStream.from(response.body).getReader();
69+
yield* this._iterLines(reader);
70+
}
7171
}

src/envFetch.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import { BrowserFetch, Fetch, NodeFetch } from 'fetch';
22

3-
export const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
3+
export const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
44
export const isWebWorker =
5-
typeof self === "object" &&
6-
typeof self?.importScripts === "function" &&
7-
(self.constructor?.name === "DedicatedWorkerGlobalScope" ||
8-
self.constructor?.name === "ServiceWorkerGlobalScope" ||
9-
self.constructor?.name === "SharedWorkerGlobalScope");
5+
typeof self === 'object' &&
6+
typeof self?.importScripts === 'function' &&
7+
(self.constructor?.name === 'DedicatedWorkerGlobalScope' ||
8+
self.constructor?.name === 'ServiceWorkerGlobalScope' ||
9+
self.constructor?.name === 'SharedWorkerGlobalScope');
1010

11-
12-
export const isNode = typeof process !== "undefined" && Boolean(process.version) && Boolean(process.versions?.node);
11+
export const isNode =
12+
typeof process !== 'undefined' && Boolean(process.version) && Boolean(process.versions?.node);
1313

1414
export function createFetchInstance(): Fetch {
15-
if (isBrowser || isWebWorker) {
16-
return new BrowserFetch();
17-
} else {
18-
return new NodeFetch();
19-
}
20-
}
15+
if (isBrowser || isWebWorker) {
16+
return new BrowserFetch();
17+
} else {
18+
return new NodeFetch();
19+
}
20+
}

src/fetch/BaseFetch.ts

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
1-
import { AI21Error } from "errors";
2-
import { Stream } from "Streaming";
3-
import { FinalRequestOptions, UnifiedResponse } from "types";
4-
import { APIResponseProps, UnifiedReadableStream } from "types/API";
5-
1+
import { AI21Error } from 'errors';
2+
import { Stream } from 'Streaming';
3+
import { FinalRequestOptions, UnifiedResponse } from 'types';
4+
import { APIResponseProps } from 'types/API';
65

76
export type APIResponse<T> = {
8-
data?: T;
9-
response: UnifiedResponse;
10-
};
7+
data?: T;
8+
response: UnifiedResponse;
9+
};
1110
export abstract class Fetch {
12-
abstract call(url: string, options: FinalRequestOptions): Promise<UnifiedResponse>;
13-
async handleResponse<T>({
14-
response,
15-
options
16-
}: APIResponseProps) {
17-
if (options.stream) {
18-
if (!response.body) {
19-
throw new AI21Error('Response body is null');
20-
}
21-
22-
return this.handleStream<T>(response);
23-
}
11+
abstract call(url: string, options: FinalRequestOptions): Promise<UnifiedResponse>;
12+
async handleResponse<T>({ response, options }: APIResponseProps) {
13+
if (options.stream) {
14+
if (!response.body) {
15+
throw new AI21Error('Response body is null');
16+
}
2417

25-
const contentType = response.headers.get('content-type');
26-
return contentType?.includes('application/json') ? await response.json() : null;
18+
return this.handleStream<T>(response);
2719
}
28-
abstract handleStream<T>(response: UnifiedResponse): Stream<T>;
20+
21+
const contentType = response.headers.get('content-type');
22+
return contentType?.includes('application/json') ? await response.json() : null;
23+
}
24+
abstract handleStream<T>(response: UnifiedResponse): Stream<T>;
2925
}

src/fetch/BrowserFetch.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ import { Stream } from 'Streaming';
44
import { BrowserSSEDecoder } from 'Streaming/SSEDecoder';
55

66
export class BrowserFetch extends Fetch {
7-
call(url: string, options: FinalRequestOptions): Promise<UnifiedResponse> {
8-
const controller = new AbortController();
7+
call(url: string, options: FinalRequestOptions): Promise<UnifiedResponse> {
8+
const controller = new AbortController();
99

10-
return fetch(url, {
11-
method: options?.method,
12-
headers: options?.headers as HeadersInit,
13-
body: options.body ? JSON.stringify(options.body) : undefined,
14-
signal: controller.signal,
15-
});
16-
}
10+
return fetch(url, {
11+
method: options?.method,
12+
headers: options?.headers as HeadersInit,
13+
body: options.body ? JSON.stringify(options.body) : undefined,
14+
signal: controller.signal,
15+
});
16+
}
1717

18-
handleStream<T>(response: UnifiedResponse): Stream<T> {
19-
return new Stream<T>(response as Response, new BrowserSSEDecoder());
20-
}
18+
handleStream<T>(response: UnifiedResponse): Stream<T> {
19+
return new Stream<T>(response as Response, new BrowserSSEDecoder());
20+
}
2121
}

src/fetch/NodeFetch.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import { FinalRequestOptions, UnifiedResponse } from "types";
2-
import { Fetch } from "./BaseFetch";
3-
import { NodeSSEDecoder } from "Streaming/SSEDecoder";
4-
import { Stream } from "Streaming";
1+
import { FinalRequestOptions, UnifiedResponse } from 'types';
2+
import { Fetch } from './BaseFetch';
3+
import { NodeSSEDecoder } from 'Streaming/SSEDecoder';
4+
import { Stream } from 'Streaming';
55

66
export class NodeFetch extends Fetch {
7-
async call(url: string, options: FinalRequestOptions): Promise<UnifiedResponse> {
8-
const nodeFetchModule = await import("node-fetch");
9-
const nodeFetch = nodeFetchModule.default;
7+
async call(url: string, options: FinalRequestOptions): Promise<UnifiedResponse> {
8+
const nodeFetchModule = await import('node-fetch');
9+
const nodeFetch = nodeFetchModule.default;
1010

11-
return nodeFetch(url, {
12-
method: options?.method,
13-
headers: options.headers as Record<string, string>,
14-
body: options?.body ? JSON.stringify(options.body) : undefined,
15-
});
16-
}
11+
return nodeFetch(url, {
12+
method: options?.method,
13+
headers: options.headers as Record<string, string>,
14+
body: options?.body ? JSON.stringify(options.body) : undefined,
15+
});
16+
}
1717

18-
handleStream<T>(response: UnifiedResponse): Stream<T> {
19-
type NodeRespose = import('node-fetch').Response
20-
return new Stream<T>(response as NodeRespose, new NodeSSEDecoder());
21-
}
18+
handleStream<T>(response: UnifiedResponse): Stream<T> {
19+
type NodeRespose = import('node-fetch').Response;
20+
return new Stream<T>(response as NodeRespose, new NodeSSEDecoder());
21+
}
2222
}

src/types/API.ts

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

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

9-
export type RequestOptions<
10-
Req = unknown | Record<string, unknown> | ArrayBufferView | ArrayBuffer,
11-
> = {
9+
export type RequestOptions<Req = unknown | Record<string, unknown> | ArrayBufferView | ArrayBuffer> = {
1210
method?: HTTPMethod;
1311
path?: string;
1412
query?: Req | undefined;
@@ -27,5 +25,5 @@ export type FinalRequestOptions = RequestOptions & {
2725

2826
export type DefaultQuery = Record<string, unknown>;
2927
export type Headers = Record<string, string | null | undefined>;
30-
export type UnifiedResponse = Response | import('node-fetch').Response;
31-
export type UnifiedReadableStream = ReadableStream<Uint8Array> | import('stream/web').ReadableStream;
28+
export type CrossPlatformResponse = Response | import('node-fetch').Response;
29+
export type CrossPlatformReadableStream = ReadableStream<Uint8Array> | import('stream/web').ReadableStream;

src/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export {
3434
type HTTPMethod,
3535
type DefaultQuery,
3636
type Headers,
37-
type UnifiedResponse,
37+
type CrossPlatformResponse as UnifiedResponse,
3838
} from './API';
3939

4040
export {

0 commit comments

Comments
 (0)