forked from cloudflare/workers-types
-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.ts
61 lines (52 loc) · 1.89 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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)
}
class MyDurableObject implements DurableObject {
async fetch(request: Request) {
return new Response("Hello, world!")
}
}
type MyDurableObjectNamespace = DurableObjectNamespace
const MyDurableObjectNamespace: DurableObjectNamespace = undefined as any
const myDurableObjectStub = MyDurableObjectNamespace.get(MyDurableObjectNamespace.newUniqueId())
myDurableObjectStub.fetch('/', { method: 'POST' })
const myKVNamespace: KVNamespace = undefined as any;
myKVNamespace.get("foo");
myKVNamespace.get("foo", {cacheTtl: 60});
myKVNamespace.get("foo", {cacheTtl: 60, type: "text"});
myKVNamespace.get("foo", {type: "text"});
myKVNamespace.get("foo", {cacheTtl: 60, type: "json"});
myKVNamespace.get("foo", {type: "json"});
myKVNamespace.get("foo", {cacheTtl: 60, type: "arrayBuffer"});
myKVNamespace.get("foo", {type: "arrayBuffer"});
myKVNamespace.get("foo", {cacheTtl: 60, type: "stream"});
myKVNamespace.get("foo", {type: "stream"});