Skip to content

Commit

Permalink
fix: change plugin interface to callback functions mutating the stack
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanZhengYP committed Oct 15, 2019
1 parent 8dc2d59 commit 23bd1fc
Showing 9 changed files with 49 additions and 133 deletions.
2 changes: 1 addition & 1 deletion clients/node/client-rds-data-node/RDSDataClient.ts
Original file line number Diff line number Diff line change
@@ -22,12 +22,12 @@ export class RDSDataClient extends SmithyClient<HttpOptions, InputTypesUnion, Ou
...RDSRuntimeConfiguration,
...configuration
});
super(intermediaConfig_0);
let intermediaConfig_1 = RegionConfiguration.resolve(intermediaConfig_0);
let intermediaConfig_2 = AwsAuthConfiguration.resolve(intermediaConfig_1);
let intermediaConfig_3 = EndpointsConfig.resolve(intermediaConfig_2);
let intermediaConfig_4 = RetryConfig.resolve(intermediaConfig_3);
let intermediaConfig_5 = UserAgentConfig.resolve(intermediaConfig_4);
super(intermediaConfig_0);
this.config = intermediaConfig_5;
super.use(contentLengthPlugin(this.config));
super.use(retryPlugin(this.config));
15 changes: 6 additions & 9 deletions packages/middleware-content-length/src/index.ts
Original file line number Diff line number Diff line change
@@ -46,12 +46,9 @@ export function contentLengthMiddleware(

export const contentLengthPlugin = (options: {
bodyLengthChecker: BodyLengthCalculator;
}): Injectable<any, any> => ({
injectedMiddleware: [
{
middleware: contentLengthMiddleware(options.bodyLengthChecker),
step: "build",
tags: { SET_CONTENT_LENGTH: true }
}
]
});
}): Injectable<any, any> => clientStack => {
clientStack.add(contentLengthMiddleware(options.bodyLengthChecker), {
step: "build",
tags: { SET_CONTENT_LENGTH: true }
});
};
25 changes: 11 additions & 14 deletions packages/middleware-serde/src/serdePlugin.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,8 @@ import {
ResponseDeserializer,
Injectable,
Protocol,
MetadataBearer
MetadataBearer,
MiddlewareStack
} from "@aws-sdk/types";
import { deserializerMiddleware } from "./deserializerMiddleware";
import { serializerMiddleware } from "./serializerMiddleware";
@@ -19,18 +20,14 @@ export function serdePlugin<
serializer: RequestSerializer<any, SerializerRuntimeUtils>,
deserializer: ResponseDeserializer<OutputType, any, DeserializerRuntimeUtils>
): Injectable<InputType, OutputType> {
return {
injectedMiddleware: [
{
middleware: deserializerMiddleware(config, deserializer),
step: "deserialize",
tags: { DESERIALIZER: true }
},
{
middleware: serializerMiddleware(config, serializer),
step: "serialize",
tags: { SERIALIZER: true }
}
]
return (commandStack: MiddlewareStack<InputType, OutputType>) => {
commandStack.add(deserializerMiddleware(config, deserializer), {
step: "deserialize",
tags: { DESERIALIZER: true }
});
commandStack.add(serializerMiddleware(config, serializer), {
step: "serialize",
tags: { SERIALIZER: true }
});
};
}
19 changes: 8 additions & 11 deletions packages/middleware-user-agent/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ import { UserAgentConfig } from "./configurations";

const userAgentHeader = "User-Agent";

export function UserAgentMiddleware(options: UserAgentConfig.Resolved) {
export function userAgentMiddleware(options: UserAgentConfig.Resolved) {
return <Output extends MetadataBearer>(
next: BuildHandler<any, any>
): BuildHandler<any, any> => (
@@ -35,13 +35,10 @@ export function UserAgentMiddleware(options: UserAgentConfig.Resolved) {
}

export const userAgentPlugin = (
options: UserAgentConfig.Resolved
): Injectable<any, any> => ({
injectedMiddleware: [
{
middleware: UserAgentMiddleware(options),
step: "build",
tags: { SET_USER_AGENT: true }
}
]
});
config: UserAgentConfig.Resolved
): Injectable<any, any> => clientStack => {
clientStack.add(userAgentMiddleware(config), {
step: "build",
tags: { SET_USER_AGENT: true }
});
};
20 changes: 8 additions & 12 deletions packages/retry-middleware/src/retryMiddleware.ts
Original file line number Diff line number Diff line change
@@ -19,15 +19,11 @@ export function retryMiddleware(options: RetryConfig.Resolved) {

export const retryPlugin = (
options: RetryConfig.Resolved
): Injectable<any, any> => ({
injectedMiddleware:
options.maxRetries > 0
? [
{
middleware: retryMiddleware(options),
step: "finalizeRequest",
tags: { RETRY: true }
}
]
: []
});
): Injectable<any, any> => clientStack => {
if (options.maxRetries > 0) {
clientStack.add(retryMiddleware(options), {
step: "finalizeRequest",
tags: { RETRY: true }
});
}
};
15 changes: 6 additions & 9 deletions packages/signing-middleware/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -27,12 +27,9 @@ export function signingMiddleware<Input extends object, Output extends object>(

export const signingPlugin = (
options: AwsAuthConfiguration.Resolved
): Injectable<any, any> => ({
injectedMiddleware: [
{
middleware: signingMiddleware(options),
step: "finalizeRequest",
tags: { SIGNATURE: true }
}
]
});
): Injectable<any, any> => clientStack => {
clientStack.add(signingMiddleware(options), {
step: "finalizeRequest",
tags: { SIGNATURE: true }
});
};
25 changes: 3 additions & 22 deletions packages/smithy-client/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { MiddlewareStack } from "@aws-sdk/middleware-stack";
import {
Protocol,
Command,
MetadataBearer,
Injectable,
HandlerOptions as InjectOptions
} from "@aws-sdk/types";
import { Protocol, Command, MetadataBearer, Injectable } from "@aws-sdk/types";

export interface SmithyConfiguration<HandlerOptions> {
protocol: Protocol<any, any, HandlerOptions>;
@@ -25,21 +19,8 @@ export class Client<
constructor(config: SmithyConfiguration<HandlerOptions>) {
this.config = config;
}
use(
injectable: Injectable<ClientInput, ClientOutput>,
injectableOverrider?: (options: InjectOptions) => InjectOptions
) {
if (injectable.toRemove && injectable.toRemove.length > 0) {
for (const toRemove of injectable.toRemove)
this.middlewareStack.remove(toRemove);
}
for (const { middleware, ...options } of injectable.injectedMiddleware) {
this.middlewareStack.add(
// @ts-ignore -- Middleware and option type matching safety is enforced by InjectableMiddleware types
middleware,
injectableOverrider ? injectableOverrider(options) : options
);
}
use(injectable: Injectable<ClientInput, ClientOutput>) {
injectable(this.middlewareStack);
}
send<InputType extends ClientInput, OutputType extends ClientOutput>(
command: Command<
17 changes: 2 additions & 15 deletions packages/smithy-client/src/command.ts
Original file line number Diff line number Diff line change
@@ -3,20 +3,7 @@ import { Injectable, HandlerOptions as InjectOptions } from "@aws-sdk/types";

export class Command<InputType extends object, OutputType extends object> {
readonly middlewareStack = new MiddlewareStack<InputType, OutputType>();
use(
injectable: Injectable<InputType, OutputType>,
injectableOverrider?: (options: InjectOptions) => InjectOptions
) {
if (injectable.toRemove && injectable.toRemove.length > 0) {
for (const toRemove of injectable.toRemove)
this.middlewareStack.remove(toRemove);
}
for (const { middleware, ...options } of injectable.injectedMiddleware) {
this.middlewareStack.add(
// @ts-ignore -- Middleware and option type matching safety is enforced by InjectableMiddleware types
middleware,
injectableOverrider ? injectableOverrider(options) : options
);
}
use(injectable: Injectable<InputType, OutputType>) {
injectable(this.middlewareStack);
}
}
44 changes: 4 additions & 40 deletions packages/types/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -366,47 +366,11 @@ export interface HandlerExecutionContext {
logger: Logger;
}

export type InjectableMiddleware<
Input extends object = any,
Output extends object = any
> =
| {
middleware: Middleware<Input, Output>;
step: "initialize";
priority?: number;
tags?: { [tag: string]: any };
}
| {
middleware: SerializeMiddleware<Input, Output>;
step: "serialize";
priority?: number;
tags?: { [tag: string]: any };
}
| {
middleware: FinalizeRequestMiddleware<Input, Output>;
step: "build";
priority?: number;
tags?: { [tag: string]: any };
}
| {
middleware: FinalizeRequestMiddleware<Input, Output>;
step: "finalizeRequest";
priority?: number;
tags?: { [tag: string]: any };
}
| {
middleware: DeserializeMiddleware<Input, Output>;
step: "deserialize";
priority?: number;
tags?: { [tag: string]: any };
};

export interface Injectable<Input extends object, Output extends object> {
/* middleware to be injected to the middleware stack */
injectedMiddleware: Array<InjectableMiddleware>;
/**
* filter function that would apply to existing middleware stack. You can
* remove the middleware by reference or the tag name.
* A function that mutate the passed in middleware stack. Functions implementing
* this interface can add, remove, modify existing middleware stack from clients
* or commands
*/
toRemove?: Array<Middleware<Input, Output> | string>;
(stack: MiddlewareStack<Input, Output>): void;
}

0 comments on commit 23bd1fc

Please sign in to comment.