@openworkers/workers-types 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -10,6 +10,8 @@ bun add -d @openworkers/workers-types
10
10
 
11
11
  ## Usage
12
12
 
13
+ ### Exclusive mode (recommended)
14
+
13
15
  Add to your `tsconfig.json`:
14
16
 
15
17
  ```json
@@ -20,14 +22,33 @@ Add to your `tsconfig.json`:
20
22
  }
21
23
  ```
22
24
 
23
- Or use a triple-slash directive:
25
+ This includes only OpenWorkers types and excludes conflicting types (like `@types/node` or `@types/bun`). Best for pure worker projects.
26
+
27
+ ### Compatible mode
28
+
29
+ If you need to mix with Node.js or Bun types, just install the package without configuring `types`. The types will merge with existing globals:
30
+
31
+ ```json
32
+ {
33
+ "compilerOptions": {
34
+ // no "types" array - all @types/* are included
35
+ }
36
+ }
37
+ ```
38
+
39
+ ### Triple-slash directive
40
+
41
+ For per-file control:
24
42
 
25
43
  ```typescript
44
+ /// <reference no-default-lib="true" />
45
+ /// <reference lib="esnext" />
26
46
  /// <reference types="@openworkers/workers-types" />
27
47
  ```
28
48
 
29
49
  ## Structure
30
50
 
51
+ - `types/globals.d.ts` - globalThis, self
31
52
  - `types/fetch.d.ts` - Request, Response, Headers, fetch
32
53
  - `types/url.d.ts` - URL, URLSearchParams
33
54
  - `types/streams.d.ts` - ReadableStream, WritableStream
package/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  // OpenWorkers Runtime Types
2
2
 
3
+ /// <reference path="./types/globals.d.ts" />
3
4
  /// <reference path="./types/events.d.ts" />
4
5
  /// <reference path="./types/abort.d.ts" />
5
6
  /// <reference path="./types/streams.d.ts" />
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openworkers/workers-types",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "TypeScript types for the OpenWorkers runtime",
5
5
  "license": "MIT",
