Skip to content

@temporal-contract/worker


@temporal-contract/worker / activity

activity

Classes

ActivityDefinitionNotFoundError

Defined in: packages/worker/src/errors.ts:20

Error thrown when an activity definition is not found in the contract

Extends

  • WorkerError

Constructors

Constructor
ts
new ActivityDefinitionNotFoundError(activityName, availableDefinitions?): ActivityDefinitionNotFoundError;

Defined in: packages/worker/src/errors.ts:21

Parameters
ParameterTypeDefault value
activityNamestringundefined
availableDefinitionsreadonly string[][]
Returns

ActivityDefinitionNotFoundError

Overrides
ts
WorkerError.constructor

Properties

PropertyModifierTypeDefault valueDescriptionInherited fromDefined in
activityNamereadonlystringundefined--packages/worker/src/errors.ts:22
availableDefinitionsreadonlyreadonly string[][]--packages/worker/src/errors.ts:23
cause?publicunknownundefined-WorkerError.causenode_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es2022.error.d.ts:26
messagepublicstringundefined-WorkerError.messagenode_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es5.d.ts:1077
namepublicstringundefined-WorkerError.namenode_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es5.d.ts:1076
stack?publicstringundefined-WorkerError.stacknode_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es5.d.ts:1078
stackTraceLimitstaticnumberundefinedThe Error.stackTraceLimit property specifies the number of stack frames collected by a stack trace (whether generated by new Error().stack or Error.captureStackTrace(obj)). The default value is 10 but may be set to any valid JavaScript number. Changes will affect any stack trace captured after the value has been changed. If set to a non-number value, or set to a negative number, stack traces will not capture any frames.WorkerError.stackTraceLimitnode_modules/.pnpm/@types+node@25.3.2/node_modules/@types/node/globals.d.ts:67

Methods

captureStackTrace()
ts
static captureStackTrace(targetObject, constructorOpt?): void;

Defined in: node_modules/.pnpm/@types+node@25.3.2/node_modules/@types/node/globals.d.ts:51

Creates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called.

js
const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack;  // Similar to `new Error().stack`

The first line of the trace will be prefixed with ${myObject.name}: ${myObject.message}.

The optional constructorOpt argument accepts a function. If given, all frames above constructorOpt, including constructorOpt, will be omitted from the generated stack trace.

The constructorOpt argument is useful for hiding implementation details of error generation from the user. For instance:

js
function a() {
  b();
}

function b() {
  c();
}

function c() {
  // Create an error without stack trace to avoid calculating the stack trace twice.
  const { stackTraceLimit } = Error;
  Error.stackTraceLimit = 0;
  const error = new Error();
  Error.stackTraceLimit = stackTraceLimit;

  // Capture the stack trace above function b
  Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
  throw error;
}

a();
Parameters
ParameterType
targetObjectobject
constructorOpt?Function
Returns

void

Inherited from
ts
WorkerError.captureStackTrace
prepareStackTrace()
ts
static prepareStackTrace(err, stackTraces): any;

Defined in: node_modules/.pnpm/@types+node@25.3.2/node_modules/@types/node/globals.d.ts:55

Parameters
ParameterType
errError
stackTracesCallSite[]
Returns

any

See

https://v8.dev/docs/stack-trace-api#customizing-stack-traces

Inherited from
ts
WorkerError.prepareStackTrace

ActivityError

Defined in: packages/worker/src/activity.ts:28

Activity error class that should be used to wrap all technical exceptions Forces proper error handling and enables retry policies

Extends

  • Error

Constructors

Constructor
ts
new ActivityError(
   code, 
   message, 
   cause?): ActivityError;

Defined in: packages/worker/src/activity.ts:29

Parameters
ParameterType
codestring
messagestring
cause?unknown
Returns

ActivityError

Overrides
ts
Error.constructor

Properties

PropertyModifierTypeDescriptionInherited fromDefined in
cause?publicunknown-Error.causenode_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es2022.error.d.ts:26
codereadonlystring--packages/worker/src/activity.ts:30
messagepublicstring-Error.messagenode_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es5.d.ts:1077
namepublicstring-Error.namenode_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es5.d.ts:1076
stack?publicstring-Error.stacknode_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es5.d.ts:1078
stackTraceLimitstaticnumberThe Error.stackTraceLimit property specifies the number of stack frames collected by a stack trace (whether generated by new Error().stack or Error.captureStackTrace(obj)). The default value is 10 but may be set to any valid JavaScript number. Changes will affect any stack trace captured after the value has been changed. If set to a non-number value, or set to a negative number, stack traces will not capture any frames.Error.stackTraceLimitnode_modules/.pnpm/@types+node@25.3.2/node_modules/@types/node/globals.d.ts:67

