@@ -75,12 +75,12 @@ export const WorkerConfigSchema = z
7575 } ,
7676 ) ;
7777
78- export interface OnCreateOptions {
79- input ?: unknown ;
78+ export interface OnCreateOptions < I > {
79+ input ?: I ;
8080}
8181
82- export interface CreateStateOptions {
83- input ?: unknown ;
82+ export interface CreateStateOptions < I > {
83+ input ?: I ;
8484}
8585
8686export interface OnConnectOptions < CP > {
@@ -98,12 +98,12 @@ export interface OnConnectOptions<CP> {
9898// This must have only one or the other or else S will not be able to be inferred
9999//
100100// Data returned from this handler will be available on `c.state`.
101- type CreateState < S , CP , CS , V > =
101+ type CreateState < S , CP , CS , V , I , AD > =
102102 | { state : S }
103103 | {
104104 createState : (
105- c : WorkerContext < undefined , undefined , undefined , undefined > ,
106- opts : CreateStateOptions ,
105+ c : WorkerContext < undefined , undefined , undefined , undefined , undefined , undefined > ,
106+ opts : CreateStateOptions < I > ,
107107 ) => S | Promise < S > ;
108108 }
109109 | Record < never , never > ;
@@ -113,11 +113,11 @@ type CreateState<S, CP, CS, V> =
113113// This must have only one or the other or else S will not be able to be inferred
114114//
115115// Data returned from this handler will be available on `c.conn.state`.
116- type CreateConnState < S , CP , CS , V > =
116+ type CreateConnState < S , CP , CS , V , I , AD > =
117117 | { connState : CS }
118118 | {
119119 createConnState : (
120- c : WorkerContext < undefined , undefined , undefined , undefined > ,
120+ c : WorkerContext < undefined , undefined , undefined , undefined , undefined , undefined > ,
121121 opts : OnConnectOptions < CP > ,
122122 ) => CS | Promise < CS > ;
123123 }
@@ -129,7 +129,7 @@ type CreateConnState<S, CP, CS, V> =
129129/**
130130 * @experimental
131131 */
132- type CreateVars < S , CP , CS , V > =
132+ type CreateVars < S , CP , CS , V , I , AD > =
133133 | {
134134 /**
135135 * @experimental
@@ -141,20 +141,23 @@ type CreateVars<S, CP, CS, V> =
141141 * @experimental
142142 */
143143 createVars : (
144- c : WorkerContext < undefined , undefined , undefined , undefined > ,
144+ c : WorkerContext < undefined , undefined , undefined , undefined , undefined , undefined > ,
145145 driverCtx : unknown ,
146146 ) => V | Promise < V > ;
147147 }
148148 | Record < never , never > ;
149149
150- export interface Actions < S , CP , CS , V > {
151- [ Action : string ] : ( c : ActionContext < S , CP , CS , V > , ...args : any [ ] ) => any ;
150+ export interface Actions < S , CP , CS , V , I , AD > {
151+ [ Action : string ] : (
152+ c : ActionContext < S , CP , CS , V , I , AD > ,
153+ ...args : any [ ]
154+ ) => any ;
152155}
153156
154- //export type WorkerConfig<S, CP, CS, V> = BaseWorkerConfig<S, CP, CS, V> &
155- // WorkerConfigLifecycle<S, CP, CS, V> &
156- // CreateState<S, CP, CS, V> &
157- // CreateConnState<S, CP, CS, V>;
157+ //export type WorkerConfig<S, CP, CS, V, I, AD > = BaseWorkerConfig<S, CP, CS, V, I, AD > &
158+ // WorkerConfigLifecycle<S, CP, CS, V, I, AD > &
159+ // CreateState<S, CP, CS, V, I, AD > &
160+ // CreateConnState<S, CP, CS, V, I, AD >;
158161
159162/**
160163 * @experimental
@@ -170,7 +173,15 @@ interface OnAuthOptions<CP> {
170173 params : CP ;
171174}
172175
173- interface BaseWorkerConfig < S , CP , CS , V , R extends Actions < S , CP , CS , V > > {
176+ interface BaseWorkerConfig <
177+ S ,
178+ CP ,
179+ CS ,
180+ V ,
181+ I ,
182+ AD ,
183+ R extends Actions < S , CP , CS , V , I , AD > ,
184+ > {
174185 /**
175186 * Called on the HTTP server before clients can interact with the worker.
176187 *
@@ -196,7 +207,7 @@ interface BaseWorkerConfig<S, CP, CS, V, R extends Actions<S, CP, CS, V>> {
196207 * @returns Authentication data to attach to connections (must be serializable)
197208 * @throws Throw an error to deny access to the worker
198209 */
199- onAuth ?: ( opts : OnAuthOptions < CP > ) => unknown | Promise < unknown > ;
210+ onAuth ?: ( opts : OnAuthOptions < CP > ) => AD | Promise < AD > ;
200211
201212 /**
202213 * Called when the worker is first initialized.
@@ -205,8 +216,8 @@ interface BaseWorkerConfig<S, CP, CS, V, R extends Actions<S, CP, CS, V>> {
205216 * This is called before any other lifecycle hooks.
206217 */
207218 onCreate ?: (
208- c : WorkerContext < S , CP , CS , V > ,
209- opts : OnCreateOptions ,
219+ c : WorkerContext < S , CP , CS , V , I , AD > ,
220+ opts : OnCreateOptions < I > ,
210221 ) => void | Promise < void > ;
211222
212223 /**
@@ -217,7 +228,7 @@ interface BaseWorkerConfig<S, CP, CS, V, R extends Actions<S, CP, CS, V>> {
217228 *
218229 * @returns Void or a Promise that resolves when startup is complete
219230 */
220- onStart ?: ( c : WorkerContext < S , CP , CS , V > ) => void | Promise < void > ;
231+ onStart ?: ( c : WorkerContext < S , CP , CS , V , I , AD > ) => void | Promise < void > ;
221232
222233 /**
223234 * Called when the worker's state changes.
@@ -227,7 +238,7 @@ interface BaseWorkerConfig<S, CP, CS, V, R extends Actions<S, CP, CS, V>> {
227238 *
228239 * @param newState The updated state
229240 */
230- onStateChange ?: ( c : WorkerContext < S , CP , CS , V > , newState : S ) => void ;
241+ onStateChange ?: ( c : WorkerContext < S , CP , CS , V , I , AD > , newState : S ) => void ;
231242
232243 /**
233244 * Called before a client connects to the worker.
@@ -250,7 +261,7 @@ interface BaseWorkerConfig<S, CP, CS, V, R extends Actions<S, CP, CS, V>> {
250261 * @throws Throw an error to reject the connection
251262 */
252263 onBeforeConnect ?: (
253- c : WorkerContext < S , CP , CS , V > ,
264+ c : WorkerContext < S , CP , CS , V , I , AD > ,
254265 opts : OnConnectOptions < CP > ,
255266 ) => void | Promise < void > ;
256267
@@ -264,8 +275,8 @@ interface BaseWorkerConfig<S, CP, CS, V, R extends Actions<S, CP, CS, V>> {
264275 * @returns Void or a Promise that resolves when connection handling is complete
265276 */
266277 onConnect ?: (
267- c : WorkerContext < S , CP , CS , V > ,
268- conn : Conn < S , CP , CS , V > ,
278+ c : WorkerContext < S , CP , CS , V , I , AD > ,
279+ conn : Conn < S , CP , CS , V , I , AD > ,
269280 ) => void | Promise < void > ;
270281
271282 /**
@@ -278,8 +289,8 @@ interface BaseWorkerConfig<S, CP, CS, V, R extends Actions<S, CP, CS, V>> {
278289 * @returns Void or a Promise that resolves when disconnect handling is complete
279290 */
280291 onDisconnect ?: (
281- c : WorkerContext < S , CP , CS , V > ,
282- conn : Conn < S , CP , CS , V > ,
292+ c : WorkerContext < S , CP , CS , V , I , AD > ,
293+ conn : Conn < S , CP , CS , V , I , AD > ,
283294 ) => void | Promise < void > ;
284295
285296 /**
@@ -295,7 +306,7 @@ interface BaseWorkerConfig<S, CP, CS, V, R extends Actions<S, CP, CS, V>> {
295306 * @returns The modified output to send to the client
296307 */
297308 onBeforeActionResponse ?: < Out > (
298- c : WorkerContext < S , CP , CS , V > ,
309+ c : WorkerContext < S , CP , CS , V , I , AD > ,
299310 name : string ,
300311 args : unknown [ ] ,
301312 output : Out ,
@@ -307,7 +318,7 @@ interface BaseWorkerConfig<S, CP, CS, V, R extends Actions<S, CP, CS, V>> {
307318// 1. Infer schema
308319// 2. Omit keys that we'll manually define (because of generics)
309320// 3. Define our own types that have generic constraints
310- export type WorkerConfig < S , CP , CS , V > = Omit <
321+ export type WorkerConfig < S , CP , CS , V , I , AD > = Omit <
311322 z . infer < typeof WorkerConfigSchema > ,
312323 | "actions"
313324 | "onAuth"
@@ -325,18 +336,20 @@ export type WorkerConfig<S, CP, CS, V> = Omit<
325336 | "vars"
326337 | "createVars"
327338> &
328- BaseWorkerConfig < S , CP , CS , V , Actions < S , CP , CS , V > > &
329- CreateState < S , CP , CS , V > &
330- CreateConnState < S , CP , CS , V > &
331- CreateVars < S , CP , CS , V > ;
339+ BaseWorkerConfig < S , CP , CS , V , I , AD , Actions < S , CP , CS , V , I , AD > > &
340+ CreateState < S , CP , CS , V , I , AD > &
341+ CreateConnState < S , CP , CS , V , I , AD > &
342+ CreateVars < S , CP , CS , V , I , AD > ;
332343
333344// See description on `WorkerConfig`
334345export type WorkerConfigInput <
335346 S ,
336347 CP ,
337348 CS ,
338349 V ,
339- R extends Actions < S , CP , CS , V > ,
350+ I ,
351+ AD ,
352+ R extends Actions < S , CP , CS , V , I , AD > ,
340353> = Omit <
341354 z . input < typeof WorkerConfigSchema > ,
342355 | "actions"
@@ -355,16 +368,31 @@ export type WorkerConfigInput<
355368 | "vars"
356369 | "createVars"
357370> &
358- BaseWorkerConfig < S , CP , CS , V , R > &
359- CreateState < S , CP , CS , V > &
360- CreateConnState < S , CP , CS , V > &
361- CreateVars < S , CP , CS , V > ;
371+ BaseWorkerConfig < S , CP , CS , V , I , AD , R > &
372+ CreateState < S , CP , CS , V , I , AD > &
373+ CreateConnState < S , CP , CS , V , I , AD > &
374+ CreateVars < S , CP , CS , V , I , AD > ;
362375
363376// For testing type definitions:
364- export function test < S , CP , CS , V , R extends Actions < S , CP , CS , V > > (
365- input : WorkerConfigInput < S , CP , CS , V , R > ,
366- ) : WorkerConfig < S , CP , CS , V > {
367- const config = WorkerConfigSchema . parse ( input ) as WorkerConfig < S , CP , CS , V > ;
377+ export function test <
378+ S ,
379+ CP ,
380+ CS ,
381+ V ,
382+ I ,
383+ AD ,
384+ R extends Actions < S , CP , CS , V , I , AD > ,
385+ > (
386+ input : WorkerConfigInput < S , CP , CS , V , I , AD , R > ,
387+ ) : WorkerConfig < S , CP , CS , V , I , AD > {
388+ const config = WorkerConfigSchema . parse ( input ) as WorkerConfig <
389+ S ,
390+ CP ,
391+ CS ,
392+ V ,
393+ I ,
394+ AD
395+ > ;
368396 return config ;
369397}
370398
0 commit comments