@@ -34,6 +34,8 @@ import {
34
34
type WorkflowDefinition ,
35
35
type WorkflowSharedHandler ,
36
36
type Serde ,
37
+ type ArgType ,
38
+ type HandlerReturnType ,
37
39
serde ,
38
40
} from "@restatedev/restate-sdk-core" ;
39
41
import { TerminalError } from "./errors.js" ;
@@ -245,7 +247,7 @@ export enum HandlerKind {
245
247
WORKFLOW ,
246
248
}
247
249
248
- export type ServiceHandlerOpts = {
250
+ export type ServiceHandlerOpts < I , O > = {
249
251
/**
250
252
* Define the acceptable content-type. Wildcards can be used, e.g. `application/*` or `* / *`.
251
253
* If not provided, the `input.contentType` will be used instead.
@@ -263,7 +265,7 @@ export type ServiceHandlerOpts = {
263
265
* restate.serde.binary the skip serialization/deserialization altogether.
264
266
* in that case, the input parameter is a Uint8Array.
265
267
*/
266
- input ?: Serde < unknown > ;
268
+ input ?: Serde < I > ;
267
269
268
270
/**
269
271
* The Serde to use for serializing the output.
@@ -273,7 +275,7 @@ export type ServiceHandlerOpts = {
273
275
* restate.serde.binary the skip serialization/deserialization altogether.
274
276
* in that case, the output parameter is a Uint8Array.
275
277
*/
276
- output ?: Serde < unknown > ;
278
+ output ?: Serde < O > ;
277
279
278
280
/**
279
281
* An additional description for the handler, for documentation purposes.
@@ -286,7 +288,7 @@ export type ServiceHandlerOpts = {
286
288
metadata ?: Record < string , string > ;
287
289
} ;
288
290
289
- export type ObjectHandlerOpts = {
291
+ export type ObjectHandlerOpts < I , O > = {
290
292
/**
291
293
* Define the acceptable content-type. Wildcards can be used, e.g. `application/*` or `* / *`.
292
294
* If not provided, the `input.contentType` will be used instead.
@@ -304,7 +306,7 @@ export type ObjectHandlerOpts = {
304
306
* restate.serde.binary the skip serialization/deserialization altogether.
305
307
* in that case, the input parameter is a Uint8Array.
306
308
*/
307
- input ?: Serde < unknown > ;
309
+ input ?: Serde < I > ;
308
310
309
311
/**
310
312
* The Serde to use for serializing the output.
@@ -314,7 +316,7 @@ export type ObjectHandlerOpts = {
314
316
* restate.serde.binary the skip serialization/deserialization altogether.
315
317
* in that case, the output parameter is a Uint8Array.
316
318
*/
317
- output ?: Serde < unknown > ;
319
+ output ?: Serde < O > ;
318
320
319
321
/**
320
322
* An additional description for the handler, for documentation purposes.
@@ -327,7 +329,7 @@ export type ObjectHandlerOpts = {
327
329
metadata ?: Record < string , string > ;
328
330
} ;
329
331
330
- export type WorkflowHandlerOpts = {
332
+ export type WorkflowHandlerOpts < I , O > = {
331
333
/**
332
334
* Define the acceptable content-type. Wildcards can be used, e.g. `application/*` or `* / *`.
333
335
* If not provided, the `input.contentType` will be used instead.
@@ -345,7 +347,7 @@ export type WorkflowHandlerOpts = {
345
347
* restate.serde.binary the skip serialization/deserialization altogether.
346
348
* in that case, the input parameter is a Uint8Array.
347
349
*/
348
- input ?: Serde < unknown > ;
350
+ input ?: Serde < I > ;
349
351
350
352
/**
351
353
* The Serde to use for serializing the output.
@@ -355,7 +357,7 @@ export type WorkflowHandlerOpts = {
355
357
* restate.serde.binary the skip serialization/deserialization altogether.
356
358
* in that case, the output parameter is a Uint8Array.
357
359
*/
358
- output ?: Serde < unknown > ;
360
+ output ?: Serde < O > ;
359
361
360
362
/**
361
363
* An additional description for the handler, for documentation purposes.
@@ -374,7 +376,10 @@ export class HandlerWrapper {
374
376
public static from (
375
377
kind : HandlerKind ,
376
378
handler : Function ,
377
- opts ?: ServiceHandlerOpts | ObjectHandlerOpts | WorkflowHandlerOpts
379
+ opts ?:
380
+ | ServiceHandlerOpts < unknown , unknown >
381
+ | ObjectHandlerOpts < unknown , unknown >
382
+ | WorkflowHandlerOpts < unknown , unknown >
378
383
) : HandlerWrapper {
379
384
const inputSerde : Serde < unknown > = opts ?. input ?? defaultSerde ( ) ;
380
385
const outputSerde : Serde < unknown > = opts ?. output ?? defaultSerde ( ) ;
@@ -463,15 +468,15 @@ export namespace handlers {
463
468
* @param fn the actual handler code to execute
464
469
*/
465
470
export function handler < F > (
466
- opts : ServiceHandlerOpts ,
471
+ opts : ServiceHandlerOpts < ArgType < F > , HandlerReturnType < F > > ,
467
472
fn : ServiceHandler < F , Context >
468
473
) : F {
469
474
return HandlerWrapper . from ( HandlerKind . SERVICE , fn , opts ) . transpose ( ) ;
470
475
}
471
476
472
477
export namespace workflow {
473
478
export function workflow < F > (
474
- opts : WorkflowHandlerOpts ,
479
+ opts : WorkflowHandlerOpts < ArgType < F > , HandlerReturnType < F > > ,
475
480
fn : WorkflowHandler < F , WorkflowContext < any > >
476
481
) : F ;
477
482
@@ -480,13 +485,18 @@ export namespace handlers {
480
485
) : F ;
481
486
482
487
export function workflow < F > (
483
- optsOrFn : WorkflowHandlerOpts | WorkflowHandler < F , WorkflowContext < any > > ,
488
+ optsOrFn :
489
+ | WorkflowHandlerOpts < ArgType < F > , HandlerReturnType < F > >
490
+ | WorkflowHandler < F , WorkflowContext < any > > ,
484
491
fn ?: WorkflowHandler < F , WorkflowContext < any > >
485
492
) : F {
486
493
if ( typeof optsOrFn === "function" ) {
487
494
return HandlerWrapper . from ( HandlerKind . WORKFLOW , optsOrFn ) . transpose ( ) ;
488
495
}
489
- const opts = optsOrFn satisfies WorkflowHandlerOpts ;
496
+ const opts = optsOrFn satisfies WorkflowHandlerOpts <
497
+ ArgType < F > ,
498
+ HandlerReturnType < F >
499
+ > ;
490
500
if ( typeof fn !== "function" ) {
491
501
throw new TypeError ( "The second argument must be a function" ) ;
492
502
}
@@ -503,7 +513,7 @@ export namespace handlers {
503
513
* @param fn the handler to execute
504
514
*/
505
515
export function shared < F > (
506
- opts : WorkflowHandlerOpts ,
516
+ opts : WorkflowHandlerOpts < ArgType < F > , HandlerReturnType < F > > ,
507
517
fn : WorkflowSharedHandler < F , WorkflowSharedContext < any > >
508
518
) : F ;
509
519
@@ -531,14 +541,17 @@ export namespace handlers {
531
541
*/
532
542
export function shared < F > (
533
543
optsOrFn :
534
- | WorkflowHandlerOpts
544
+ | WorkflowHandlerOpts < ArgType < F > , HandlerReturnType < F > >
535
545
| WorkflowSharedHandler < F , WorkflowSharedContext < any > > ,
536
546
fn ?: WorkflowSharedHandler < F , WorkflowSharedContext < any > >
537
547
) : F {
538
548
if ( typeof optsOrFn === "function" ) {
539
549
return HandlerWrapper . from ( HandlerKind . SHARED , optsOrFn ) . transpose ( ) ;
540
550
}
541
- const opts = optsOrFn satisfies ObjectHandlerOpts ;
551
+ const opts = optsOrFn satisfies ObjectHandlerOpts <
552
+ ArgType < F > ,
553
+ HandlerReturnType < F >
554
+ > ;
542
555
if ( typeof fn !== "function" ) {
543
556
throw new TypeError ( "The second argument must be a function" ) ;
544
557
}
@@ -556,7 +569,7 @@ export namespace handlers {
556
569
* @param fn the handler to execute
557
570
*/
558
571
export function exclusive < F > (
559
- opts : ObjectHandlerOpts ,
572
+ opts : ObjectHandlerOpts < ArgType < F > , HandlerReturnType < F > > ,
560
573
fn : ObjectHandler < F , ObjectContext < any > >
561
574
) : F ;
562
575
@@ -588,13 +601,18 @@ export namespace handlers {
588
601
* @param fn the handler to execute
589
602
*/
590
603
export function exclusive < F > (
591
- optsOrFn : ObjectHandlerOpts | ObjectHandler < F , ObjectContext < any > > ,
604
+ optsOrFn :
605
+ | ObjectHandlerOpts < ArgType < F > , HandlerReturnType < F > >
606
+ | ObjectHandler < F , ObjectContext < any > > ,
592
607
fn ?: ObjectHandler < F , ObjectContext < any > >
593
608
) : F {
594
609
if ( typeof optsOrFn === "function" ) {
595
610
return HandlerWrapper . from ( HandlerKind . EXCLUSIVE , optsOrFn ) . transpose ( ) ;
596
611
}
597
- const opts = optsOrFn satisfies ObjectHandlerOpts ;
612
+ const opts = optsOrFn satisfies ObjectHandlerOpts <
613
+ ArgType < F > ,
614
+ HandlerReturnType < F >
615
+ > ;
598
616
if ( typeof fn !== "function" ) {
599
617
throw new TypeError ( "The second argument must be a function" ) ;
600
618
}
@@ -613,7 +631,7 @@ export namespace handlers {
613
631
* @param fn the handler to execute
614
632
*/
615
633
export function shared < F > (
616
- opts : ObjectHandlerOpts ,
634
+ opts : ObjectHandlerOpts < ArgType < F > , HandlerReturnType < F > > ,
617
635
fn : ObjectSharedHandler < F , ObjectSharedContext < any > >
618
636
) : F ;
619
637
@@ -645,14 +663,17 @@ export namespace handlers {
645
663
*/
646
664
export function shared < F > (
647
665
optsOrFn :
648
- | ObjectHandlerOpts
666
+ | ObjectHandlerOpts < ArgType < F > , HandlerReturnType < F > >
649
667
| ObjectSharedHandler < F , ObjectSharedContext < any > > ,
650
668
fn ?: ObjectSharedHandler < F , ObjectSharedContext < any > >
651
669
) : F {
652
670
if ( typeof optsOrFn === "function" ) {
653
671
return HandlerWrapper . from ( HandlerKind . SHARED , optsOrFn ) . transpose ( ) ;
654
672
}
655
- const opts = optsOrFn satisfies ObjectHandlerOpts ;
673
+ const opts = optsOrFn satisfies ObjectHandlerOpts <
674
+ ArgType < F > ,
675
+ HandlerReturnType < F >
676
+ > ;
656
677
if ( typeof fn !== "function" ) {
657
678
throw new TypeError ( "The second argument must be a function" ) ;
658
679
}
0 commit comments