Methods

captureStackTrace()
ts
static captureStackTrace(targetObject, constructorOpt?): void;

Defined in: node_modules/.pnpm/@types+node@25.3.2/node_modules/@types/node/globals.d.ts:51

Creates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called.

js
const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack;  // Similar to `new Error().stack`

The first line of the trace will be prefixed with ${myObject.name}: ${myObject.message}.

The optional constructorOpt argument accepts a function. If given, all frames above constructorOpt, including constructorOpt, will be omitted from the generated stack trace.

The constructorOpt argument is useful for hiding implementation details of error generation from the user. For instance:

js
function a() {
  b();
}

function b() {
  c();
}

function c() {
  // Create an error without stack trace to avoid calculating the stack trace twice.
  const { stackTraceLimit } = Error;
  Error.stackTraceLimit = 0;
  const error = new Error();
  Error.stackTraceLimit = stackTraceLimit;

  // Capture the stack trace above function b
  Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
  throw error;
}

a();
Parameters
ParameterType
targetObjectobject
constructorOpt?Function
Returns

void

Inherited from
ts
Error.captureStackTrace
prepareStackTrace()
ts
static prepareStackTrace(err, stackTraces): any;

Defined in: node_modules/.pnpm/@types+node@25.3.2/node_modules/@types/node/globals.d.ts:55

Parameters
ParameterType
errError
stackTracesCallSite[]
Returns

any

See

https://v8.dev/docs/stack-trace-api#customizing-stack-traces

Inherited from
ts
Error.prepareStackTrace

ActivityInputValidationError

Defined in: packages/worker/src/errors.ts:36

Error thrown when activity input validation fails

Extends

  • WorkerError

Constructors

Constructor
ts
new ActivityInputValidationError(activityName, issues): ActivityInputValidationError;

Defined in: packages/worker/src/errors.ts:37

Parameters
ParameterType
activityNamestring
issuesreadonly Issue[]
Returns

ActivityInputValidationError

Overrides
ts
WorkerError.constructor

Properties

PropertyModifierTypeDescriptionInherited fromDefined in
activityNamereadonlystring--packages/worker/src/errors.ts:38
cause?publicunknown-WorkerError.causenode_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es2022.error.d.ts:26
issuesreadonlyreadonly Issue[]--packages/worker/src/errors.ts:39
messagepublicstring-WorkerError.messagenode_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es5.d.ts:1077
namepublicstring-WorkerError.namenode_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es5.d.ts:1076
stack?publicstring-WorkerError.stacknode_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es5.d.ts:1078
stackTraceLimitstaticnumberThe Error.stackTraceLimit property specifies the number of stack frames collected by a stack trace (whether generated by new Error().stack or Error.captureStackTrace(obj)). The default value is 10 but may be set to any valid JavaScript number. Changes will affect any stack trace captured after the value has been changed. If set to a non-number value, or set to a negative number, stack traces will not capture any frames.WorkerError.stackTraceLimitnode_modules/.pnpm/@types+node@25.3.2/node_modules/@types/node/globals.d.ts:67

Methods

captureStackTrace()
ts
static captureStackTrace(targetObject, constructorOpt?): void;

Defined in: node_modules/.pnpm/@types+node@25.3.2/node_modules/@types/node/globals.d.ts:51

Creates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called.

js
const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack;  // Similar to `new Error().stack`

The first line of the trace will be prefixed with ${myObject.name}: ${myObject.message}.

The optional constructorOpt argument accepts a function. If given, all frames above constructorOpt, including constructorOpt, will be omitted from the generated stack trace.

The constructorOpt argument is useful for hiding implementation details of error generation from the user. For instance:

js
function a() {
  b();
}

function b() {
  c();
}

function c() {
  // Create an error without stack trace to avoid calculating the stack trace twice.
  const { stackTraceLimit } = Error;
  Error.stackTraceLimit = 0;
  const error = new Error();
  Error.stackTraceLimit = stackTraceLimit;

  // Capture the stack trace above function b
  Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
  throw error;
}

a();
Parameters
ParameterType
targetObjectobject
constructorOpt?Function
Returns

void

Inherited from
ts
WorkerError.captureStackTrace
prepareStackTrace()
ts
static prepareStackTrace(err, stackTraces): any;

