|
| 1 | +import { DataConverter, LoadedDataConverter } from '@temporalio/common'; |
| 2 | +import { filterNullAndUndefined, loadDataConverter } from '@temporalio/internal-non-workflow-common'; |
| 3 | +import { Replace } from '@temporalio/internal-workflow-common'; |
| 4 | +import { temporal } from '@temporalio/proto'; |
| 5 | +import os from 'os'; |
| 6 | +import { AsyncCompletionClient } from './async-completion-client'; |
| 7 | +import { Connection } from './connection'; |
| 8 | +import { ClientInterceptors } from './interceptors'; |
| 9 | +import { ConnectionLike, Metadata, WorkflowService } from './types'; |
| 10 | +import { WorkflowClient } from './workflow-client'; |
| 11 | + |
| 12 | +export interface ClientOptions { |
| 13 | + /** |
| 14 | + * {@link DataConverter} to use for serializing and deserializing payloads |
| 15 | + */ |
| 16 | + dataConverter?: DataConverter; |
| 17 | + |
| 18 | + /** |
| 19 | + * Used to override and extend default Connection functionality |
| 20 | + * |
| 21 | + * Useful for injecting auth headers and tracing Workflow executions |
| 22 | + */ |
| 23 | + interceptors?: ClientInterceptors; |
| 24 | + |
| 25 | + /** |
| 26 | + * Identity to report to the server |
| 27 | + * |
| 28 | + * @default `${process.pid}@${os.hostname()}` |
| 29 | + */ |
| 30 | + identity?: string; |
| 31 | + |
| 32 | + /** |
| 33 | + * Connection to use to communicate with the server. |
| 34 | + * |
| 35 | + * By default `WorkflowClient` connects to localhost. |
| 36 | + * |
| 37 | + * Connections are expensive to construct and should be reused. |
| 38 | + */ |
| 39 | + connection?: ConnectionLike; |
| 40 | + |
| 41 | + /** |
| 42 | + * Server namespace |
| 43 | + * |
| 44 | + * @default default |
| 45 | + */ |
| 46 | + namespace?: string; |
| 47 | + |
| 48 | + workflow?: { |
| 49 | + /** |
| 50 | + * Should a query be rejected by closed and failed workflows |
| 51 | + * |
| 52 | + * @default QUERY_REJECT_CONDITION_UNSPECIFIED which means that closed and failed workflows are still queryable |
| 53 | + */ |
| 54 | + queryRejectCondition?: temporal.api.enums.v1.QueryRejectCondition; |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +export type ClientOptionsWithDefaults = Replace< |
| 59 | + Required<ClientOptions>, |
| 60 | + { |
| 61 | + connection?: ConnectionLike; |
| 62 | + } |
| 63 | +>; |
| 64 | + |
| 65 | +export type LoadedClientOptions = ClientOptionsWithDefaults & { |
| 66 | + loadedDataConverter: LoadedDataConverter; |
| 67 | +}; |
| 68 | + |
| 69 | +export function defaultClientOptions(): ClientOptionsWithDefaults { |
| 70 | + return { |
| 71 | + dataConverter: {}, |
| 72 | + identity: `${process.pid}@${os.hostname()}`, |
| 73 | + interceptors: {}, |
| 74 | + namespace: 'default', |
| 75 | + workflow: { |
| 76 | + queryRejectCondition: temporal.api.enums.v1.QueryRejectCondition.QUERY_REJECT_CONDITION_UNSPECIFIED, |
| 77 | + }, |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * High level SDK client. |
| 83 | + * |
| 84 | + * |
| 85 | + */ |
| 86 | +export class Client { |
| 87 | + /** |
| 88 | + * Underlying gRPC connection to the Temporal service |
| 89 | + */ |
| 90 | + public readonly connection: ConnectionLike; |
| 91 | + public readonly options: LoadedClientOptions; |
| 92 | + /** |
| 93 | + * Workflow sub-client - use to start and interact with Workflows |
| 94 | + */ |
| 95 | + public readonly workflow: WorkflowClient; |
| 96 | + /** |
| 97 | + * (Async) Activity completion sub-client - use to manually manage Activities |
| 98 | + */ |
| 99 | + public readonly activity: AsyncCompletionClient; |
| 100 | + |
| 101 | + constructor(options?: ClientOptions) { |
| 102 | + this.connection = options?.connection ?? Connection.lazy(); |
| 103 | + this.options = { |
| 104 | + ...defaultClientOptions(), |
| 105 | + ...filterNullAndUndefined(options ?? {}), |
| 106 | + loadedDataConverter: loadDataConverter(options?.dataConverter), |
| 107 | + }; |
| 108 | + |
| 109 | + const { workflow, loadedDataConverter, interceptors, ...base } = this.options; |
| 110 | + |
| 111 | + this.workflow = new WorkflowClient({ |
| 112 | + ...base, |
| 113 | + ...workflow, |
| 114 | + connection: this.connection, |
| 115 | + dataConverter: loadedDataConverter, |
| 116 | + interceptors: interceptors.workflow, |
| 117 | + }); |
| 118 | + |
| 119 | + this.activity = new AsyncCompletionClient({ |
| 120 | + ...base, |
| 121 | + connection: this.connection, |
| 122 | + dataConverter: loadedDataConverter, |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Raw gRPC access to the Temporal service. |
| 128 | + * |
| 129 | + * **NOTE**: The namespace provided in {@link options} is **not** automatically set on requests made to the service. |
| 130 | + */ |
| 131 | + get workflowService(): WorkflowService { |
| 132 | + return this.connection.workflowService; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Set the deadline for any service requests executed in `fn`'s scope. |
| 137 | + */ |
| 138 | + async withDeadline<R>(deadline: number | Date, fn: () => Promise<R>): Promise<R> { |
| 139 | + return await this.connection.withDeadline(deadline, fn); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Set metadata for any service requests executed in `fn`'s scope. |
| 144 | + * |
| 145 | + * @returns returned value of `fn` |
| 146 | + * |
| 147 | + * @see {@link Connection.withMetadata} |
| 148 | + */ |
| 149 | + async withMetadata<R>(metadata: Metadata, fn: () => Promise<R>): Promise<R> { |
| 150 | + return await this.connection.withMetadata(metadata, fn); |
| 151 | + } |
| 152 | +} |
0 commit comments