better-cf 1.0.0 → 2.0.0

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.
@@ -0,0 +1,239 @@
1
+ import { z } from 'zod';
2
+
3
+ interface BetterCfGeneratedBindings {
4
+ }
5
+ interface BetterCfGeneratedApi {
6
+ }
7
+ interface BetterCfAutoEnv extends BetterCfGeneratedBindings {
8
+ [binding: string]: unknown;
9
+ }
10
+ type RuntimeEnv<E> = E & BetterCfGeneratedBindings;
11
+ type RuntimeApi = BetterCfGeneratedApi;
12
+ type Duration = number | `${number}s` | `${number}m` | `${number}h`;
13
+ type ContentType = 'json' | 'text' | 'bytes' | 'v8';
14
+ interface SendOptions {
15
+ delay?: Duration;
16
+ contentType?: ContentType;
17
+ }
18
+ interface SendBatchEntry<T> {
19
+ data: T;
20
+ delay?: Duration;
21
+ contentType?: ContentType;
22
+ }
23
+ interface BaseExecutionContext<E> {
24
+ env: RuntimeEnv<E>;
25
+ executionCtx: ExecutionContext;
26
+ api: RuntimeApi;
27
+ }
28
+ interface WorkerContext<E> extends BaseExecutionContext<E> {
29
+ }
30
+ interface QueueMessageContext<E> extends BaseExecutionContext<E> {
31
+ message: {
32
+ id: string;
33
+ timestamp: Date;
34
+ attempts: number;
35
+ queue: string;
36
+ };
37
+ }
38
+ interface QueueBatchContext<E> extends BaseExecutionContext<E> {
39
+ batch: {
40
+ queue: string;
41
+ receivedAt: Date;
42
+ firstMessageTimestamp?: Date;
43
+ ackAll: () => void;
44
+ retryAll: (options?: {
45
+ delaySeconds?: number;
46
+ }) => void;
47
+ };
48
+ }
49
+ interface DurableBaseContext<E> extends BaseExecutionContext<E> {
50
+ state: DurableObjectState;
51
+ storage: DurableObjectStorage;
52
+ sql: DurableObjectStorage['sql'];
53
+ }
54
+ interface DurableFetchContext<E> extends DurableBaseContext<E> {
55
+ request: Request;
56
+ }
57
+ interface DurableAlarmContext<E> extends DurableBaseContext<E> {
58
+ alarmInfo: AlarmInvocationInfo;
59
+ }
60
+ interface DurableWebSocketConnectContext<E, TAttachment> extends DurableBaseContext<E> {
61
+ request: Request;
62
+ client: WebSocket;
63
+ server: WebSocket;
64
+ accept: (options?: {
65
+ tags?: string[];
66
+ attachment?: TAttachment;
67
+ }) => void;
68
+ }
69
+ interface DurableWebSocketEventContext<E, TAttachment> extends DurableBaseContext<E> {
70
+ socket: WebSocket;
71
+ attachment: TAttachment | undefined;
72
+ }
73
+ interface QueueCommonConfig {
74
+ retry?: number;
75
+ retryDelay?: Duration;
76
+ deadLetter?: string;
77
+ deliveryDelay?: Duration;
78
+ visibilityTimeout?: Duration;
79
+ batch?: {
80
+ maxSize?: number;
81
+ timeout?: Duration;
82
+ maxConcurrency?: number;
83
+ };
84
+ }
85
+ interface PullConsumerConfig {
86
+ type: 'http_pull';
87
+ visibilityTimeout?: Duration;
88
+ }
89
+ interface QueueConfig<TSchema extends z.ZodTypeAny> extends QueueCommonConfig {
90
+ args: TSchema;
91
+ description?: string;
92
+ consumer?: {
93
+ type?: 'worker';
94
+ } | PullConsumerConfig;
95
+ }
96
+ interface QueueJobConfig<TSchema extends z.ZodTypeAny> {
97
+ args: TSchema;
98
+ description?: string;
99
+ }
100
+ type MultiJobQueueConfig<TConfig extends Record<string, unknown>> = QueueCommonConfig & {
101
+ description?: string;
102
+ consumer?: never;
103
+ args?: never;
104
+ } & TConfig;
105
+ interface QueueMessageConsumerConfig<E, TMessage> {
106
+ description?: string;
107
+ handler: (ctx: QueueMessageContext<E>, args: TMessage) => Promise<void> | void;
108
+ failure?: (ctx: QueueMessageContext<E>, args: TMessage | null, error: Error) => Promise<void> | void;
109
+ }
110
+ interface QueueBatchConsumerConfig<E, TMessage> {
111
+ description?: string;
112
+ handler: (ctx: QueueBatchContext<E>, args: ConsumerBatchEntry<TMessage>[]) => Promise<void> | void;
113
+ failure?: (ctx: QueueBatchContext<E>, args: ConsumerBatchEntry<TMessage>[] | null, error: Error) => Promise<void> | void;
114
+ }
115
+ interface ConsumerBatchEntry<T> {
116
+ data: T;
117
+ id: string;
118
+ timestamp: Date;
119
+ attempts: number;
120
+ }
121
+ interface DurableObjectConfig<TKeySchema extends z.ZodTypeAny> {
122
+ name: string;
123
+ key: TKeySchema;
124
+ version?: number;
125
+ description?: string;
126
+ }
127
+ interface DurableFnConfig<E, TArgsSchema extends z.ZodTypeAny, TReturn> {
128
+ args: TArgsSchema;
129
+ returns?: z.ZodType<TReturn>;
130
+ description?: string;
131
+ handler: (ctx: DurableBaseContext<E>, args: z.infer<TArgsSchema>) => Promise<TReturn> | TReturn;
132
+ }
133
+ interface DurableFetchConfig<E> {
134
+ description?: string;
135
+ handler: (ctx: DurableFetchContext<E>) => Promise<Response> | Response;
136
+ }
137
+ interface DurableAlarmConfig<E> {
138
+ description?: string;
139
+ handler: (ctx: DurableAlarmContext<E>) => Promise<void> | void;
140
+ }
141
+ interface DurableInitConfig<E> {
142
+ description?: string;
143
+ handler: (ctx: DurableBaseContext<E>) => Promise<void> | void;
144
+ }
145
+ interface DurableWebSocketConfig<E, TAttachment = unknown> {
146
+ description?: string;
147
+ connect?: (ctx: DurableWebSocketConnectContext<E, TAttachment>) => Promise<Response | void> | Response | void;
148
+ message?: (ctx: DurableWebSocketEventContext<E, TAttachment>, message: string | ArrayBuffer) => Promise<void> | void;
149
+ close?: (ctx: DurableWebSocketEventContext<E, TAttachment>, code: number, reason: string, wasClean: boolean) => Promise<void> | void;
150
+ error?: (ctx: DurableWebSocketEventContext<E, TAttachment>, error: unknown) => Promise<void> | void;
151
+ serializeAttachment?: (attachment: TAttachment) => unknown;
152
+ hydrateAttachment?: (attachment: unknown) => TAttachment;
153
+ }
154
+ interface WorkerConfig<E> {
155
+ fetch: (request: Request, ctx: WorkerContext<E>) => Promise<Response>;
156
+ scheduled?: (event: ScheduledEvent, ctx: WorkerContext<E>) => Promise<void>;
157
+ }
158
+ interface WorkerEntrypoint<E> {
159
+ fetch(request: Request, env: E, executionCtx: ExecutionContext): Promise<Response>;
160
+ scheduled?: (event: ScheduledEvent, env: E, executionCtx: ExecutionContext) => Promise<void>;
161
+ }
162
+ interface QueueHandle<E, TMessage> {
163
+ message(config: QueueMessageConsumerConfig<E, TMessage>): QueueConsumerRef<TMessage>;
164
+ batch(config: QueueBatchConsumerConfig<E, TMessage>): QueueBatchConsumerRef<TMessage>;
165
+ }
166
+ interface QueueJobHandle<E, TMessage> {
167
+ message(config: QueueMessageConsumerConfig<E, TMessage>): QueueJobConsumerRef<TMessage>;
168
+ }
169
+ type MultiJobQueueHandle<E, TJobs extends Record<string, QueueJobConfig<z.ZodTypeAny>>> = {
170
+ [K in keyof TJobs]: QueueJobHandle<E, z.infer<TJobs[K]['args']>>;
171
+ };
172
+ interface DurableObjectHandle<E, TKey> {
173
+ fn<TArgsSchema extends z.ZodTypeAny, TReturn>(config: DurableFnConfig<E, TArgsSchema, TReturn>): DurableFnRef<TKey, z.infer<TArgsSchema>, TReturn>;
174
+ internal<TArgsSchema extends z.ZodTypeAny, TReturn>(config: DurableFnConfig<E, TArgsSchema, TReturn>): DurableFnRef<TKey, z.infer<TArgsSchema>, TReturn>;
175
+ fetch(config: DurableFetchConfig<E>): DurableFetchRef;
176
+ alarm(config: DurableAlarmConfig<E>): DurableAlarmRef;
177
+ init(config: DurableInitConfig<E>): DurableInitRef;
178
+ websocket<TAttachment = unknown>(config: DurableWebSocketConfig<E, TAttachment>): DurableWebSocketRef<TAttachment>;
179
+ }
180
+ interface QueueConsumerRef<TMessage> {
181
+ readonly __type?: 'queue-message';
182
+ readonly __message?: TMessage;
183
+ }
184
+ interface QueueBatchConsumerRef<TMessage> {
185
+ readonly __type?: 'queue-batch';
186
+ readonly __message?: TMessage;
187
+ }
188
+ interface QueueJobConsumerRef<TMessage> {
189
+ readonly __type?: 'queue-job-message';
190
+ readonly __message?: TMessage;
191
+ }
192
+ interface DurableFnRef<TKey, TArgs, TReturn> {
193
+ readonly __type?: 'durable-fn';
194
+ readonly __key?: TKey;
195
+ readonly __args?: TArgs;
196
+ readonly __return?: TReturn;
197
+ }
198
+ interface DurableFetchRef {
199
+ readonly __type?: 'durable-fetch';
200
+ }
201
+ interface DurableAlarmRef {
202
+ readonly __type?: 'durable-alarm';
203
+ }
204
+ interface DurableInitRef {
205
+ readonly __type?: 'durable-init';
206
+ }
207
+ interface DurableWebSocketRef<TAttachment> {
208
+ readonly __type?: 'durable-websocket';
209
+ readonly __attachment?: TAttachment;
210
+ }
211
+ type QueuePayload<TQueue> = TQueue extends QueueHandle<any, infer TMessage> ? TMessage : never;
212
+ type QueueJobPayload<TJob> = TJob extends QueueJobHandle<any, infer TMessage> ? TMessage : never;
213
+ type DurableObjectKey<TObject> = TObject extends DurableObjectHandle<any, infer TKey> ? TKey : never;
214
+ type DurableFnKey<TFn> = TFn extends DurableFnRef<infer TKey, any, any> ? TKey : never;
215
+ type DurableFnArgs<TFn> = TFn extends DurableFnRef<any, infer TArgs, any> ? TArgs : never;
216
+ type DurableFnReturn<TFn> = TFn extends DurableFnRef<any, any, infer TReturn> ? TReturn : never;
217
+ interface DefineDurableObject<E> {
218
+ <TKeySchema extends z.ZodTypeAny>(config: DurableObjectConfig<TKeySchema>): DurableObjectHandle<E, z.infer<TKeySchema>>;
219
+ }
220
+ interface DefineQueue<E> {
221
+ <TSchema extends z.ZodTypeAny>(config: QueueConfig<TSchema>): QueueHandle<E, z.infer<TSchema>>;
222
+ }
223
+ interface DefineQueues<E> {
224
+ <const TConfig extends Record<string, unknown>>(config: MultiJobQueueConfig<TConfig>): MultiJobQueueHandle<E, ExtractQueueJobs<TConfig>>;
225
+ }
226
+ type ExtractQueueJobs<TConfig extends Record<string, unknown>> = {
227
+ [K in keyof TConfig as TConfig[K] extends QueueJobConfig<z.ZodTypeAny> ? K : never]: TConfig[K] extends QueueJobConfig<z.ZodTypeAny> ? TConfig[K] : never;
228
+ };
229
+ interface DefineWorker<E> {
230
+ (config: WorkerConfig<E>): WorkerEntrypoint<E>;
231
+ }
232
+ interface BetterCfSDK<E> {
233
+ defineDurableObject: DefineDurableObject<E>;
234
+ defineQueue: DefineQueue<E>;
235
+ defineQueues: DefineQueues<E>;
236
+ defineWorker: DefineWorker<E>;
237
+ }
238
+
239
+ export type { MultiJobQueueHandle as A, BetterCfAutoEnv as B, ConsumerBatchEntry as C, DefineDurableObject as D, QueueBatchConsumerRef as E, QueueBatchContext as F, QueueCommonConfig as G, QueueConfig as H, QueueConsumerRef as I, QueueHandle as J, QueueJobConfig as K, QueueJobConsumerRef as L, MultiJobQueueConfig as M, QueueJobHandle as N, QueueJobPayload as O, PullConsumerConfig as P, QueueBatchConsumerConfig as Q, QueueMessageConsumerConfig as R, QueueMessageContext as S, QueuePayload as T, RuntimeApi as U, RuntimeEnv as V, SendBatchEntry as W, SendOptions as X, WorkerConfig as Y, WorkerContext as Z, WorkerEntrypoint as _, BetterCfSDK as a, BetterCfGeneratedApi as b, BetterCfGeneratedBindings as c, ContentType as d, DefineQueue as e, DefineQueues as f, DefineWorker as g, DurableAlarmConfig as h, DurableBaseContext as i, DurableFetchConfig as j, DurableFetchRef as k, DurableFnArgs as l, DurableFnConfig as m, DurableFnKey as n, DurableFnRef as o, DurableFnReturn as p, DurableInitConfig as q, DurableInitRef as r, DurableObjectConfig as s, DurableObjectHandle as t, DurableObjectKey as u, DurableWebSocketConfig as v, DurableWebSocketConnectContext as w, DurableWebSocketEventContext as x, DurableWebSocketRef as y, Duration as z };
package/package.json CHANGED
@@ -1,10 +1,18 @@
1
1
  {
2
2
  "name": "better-cf",
3
- "version": "1.0.0",
4
- "description": "Type-safe Cloudflare Queue SDK and CLI",
3
+ "version": "2.0.0",
4
+ "description": "Functional, type-safe Cloudflare SDK suite for Durable Objects and Queues",
5
+ "keywords": [
6
+ "cloudflare",
7
+ "durable-objects",
8
+ "queues",
9
+ "workers",
10
+ "typescript",
11
+ "zod"
12
+ ],
5
13
  "repository": {
6
14
  "type": "git",
7
- "url": "https://github.com/imaxisXD/Better-Cf-Sdk"
15
+ "url": "git+https://github.com/imaxisXD/Better-Cf-Sdk.git"
8
16
  },
9
17
  "homepage": "https://github.com/imaxisXD/Better-Cf-Sdk#readme",
10
18
  "bugs": {
@@ -26,6 +34,14 @@
26
34
  "types": "./dist/queue/internal.d.ts",
27
35
  "default": "./dist/queue/internal.js"
28
36
  },
37
+ "./durable-object": {
38
+ "types": "./dist/durable-object/index.d.ts",
39
+ "default": "./dist/durable-object/index.js"
40
+ },
41
+ "./durable-object/internal": {
42
+ "types": "./dist/durable-object/internal.d.ts",
43
+ "default": "./dist/durable-object/internal.js"
44
+ },
29
45
  "./testing": {
30
46
  "types": "./dist/testing/index.d.ts",
31
47
  "default": "./dist/testing/index.js"
@@ -60,12 +76,16 @@
60
76
  "release:publish": "npm run build && npm publish --access public --tag ${NPM_TAG:-latest}"
61
77
  },
62
78
  "dependencies": {
79
+ "@clack/prompts": "^0.10.1",
63
80
  "@iarna/toml": "^2.2.5",
64
81
  "chokidar": "^3.6.0",
65
82
  "commander": "^12.1.0",
66
83
  "jsonc-parser": "^3.3.1",
84
+ "magic-string": "^0.30.17",
85
+ "oxc-parser": "^0.81.0",
86
+ "package-manager-detector": "^0.2.11",
67
87
  "picocolors": "^1.1.1",
68
- "ts-morph": "^27.0.2"
88
+ "tinyexec": "^0.3.2"
69
89
  },
70
90
  "peerDependencies": {
71
91
  "@cloudflare/workers-types": ">=4.20240117.0",