Defined in: node_modules/.pnpm/@types+node@25.3.2/node_modules/@types/node/globals.d.ts:55

Parameters
ParameterType
errError
stackTracesCallSite[]
Returns

any

See

https://v8.dev/docs/stack-trace-api#customizing-stack-traces

Inherited from
ts
WorkerError.prepareStackTrace

ActivityOutputValidationError

Defined in: packages/worker/src/errors.ts:50

Error thrown when activity output validation fails

Extends

  • WorkerError

Constructors

Constructor
ts
new ActivityOutputValidationError(activityName, issues): ActivityOutputValidationError;

Defined in: packages/worker/src/errors.ts:51

Parameters
ParameterType
activityNamestring
issuesreadonly Issue[]
Returns

ActivityOutputValidationError

Overrides
ts
WorkerError.constructor

Properties

PropertyModifierTypeDescriptionInherited fromDefined in
activityNamereadonlystring--packages/worker/src/errors.ts:52
cause?publicunknown-WorkerError.causenode_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es2022.error.d.ts:26
issuesreadonlyreadonly Issue[]--packages/worker/src/errors.ts:53
messagepublicstring-WorkerError.messagenode_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es5.d.ts:1077
namepublicstring-WorkerError.namenode_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es5.d.ts:1076
stack?publicstring-WorkerError.stacknode_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es5.d.ts:1078
stackTraceLimitstaticnumberThe Error.stackTraceLimit property specifies the number of stack frames collected by a stack trace (whether generated by new Error().stack or Error.captureStackTrace(obj)). The default value is 10 but may be set to any valid JavaScript number. Changes will affect any stack trace captured after the value has been changed. If set to a non-number value, or set to a negative number, stack traces will not capture any frames.WorkerError.stackTraceLimitnode_modules/.pnpm/@types+node@25.3.2/node_modules/@types/node/globals.d.ts:67

Methods

captureStackTrace()
ts
static captureStackTrace(targetObject, constructorOpt?): void;

Defined in: node_modules/.pnpm/@types+node@25.3.2/node_modules/@types/node/globals.d.ts:51

Creates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called.

js
const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack;  // Similar to `new Error().stack`

The first line of the trace will be prefixed with ${myObject.name}: ${myObject.message}.

The optional constructorOpt argument accepts a function. If given, all frames above constructorOpt, including constructorOpt, will be omitted from the generated stack trace.

The constructorOpt argument is useful for hiding implementation details of error generation from the user. For instance:

js
function a() {
  b();
}

function b() {
  c();
}

function c() {
  // Create an error without stack trace to avoid calculating the stack trace twice.
  const { stackTraceLimit } = Error;
  Error.stackTraceLimit = 0;
  const error = new Error();
  Error.stackTraceLimit = stackTraceLimit;

  // Capture the stack trace above function b
  Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
  throw error;
}

a();
Parameters
ParameterType
targetObjectobject
constructorOpt?Function
Returns

void

Inherited from
ts
WorkerError.captureStackTrace
prepareStackTrace()
ts
static prepareStackTrace(err, stackTraces): any;

Defined in: node_modules/.pnpm/@types+node@25.3.2/node_modules/@types/node/globals.d.ts:55

Parameters
ParameterType
errError
stackTracesCallSite[]
Returns

any

See

https://v8.dev/docs/stack-trace-api#customizing-stack-traces

Inherited from
ts
WorkerError.prepareStackTrace

Type Aliases

ActivitiesHandler

ts
type ActivitiesHandler<TContract> = TContract["activities"] extends Record<string, ActivityDefinition> ? ActivitiesImplementations<TContract["activities"]> : object & UnionToIntersection<{ [TWorkflow in keyof TContract["workflows"]]: TContract["workflows"][TWorkflow]["activities"] extends Record<string, ActivityDefinition> ? ActivitiesImplementations<TContract["workflows"][TWorkflow]["activities"]> : {} }[keyof TContract["workflows"]]>;

Defined in: packages/worker/src/activity.ts:102

Activities handler ready for Temporal Worker

Flat structure: all activities (global + all workflow-specific) are at the root level

Type Parameters

Type Parameter
TContract extends ContractDefinition

Functions

declareActivitiesHandler()

ts
function declareActivitiesHandler<TContract>(options): ActivitiesHandler<TContract>;

Defined in: packages/worker/src/activity.ts:172

Create a typed activities handler with automatic validation and Result pattern

