File tree Expand file tree Collapse file tree 5 files changed +16
-14
lines changed Expand file tree Collapse file tree 5 files changed +16
-14
lines changed Original file line number Diff line number Diff line change 1
1
import { AI21Error } from '../errors' ;
2
- import { Stream } from '../Streaming ' ;
2
+ import { Stream } from '../streaming ' ;
3
3
import { FinalRequestOptions , CrossPlatformResponse } from '../types' ;
4
4
import { APIResponseProps } from '../types/API' ;
5
5
6
6
export type APIResponse < T > = {
7
7
data ?: T ;
8
8
response : CrossPlatformResponse ;
9
9
} ;
10
- export abstract class Fetch {
10
+ export abstract class BaseFetch {
11
11
abstract call ( url : string , options : FinalRequestOptions ) : Promise < CrossPlatformResponse > ;
12
12
async handleResponse < T > ( { response, options } : APIResponseProps ) {
13
13
if ( options . stream ) {
Original file line number Diff line number Diff line change 1
1
import { FinalRequestOptions , CrossPlatformResponse } from 'types' ;
2
- import { Fetch } from './BaseFetch' ;
3
- import { Stream , BrowserSSEDecoder } from '../Streaming ' ;
2
+ import { BaseFetch } from './BaseFetch' ;
3
+ import { Stream , BrowserSSEDecoder } from '../streaming ' ;
4
4
5
- export class BrowserFetch extends Fetch {
5
+ export class BrowserFetch extends BaseFetch {
6
6
call ( url : string , options : FinalRequestOptions ) : Promise < CrossPlatformResponse > {
7
7
const controller = new AbortController ( ) ;
8
8
9
9
return fetch ( url , {
10
- method : options ? .method ,
11
- headers : options ?. headers as HeadersInit ,
12
- body : options . body ? JSON . stringify ( options . body ) : undefined ,
10
+ method : options . method ,
11
+ headers : options ?. headers ? options . headers as HeadersInit : undefined ,
12
+ body : options ? .body ? JSON . stringify ( options . body ) : undefined ,
13
13
signal : controller . signal ,
14
14
} ) ;
15
15
}
Original file line number Diff line number Diff line change 1
1
import { FinalRequestOptions , CrossPlatformResponse } from 'types' ;
2
- import { Fetch } from './BaseFetch' ;
3
- import { Stream , NodeSSEDecoder } from '../Streaming ' ;
2
+ import { BaseFetch } from './BaseFetch' ;
3
+ import { Stream , NodeSSEDecoder } from '../streaming ' ;
4
4
5
- export class NodeFetch extends Fetch {
5
+ export class NodeFetch extends BaseFetch {
6
6
async call ( url : string , options : FinalRequestOptions ) : Promise < CrossPlatformResponse > {
7
7
const nodeFetchModule = await import ( 'node-fetch' ) ;
8
8
const nodeFetch = nodeFetchModule . default ;
9
9
10
10
return nodeFetch ( url , {
11
- method : options ? .method ,
12
- headers : options . headers as Record < string , string > ,
11
+ method : options . method ,
12
+ headers : options ? .headers ? options . headers as Record < string , string > : undefined ,
13
13
body : options ?. body ? JSON . stringify ( options . body ) : undefined ,
14
14
} ) ;
15
15
}
Original file line number Diff line number Diff line change 1
- export { Fetch } from './BaseFetch' ;
1
+ export { BaseFetch as Fetch } from './BaseFetch' ;
2
2
export { BrowserFetch } from './BrowserFetch' ;
3
3
export { NodeFetch } from './NodeFetch' ;
Original file line number Diff line number Diff line change @@ -25,5 +25,7 @@ export type FinalRequestOptions = RequestOptions & {
25
25
26
26
export type DefaultQuery = Record < string , unknown > ;
27
27
export type Headers = Record < string , string | null | undefined > ;
28
+
29
+ // Platforms specific types for NodeJS and Browser
28
30
export type CrossPlatformResponse = Response | import ( 'node-fetch' ) . Response ;
29
31
export type CrossPlatformReadableStream = ReadableStream < Uint8Array > | import ( 'stream/web' ) . ReadableStream ;
You can’t perform that action at this time.
0 commit comments