Skip to content

Commit eac5e6e

Browse files
amirai21asafgardin
authored andcommitted
feat: wip
1 parent a93d32c commit eac5e6e

File tree

5 files changed

+14
-12
lines changed

5 files changed

+14
-12
lines changed

src/fetch/BaseFetch.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { AI21Error } from '../errors';
33
import { FinalRequestOptions, CrossPlatformResponse } from '../types';
44
import { APIResponseProps } from '../types/API';
55

6+
export type APIResponse<T> = {
7+
data?: T;
8+
response: CrossPlatformResponse;
9+
};
10+
611
export abstract class BaseFetch {
712
abstract call(url: string, options: FinalRequestOptions): Promise<CrossPlatformResponse>;
813
async handleResponse<T>({ response, options }: APIResponseProps) {

src/files/BrowserFilesHandler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export class BrowserFilesHandler extends BaseFilesHandler {
1717

1818
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1919
getMultipartFormDataHeaders(formData: UnifiedFormData): Record<string, string> | null {
20+
/* In browser, we don't need to set any additional headers for multipart/form-data, as the browser will handle it */
2021
return {};
2122
}
2223
}

src/files/NodeFilesHandler.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { UnifiedFormData } from 'types';
22
import { FilePathOrFileObject } from 'types/rag';
33
import { BaseFilesHandler } from './BaseFilesHandler';
4+
import { FormDataNode } from 'types/API';
45

56
export class NodeFilesHandler extends BaseFilesHandler {
67
async convertReadableStream(whatwgStream: ReadableStream): Promise<NodeJS.ReadableStream> {
@@ -19,15 +20,9 @@ export class NodeFilesHandler extends BaseFilesHandler {
1920
});
2021
}
2122

22-
async getBoundary(formData: UnifiedFormData): Promise<string | undefined> {
23-
const { default: FormDataNode } = await import('form-data');
24-
if (formData instanceof FormDataNode) {
25-
return formData.getBoundary();
26-
} else {
27-
throw new Error(
28-
'getBoundary invoked with native browser FormData instance instead of NodeJS form-data',
29-
);
30-
}
23+
getBoundary(formData: UnifiedFormData): string {
24+
const formDataNode = formData as FormDataNode;
25+
return formDataNode.getBoundary();
3126
}
3227

3328
async createFormData(file: FilePathOrFileObject): Promise<UnifiedFormData> {
@@ -51,9 +46,8 @@ export class NodeFilesHandler extends BaseFilesHandler {
5146

5247
getMultipartFormDataHeaders(formData: UnifiedFormData): Record<string, string> | null {
5348
if (formData instanceof FormData) {
54-
return null;
49+
throw new Error('getMultipartFormDataHeaders invoked with native browser FormData instance instead of NodeJS form-data');
5550
}
56-
5751
const boundary = formData.getBoundary();
5852
return {
5953
'Content-Type': `multipart/form-data; boundary=${boundary}`,

src/types/API.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ export type Headers = Record<string, string | null | undefined>;
3030
export type CrossPlatformResponse = Response | import('node-fetch').Response;
3131
export type CrossPlatformReadableStream = ReadableStream<Uint8Array> | import('stream/web').ReadableStream;
3232

33-
export type UnifiedFormData = FormData | import('form-data');
33+
export type FormDataNode = import('form-data');
34+
export type UnifiedFormData = FormData | FormDataNode;

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export {
3636
type Headers,
3737
type CrossPlatformResponse,
3838
type UnifiedFormData,
39+
type FormDataNode,
3940
} from './API';
4041

4142
export {

0 commit comments

Comments
 (0)