Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@temporalio/client

Package Overview
Dependencies
Maintainers
4
Versions
81
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@temporalio/client - npm Package Compare versions

Comparing version 0.12.0 to 0.13.0

7

lib/interceptors.d.ts

@@ -14,5 +14,3 @@ /**

/** Name of Workflow to start */
readonly name: string;
/** Workflow arguments */
readonly args: unknown[];
readonly workflowType: string;
readonly headers: Headers;

@@ -29,4 +27,3 @@ readonly options: CompiledWorkflowOptions;

export interface WorkflowSignalWithStartInput {
readonly workflowName: string;
readonly workflowArgs: unknown[];
readonly workflowType: string;
readonly signalName: string;

@@ -33,0 +30,0 @@ readonly signalArgs: unknown[];

import { temporal } from '@temporalio/proto';
import { WorkflowClientInterceptors } from './interceptors';
import { DataConverter, Workflow, BaseWorkflowHandle, QueryDefinition, SignalDefinition, WorkflowResultType } from '@temporalio/common';
import { WorkflowOptions } from './workflow-options';
import { DataConverter, Workflow, BaseWorkflowHandle, QueryDefinition, WorkflowResultType, WithWorkflowArgs, WorkflowReturnType } from '@temporalio/common';
import { WorkflowOptions, WorkflowSignalWithStartOptions } from './workflow-options';
import { WorkflowCancelInput, WorkflowClientCallsInterceptor, WorkflowQueryInput, WorkflowSignalInput, WorkflowSignalWithStartInput, WorkflowStartInput, WorkflowTerminateInput } from './interceptors';

@@ -22,5 +22,8 @@ import { DescribeWorkflowExecutionResponse, TerminateWorkflowExecutionResponse, RequestCancelWorkflowExecutionResponse } from './types';

* const client = new WorkflowClient();
* const handle = connection.createWorkflowHandle(counterWorkflow, { taskQueue: 'tutorial' });
* // Start the Workflow with initialValue of 2.
* await handle.start(2);
* const handle = await client.start({
* workflowType: counterWorkflow,
* args: [2],
* taskQueue: 'tutorial',
* });
* await handle.signal(incrementSignal, 2);

@@ -46,9 +49,2 @@ * await handle.query(getValueQuery); // 4

/**
* Sends a signal to a running Workflow or starts a new one if not already running and immediately signals it.
* Useful when you're unsure of the Workflows' run state.
*
* @returns the runId of the Workflow
*/
signalWithStart<Args extends any[] = []>(def: SignalDefinition<Args> | string, signalArgs: Args, workflowArgs: Parameters<T>): Promise<string>;
/**
* Terminate a running Workflow

@@ -70,2 +66,12 @@ */

}
/**
* This interface is exactly the same as {@link WorkflowHandle} except it
* includes the `originalRunId` returned after starting a new Workflow.
*/
export interface WorkflowHandleWithRunId<T extends Workflow> extends WorkflowHandle<T> {
/**
* The runId of the initial run of the bound Workflow
*/
readonly originalRunId: string;
}
export interface WorkflowClientOptions {

@@ -104,3 +110,3 @@ /**

* These defaults are **shallowly** merged with options provided to methods that start Workflows
* e.g. {@link WorkflowHandle.start}.
* e.g. {@link WorkflowClient.start}.
*/

@@ -112,11 +118,2 @@ workflowDefaults?: Partial<WorkflowOptions>;

/**
* Same as the protocol's {@link WorkflowExecution} but `workflowId` is required.
*
* NOTE: Does not accept nulls.
*/
export interface ValidWorkflowExecution {
workflowId: string;
runId?: string;
}
/**
* Options for getting a result of a Workflow execution.

@@ -136,2 +133,6 @@ */

/**
* Options for starting a Workflow
*/
export declare type WorkflowStartOptions<T extends Workflow> = WithWorkflowArgs<T, Partial<WorkflowOptions>>;
/**
* Client for starting Workflow executions and creating Workflow handles

@@ -144,10 +145,50 @@ */

/**
* Start a new Workflow execution. Resolves with the execution `runId`.
* Start a new Workflow execution.
*
* @returns the execution's `runId`.
*/
start<T extends Workflow>(workflowTypeOrFunc: string | T, opts: Partial<WorkflowOptions>, ...args: Parameters<T>): Promise<string>;
protected _start<T extends Workflow>(workflowTypeOrFunc: string | T, options: WithWorkflowArgs<T, Partial<WorkflowOptions>>, interceptors: WorkflowClientCallsInterceptor[]): Promise<string>;
/**
* Starts a new Workflow execution and awaits its completion
* Sends a signal to a running Workflow or starts a new one if not already running and immediately signals it.
* Useful when you're unsure of the Workflows' run state.
*
* @returns the runId of the Workflow
*/
execute<T extends Workflow>(workflowTypeOrFunc: string | T, opts: Partial<WorkflowOptions>, ...args: Parameters<T>): Promise<WorkflowResultType<T>>;
protected _signalWithStart<T extends Workflow, SA extends any[]>(workflowTypeOrFunc: string | T, options: WithWorkflowArgs<T, WorkflowSignalWithStartOptions<SA>>, interceptors: WorkflowClientCallsInterceptor[]): Promise<string>;
/**
* Start a new Workflow execution.
*
* **Override for Workflows that accept no arguments**.
*
* @returns a WorkflowHandle to the started Workflow
*/
start<T extends () => WorkflowReturnType>(workflowTypeOrFunc: string | T): Promise<WorkflowHandleWithRunId<T>>;
/**
* Start a new Workflow execution.
*
* @returns a WorkflowHandle to the started Workflow
*/
start<T extends Workflow>(workflowTypeOrFunc: string | T, options: WorkflowStartOptions<T>): Promise<WorkflowHandleWithRunId<T>>;
/**
* Sends a signal to a running Workflow or starts a new one if not already running and immediately signals it.
* Useful when you're unsure of the Workflows' run state.
*
* @returns a WorkflowHandle to the started Workflow
*/
signalWithStart<T extends Workflow, SA extends any[] = []>(workflowTypeOrFunc: string | T, options: WithWorkflowArgs<T, WorkflowSignalWithStartOptions<SA>>): Promise<WorkflowHandleWithRunId<T>>;
/**
* Starts a new Workflow execution and awaits its completion.
*
* @returns the result of the Workflow execution
*/
execute<T extends Workflow>(workflowTypeOrFunc: string | T, options: WorkflowStartOptions<T>): Promise<WorkflowResultType<T>>;
/**
* Starts a new Workflow execution and awaits its completion.
*
* **Override for Workflows that accept no arguments**.
*
* @returns the result of the Workflow execution
*/
execute<T extends () => WorkflowReturnType>(workflowTypeOrFunc: string | T): Promise<WorkflowResultType<T>>;
/**
* Gets the result of a Workflow execution.

@@ -157,3 +198,3 @@ *

*/
result<T extends Workflow>({ workflowId, runId }: ValidWorkflowExecution, opts?: WorkflowResultOptions): Promise<WorkflowResultType<T>>;
result<T extends Workflow>(workflowId: string, runId?: string, opts?: WorkflowResultOptions): Promise<WorkflowResultType<T>>;
/**

@@ -196,31 +237,9 @@ * Uses given input to make a queryWorkflow call to the service

/**
* Create a {@link WorkflowHandle} for starting a new Workflow execution
*
* @param name workflow type name (the exported function name in the Node.js SDK)
* @param options used to start the Workflow
*/
createWorkflowHandle<T extends Workflow>(name: string, options?: Partial<WorkflowOptions>): WorkflowHandle<T>;
/**
* Create a {@link WorkflowHandle} for starting a new Workflow execution
*
* @param func an exported function
* @param options used to start the Workflow
*/
createWorkflowHandle<T extends Workflow>(func: T, options?: Partial<WorkflowOptions>): WorkflowHandle<T>;
/**
* Create a {@link WorkflowHandle} for an existing Workflow execution
*/
createWorkflowHandle<T extends Workflow>(execution: ValidWorkflowExecution): WorkflowHandle<T>;
/**
* Create a new workflow handle for new or existing Workflow execution
*/
protected _createWorkflowHandle<T extends Workflow>(workflowId: string, runId: string | undefined, interceptors: WorkflowClientCallsInterceptor[], start: WorkflowHandle<T>['start'], signalWithStart: WorkflowHandle<T>['signalWithStart'], resultOptions: WorkflowResultOptions): WorkflowHandle<T>;
protected _createWorkflowHandle<T extends Workflow>(workflowId: string, runId: string | undefined, interceptors: WorkflowClientCallsInterceptor[], resultOptions: WorkflowResultOptions): WorkflowHandle<T>;
/**
* Creates a Workflow handle for existing Workflow using `workflowId` and optional `runId`
*/
protected connectToExistingWorkflow<T extends Workflow>({ workflowId, runId, }: ValidWorkflowExecution): WorkflowHandle<T>;
/**
* Creates a Workflow handle for new Workflow execution
*/
protected createNewWorkflow<T extends Workflow>(name: string, options?: Partial<WorkflowOptions>): WorkflowHandle<T>;
getHandle<T extends Workflow>(workflowId: string, runId?: string): WorkflowHandle<T>;
}

