Skip to content

Commit 448aacd

Browse files
committed
Add comments
1 parent fe67ec0 commit 448aacd

13 files changed

Lines changed: 1021 additions & 0 deletions

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Add to your `tsconfig.json`:
2323
Or use a triple-slash directive:
2424

2525
```typescript
26+
/// <reference no-default-lib="true" />
27+
/// <reference lib="esnext" />
2628
/// <reference types="@openworkers/workers-types" />
2729
```
2830

types/abort.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
11
// AbortController API types
22

3+
/**
4+
* A signal object that allows you to communicate with an async operation
5+
* and abort it if required via an AbortController.
6+
*/
37
interface AbortSignal extends EventTarget {
8+
/** Returns true if the signal has been aborted. */
49
readonly aborted: boolean;
10+
/** The reason for aborting, if any. */
511
readonly reason: unknown;
12+
/** Throws the abort reason if the signal has been aborted. */
613
throwIfAborted(): void;
714
}
815

16+
/**
17+
* A controller object that allows you to abort one or more async operations.
18+
*
19+
* @example
20+
* ```ts
21+
* const controller = new AbortController();
22+
* fetch(url, { signal: controller.signal });
23+
* controller.abort(); // Cancels the fetch
24+
* ```
25+
*/
926
declare class AbortController {
27+
/** The AbortSignal associated with this controller. */
1028
readonly signal: AbortSignal;
29+
/** Aborts the associated signal with an optional reason. */
1130
abort(reason?: unknown): void;
1231
}

types/bindings.d.ts

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,199 @@
11
// OpenWorkers Bindings types
22

3+
/**
4+
* Static asset binding for serving files from the worker bundle.
5+
*
6+
* @example
7+
* ```ts
8+
* export default {
9+
* async fetch(request, env) {
10+
* return env.ASSETS.fetch('/index.html');
11+
* }
12+
* }
13+
* ```
14+
*/
315
interface BindingAssets {
16+
/**
17+
* Fetches a static asset from the bundle.
18+
* @param path The asset path (e.g., "/index.html").
19+
* @param options Optional request options.
20+
*/
421
fetch(path: string, options?: RequestInit): Promise<Response>;
522
}
623

24+
/**
25+
* Result of a storage head operation.
26+
*/
727
interface StorageHeadResult {
28+
/** The size of the object in bytes. */
829
size: number;
30+
31+
/** The ETag of the object, if available. */
932
etag?: string;
1033
}
1134

35+
/**
36+
* Options for listing storage objects.
37+
*/
1238
interface StorageListOptions {
39+
/** Only return keys starting with this prefix. */
1340
prefix?: string;
41+
42+
/** Maximum number of keys to return. */
1443
limit?: number;
1544
}
1645

46+
/**
47+
* Result of a storage list operation.
48+
*/
1749
interface StorageListResult {
50+
/** The matching keys. */
1851
keys: string[];
52+
53+
/** Whether there are more keys beyond the limit. */
1954
truncated: boolean;
2055
}
2156

57+
/**
58+
* Object storage binding for storing binary data.
59+
* Suitable for files, images, and large data.
60+
*
61+
* @example
62+
* ```ts
63+
* // Store data
64+
* await env.STORAGE.put('file.txt', 'Hello, World!');
65+
*
66+
* // Retrieve data
67+
* const data = await env.STORAGE.get('file.txt');
68+
* ```
69+
*/
2270
interface BindingStorage {
71+
/**
72+
* Retrieves a value by key.
73+
* @returns The value as a string, or null if not found.
74+
*/
2375
get(key: string): Promise<string | null>;
76+
77+
/**
78+
* Stores a value.
79+
* @param key The storage key.
80+
* @param value The value to store.
81+
*/
2482
put(key: string, value: string | Uint8Array): Promise<void>;
83+
84+
/**
85+
* Gets metadata about an object without retrieving its contents.
86+
*/
2587
head(key: string): Promise<StorageHeadResult>;
88+
89+
/**
90+
* Lists keys in storage.
91+
*/
2692
list(options?: StorageListOptions): Promise<StorageListResult>;
93+
94+
/**
95+
* Deletes a key.
96+
*/
2797
delete(key: string): Promise<void>;
2898
}
2999

100+
/**
101+
* Options for KV put operations.
102+
*/
30103
interface KVPutOptions {
104+
/** Time-to-live in seconds. After this time, the key expires. */
31105
expiresIn?: number;
32106
}
33107

108+
/**
109+
* Options for KV list operations.
110+
*/
34111
interface KVListOptions {
112+
/** Only return keys starting with this prefix. */
35113
prefix?: string;
114+
115+
/** Maximum number of keys to return. */
36116
limit?: number;
37117
}
38118

119+
/**
120+
* Key-Value storage binding for fast, low-latency data access.
121+
* Ideal for configuration, sessions, and small data.
122+
*
123+
* @example
124+
* ```ts
125+
* // Store with expiration
126+
* await env.KV.put('session:abc', userData, { expiresIn: 3600 });
127+
*
128+
* // Retrieve
129+
* const session = await env.KV.get('session:abc');
130+
* ```
131+
*/
39132
interface BindingKV {
133+
/**
134+
* Retrieves a value by key.
135+
* @returns The value as a string, or null if not found.
136+
*/
40137
get(key: string): Promise<string | null>;
138+
139+
/**
140+
* Stores a value with optional expiration.
141+
* @param key The storage key.
142+
* @param value The value to store.
143+
* @param options Optional settings like TTL.
144+
*/
41145
put(key: string, value: string, options?: KVPutOptions): Promise<void>;
146+
147+
/**
148+
* Deletes a key.
149+
*/
42150
delete(key: string): Promise<void>;
151+
152+
/**
153+
* Lists keys with optional filtering.
154+
*/
43155
list(options?: KVListOptions): Promise<string[]>;
44156
}
45157

