forked from cloudflare/workers-types
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
37 lines (32 loc) · 994 Bytes
/
test.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
const init: CfRequestInit = {
cf: {
cacheEverything: true,
// properties from IncomingRequestCfProperties
// should not be assignable here
// @ts-expect-error
colo: 'hi',
},
}
if (init.cf) {
// properties on init.cf are known to be RequestInitCfProperties
init.cf.cacheEverything = false
}
// CfRequestInit works with fetch
fetch('hi', init)
// CfRequestInit works with Request
new Request('hi', init)
// FetchEvent is manually specified and assignable
addEventListener('fetch', (event: FetchEvent) => {
// RequestInitCfProperties should not be present
// @ts-expect-error
event.request.cf.cacheEverything
// request from FetchEvent is assignable within request
// constructor as RequestInit
new Request('hi', event.request)
// request from FetchEvent works with handle function
event.respondWith(handle(event.request))
})
function handle(request: Request) {
if (!request.cf) return new Response('hi')
return new Response(request.cf.colo)
}