Skip to content

Commit 8dd27dc

Browse files
committed
Minor tidying of interceptors
1 parent b3272f3 commit 8dd27dc

File tree

3 files changed

+295
-193
lines changed

3 files changed

+295
-193
lines changed

packages/worker/src/interceptors.ts

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,20 @@ import { ClientInterceptors } from '@temporalio/client';
33
import { Headers, MetricTags, Next } from '@temporalio/common';
44

55
export { Next, Headers };
6-
/** Input for ActivityInboundCallsInterceptor.execute */
7-
export interface ActivityExecuteInput {
8-
readonly args: unknown[];
9-
readonly headers: Headers;
6+
7+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
8+
// Activity Interceptors
9+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
10+
11+
export type ActivityInterceptorsFactory = (ctx: ActivityContext) => ActivityInterceptors;
12+
13+
/**
14+
* A function that takes Activity Context and returns an interceptor
15+
*/
16+
17+
export interface ActivityInterceptors {
18+
inbound?: ActivityInboundCallsInterceptor;
19+
outbound?: ActivityOutboundCallsInterceptor;
1020
}
1121

1222
/**
@@ -18,24 +28,17 @@ export interface ActivityInboundCallsInterceptor {
1828
*
1929
* @return result of Activity function
2030
*/
21-
execute?: (input: ActivityExecuteInput, next: Next<this, 'execute'>) => Promise<unknown>;
31+
execute?: (input: ActivityExecuteInput, next: Next<ActivityInboundCallsInterceptor, 'execute'>) => Promise<unknown>;
2232
}
2333

2434
/**
25-
* A function that takes Activity Context and returns an interceptor
26-
*
27-
* @deprecated Use {@link ActivityInterceptorsFactory} instead
35+
* Input for {@link ActivityInboundCallsInterceptor.execute}
2836
*/
29-
export interface ActivityInboundCallsInterceptorFactory {
30-
(ctx: ActivityContext): ActivityInboundCallsInterceptor;
37+
export interface ActivityExecuteInput {
38+
readonly args: unknown[];
39+
readonly headers: Headers;
3140
}
3241

33-
/** Input for ActivityOutboundCallsInterceptor.getLogAttributes */
34-
export type GetLogAttributesInput = Record<string, unknown>;
35-
36-
/** Input for ActivityOutboundCallsInterceptor.getMetricTags */
37-
export type GetMetricTagsInput = MetricTags;
38-
3942
/**
4043
* Implement any of these methods to intercept Activity outbound calls
4144
*/
@@ -45,7 +48,10 @@ export interface ActivityOutboundCallsInterceptor {
4548
*
4649
* The attributes returned in this call are attached to every log message.
4750
*/
48-
getLogAttributes?: (input: GetLogAttributesInput, next: Next<this, 'getLogAttributes'>) => Record<string, unknown>;
51+
getLogAttributes?: (
52+
input: GetLogAttributesInput,
53+
next: Next<ActivityOutboundCallsInterceptor, 'getLogAttributes'>
54+
) => Record<string, unknown>;
4955

5056
/**
5157
* Called once every time a metric is emitted from an Activity metric
@@ -54,18 +60,34 @@ export interface ActivityOutboundCallsInterceptor {
5460
* Tags returned by this hook are _prepended_ to tags defined at the metric level and tags defined
5561
* on the emitter function itself.
5662
*/
57-
getMetricTags?: (input: GetMetricTagsInput, next: Next<this, 'getMetricTags'>) => MetricTags;
63+
getMetricTags?: (
64+
input: GetMetricTagsInput,
65+
next: Next<ActivityOutboundCallsInterceptor, 'getMetricTags'>
66+
) => MetricTags;
5867
}
5968

60-
export interface ActivityInterceptors {
61-
inbound?: ActivityInboundCallsInterceptor;
62-
outbound?: ActivityOutboundCallsInterceptor;
63-
}
69+
/**
70+
* Input for {@link ActivityOutboundCallsInterceptor.getLogAttributes}
71+
*/
72+
export type GetLogAttributesInput = Record<string, unknown>;
73+
74+
/**
75+
* Input for {@link ActivityOutboundCallsInterceptor.getMetricTags}
76+
*/
77+
export type GetMetricTagsInput = MetricTags;
6478

6579
/**
6680
* A function that takes Activity Context and returns an interceptor
81+
*
82+
* @deprecated Use {@link ActivityInterceptorsFactory} instead
6783
*/
68-
export type ActivityInterceptorsFactory = (ctx: ActivityContext) => ActivityInterceptors;
84+
export interface ActivityInboundCallsInterceptorFactory {
85+
(ctx: ActivityContext): ActivityInboundCallsInterceptor;
86+
}
87+
88+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
89+
// Worker Interceptors Configuration
90+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
6991

7092
/**
7193
* Structure for passing in Worker interceptors via {@link WorkerOptions}
@@ -79,11 +101,13 @@ export interface WorkerInterceptors {
79101
* instead. Client doesn't support cancellation through a Signal.
80102
*/
81103
client?: ClientInterceptors;
104+
82105
/**
83106
* List of factory functions that instanciate {@link ActivityInboundCallsInterceptor}s and
84107
* {@link ActivityOutboundCallsInterceptor}s.
85108
*/
86109
activity?: ActivityInterceptorsFactory[];
110+
87111
/**
88112
* List of factory functions returning {@link ActivityInboundCallsInterceptor}s. If both `activity` and
89113
* `activityInbound` is supplied, then entries from `activityInbound` will be prepended to inbound interceptors
@@ -92,6 +116,7 @@ export interface WorkerInterceptors {
92116
* @deprecated Use {@link WorkerInterceptors.activity} instead.
93117
*/
94118
activityInbound?: ActivityInboundCallsInterceptorFactory[]; // eslint-disable-line deprecation/deprecation
119+
95120
/**
96121
* List of modules to search for Workflow interceptors in
97122
* - Modules should export an `interceptors` variable of type {@link WorkflowInterceptorsFactory}

0 commit comments

Comments
 (0)