@@ -12,20 +12,36 @@ Instead of defining activity implementations inline, you can extract types for r
1212
1313``` typescript
1414import type { ActivityHandlers } from ' @temporal-contract/worker/activity' ;
15+ import { declareActivitiesHandler , ActivityError } from ' @temporal-contract/worker/activity' ;
16+ import { Future , Result } from ' @swan-io/boxed' ;
1517import { orderContract } from ' ./contract' ;
1618
1719// Extract all activity handler types from contract
1820type OrderActivityHandlers = ActivityHandlers <typeof orderContract >;
1921
20- // Implement activities with explicit types
21- const sendEmail: OrderActivityHandlers [' sendEmail' ] = async ({ to , body }) => {
22- await emailService .send ({ to , body });
23- return { sent: true };
22+ // Implement activities with explicit types using Future/Result pattern
23+ const sendEmail: OrderActivityHandlers [' sendEmail' ] = ({ to , body }) => {
24+ return Future .fromPromise (emailService .send ({ to , body }))
25+ .mapError ((error ) =>
26+ new ActivityError (
27+ ' EMAIL_FAILED' ,
28+ error instanceof Error ? error .message : ' Failed to send email' ,
29+ error
30+ )
31+ )
32+ .mapOk (() => ({ sent: true }));
2433};
2534
26- const processPayment: OrderActivityHandlers [' processPayment' ] = async ({ amount }) => {
27- const txId = await paymentGateway .charge (amount );
28- return { transactionId: txId };
35+ const processPayment: OrderActivityHandlers [' processPayment' ] = ({ amount }) => {
36+ return Future .fromPromise (paymentGateway .charge (amount ))
37+ .mapError ((error ) =>
38+ new ActivityError (
39+ ' PAYMENT_FAILED' ,
40+ error instanceof Error ? error .message : ' Payment failed' ,
41+ error
42+ )
43+ )
44+ .mapOk ((txId ) => ({ transactionId: txId }));
2945};
3046
3147// Use in handler
@@ -77,26 +93,44 @@ Implement activities in separate files:
7793``` typescript
7894// activities/email.ts
7995import type { ActivityHandlers } from ' @temporal-contract/worker/activity' ;
96+ import { ActivityError } from ' @temporal-contract/worker/activity' ;
97+ import { Future , Result } from ' @swan-io/boxed' ;
8098import { orderContract } from ' ../contracts/order.contract' ;
8199
82100type Handlers = ActivityHandlers <typeof orderContract >;
83101
84- export const sendEmail: Handlers [' sendEmail' ] = async ({ to , body }) => {
85- await emailService .send ({ to , body });
86- return { sent: true };
102+ export const sendEmail: Handlers [' sendEmail' ] = ({ to , body }) => {
103+ return Future .fromPromise (emailService .send ({ to , body }))
104+ .mapError ((error ) =>
105+ new ActivityError (
106+ ' EMAIL_FAILED' ,
107+ error instanceof Error ? error .message : ' Failed to send email' ,
108+ error
109+ )
110+ )
111+ .mapOk (() => ({ sent: true }));
87112};
88113```
89114
90115``` typescript
91116// activities/payment.ts
92117import type { ActivityHandlers } from ' @temporal-contract/worker/activity' ;
118+ import { ActivityError } from ' @temporal-contract/worker/activity' ;
119+ import { Future , Result } from ' @swan-io/boxed' ;
93120import { orderContract } from ' ../contracts/order.contract' ;
94121
95122type Handlers = ActivityHandlers <typeof orderContract >;
96123
97- export const processPayment: Handlers [' processPayment' ] = async ({ amount }) => {
98- const txId = await paymentGateway .charge (amount );
99- return { transactionId: txId };
124+ export const processPayment: Handlers [' processPayment' ] = ({ amount }) => {
125+ return Future .fromPromise (paymentGateway .charge (amount ))
126+ .mapError ((error ) =>
127+ new ActivityError (
128+ ' PAYMENT_FAILED' ,
129+ error instanceof Error ? error .message : ' Payment failed' ,
130+ error
131+ )
132+ )
133+ .mapOk ((txId ) => ({ transactionId: txId }));
100134};
101135```
102136
@@ -165,13 +199,14 @@ Mock activities with correct types:
165199
166200``` typescript
167201import type { ActivityHandlers } from ' @temporal-contract/worker/activity' ;
202+ import { Future , Result } from ' @swan-io/boxed' ;
168203
169204type Handlers = ActivityHandlers <typeof orderContract >;
170205
171206// Create mock activities for testing
172207const mockActivities: Handlers = {
173- sendEmail : async ({ to , body }) => ( { sent: true }),
174- processPayment : async ({ amount }) => ( { transactionId: ' TEST-TXN' })
208+ sendEmail : ({ to , body }) => Future . value ( Result . Ok ( { sent: true }) ),
209+ processPayment : ({ amount }) => Future . value ( Result . Ok ( { transactionId: ' TEST-TXN' }) )
175210};
176211
177212// Use in tests
0 commit comments