Skip to content

Commit 679f804

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

File tree

2 files changed

+118
-1
lines changed

2 files changed

+118
-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: 113 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,109 @@ 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+
m(...middleware: Pluggable<any, any>[]) {
50+
args.middlewareQueue.push(...middleware);
51+
return this;
52+
},
53+
n(clientName: string, commandName: string, service: string, operation: string) {
54+
args.clientName = clientName;
55+
args.commandName = commandName;
56+
args.service = service;
57+
args.operation = operation;
58+
return this;
59+
},
60+
f(inputFilter: (_: any) => any = (_) => _, outputFilter: (_: any) => any = (_) => _) {
61+
args.inputFilterSensitiveLog = inputFilter;
62+
args.outputFilterSensitiveLog = outputFilter;
63+
return this;
64+
},
65+
build: () => {
66+
return (
67+
clientStack: IMiddlewareStack<ClientInput, ClientOutput>,
68+
configuration: ResolvedClientConfiguration & {
69+
logger: Logger;
70+
requestHandler: RequestHandler<any, any, any>;
71+
},
72+
options: any
73+
) => {
74+
this.__resolveMiddleware(clientStack, configuration, options, args);
75+
};
76+
},
77+
};
78+
}
79+
80+
/**
81+
* @internal
82+
*/
83+
protected __resolveMiddleware(
84+
clientStack: IMiddlewareStack<ClientInput, ClientOutput>,
85+
configuration: ResolvedClientConfiguration & { logger: Logger; requestHandler: RequestHandler<any, any, any> },
86+
options: any,
87+
{
88+
middlewareQueue,
89+
clientName,
90+
commandName,
91+
service,
92+
operation,
93+
inputFilterSensitiveLog,
94+
outputFilterSensitiveLog,
95+
}: ResolveMiddlewareContextArgs
96+
) {
97+
for (const mw of middlewareQueue) {
98+
this.middlewareStack.use(mw);
99+
}
100+
const stack = clientStack.concat(this.middlewareStack);
101+
const { logger } = configuration;
102+
const handlerExecutionContext: HandlerExecutionContext = {
103+
logger,
104+
clientName,
105+
commandName,
106+
inputFilterSensitiveLog,
107+
outputFilterSensitiveLog,
108+
[SMITHY_CONTEXT_KEY]: {
109+
service,
110+
operation,
111+
},
112+
};
113+
const { requestHandler } = configuration;
114+
return stack.resolve(
115+
(request: FinalizeHandlerArguments<any>) =>
116+
requestHandler.handle(request.request as __HttpRequest, options || {}),
117+
handlerExecutionContext
118+
);
119+
}
21120
}
121+
122+
/**
123+
* @internal
124+
*/
125+
type ResolveMiddlewareContextArgs = {
126+
middlewareQueue: Pluggable<any, any>[];
127+
clientName: string;
128+
commandName: string;
129+
service: string;
130+
operation: string;
131+
inputFilterSensitiveLog: (_: any) => any;
132+
outputFilterSensitiveLog: (_: any) => any;
133+
};

0 commit comments

Comments
 (0)