Skip to content

Commit

Permalink
feat: resolve method helper for Command
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Dec 19, 2023
1 parent 3ad9987 commit f323ce6
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/quick-dolphins-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/smithy-client": minor
---

resolve method builder for Command class
126 changes: 125 additions & 1 deletion packages/smithy-client/src/command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { constructStack } from "@smithy/middleware-stack";
import { Command as ICommand, Handler, MetadataBearer, MiddlewareStack as IMiddlewareStack } from "@smithy/types";
import type { HttpRequest as __HttpRequest } from "@smithy/protocol-http";
import type {
Command as ICommand,
FinalizeHandlerArguments,
Handler,
HandlerExecutionContext,
Logger,
MetadataBearer,
MiddlewareStack as IMiddlewareStack,
Pluggable,
RequestHandler,
} from "@smithy/types";
import { SMITHY_CONTEXT_KEY } from "@smithy/types";

/**
* @public
Expand All @@ -13,9 +25,121 @@ export abstract class Command<
> implements ICommand<ClientInput, Input, ClientOutput, Output, ResolvedClientConfiguration> {
abstract input: Input;
readonly middlewareStack: IMiddlewareStack<Input, Output> = constructStack<Input, Output>();

abstract resolveMiddleware(
stack: IMiddlewareStack<ClientInput, ClientOutput>,
configuration: ResolvedClientConfiguration,
options: any
): Handler<Input, Output>;

/**
* @internal
*/
protected resolveBuilder() {
const args: ResolveMiddlewareContextArgs = {
middlewareQueue: [] as Pluggable<any, any>[],
commandName: "",
clientName: "",
service: "",
operation: "",
inputFilterSensitiveLog: () => {},
outputFilterSensitiveLog: () => {},
};
return {
/**
* Add any number of middleware.
*/
m(...middleware: Pluggable<any, any>[]) {
args.middlewareQueue.push(...middleware);
return this;
},
/**
* Set constant string identifiers for the operation.
*/
n(clientName: string, commandName: string, service: string, operation: string) {
args.clientName = clientName;
args.commandName = commandName;
args.service = service;
args.operation = operation;
return this;
},
/**
* Set the input and output sensistive log filters.
*/
f(inputFilter: (_: any) => any = (_) => _, outputFilter: (_: any) => any = (_) => _) {
args.inputFilterSensitiveLog = inputFilter;
args.outputFilterSensitiveLog = outputFilter;
return this;
},
/**
* @returns the implementation of the built resolveMiddleware function.
*/
build: () => {
return (
clientStack: IMiddlewareStack<ClientInput, ClientOutput>,
configuration: ResolvedClientConfiguration & {
logger: Logger;
requestHandler: RequestHandler<any, any, any>;
},
options: any
) => {
return this.__resolveMiddleware(clientStack, configuration, options, args);
};
},
};
}

/**
* @internal
*/
protected __resolveMiddleware(
clientStack: IMiddlewareStack<ClientInput, ClientOutput>,
configuration: ResolvedClientConfiguration & { logger: Logger; requestHandler: RequestHandler<any, any, any> },
options: any,
{
middlewareQueue,
clientName,
commandName,
service,
operation,
inputFilterSensitiveLog,
outputFilterSensitiveLog,
}: ResolveMiddlewareContextArgs
) {
for (const mw of middlewareQueue) {
this.middlewareStack.use(mw);
}
const stack = clientStack.concat(this.middlewareStack);
const { logger } = configuration;
const handlerExecutionContext: HandlerExecutionContext = {
logger,
clientName,
commandName,
inputFilterSensitiveLog,
outputFilterSensitiveLog,
[SMITHY_CONTEXT_KEY]: {
service,
operation,
},
};
const { requestHandler } = configuration;
return stack.resolve(
(request: FinalizeHandlerArguments<any>) =>
requestHandler.handle(request.request as __HttpRequest, options || {}),
handlerExecutionContext
);
}
}

/**
* @internal
*/
type ResolveMiddlewareContextArgs = {
middlewareQueue: Pluggable<any, any>[];
clientName: string;
commandName: string;
service: string;
operation: string;
inputFilterSensitiveLog: (_: any) => any;
outputFilterSensitiveLog: (_: any) => any;
};

0 comments on commit f323ce6

Please sign in to comment.