158+
/**
159+
* SQL database binding for relational data.
160+
*
161+
* @example
162+
* ```ts
163+
* const users = await env.DB.query<User>(
164+
* 'SELECT * FROM users WHERE active = ?',
165+
* [true]
166+
* );
167+
* ```
168+
*/
46169
interface BindingDatabase {
170+
/**
171+
* Executes a SQL query.
172+
* @param sql The SQL query with ? placeholders.
173+
* @param params Parameter values to bind.
174+
* @returns An array of result rows.
175+
*/
47176
query<T = Record<string, unknown>>(sql: string, params?: unknown[]): Promise<T[]>;
48177
}
49178

179+
/**
180+
* Service binding for calling other workers.
181+
*
182+
* @example
183+
* ```ts
184+
* const response = await env.AUTH_SERVICE.fetch(
185+
* new Request('https://internal/validate', {
186+
* method: 'POST',
187+
* body: JSON.stringify({ token })
188+
* })
189+
* );
190+
* ```
191+
*/
50192
interface BindingWorker {
193+
/**
194+
* Calls another worker with a request.
195+
* @param request The request to send.
196+
* @param init Optional request options (when using string URL).
197+
*/
51198
fetch(request: Request | string, init?: RequestInit): Promise<Response>;
52199
}

types/blob.d.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,118 @@
11
// Blob & FormData API types
22

3+
/**
4+
* A file-like object of immutable, raw data.
5+
* Can be read as text, ArrayBuffer, or streamed.
6+
*
7+
* @example
8+
* ```ts
9+
* const blob = new Blob(['Hello, World!'], { type: 'text/plain' });
10+
* const text = await blob.text(); // "Hello, World!"
11+
* ```
12+
*/
313
declare class Blob {
14+
/**
15+
* Creates a new Blob.
16+
* @param blobParts Array of data to include in the blob.
17+
* @param options Optional settings like MIME type.
18+
*/
419
constructor(
520
blobParts?: (ArrayBuffer | Uint8Array | Blob | string)[],
621
options?: { type?: string }
722
);
23+
24+
/** The size of the blob in bytes. */
825
readonly size: number;
26+
27+
/** The MIME type of the blob. */
928
readonly type: string;
29+
30+
/** Returns a promise that resolves with an ArrayBuffer containing the blob's data. */
1031
arrayBuffer(): Promise<ArrayBuffer>;
32+
33+
/**
34+
* Returns a new Blob containing a subset of this blob's data.
35+
* @param start Starting byte index (inclusive).
36+
* @param end Ending byte index (exclusive).
37+
* @param contentType MIME type for the new blob.
38+
*/
1139
slice(start?: number, end?: number, contentType?: string): Blob;
40+
41+
/** Returns a ReadableStream for reading the blob's contents. */
1242
stream(): ReadableStream<Uint8Array>;
43+
44+
/** Returns a promise that resolves with the blob's contents as a string. */
1345
text(): Promise<string>;
1446
}
1547

48+
/**
49+
* A File is a Blob with a name and last modified timestamp.
50+
* Typically obtained from FormData or file uploads.
51+
*/
1652
interface File extends Blob {
53+
/** The last modified timestamp in milliseconds since Unix epoch. */
1754
readonly lastModified: number;
55+
56+
/** The name of the file. */
1857
readonly name: string;
1958
}
2059

60+
/**
61+
* A set of key/value pairs representing form fields and their values.
62+
* Used for building multipart/form-data requests.
63+
*
64+
* @example
65+
* ```ts
66+
* const form = new FormData();
67+
* form.append('name', 'John');
68+
* form.append('file', new Blob(['content']), 'file.txt');
69+
* await fetch('/upload', { method: 'POST', body: form });
70+
* ```
71+
*/
2172
declare class FormData {
2273
constructor();
74+
75+
/**
76+
* Appends a new value to an existing key, or adds the key if it doesn't exist.
77+
* @param name The field name.
78+
* @param value The field value.
79+
* @param filename Optional filename for Blob values.
80+
*/
2381
append(name: string, value: string | Blob, filename?: string): void;
82+
83+
/** Deletes all values associated with a given key. */
2484
delete(name: string): void;
85+
86+
/** Returns the first value associated with a given key. */
2587
get(name: string): string | File | null;
88+
89+
/** Returns all values associated with a given key. */
2690
getAll(name: string): (string | File)[];
91+
92+
/** Returns whether a given key exists. */
2793
has(name: string): boolean;
94+
95+
/**
96+
* Sets a value for a key, replacing any existing values.
97+
* @param name The field name.
98+
* @param value The field value.
99+
* @param filename Optional filename for Blob values.
100+
*/
28101
set(name: string, value: string | Blob, filename?: string): void;
102+
103+
/** Executes a callback for each key/value pair. */
29104
forEach(
30105
callback: (value: string | File, key: string, parent: FormData) => void
31106
): void;
107+
108+
/** Returns an iterator of all key/value pairs. */
32109
entries(): IterableIterator<[string, string | File]>;
110+
111+
/** Returns an iterator of all keys. */
33112
keys(): IterableIterator<string>;
113+
114+
/** Returns an iterator of all values. */
34115
values(): IterableIterator<string | File>;
116+
35117
[Symbol.iterator](): IterableIterator<[string, string | File]>;
36118
}

0 commit comments

Comments
 (0)