@temporal-contract/worker
v0.0.7
Published
Worker utilities with Result/Future pattern for implementing temporal-contract workflows and activities
Maintainers
Readme
@temporal-contract/worker
Type-safe worker implementation for Temporal
Installation
pnpm add @temporal-contract/worker @temporal-contract/contract @temporalio/workflow zodQuick Example
// activities.ts
import { declareActivitiesHandler } from '@temporal-contract/worker/activity';
export const activities = declareActivitiesHandler({
contract: myContract,
activities: {
sendEmail: async ({ to, body }) => ({ sent: true })
}
});
// workflows.ts
import { declareWorkflow } from '@temporal-contract/worker/workflow';
export const processOrder = declareWorkflow({
workflowName: 'processOrder',
contract: myContract,
implementation: async (context, input) => {
await context.activities.sendEmail({ to: '[email protected]', body: 'Done!' });
return { success: true };
}
});
// worker.ts
import { NativeConnection } from '@temporalio/worker';
import { createWorker } from '@temporal-contract/worker/worker';
import { activities } from './activities';
import myContract from './contract';
async function run() {
const connection = await NativeConnection.connect({
address: 'localhost:7233',
});
const worker = await createWorker({
contract: myContract,
connection,
workflowsPath: require.resolve('./workflows'),
activities,
});
await worker.run();
}
run().catch(console.error);Child Workflows
Execute child workflows with type-safe Future/Result pattern. Supports both same-contract and cross-contract child workflows:
// workflows.ts
import { declareWorkflow } from '@temporal-contract/worker/workflow';
export const parentWorkflow = declareWorkflow({
workflowName: 'parentWorkflow',
contract: myContract,
implementation: async (context, input) => {
// Execute child workflow from same contract and wait for result
const childResult = await context.executeChildWorkflow(myContract, 'processPayment', {
workflowId: `payment-${input.orderId}`,
args: { amount: input.totalAmount }
});
childResult.match({
Ok: (output) => console.log('Payment processed:', output),
Error: (error) => console.error('Payment failed:', error),
});
// Execute child workflow from another contract (another worker)
const notificationResult = await context.executeChildWorkflow(notificationContract, 'sendNotification', {
workflowId: `notification-${input.orderId}`,
args: { message: 'Order received' }
});
// Or start child workflow without waiting
const handleResult = await context.startChildWorkflow(myContract, 'sendEmail', {
workflowId: `email-${input.orderId}`,
args: { to: '[email protected]', body: 'Order received' }
});
handleResult.match({
Ok: async (handle) => {
// Can wait for result later
const result = await handle.result();
// ...
},
Error: (error) => console.error('Failed to start:', error),
});
return { success: true };
}
});Documentation
📖 Read the full documentation →
License
MIT
