forked from cloudflare/workers-types
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
477 lines (450 loc) · 13.6 KB
/
index.d.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
interface FetchEvent {
passThroughOnException: () => void
}
interface CfRequestInit {
/**
* In addition to the properties you can set in the RequestInit dict
* that you pass as an argument to the Request constructor, you can
* set certain properties of a `cf` object to control how Cloudflare
* features are applied to that new Request.
*
* Note: Currently, these properties cannot be tested in the
* playground.
*/
cacheEverything?: boolean
/**
* A request's cache key is what determines if two requests are
* "the same" for caching purposes. If a request has the same cache key
* as some previous request, then we can serve the same cached response for
* both. (e.g. 'some-key')
*
* Only available for Enterprise customers.
*/
cacheKey?: string
/**
* Force response to be cached for a given number of seconds. (e.g. 300)
*/
cacheTtl?: number
/**
* Force response to be cached for a given number of seconds based on the Origin status code.
* (e.g. { '200-299': 86400, '404': 1, '500-599': 0 })
*/
cacheTtlByStatus?: { [key: string]: number }
scrapeShield?: boolean
apps?: boolean
image?: {
/**
* Maximum width in image pixels. The value must be an integer.
*/
width?: number
/**
* Maximum height in image pixels.
*/
height?: number
/**
* Resizing mode as a string. It affects interpretation of width and height
* options:
* - scale-down: Similar to contain, but the image is never enlarged. If
* the image is larger than given width or height, it will be resized.
* Otherwise its original size will be kept.
* - contain: Resizes to maximum size that fits within the given width and
* height. If only a single dimension is given (e.g. only width), the
* image will be shrunk or enlarged to exactly match that dimension.
* Aspect ratio is always preserved.
* - cover: Resizes (shrinks or enlarges) to fill the entire area of width
* and height. If the image has an aspect ratio different from the ratio
* of width and height, it will be cropped to fit.
*/
fit?: 'scale-down' | 'contain' | 'cover'
/**
* When cropping with fit: "cover", this defines the side or point that should
* be left uncropped. The value is either a string
* "left", "right", "top", "bottom" or "center" (the default),
* or an object {x, y} containing focal point coordinates in the original
* image expressed as fractions ranging from 0.0 (top or left) to 1.0
* (bottom or right), 0.5 being the center. {fit: "cover", gravity: "top"} will
* crop bottom or left and right sides as necessary, but won’t crop anything
* from the top. {fit: "cover", gravity: {x:0.5, y:0.2}} will crop each side to
* preserve as much as possible around a point at 20% of the height of the
* source image.
*/
gravity?: 'left' | 'right' | 'top' | 'bottom' | 'center' | { x: number; y: number }
/**
* Quality setting from 1-100 (useful values are in 60-90 range). Lower values
* make images look worse, but load faster. The default is 85. It applies only
* to JPEG and WebP images. It doesn’t have any effect on PNG.
*/
quality?: number
/**
* Output format to generate. It can be:
* - webp: generate images in Google WebP format. Set quality to 100 to get
* the WebP-lossles format.
* - json: instead of generating an image, outputs information about the
* image, in JSON format. The JSON object will contain image size
* (before and after resizing), source image’s MIME type, file size, etc.
*/
format?: 'webp' | 'json'
}
minify?: {
javascript?: boolean
css?: boolean
html?: boolean
}
mirage?: boolean
/**
* Redirects the request to an alternate origin server. You can use this,
* for example, to implement load balancing across several origins.
* (e.g.us-east.example.com)
*
* Note - For security reasons, the hostname set in resolveOverride must
* be proxied on the same Cloudflare zone of the incoming request.
* Otherwise, the setting is ignored. CNAME hosts are allowed, so to
* resolve to a host under a different domain or a DNS only domain first
* declare a CNAME record within your own zone’s DNS mapping to the
* external hostname, set proxy on Cloudflare, then set resolveOverride
* to point to that CNAME record.
*/
resolveOverride?: string
}
interface CfRequestProperties {
/**
* In addition to the properties on the standard Request object,
* the cf object contains extra information about the request provided
* by Cloudflare's edge.
*
* Note: Currently, settings in the cf object cannot be accessed in the
* playground.
*/
/**
* (e.g. 395747)
*/
asn: number
botManagement?: {
score: number
staticResource: boolean
verifiedBot: boolean
}
city?: string
clientTcpRtt: number
clientTrustScore?: number
/**
* The three-letter airport code of the data center that the request
* hit. (e.g. "DFW")
*/
colo: string
continent?: string
/**
* The two-letter country code in the request. This is the same value
* as that provided in the CF-IPCountry header. (e.g. "US")
*/
country: string
httpProtocol: string
latitude?: number
longitude?: number
/**
* DMA metro code from which the request was issued, e.g. "635"
*/
metroCode?: number
postalCode?: string
/**
* e.g. "Texas"
*/
region?: string
/**
* e.g. "TX"
*/
regionCode?: string
/**
* e.g. "weight=256;exclusive=1"
*/
requestPriority: string
/**
* e.g. "America/Chicago"
*/
timezone?: string
tlsVersion: string
tlsCipher: string
tlsClientAuth: {
certIssuerDNLegacy: string
certIssuerDN: string
certPresented: '0' | '1'
certSubjectDNLegacy: string
certSubjectDN: string
certNotBefore: string // In format "Dec 22 19:39:00 2018 GMT"
certNotAfter: string // In format "Dec 22 19:39:00 2018 GMT"
certSerial: string
certFingerprintSHA1: string
certVerified: string // “SUCCESS”, “FAILED:reason”, “NONE”
}
}
interface RequestInit {
cf?: CfRequestInit|CfRequestProperties
}
declare function addEventListener(
type: 'fetch',
handler: (event: FetchEvent) => void,
): void
interface Request {
cf: CfRequestProperties
}
interface FormData {
[Symbol.iterator](): IterableIterator<[string, FormDataEntryValue]>;
/**
* Returns an array of key, value pairs for every entry in the list.
*/
entries(): IterableIterator<[string, FormDataEntryValue]>;
/**
* Returns a list of keys in the list.
*/
keys(): IterableIterator<string>;
/**
* Returns a list of values in the list.
*/
values(): IterableIterator<FormDataEntryValue>;
}
interface Headers {
[Symbol.iterator](): IterableIterator<[string, string]>;
/**
* Returns an iterator allowing to go through all key/value pairs contained in this object.
*/
entries(): IterableIterator<[string, string]>;
/**
* Returns an iterator allowing to go through all keys of the key/value pairs contained in this object.
*/
keys(): IterableIterator<string>;
/**
* Returns an iterator allowing to go through all values of the key/value pairs contained in this object.
*/
values(): IterableIterator<string>;
}
interface URLSearchParams {
[Symbol.iterator](): IterableIterator<[string, string]>;
/**
* Returns an array of key, value pairs for every entry in the search params.
*/
entries(): IterableIterator<[string, string]>;
/**
* Returns a list of keys in the search params.
*/
keys(): IterableIterator<string>;
/**
* Returns a list of values in the search params.
*/
values(): IterableIterator<string>;
}
interface ContentOptions {
/**
* Controls the way the HTMLRewriter treats inserted content.
*
* - true: Raw HTML
* - false: (Default) Text and any HTML will be escaped
*/
html: boolean
}
interface Element {
/**
* The namespace URI of the element according to Infra Spec
* (https://infra.spec.whatwg.org/#namespaces).
*/
namespaceURI: string
/**
* e.g. "div"
*/
tagName: string
/**
* Read-Only - key/value pairs of attributes.
*/
readonly attributes: Iterator<{ name: string; value: string }>
/**
* Indicates whether the element was removed/replaced in a previous handler
*/
removed: boolean
/**
* Returns the value for a given attribute name on the element, or null if it isn’t found.
*/
getAttribute(name: string): string | null
/**
* Returns a boolean indicating whether an attribute exists on the element.
*/
hasAttribute(name: string): boolean
/**
* Sets an attribute to a provided value, creating the attribute if it doesn’t exist.
*/
setAttribute(name: string, value: string): Element
/**
* Removes the attribute.
*/
removeAttribute(name: string): Element
/**
* Inserts content before the element.
*/
before(content: string, options?: ContentOptions): Element
/**
* Inserts content right after the element.
*/
after(content: string, options?: ContentOptions): Element
/**
* Inserts content right after the start tag of the element.
*/
prepend(content: string, options?: ContentOptions): Element
/**
* Inserts content right before the end tag of the element.
*/
append(content: string, options?: ContentOptions): Element
/**
* Removes the element and inserts content in place of it.
*/
replace(content: string, options?: ContentOptions): Element
/**
* Replaces content of the element.
*/
setInnerContent(content: string, options?: ContentOptions): Element
/**
* Removes the element with all its content.
*/
remove(): Element
/**
* Removes the start tag and end tag of the element, but keeps its inner content intact.
*/
removeAndKeepContent(): Element
}
interface Text {
/**
* Indicates whether the element was removed/replaced in a previous handler.
*/
removed: boolean
/**
* Read-Only - The text contents of the chunk. Could be empty if the chunk
* is the last chunk of the text node.
*/
readonly text: string
/**
* Read-Only - indicates whether the chunk is the last chunk of the text node.
*/
readonly lastInTextNode: boolean
/**
* Inserts content before the element.
*/
before(content: string, options?: ContentOptions): Element
/**
* Inserts content right after the element.
*/
after(content: string, options?: ContentOptions): Element
/**
* Removes the element and inserts content in place of it.
*/
replace(content: string, options?: ContentOptions): Element
/**
* Removes the element with all its content.
*/
remove(): Element
}
interface Comment {
/**
* Indicates whether the element was removed/replaced in a previous handler.
*/
removed: boolean
/**
* This property can be assigned different values, to modify comment’s text.
*/
text: string
/**
* Inserts content before the element.
*/
before(content: string, options?: ContentOptions): Element
/**
* Inserts content right after the element.
*/
after(content: string, options?: ContentOptions): Element
/**
* Removes the element and inserts content in place of it.
*/
replace(content: string, options?: ContentOptions): Element
/**
* Removes the element with all its content.
*/
remove(): Element
}
interface Doctype {
readonly name: string | null
/**
* Read-Only, The quoted string in the doctype after the PUBLIC atom.
*/
readonly publicId: string | null
/**
* Read-Only, The quoted string in the doctype after the SYSTEM atom or immidiately after the publicId.
*/
readonly systemId: string | null
}
interface ElementHandlerOptionals {
/**
* An incoming element, such as `div`
*/
element?(element: Element): void
/**
* An incoming comment
*/
comments?(comment: Comment): void
/**
* An incoming piece of text
*/
text?(text: Text): void
}
// See https://stackoverflow.com/a/49725198
type RequireAtLeastOne<T, Keys extends keyof T = keyof T> =
Pick<T, Exclude<keyof T, Keys>>
& {
[K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>
}[Keys]
type ElementHandler = RequireAtLeastOne<ElementHandlerOptionals, 'element' | 'comments' | 'text'>
interface DocumentHandler {
/**
* An incoming doctype, such as <!DOCTYPE html>
*/
doctype(doctype: Doctype): void
/**
* An incoming comment
*/
comments(comment: Comment): void
/**
* An incoming piece of text
*/
text(text: Text): void
}
declare class HTMLRewriter {
constructor()
public on(selector: string, handlers: ElementHandler): HTMLRewriter
public onDocument(selector: string, handlers: DocumentHandler): HTMLRewriter
public transform(response: Response): Response
}
declare interface CacheStorage {
default: Cache
}
type KVValue<Value> = Promise<Value | null>
declare module '@cloudflare/workers-types' {
export interface KVNamespace {
get(key: string): KVValue<string>
get(key: string, type: 'text'): KVValue<string>
get<ExpectedValue = unknown>(key: string, type: 'json'): KVValue<ExpectedValue>
get(key: string, type: 'arrayBuffer'): KVValue<ArrayBuffer>
get(key: string, type: 'stream'): KVValue<ReadableStream>
put(
key: string,
value: string | ReadableStream | ArrayBuffer | FormData,
options?: {
expiration?: string | number
expirationTtl?: string | number
},
): Promise<void>
delete(key: string): Promise<void>
list(options: {
prefix?: string
limit?: number
cursor?: string
}): Promise<{
keys: { name: string; expiration?: number }[]
list_complete: boolean
cursor: string
}>
}
}