Skip to content

Commit 862d18f

Browse files
committed
fix: Format
1 parent 7d6dfa6 commit 862d18f

File tree

7 files changed

+50
-59
lines changed

7 files changed

+50
-59
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"scripts": {
1616
"build": "tsc",
1717
"test": "jest",
18-
"lint": "eslint . .ts,.tsx",
18+
"lint": "npx eslint 'src/**/*.{ts,tsx}' --no-ignore",
1919
"format": "prettier --write \"src/**/*.ts\"",
2020
"prepare": "npm run build",
2121
"example": "npx tsx src/script.ts"

src/Core.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,3 @@ export abstract class APIClient {
155155
);
156156
}
157157
}
158-

src/ResponseHandler.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { APIResponseProps } from './models';
22
import { AI21Error } from './errors';
33
import { Stream } from './Streaming';
44

5-
6-
export async function handleAPIResponse({response, options }: APIResponseProps): Promise<any> {
5+
export async function handleAPIResponse({ response, options }: APIResponseProps): Promise<any> {
76
if (options.stream) {
87
if (!response.body) {
98
throw new AI21Error('Response body is null');
@@ -12,13 +11,10 @@ export async function handleAPIResponse({response, options }: APIResponseProps):
1211
}
1312

1413
const contentType = response.headers.get('content-type');
15-
const data = contentType?.includes('application/json')
16-
? await response.json()
17-
: await response.text();
14+
const data = contentType?.includes('application/json') ? await response.json() : await response.text();
1815

1916
return {
2017
data,
2118
response,
2219
};
2320
}
24-

src/Streaming/SSEDecoder.ts

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,55 @@ import { Readable } from 'stream';
33
import { SSE_DATA_PREFIX } from './Consts';
44
import { StreamingDecodeError } from '../errors';
55

6+
export interface SSEDecoder {
7+
decode(line: string): string | null;
8+
iterLines(response: NodeResponse): AsyncIterableIterator<string>;
9+
}
610

11+
export class DefaultSSEDecoder implements SSEDecoder {
12+
decode(line: string): string | null {
13+
if (!line) return null;
714

8-
export interface SSEDecoder {
9-
decode(line: string): string | null;
10-
iterLines(response: NodeResponse): AsyncIterableIterator<string>;
15+
if (line.startsWith(SSE_DATA_PREFIX)) {
16+
return line.slice(SSE_DATA_PREFIX.length);
17+
}
18+
19+
throw new StreamingDecodeError(`Invalid SSE line: ${line}`);
1120
}
12-
13-
export class DefaultSSEDecoder implements SSEDecoder {
14-
decode(line: string): string | null {
15-
if (!line) return null;
16-
17-
if (line.startsWith(SSE_DATA_PREFIX)) {
18-
return line.slice(SSE_DATA_PREFIX.length);
19-
}
20-
21-
throw new StreamingDecodeError(`Invalid SSE line: ${line}`);
21+
22+
async *iterLines(response: NodeResponse): AsyncIterableIterator<string> {
23+
if (!response.body) {
24+
throw new Error('Response body is null');
2225
}
23-
24-
async *iterLines(response: NodeResponse): AsyncIterableIterator<string> {
25-
if (!response.body) {
26-
throw new Error('Response body is null');
27-
}
28-
29-
const webReadableStream = Readable.toWeb(response.body as any) as ReadableStream;
30-
const reader = webReadableStream.getReader();
31-
32-
let buffer = '';
33-
34-
try {
35-
while (true) {
36-
const { done, value } = await reader.read();
37-
38-
if (done) {
39-
if (buffer.length > 0) {
40-
const decoded = this.decode(buffer.trim());
41-
if (decoded) yield decoded;
42-
}
43-
break;
44-
}
45-
46-
buffer += new TextDecoder().decode(value);
47-
const lines = buffer.split('\n');
48-
buffer = lines.pop() || '';
49-
50-
for (const line of lines) {
51-
const decoded = this.decode(line.trim());
26+
27+
const webReadableStream = Readable.toWeb(response.body as any) as ReadableStream;
28+
const reader = webReadableStream.getReader();
29+
30+
let buffer = '';
31+
32+
try {
33+
while (true) {
34+
const { done, value } = await reader.read();
35+
36+
if (done) {
37+
if (buffer.length > 0) {
38+
const decoded = this.decode(buffer.trim());
5239
if (decoded) yield decoded;
5340
}
41+
break;
42+
}
43+
44+
buffer += new TextDecoder().decode(value);
45+
const lines = buffer.split('\n');
46+
buffer = lines.pop() || '';
47+
48+
for (const line of lines) {
49+
const decoded = this.decode(line.trim());
50+
if (decoded) yield decoded;
5451
}
55-
} finally {
56-
reader.releaseLock();
5752
}
53+
} finally {
54+
reader.releaseLock();
5855
}
59-
}
56+
}
57+
}

src/Streaming/Stream.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { SSEDecoder } from './SSEDecoder';
55
import { SSE_DONE_MSG } from './Consts';
66
import { StreamingDecodeError } from '../errors';
77

8-
98
function getStreamMessage<T>(chunk: string): T {
109
try {
1110
return JSON.parse(chunk);
@@ -39,4 +38,4 @@ export class Stream<T> implements AsyncIterableIterator<T> {
3938
[Symbol.asyncIterator](): AsyncIterableIterator<T> {
4039
return this;
4140
}
42-
}
41+
}

src/Streaming/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { Stream } from './Stream';
1+
export { Stream } from './Stream';

src/errors.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ export class MissingAPIKeyError extends AI21Error {
77
}
88

99
export class StreamingDecodeError extends Error {
10-
constructor(chunk: string) {
11-
super(`Failed to decode chunk: ${chunk}`);
10+
constructor(chunk: string) {
11+
super(`Failed to decode chunk: ${chunk}`);
1212
}
1313
}
14-

0 commit comments

Comments
 (0)