Skip to content

Commit f323ce6

Browse files
committed
feat: resolve method helper for Command
1 parent 3ad9987 commit f323ce6

File tree

2 files changed

+130
-1
lines changed

2 files changed

+130
-1
lines changed

.changeset/quick-dolphins-visit.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/smithy-client": minor
3+
---
4+
5+
resolve method builder for Command class

packages/smithy-client/src/command.ts

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
import { constructStack } from "@smithy/middleware-stack";
2-
import { Command as ICommand, Handler, MetadataBearer, MiddlewareStack as IMiddlewareStack } from "@smithy/types";
2+
import type { HttpRequest as __HttpRequest } from "@smithy/protocol-http";
3+
import type {
4+
Command as ICommand,
5+
FinalizeHandlerArguments,
6+
Handler,
7+
HandlerExecutionContext,
8+
Logger,
9+
MetadataBearer,
10+
MiddlewareStack as IMiddlewareStack,
11+
Pluggable,
12+
RequestHandler,
13+
} from "@smithy/types";
14+
import { SMITHY_CONTEXT_KEY } from "@smithy/types";
315

416
/**
517
* @public
@@ -13,9 +25,121 @@ export abstract class Command<
1325
> implements ICommand<ClientInput, Input, ClientOutput, Output, ResolvedClientConfiguration> {
1426
abstract input: Input;
1527
readonly middlewareStack: IMiddlewareStack<Input, Output> = constructStack<Input, Output>();
28+
1629
abstract resolveMiddleware(
1730
stack: IMiddlewareStack<ClientInput, ClientOutput>,
1831
configuration: ResolvedClientConfiguration,
1932
options: any
2033
): Handler<Input, Output>;
34+
35+
/**
36+
* @internal
37+
*/
38+
protected resolveBuilder() {
39+
const args: ResolveMiddlewareContextArgs = {
40+
middlewareQueue: [] as Pluggable<any, any>[],
41+
commandName: "",
42+
clientName: "",
43+
service: "",
44+
operation: "",
45+
inputFilterSensitiveLog: () => {},
46+
outputFilterSensitiveLog: () => {},
47+
};
48+
return {
49+
/**
50+
* Add any number of middleware.
51+
*/
52+
m(...middleware: Pluggable<any, any>[]) {
53+
args.middlewareQueue.push(...middleware);
54+
return this;
55+
},
56+
/**
57+
* Set constant string identifiers for the operation.
58+
*/
59+
n(clientName: string, commandName: string, service: string, operation: string) {
60+
args.clientName = clientName;
61+
args.commandName = commandName;
62+
args.service = service;
63+
args.operation = operation;
64+
return this;
65+
},
66+
/**
67+
* Set the input and output sensistive log filters.
68+
*/
69+
f(inputFilter: (_: any) => any = (_) => _, outputFilter: (_: any) => any = (_) => _) {
70+
args.inputFilterSensitiveLog = inputFilter;
71+
args.outputFilterSensitiveLog = outputFilter;
72+
return this;
73+
},
74+
/**
75+
* @returns the implementation of the built resolveMiddleware function.
76+
*/
77+
build: () => {
78+
return (
79+
clientStack: IMiddlewareStack<ClientInput, ClientOutput>,
80+
configuration: ResolvedClientConfiguration & {
81+
logger: Logger;
82+
requestHandler: RequestHandler<any, any, any>;
83+
},
84+
options: any
85+
) => {
86+
return this.__resolveMiddleware(clientStack, configuration, options, args);
87+
};
88+
},
89+
};
90+
}
91+
92+
/**
93+
* @internal
94+
*/
95+
protected __resolveMiddleware(
96+
clientStack: IMiddlewareStack<ClientInput, ClientOutput>,
97+
configuration: ResolvedClientConfiguration & { logger: Logger; requestHandler: RequestHandler<any, any, any> },
98+
options: any,
99+
{
100+
middlewareQueue,
101+
clientName,
102+
commandName,
103+
service,
104+
operation,
105+
inputFilterSensitiveLog,
106+
outputFilterSensitiveLog,
107+
}: ResolveMiddlewareContextArgs
108+
) {
109+
for (const mw of middlewareQueue) {
110+
this.middlewareStack.use(mw);
111+
}
112+
const stack = clientStack.concat(this.middlewareStack);
113+
const { logger } = configuration;
114+
const handlerExecutionContext: HandlerExecutionContext = {
115+
logger,
116+
clientName,
117+
commandName,
118+
inputFilterSensitiveLog,
119+
outputFilterSensitiveLog,
120+
[SMITHY_CONTEXT_KEY]: {
121+
service,
122+
operation,
123+
},
124+
};
125+
const { requestHandler } = configuration;
126+
return stack.resolve(
127+
(request: FinalizeHandlerArguments<any>) =>
128+
requestHandler.handle(request.request as __HttpRequest, options || {}),
129+
handlerExecutionContext
130+
);
131+
}
21132
}
133+
134+
/**
135+
* @internal
136+
*/
137+
type ResolveMiddlewareContextArgs = {
138+
middlewareQueue: Pluggable<any, any>[];
139+
clientName: string;
140+
commandName: string;
141+
service: string;
142+
operation: string;
143+
inputFilterSensitiveLog: (_: any) => any;
144+
outputFilterSensitiveLog: (_: any) => any;
145+
};

0 commit comments

Comments
 (0)