This wraps all activity implementations with:

  • Validation at network boundaries
  • Result<T, ActivityError> pattern for explicit error handling
  • Automatic conversion from Result to Promise (throwing on Error)

TypeScript ensures ALL activities (global + workflow-specific) are implemented.

Use this to create the activities object for the Temporal Worker.

Type Parameters

Type Parameter
TContract extends ContractDefinition

Parameters

ParameterType
optionsDeclareActivitiesHandlerOptions<TContract>

Returns

ActivitiesHandler<TContract>

Example

ts
import { declareActivitiesHandler, ActivityError } from '@temporal-contract/worker/activity';
import { Result, Future } from '@swan-io/boxed';
import myContract from './contract';

export const activities = declareActivitiesHandler({
  contract: myContract,
  activities: {
    // Activity returns Result instead of throwing
    // All technical exceptions must be wrapped in ActivityError for retry policies
    sendEmail: (args) => {
      return Future.make(async resolve => {
        try {
          await emailService.send(args);
          resolve(Result.Ok({ sent: true }));
        } catch (error) {
          // Wrap technical errors in ActivityError to enable retries
          resolve(Result.Error(
            new ActivityError(
              'EMAIL_SEND_FAILED',
              'Failed to send email',
              error // Original error as cause for debugging
            )
          ));
        }
      });
    },
  },
});

// Use with Temporal Worker
import { Worker } from '@temporalio/worker';

const worker = await Worker.create({
  workflowsPath: require.resolve('./workflows'),
  activities: activities,
  taskQueue: contract.taskQueue,
});

getWorkflowActivities()

ts
function getWorkflowActivities<TContract, TWorkflowName>(contract, workflowName): Record<string, ActivityDefinition>;

Defined in: packages/worker/src/activity-utils.ts:22

Extract activity definitions for a specific workflow from a contract

This includes both:

  • Workflow-specific activities defined under workflow.activities
  • Global activities defined under contract.activities

Type Parameters

Type Parameter
TContract extends ContractDefinition
TWorkflowName extends string | number | symbol

Parameters

ParameterTypeDescription
contractTContractThe contract definition
workflowNameTWorkflowNameThe name of the workflow

Returns

Record<string, ActivityDefinition>

Activity definitions for the workflow (workflow-specific + global activities merged)

Example

ts
const orderWorkflowActivities = getWorkflowActivities(myContract, 'processOrder');
// Returns: { processPayment: ActivityDef, reserveInventory: ActivityDef, sendEmail: ActivityDef }
// where sendEmail is a global activity

getWorkflowActivityNames()

ts
function getWorkflowActivityNames<TContract, TWorkflowName>(contract, workflowName): string[];

Defined in: packages/worker/src/activity-utils.ts:52

Extract all activity names for a specific workflow from a contract

Type Parameters

Type Parameter
TContract extends ContractDefinition
TWorkflowName extends string | number | symbol

Parameters

ParameterTypeDescription
contractTContractThe contract definition
workflowNameTWorkflowNameThe name of the workflow

Returns

string[]

Array of activity names (strings) available for the workflow

Example

ts
const activityNames = getWorkflowActivityNames(myContract, 'processOrder');
// Returns: ['processPayment', 'reserveInventory', 'sendEmail']

getWorkflowNames()

ts
function getWorkflowNames<TContract>(contract): keyof TContract["workflows"][];

Defined in: packages/worker/src/activity-utils.ts:95

Get all workflow names from a contract

Type Parameters

Type Parameter
TContract extends ContractDefinition

Parameters

ParameterTypeDescription
contractTContractThe contract definition

Returns

keyof TContract["workflows"][]

Array of workflow names defined in the contract

Example

ts
const workflows = getWorkflowNames(myContract);
// Returns: ['processOrder', 'processRefund']

isWorkflowActivity()

ts
function isWorkflowActivity<TContract, TWorkflowName>(
   contract, 
   workflowName, 
   activityName): boolean;

Defined in: packages/worker/src/activity-utils.ts:75

Check if an activity belongs to a specific workflow

Type Parameters

Type Parameter
TContract extends ContractDefinition
TWorkflowName extends string | number | symbol

Parameters

ParameterTypeDescription
contractTContractThe contract definition
workflowNameTWorkflowNameThe name of the workflow
activityNamestringThe name of the activity to check

Returns

boolean

True if the activity is available for the workflow, false otherwise

Example

ts
if (isWorkflowActivity(myContract, 'processOrder', 'processPayment')) {
  // Activity is available for this workflow
}

Released under the MIT License.