6
6
  "repository": {
package/types/abort.d.ts CHANGED
@@ -1,12 +1,38 @@
1
1
  // AbortController API types
2
2
 
3
- interface AbortSignal extends EventTarget {
4
- readonly aborted: boolean;
5
- readonly reason: unknown;
6
- throwIfAborted(): void;
7
- }
3
+ export {};
4
+
5
+ declare global {
6
+ /**
7
+ * A signal object that allows you to communicate with an async operation
8
+ * and abort it if required via an AbortController.
9
+ */
10
+ interface AbortSignal extends EventTarget {
11
+ /** Returns true if the signal has been aborted. */
12
+ readonly aborted: boolean;
13
+
14
+ /** The reason for aborting, if any. */
15
+ readonly reason: unknown;
16
+
17
+ /** Throws the abort reason if the signal has been aborted. */
18
+ throwIfAborted(): void;
19
+ }
20
+
21
+ /**
22
+ * A controller object that allows you to abort one or more async operations.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * const controller = new AbortController();
27
+ * fetch(url, { signal: controller.signal });
28
+ * controller.abort(); // Cancels the fetch
29
+ * ```
30
+ */
31
+ class AbortController {
32
+ /** The AbortSignal associated with this controller. */
33
+ readonly signal: AbortSignal;
8
34
 
9
- declare class AbortController {
10
- readonly signal: AbortSignal;
11
- abort(reason?: unknown): void;
35
+ /** Aborts the associated signal with an optional reason. */
36
+ abort(reason?: unknown): void;
37
+ }
12
38
  }
@@ -1,52 +1,203 @@
1
1
  // OpenWorkers Bindings types
2
2
 
3
- interface BindingAssets {
4
- fetch(path: string, options?: RequestInit): Promise<Response>;
5
- }
3
+ export {};
6
4
 
7
- interface StorageHeadResult {
8
- size: number;
9
- etag?: string;
10
- }
5
+ declare global {
6
+ /**
7
+ * Static asset binding for serving files from the worker bundle.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * export default {
12
+ * async fetch(request, env) {
13
+ * return env.ASSETS.fetch('/index.html');
14
+ * }
15
+ * }
16
+ * ```
17
+ */
18
+ interface BindingAssets {
19
+ /**
20
+ * Fetches a static asset from the bundle.
21
+ * @param path The asset path (e.g., "/index.html").
22
+ * @param options Optional request options.
23
+ */
24
+ fetch(path: string, options?: RequestInit): Promise<Response>;
25
+ }
11
26
 
12
- interface StorageListOptions {
13
- prefix?: string;
14
- limit?: number;
15
- }
27
+ /**
28
+ * Result of a storage head operation.
29
+ */
30
+ interface StorageHeadResult {
31
+ /** The size of the object in bytes. */
32
+ size: number;
16
33
 
17
- interface StorageListResult {
18
- keys: string[];
19
- truncated: boolean;
20
- }
34
+ /** The ETag of the object, if available. */
35
+ etag?: string;
36
+ }
21
37
 
22
- interface BindingStorage {
23
- get(key: string): Promise<string | null>;
24
- put(key: string, value: string | Uint8Array): Promise<void>;
25
- head(key: string): Promise<StorageHeadResult>;
26
- list(options?: StorageListOptions): Promise<StorageListResult>;
27
- delete(key: string): Promise<void>;
28
- }
38
+ /**
39
+ * Options for listing storage objects.
40
+ */
41
+ interface StorageListOptions {
42
+ /** Only return keys starting with this prefix. */
43
+ prefix?: string;
29
44
 
30
- interface KVPutOptions {
31
- expiresIn?: number;
32
- }
45
+ /** Maximum number of keys to return. */
46
+ limit?: number;
47
+ }
33
48
 
34
- interface KVListOptions {
35
- prefix?: string;
36
- limit?: number;
37
- }
49
+ /**
50
+ * Result of a storage list operation.
51
+ */
52
+ interface StorageListResult {
53
+ /** The matching keys. */
54
+ keys: string[];
38
55
 
39
- interface BindingKV {
40
- get(key: string): Promise<string | null>;
41
- put(key: string, value: string, options?: KVPutOptions): Promise<void>;
42
- delete(key: string): Promise<void>;
43
- list(options?: KVListOptions): Promise<string[]>;
44
- }
56
+ /** Whether there are more keys beyond the limit. */
57
+ truncated: boolean;
58
+ }
45
59
 
46
- interface BindingDatabase {
47
- query<T = Record<string, unknown>>(sql: string, params?: unknown[]): Promise<T[]>;
48
- }
60
+ /**
61
+ * Object storage binding for storing binary data.
62
+ * Suitable for files, images, and large data.
63
+ *
64
+ * @example
65
+ * ```ts
66
+ * // Store data
67
+ * await env.STORAGE.put('file.txt', 'Hello, World!');
68
+ *
69
+ * // Retrieve data
70
+ * const data = await env.STORAGE.get('file.txt');
71
+ * ```
72
+ */
73
+ interface BindingStorage {
74
+ /**
75
+ * Retrieves a value by key.
76
+ * @returns The value as a string, or null if not found.
77
+ */
78
+ get(key: string): Promise<string | null>;
79
+
80
+ /**
81
+ * Stores a value.
82
+ * @param key The storage key.
83
+ * @param value The value to store.
84
+ */
85
+ put(key: string, value: string | Uint8Array): Promise<void>;
86
+
87
+ /**
88
+ * Gets metadata about an object without retrieving its contents.
89
+ */
90
+ head(key: string): Promise<StorageHeadResult>;
91
+
92
+ /**
93
+ * Lists keys in storage.
94
+ */
95
+ list(options?: StorageListOptions): Promise<StorageListResult>;
96
+
97
+ /**
98
+ * Deletes a key.
99
+ */
100
+ delete(key: string): Promise<void>;
101
+ }
102
+
103
+ /**
104
+ * Options for KV put operations.
105
+ */
106
+ interface KVPutOptions {
107
+ /** Time-to-live in seconds. After this time, the key expires. */
108
+ expiresIn?: number;
109
+ }
110
+
111
+ /**
112
+ * Options for KV list operations.
113
+ */
114
+ interface KVListOptions {
115
+ /** Only return keys starting with this prefix. */
116
+ prefix?: string;
117
+
118
+ /** Maximum number of keys to return. */
119
+ limit?: number;
120
+ }
121
+
122
+ /**
123
+ * Key-Value storage binding for fast, low-latency data access.
124
+ * Ideal for configuration, sessions, and small data.
125
+ *
126
+ * @example
127
+ * ```ts
128
+ * // Store with expiration
129
+ * await env.KV.put('session:abc', userData, { expiresIn: 3600 });
130
+ *
131
+ * // Retrieve
132
+ * const session = await env.KV.get('session:abc');
133
+ * ```
134
+ */
135
+ interface BindingKV {
136
+ /**
137
+ * Retrieves a value by key.
138
+ * @returns The value as a string, or null if not found.
139
+ */
140
+ get(key: string): Promise<string | null>;
141
+
142
+ /**
143
+ * Stores a value with optional expiration.
144
+ * @param key The storage key.
145
+ * @param value The value to store.
146
+ * @param options Optional settings like TTL.
147
+ */
148
+ put(key: string, value: string, options?: KVPutOptions): Promise<void>;
149
+
150
+ /**
151
+ * Deletes a key.
152
+ */
153
+ delete(key: string): Promise<void>;
154
+
155
+ /**
156
+ * Lists keys with optional filtering.
157
+ */
158
+ list(options?: KVListOptions): Promise<string[]>;
159
+ }
160
+
161
+ /**
162
+ * SQL database binding for relational data.
163
+ *
164
+ * @example
165
+ * ```ts
166
+ * const users = await env.DB.query<User>(
167
+ * 'SELECT * FROM users WHERE active = ?',
168
+ * [true]
169
+ * );
170
+ * ```
171
+ */
172
+ interface BindingDatabase {
173
+ /**
174
+ * Executes a SQL query.
175
+ * @param sql The SQL query with ? placeholders.
176
+ * @param params Parameter values to bind.
177
+ * @returns An array of result rows.
178
+ */
179
+ query<T = Record<string, unknown>>(sql: string, params?: unknown[]): Promise<T[]>;
180
+ }
49
181
 
50
- interface BindingWorker {
51
- fetch(request: Request | string, init?: RequestInit): Promise<Response>;
182
+ /**
183
+ * Service binding for calling other workers.
184
+ *
185
+ * @example
186
+ * ```ts
187
+ * const response = await env.AUTH_SERVICE.fetch(
188
+ * new Request('https://internal/validate', {
189
+ * method: 'POST',
190
+ * body: JSON.stringify({ token })
191
+ * })
192
+ * );
193
+ * ```
194
+ */
195
+ interface BindingWorker {
196
+ /**
197
+ * Calls another worker with a request.
198
+ * @param request The request to send.
199
+ * @param init Optional request options (when using string URL).
200
+ */
201
+ fetch(request: Request | string, init?: RequestInit): Promise<Response>;
202
+ }
52
203
  }
package/types/blob.d.ts CHANGED
@@ -1,36 +1,122 @@
1
1
  // Blob & FormData API types
2
2
 
3
- declare class Blob {
4
- constructor(
5
- blobParts?: (ArrayBuffer | Uint8Array | Blob | string)[],
6
- options?: { type?: string }
7
- );
8
- readonly size: number;
9
- readonly type: string;
10
- arrayBuffer(): Promise<ArrayBuffer>;
11
- slice(start?: number, end?: number, contentType?: string): Blob;
12
- stream(): ReadableStream<Uint8Array>;
13
- text(): Promise<string>;
14
- }
3
+ export {};
15
4
 
16
- interface File extends Blob {
17
- readonly lastModified: number;
18
- readonly name: string;
19
- }
5
+ declare global {
6
+ /**
7
+ * A file-like object of immutable, raw data.
8
+ * Can be read as text, ArrayBuffer, or streamed.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * const blob = new Blob(['Hello, World!'], { type: 'text/plain' });
13
+ * const text = await blob.text(); // "Hello, World!"
14
+ * ```
15
+ */
16
+ class Blob {
17
+ /**
18
+ * Creates a new Blob.
19
+ * @param blobParts Array of data to include in the blob.
20
+ * @param options Optional settings like MIME type.
21
+ */
22
+ constructor(
23
+ blobParts?: (ArrayBuffer | Uint8Array | Blob | string)[],
24
+ options?: { type?: string }
25
+ );
26
+
27
+ /** The size of the blob in bytes. */
28
+ readonly size: number;
29
+
30
+ /** The MIME type of the blob. */
31
+ readonly type: string;
32
+
33
+ /** Returns a promise that resolves with an ArrayBuffer containing the blob's data. */
34
+ arrayBuffer(): Promise<ArrayBuffer>;
35
+
36
+ /**
37
+ * Returns a new Blob containing a subset of this blob's data.
38
+ * @param start Starting byte index (inclusive).
39
+ * @param end Ending byte index (exclusive).
40
+ * @param contentType MIME type for the new blob.
41
+ */
42
+ slice(start?: number, end?: number, contentType?: string): Blob;
43
+
44
+ /** Returns a ReadableStream for reading the blob's contents. */
45
+ stream(): ReadableStream<Uint8Array>;
46
+
47
+ /** Returns a promise that resolves with the blob's contents as a string. */
48
+ text(): Promise<string>;
49
+ }
50
+
51
+ /**
52
+ * A File is a Blob with a name and last modified timestamp.
53
+ * Typically obtained from FormData or file uploads.
54
+ */
55
+ interface File extends Blob {
56
+ /** The last modified timestamp in milliseconds since Unix epoch. */
57
+ readonly lastModified: number;
58
+
59
+ /** The name of the file. */
60
+ readonly name: string;
61
+ }
62
+
63
+ /**
64
+ * A set of key/value pairs representing form fields and their values.
65
+ * Used for building multipart/form-data requests.
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * const form = new FormData();
70
+ * form.append('name', 'John');
71
+ * form.append('file', new Blob(['content']), 'file.txt');
72
+ * await fetch('/upload', { method: 'POST', body: form });
73
+ * ```
74
+ */
75
+ class FormData {
76
+ constructor();
77
+
78
+ /**
79
+ * Appends a new value to an existing key, or adds the key if it doesn't exist.
80
+ * @param name The field name.
81
+ * @param value The field value.
82
+ * @param filename Optional filename for Blob values.
83
+ */
84
+ append(name: string, value: string | Blob, filename?: string): void;
85
+
86
+ /** Deletes all values associated with a given key. */
87
+ delete(name: string): void;
88
+
89
+ /** Returns the first value associated with a given key. */
90
+ get(name: string): string | File | null;
91
+
92
+ /** Returns all values associated with a given key. */
93
+ getAll(name: string): (string | File)[];
94
+
95
+ /** Returns whether a given key exists. */
96
+ has(name: string): boolean;
97
+
98
+ /**
99
+ * Sets a value for a key, replacing any existing values.
100
+ * @param name The field name.
101
+ * @param value The field value.
102
+ * @param filename Optional filename for Blob values.
103
+ */
104
+ set(name: string, value: string | Blob, filename?: string): void;
105
+
106
+ /** Executes a callback for each key/value pair. */
107
+ forEach(
108
+ callback: (value: string | File, key: string, parent: FormData) => void
109
+ ): void;
110
+
111
+ /** Returns an iterator of all key/value pairs. */
112
+ entries(): IterableIterator<[string, string | File]>;
113
+
114
+ /** Returns an iterator of all keys. */
115
+ keys(): IterableIterator<string>;
116
+
117
+ /** Returns an iterator of all values. */
118
+ values(): IterableIterator<string | File>;
20
119
 
21
- declare class FormData {
22
- constructor();
23
- append(name: string, value: string | Blob, filename?: string): void;
24
- delete(name: string): void;
25
- get(name: string): string | File | null;
26
- getAll(name: string): (string | File)[];
27
- has(name: string): boolean;
28
- set(name: string, value: string | Blob, filename?: string): void;
29
- forEach(
30
- callback: (value: string | File, key: string, parent: FormData) => void
31
- ): void;
32
- entries(): IterableIterator<[string, string | File]>;
33
- keys(): IterableIterator<string>;
34
- values(): IterableIterator<string | File>;
35
- [Symbol.iterator](): IterableIterator<[string, string | File]>;
120
+ [Symbol.iterator](): IterableIterator<[string, string | File]>;
121
+ }
36
122
  }
@@ -1,15 +1,59 @@
1
1
  // Console API types
2
2
 
3
- interface Console {
4
- log(...args: unknown[]): void;
5
- info(...args: unknown[]): void;
6
- warn(...args: unknown[]): void;
7
- error(...args: unknown[]): void;
8
- debug(...args: unknown[]): void;
9
- trace(...args: unknown[]): void;
10
- time(label?: string): void;
11
- timeEnd(label?: string): void;
12
- timeLog(label?: string, ...args: unknown[]): void;
13
- }
3
+ export {};
4
+
5
+ declare global {
6
+ /**
7
+ * Provides logging functionality for debugging and diagnostics.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * console.log('Hello, World!');
12
+ * console.error('Something went wrong:', error);
13
+ * console.time('fetch');
14
+ * await fetch(url);
15
+ * console.timeEnd('fetch'); // "fetch: 123ms"
16
+ * ```
17
+ */
18
+ interface Console {
19
+ /** Outputs a message at the "log" level. */
20
+ log(...args: unknown[]): void;
21
+
22
+ /** Outputs a message at the "info" level. */
23
+ info(...args: unknown[]): void;
24
+
25
+ /** Outputs a message at the "warn" level. */
26
+ warn(...args: unknown[]): void;
27
+
28
+ /** Outputs a message at the "error" level. */
29
+ error(...args: unknown[]): void;
30
+
31
+ /** Outputs a message at the "debug" level. */
32
+ debug(...args: unknown[]): void;
14
33
 
15
- declare var console: Console;
34
+ /** Outputs a stack trace. */
35
+ trace(...args: unknown[]): void;
36
+
37
+ /**
38
+ * Starts a timer with the given label.
39
+ * @param label The timer label (default: "default").
40
+ */
41
+ time(label?: string): void;
42
+
43
+ /**
44
+ * Stops a timer and logs the elapsed time.
45
+ * @param label The timer label (default: "default").
46
+ */
47
+ timeEnd(label?: string): void;
48
+
49
+ /**
50
+ * Logs the current elapsed time without stopping the timer.
51
+ * @param label The timer label (default: "default").
52
+ * @param args Additional values to log.
53
+ */
54
+ timeLog(label?: string, ...args: unknown[]): void;
55
+ }
56
+
57
+ /** Global console object for logging. */
58
+ var console: Console;
59
+ }