@@ -227,0 +246,0 @@ export declare class QueryRejectedError extends Error {

@@ -59,28 +59,77 @@ "use strict";

/**
* Start a new Workflow execution. Resolves with the execution `runId`.
* Start a new Workflow execution.
*
* @returns the execution's `runId`.
*/
async start(workflowTypeOrFunc, opts, ...args) {
async _start(workflowTypeOrFunc, options, interceptors) {
const workflowType = typeof workflowTypeOrFunc === 'string' ? workflowTypeOrFunc : workflowTypeOrFunc.name;
const mergedOptions = { ...this.options.workflowDefaults, ...opts };
assertRequiredWorkflowOptions(mergedOptions);
const compiledOptions = (0, workflow_options_1.compileWorkflowOptions)((0, workflow_options_1.addDefaults)(mergedOptions));
const interceptors = (this.options.interceptors.calls ?? []).map((ctor) => ctor({ workflowId: compiledOptions.workflowId }));
const next = (0, common_1.composeInterceptors)(interceptors, 'start', this._startWorkflowHandler.bind(this));
const start = (...args) => next({
assertRequiredWorkflowOptions(options);
const compiledOptions = (0, workflow_options_1.compileWorkflowOptions)((0, workflow_options_1.addDefaults)(options));
const start = (0, common_1.composeInterceptors)(interceptors, 'start', this._startWorkflowHandler.bind(this));
return start({
options: compiledOptions,
headers: {},
args,
name: workflowType,
workflowType,
});
return start(...args);
}
/**
* Starts a new Workflow execution and awaits its completion
* Sends a signal to a running Workflow or starts a new one if not already running and immediately signals it.
* Useful when you're unsure of the Workflows' run state.
*
* @returns the runId of the Workflow
*/
async execute(workflowTypeOrFunc, opts, ...args) {
const workflowId = opts.workflowId ?? (0, uuid_1.v4)();
const runId = await this.start(workflowTypeOrFunc, { ...opts, workflowId }, ...args);
return this.result({ workflowId, runId });
async _signalWithStart(workflowTypeOrFunc, options, interceptors) {
const workflowType = typeof workflowTypeOrFunc === 'string' ? workflowTypeOrFunc : workflowTypeOrFunc.name;
const { signal, signalArgs, ...rest } = options;
assertRequiredWorkflowOptions(rest);
const compiledOptions = (0, workflow_options_1.compileWorkflowOptions)((0, workflow_options_1.addDefaults)(rest));
const signalWithStart = (0, common_1.composeInterceptors)(interceptors, 'signalWithStart', this._signalWithStartWorkflowHandler.bind(this));
return signalWithStart({
options: compiledOptions,
headers: {},
workflowType,
signalName: typeof signal === 'string' ? signal : signal.name,
signalArgs,
});
}
async start(workflowTypeOrFunc, maybeOptions) {
// Cast is needed because it's impossible to deduce the type in this situation
const options = { ...this.options.workflowDefaults, ...maybeOptions };
const workflowId = options.workflowId ?? (0, uuid_1.v4)();
const interceptors = (this.options.interceptors.calls ?? []).map((ctor) => ctor({ workflowId }));
const runId = await this._start(workflowTypeOrFunc, { ...options, workflowId }, interceptors);
const handle = this._createWorkflowHandle(workflowId, runId, interceptors, {
followRuns: options.followRuns ?? true,
}); // Cast is safe because we know we add the originalRunId below
handle /* readonly */.originalRunId = runId;
return handle;
}
/**
* Sends a signal to a running Workflow or starts a new one if not already running and immediately signals it.
* Useful when you're unsure of the Workflows' run state.
*
* @returns a WorkflowHandle to the started Workflow
*/
async signalWithStart(workflowTypeOrFunc, options) {
options = { ...this.options.workflowDefaults, ...options };
const workflowId = options.workflowId ?? (0, uuid_1.v4)();
const interceptors = (this.options.interceptors.calls ?? []).map((ctor) => ctor({ workflowId }));
const runId = await this._signalWithStart(workflowTypeOrFunc, { ...options, workflowId }, interceptors);
const handle = this._createWorkflowHandle(workflowId, runId, interceptors, {
followRuns: options.followRuns ?? true,
}); // Cast is safe because we know we add the originalRunId below
handle /* readonly */.originalRunId = runId;
return handle;
}
async execute(workflowTypeOrFunc, maybeOptions) {
// Cast is needed because it's impossible to deduce the type in this situation
const options = { ...this.options.workflowDefaults, ...maybeOptions };
const workflowId = options.workflowId ?? (0, uuid_1.v4)();
const interceptors = (this.options.interceptors.calls ?? []).map((ctor) => ctor({ workflowId }));
await this._start(workflowTypeOrFunc, { ...options, workflowId }, interceptors);
return await this.result(workflowId, undefined, {
followRuns: options.followRuns ?? true,
});
}
/**
* Gets the result of a Workflow execution.

@@ -90,3 +139,3 @@ *

*/
async result({ workflowId, runId }, opts) {
async result(workflowId, runId, opts) {
const followRuns = opts?.followRuns ?? true;

@@ -217,3 +266,3 @@ const execution = { workflowId, runId };

const { identity, dataConverter } = this.options;
const { options, workflowName, workflowArgs, signalName, signalArgs, headers } = input;
const { options, workflowType, signalName, signalArgs, headers } = input;
const { runId } = await this.service.signalWithStartWorkflowExecution({

@@ -225,4 +274,4 @@ namespace: this.options.namespace,

workflowIdReusePolicy: options.workflowIdReusePolicy,
workflowType: { name: workflowName },
input: { payloads: await dataConverter.toPayloads(...workflowArgs) },
workflowType: { name: workflowType },
input: { payloads: await dataConverter.toPayloads(...options.args) },
signalName,

@@ -255,3 +304,3 @@ signalInput: { payloads: await dataConverter.toPayloads(...signalArgs) },

async _startWorkflowHandler(input) {
const { options: opts, name, args, headers } = input;
const { options: opts, workflowType: name, headers } = input;
const { identity, dataConverter } = this.options;

@@ -265,3 +314,3 @@ const req = {

workflowType: { name },
input: { payloads: await dataConverter.toPayloads(...args) },
input: { payloads: await dataConverter.toPayloads(...opts.args) },
taskQueue: {

@@ -313,41 +362,12 @@ kind: proto_1.temporal.api.enums.v1.TaskQueueKind.TASK_QUEUE_KIND_UNSPECIFIED,

}
createWorkflowHandle(executionOrNameOrFunc, options) {
const workflowExecution = typeof executionOrNameOrFunc === 'object' && executionOrNameOrFunc.workflowId ? executionOrNameOrFunc : undefined;
if (workflowExecution !== undefined) {
return this.connectToExistingWorkflow(workflowExecution);
}
const workflowType = typeof executionOrNameOrFunc === 'string'
? executionOrNameOrFunc
: typeof executionOrNameOrFunc === 'function'
? executionOrNameOrFunc.name
: undefined;
if (typeof workflowType !== 'string') {
throw new TypeError(`Invalid argument: ${executionOrNameOrFunc}, expected one of: Workflow function, a string with the Workflow type name, or WorkflowExecution`);
}
return this.createNewWorkflow(workflowType, options);
}
/**
* Create a new workflow handle for new or existing Workflow execution
*/
_createWorkflowHandle(workflowId, runId, interceptors, start, signalWithStart, resultOptions) {
_createWorkflowHandle(workflowId, runId, interceptors, resultOptions) {
const namespace = this.options.namespace;
let startPromise = undefined;
return {
client: this,
workflowId,
async execute(...args) {
await this.start(...args);
return await this.result();
},
async start(...args) {
if (startPromise !== undefined) {
throw new common_1.IllegalStateError('Workflow execution already started');
}
startPromise = start(...args);
// Override runId in outer scope
runId = await startPromise;
return runId;
},
async result() {
return this.client.result({ workflowId, runId }, resultOptions);
return this.client.result(workflowId, runId, resultOptions);
},

@@ -397,3 +417,2 @@ async terminate(reason) {

},
signalWithStart,
};

@@ -404,43 +423,8 @@ }

*/
connectToExistingWorkflow({ workflowId, runId, }) {
getHandle(workflowId, runId) {
const interceptors = (this.options.interceptors.calls ?? []).map((ctor) => ctor({ workflowId, runId }));
const startCallback = () => {
throw new common_1.IllegalStateError('WorkflowHandle created with no WorkflowOptions cannot be started');
};
return this._createWorkflowHandle(workflowId, runId, interceptors, startCallback,
// Requires cast because workflow signals are optional which complicate the type
startCallback, { followRuns: this.options.workflowDefaults.followRuns ?? true });
return this._createWorkflowHandle(workflowId, runId, interceptors, {
followRuns: this.options.workflowDefaults.followRuns ?? true,
});
}
/**
* Creates a Workflow handle for new Workflow execution
*/
createNewWorkflow(name, options) {
const mergedOptions = { ...this.options.workflowDefaults, ...options };
assertRequiredWorkflowOptions(mergedOptions);
const compiledOptions = (0, workflow_options_1.compileWorkflowOptions)((0, workflow_options_1.addDefaults)(mergedOptions));
const interceptors = (this.options.interceptors.calls ?? []).map((ctor) => ctor({ workflowId: compiledOptions.workflowId }));
const start = (...args) => {
const next = (0, common_1.composeInterceptors)(interceptors, 'start', this._startWorkflowHandler.bind(this));
return next({
options: compiledOptions,
headers: {},
args,
name,
});
};
const signalWithStart = (def, signalArgs, workflowArgs) => {
const next = (0, common_1.composeInterceptors)(interceptors, 'signalWithStart', this._signalWithStartWorkflowHandler.bind(this));
return next({
options: compiledOptions,
headers: {},
workflowArgs,
workflowName: name,
signalName: typeof def === 'string' ? def : def.name,
signalArgs,
});
};
return this._createWorkflowHandle(compiledOptions.workflowId, undefined, interceptors, start,
// Requires cast because workflow signals are optional which complicate the type
signalWithStart, { followRuns: mergedOptions.followRuns ?? true });
}
}

@@ -447,0 +431,0 @@ exports.WorkflowClient = WorkflowClient;

@@ -1,4 +0,4 @@

import { WorkflowOptions as BaseWorkflowOptions, WorkflowOptionsWithDefaults as BaseWorkflowOptionsWithDefaults } from '@temporalio/common/lib/workflow-options';
export { CompiledWorkflowOptions, compileWorkflowOptions } from '@temporalio/common/lib/workflow-options';
export interface WorkflowOptions extends BaseWorkflowOptions {
import { WithWorkflowArgs, WorkflowOptions as CommonWorkflowOptions, WorkflowOptionsWithDefaults as CommonWorkflowOptionsWithDefaults, SignalDefinition, Workflow } from '@temporalio/common';
export { CompiledWorkflowOptions, compileWorkflowOptions } from '@temporalio/common';
export interface WorkflowOptions extends CommonWorkflowOptions {
/**

@@ -11,4 +11,14 @@ * Task queue to use for Workflow tasks. It should match a task queue specified when creating a

}
export interface WorkflowOptionsWithDefaults extends BaseWorkflowOptionsWithDefaults {
export interface WorkflowSignalWithStartOptions<SA extends any[] = []> extends Partial<WorkflowOptions> {
/**
* SignalDefinition or name of signal
*/
signal: SignalDefinition<SA> | string;
/**
* Arguments to invoke the signal handler with
*/
signalArgs: SA;
}
export interface WorkflowOptionsWithDefaults<T extends Workflow> extends CommonWorkflowOptionsWithDefaults<T> {
/**
* If set to true, instructs the client to follow the chain of execution before returning a Workflow's result.

@@ -26,2 +36,2 @@ *

*/
export declare function addDefaults(opts: WorkflowOptions): WorkflowOptionsWithDefaults;
export declare function addDefaults<T extends Workflow>(opts: WithWorkflowArgs<T, WorkflowOptions>): WorkflowOptionsWithDefaults<T>;

@@ -5,4 +5,4 @@ "use strict";

const uuid_1 = require("uuid");
var workflow_options_1 = require("@temporalio/common/lib/workflow-options");
Object.defineProperty(exports, "compileWorkflowOptions", { enumerable: true, get: function () { return workflow_options_1.compileWorkflowOptions; } });
var common_1 = require("@temporalio/common");
Object.defineProperty(exports, "compileWorkflowOptions", { enumerable: true, get: function () { return common_1.compileWorkflowOptions; } });
/**

@@ -12,6 +12,8 @@ * Adds default values to `workflowId` and `workflowIdReusePolicy` to given workflow options.

function addDefaults(opts) {
const { workflowId, args, ...rest } = opts;
return {
followRuns: true,
workflowId: opts.workflowId ?? (0, uuid_1.v4)(),
...opts,
args: args ?? [],
workflowId: workflowId ?? (0, uuid_1.v4)(),
...rest,
};

@@ -18,0 +20,0 @@ }

{
"name": "@temporalio/client",
"version": "0.12.0",
"version": "0.13.0",
"description": "Temporal.io SDK Client sub-package",

@@ -17,4 +17,4 @@ "main": "lib/index.js",

"@grpc/grpc-js": "^1.3.7",
"@temporalio/common": "^0.12.0",
"@temporalio/proto": "^0.12.0",
"@temporalio/common": "^0.13.0",
"@temporalio/proto": "^0.13.0",
"ms": "^2.1.3",

@@ -34,3 +34,3 @@ "protobufjs": "^6.11.2",

},
"gitHead": "eaa2d205c9bc5ff4a3b17c0b34f2dcf6b1e0264a"
"gitHead": "bbb8d36ad9d310eb44bd19f3f8bb9356e1dd9dfb"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc