forked from grantila/fetch-h2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
60 lines (53 loc) · 1.25 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict'
import { Body, JsonBody, StreamBody, DataBody } from './lib/body'
import { RawHeaders, Headers } from './lib/headers'
import { Request } from './lib/request'
import { Response } from './lib/response'
import {
AbortError,
TimeoutError,
FetchInit,
OnTrailers,
} from './lib/core'
import { Context, ContextOptions, PushHandler } from './lib/context'
const defaultContext = new Context( );
const fetch =
( input: string | Request, init?: Partial< FetchInit > ) =>
defaultContext.fetch( input, init );
const disconnect =
( url: string ) =>
defaultContext.disconnect( url );
const disconnectAll =
( ) =>
defaultContext.disconnectAll( );
const onPush =
( handler: PushHandler ) =>
defaultContext.onPush( handler );
function context( opts?: Partial< ContextOptions > )
{
const ctx = new Context( opts );
return {
fetch: ctx.fetch.bind( ctx ) as typeof fetch,
disconnect: ctx.disconnect.bind( ctx ) as typeof disconnect,
disconnectAll: ctx.disconnectAll.bind( ctx ) as typeof disconnectAll,
onPush: ctx.onPush.bind( ctx ) as typeof onPush,
};
}
export {
context,
fetch,
disconnect,
disconnectAll,
onPush,
// Re-export
Body,
JsonBody,
StreamBody,
DataBody,
Headers,
Request,
Response,
AbortError,
TimeoutError,
OnTrailers,
}