forked from cloudflare/workers-types
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
716 lines (673 loc) · 22.1 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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
interface FetchEvent {
passThroughOnException: () => void;
}
interface ScheduledEvent {
/**
* The type of event. This will always return `"scheduled"`.
*/
type: string;
/**
* The time the `ScheduledEvent` was scheduled to be executed in
* milliseconds since January 1, 1970, UTC.
* It can be parsed as `new Date(event.scheduledTime)`
*/
scheduledTime: number;
/**
* Use this method to notify the runtime to wait for asynchronous tasks
* (e.g. logging, analytics to third-party services, streaming and caching).
* The first `event.waitUntil` to fail will be observed and recorded as the
* status in the Cron Trigger Past Events table. Otherwise, it will be
* reported as a Success.
*/
waitUntil(promise: Promise<any>): void;
}
interface BasicImageTransformations {
/**
* Maximum width in image pixels. The value must be an integer.
*/
width?: number;
/**
* Maximum height in image pixels. The value must be an integer.
*/
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.
* - crop: The image will shrunk and cropped to fit within the area
* specified by width and height. The image won’t be enlarged. For images
* smaller than the given dimensions it’s the same as scale-down. For
* images larger than the given dimensions, it’s the same as cover.
* - pad: Resizes to the maximum size that fits within the given width and
* height, and then fills the remaining area with a background color
* (white by default). Use of this mode is not recommended, as the same
* effect can be more efficiently achieved with the contain mode and the
* CSS object-fit: contain property.
*/
fit?: 'scale-down' | 'contain' | 'cover' | 'crop' | 'pad';
/**
* 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 };
/**
* Background color to add underneath the image. Applies only to images with
* transparency (such as PNG). Accepts any CSS color (#RRGGBB, rgba(…),
* hsl(…), etc.)
*/
background?: string;
/**
* Number of degrees (90, 180, 270) to rotate the image by. width and height
* options refer to axes after rotation.
*/
rotate?: 0 | 90 | 180 | 270 | 360;
}
interface RequestInitCfProperties {
/**
* 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?: BasicImageTransformations & {
/**
* Device Pixel Ratio. Default 1. Multiplier for width/height that makes it
* easier to specify higher-DPI sizes in <img srcset>.
*/
dpr?: 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:
* - avif: generate images in AVIF format.
* - webp: generate images in Google WebP format. Set quality to 100 to get
* the WebP-lossless 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?: 'avif' | 'webp' | 'json';
/**
* What EXIF data should be preserved in the output image. Note that EXIF
* rotation and embedded color profiles are always applied ("baked in" into
* the image), and aren't affected by this option. Note that if the Polish
* feature is enabled, all metadata may have been removed already and this
* option may have no effect.
* - keep: Preserve most of EXIF metadata, including GPS location if there's
* any.
* - copyright: Only keep the copyright tag, and discard everything else.
* This is the default behavior for JPEG files.
* - none: Discard all invisible EXIF metadata. Currently WebP and PNG
* output formats always discard metadata.
*/
metadata?: 'keep' | 'copyright' | 'none';
/**
* Overlays are drawn in the order they appear in the array (last array
* entry is the topmost layer).
*/
draw?: (BasicImageTransformations & {
/**
* Absolute URL of the image file to use for the drawing. It can be any of
* the supported file formats. For drawing of watermarks or non-rectangular
* overlays we recommend using PNG or WebP images.
*/
url: string;
/**
* Floating-point number between 0 (transparent) and 1 (opaque).
* For example, opacity: 0.5 makes overlay semitransparent.
*/
opacity?: number;
/**
* - If set to true, the overlay image will be tiled to cover the entire
* area. This is useful for stock-photo-like watermarks.
* - If set to "x", the overlay image will be tiled horizontally only
* (form a line).
* - If set to "y", the overlay image will be tiled vertically only
* (form a line).
*/
repeat?: true | 'x' | 'y';
/**
* Position of the overlay image relative to a given edge. Each property is
* an offset in pixels. 0 aligns exactly to the edge. For example, left: 10
* positions left side of the overlay 10 pixels from the left edge of the
* image it's drawn over. bottom: 0 aligns bottom of the overlay with bottom
* of the background image.
*
* Setting both left & right, or both top & bottom is an error.
*
* If no position is specified, the image will be centered.
*/
top?: number;
left?: number;
bottom?: number;
right?: number;
})[];
};
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 IncomingRequestCfProperties {
/**
* 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?: string;
longitude?: string;
/**
* DMA metro code from which the request was issued, e.g. "635"
*/
metroCode?: string;
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 CfRequestInit extends Omit<RequestInit, 'cf'> {
cf?: RequestInitCfProperties;
}
interface RequestInit {
/**
* cf is a union of these two types because there are multiple
* scenarios in which it might be one or the other. If you need
* a type that only contains RequestInitCfProperties, use the
* CfRequestInit type.
*
* IncomingRequestCfProperties is required to allow
* new Request(someUrl, event.request)
*
* RequestInitCfProperties is required to allow
* new Request(event.request, {cf: { ... } })
* fetch(someUrl, {cf: { ... } })
*/
cf?: IncomingRequestCfProperties | RequestInitCfProperties;
}
declare function addEventListener(type: 'fetch', handler: (event: FetchEvent) => void): void;
declare function addEventListener(type: 'scheduled', handler: (event: ScheduledEvent) => void): void;
interface Request {
cf: IncomingRequestCfProperties;
}
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: IterableIterator<[string, 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 immediately after the publicId.
*/
readonly systemId: string | null;
}
interface DocumentEnd {
/**
* Inserts content right after the end of the document.
*/
append(content: string, options?: ContentOptions): DocumentEnd;
}
interface ElementHandlerOptionals {
/**
* An incoming element, such as `div`
*/
element?(element: Element): void | Promise<void>;
/**
* An incoming comment
*/
comments?(comment: Comment): void | Promise<void>;
/**
* An incoming piece of text
*/
text?(text: Text): void | Promise<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 | Promise<void>;
/**
* An incoming comment
*/
comments(comment: Comment): void | Promise<void>;
/**
* An incoming piece of text
*/
text(text: Text): void | Promise<void>;
/**
* The ending of the document
*/
end(end: DocumentEnd): void | Promise<void>;
}
declare class HTMLRewriter {
constructor();
public on(selector: string, handlers: ElementHandler): HTMLRewriter;
public onDocument(handlers: DocumentHandler): HTMLRewriter;
public transform(response: Response): Response;
}
declare interface CacheStorage {
default: Cache;
}
type KVValue<Value> = Promise<Value | null>;
type KVValueWithMetadata<Value, Metadata> = Promise<{
value: Value | null;
metadata: Metadata | null;
}>;
interface KVNamespace {
get(key: string, options?: {cacheTtl?: number;}): 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>;
get(key: string, options?: {
type: 'text',
cacheTtl?: number;
}): KVValue<string>;
get<ExpectedValue = unknown>(key: string, options?: {
type: 'json',
cacheTtl?: number;
}): KVValue<ExpectedValue>;
get(key: string, options?: {
type: 'arrayBuffer',
cacheTtl?: number;
}): KVValue<ArrayBuffer>;
get(key: string, options?: {
type: 'stream',
cacheTtl?: number;
}): KVValue<ReadableStream>;
getWithMetadata<Metadata = unknown>(key: string): KVValueWithMetadata<string, Metadata>;
getWithMetadata<Metadata = unknown>(
key: string,
type: 'text'
): KVValueWithMetadata<string, Metadata>;
getWithMetadata<ExpectedValue = unknown, Metadata = unknown>(
key: string,
type: 'json'
): KVValueWithMetadata<ExpectedValue, Metadata>;
getWithMetadata<Metadata = unknown>(
key: string,
type: 'arrayBuffer'
): KVValueWithMetadata<ArrayBuffer, Metadata>;
getWithMetadata<Metadata = unknown>(
key: string,
type: 'stream'
): KVValueWithMetadata<ReadableStream, Metadata>;
put(
key: string,
value: string | ReadableStream | ArrayBuffer,
options?: {
expiration?: string | number;
expirationTtl?: string | number;
metadata?: any;
}
): Promise<void>;
delete(key: string): Promise<void>;
list(options?: {
prefix?: string;
limit?: number;
cursor?: string;
}): Promise<{
keys: { name: string; expiration?: number; metadata?: unknown }[];
list_complete: boolean;
cursor?: string;
}>;
}
interface DurableObjectEntries<T> {
[key: string]: T;
}
interface DurableObjectListOptions {
start?: string;
end?: string;
reverse?: boolean;
limit?: number;
prefix?: string;
}
interface DurableObjectOperator {
get<T = unknown>(key: string): Promise<T>;
get<T = unknown>(keys: Array<string>): Promise<Map<string, T>>;
put<T = unknown>(key: string, value: T): Promise<void>;
put<T = unknown>(entries: DurableObjectEntries<T>): Promise<void>;
delete(key: string): Promise<boolean>;
delete(keys: Array<string>): Promise<number>;
deleteAll(): Promise<void>;
list<T = unknown>(options?: DurableObjectListOptions): Promise<Map<string, T>>;
}
interface DurableObjectTransaction extends DurableObjectOperator {
rollback(): void;
}
interface DurableObjectStorage extends DurableObjectOperator {
transaction(
closure: (txn: DurableObjectTransaction) => Promise<void>
): Promise<void>;
}
interface DurableObjectState {
id: DurableObjectId;
storage: DurableObjectStorage;
/**
* Use this method to notify the runtime to wait for asynchronous tasks
* (e.g. logging, analytics to third-party services, streaming and caching).
*/
waitUntil(promise: Promise<any>): void;
}
/**
* DurableObject is a class that defines a template for creating Durable Objects
*/
interface DurableObject {
fetch: (request: Request) => Promise<Response>;
}
/**
* DurableObjectStub is a client object used to send requests to a remote Durable Object
*/
interface DurableObjectStub {
name?: string;
id: DurableObjectId;
fetch: (input: RequestInfo, init?: RequestInit) => Promise<Response>;
}
interface DurableObjectId {
name?: string;
toString: () => string;
}
interface DurableObjectNamespace {
newUniqueId: () => DurableObjectId;
idFromName: (name: string) => DurableObjectId;
idFromString: (hexId: string) => DurableObjectId;
get: (id: DurableObjectId) => DurableObjectStub;
}