Skip to content

Commit e30bd0f

Browse files
committed
feat: move client.use() to client.middlewarestack.use()
1 parent 885a226 commit e30bd0f

File tree

14 files changed

+205
-124
lines changed

14 files changed

+205
-124
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { contentLengthPlugin } from "@aws-sdk/middleware-content-length";
2+
import {
3+
userAgentPlugin,
4+
UserAgentConfig
5+
} from "@aws-sdk/middleware-user-agent";
6+
import { retryPlugin, RetryConfig } from "@aws-sdk/retry-middleware";
7+
import {
8+
awsAuthPlugin,
9+
AwsAuthConfiguration
10+
} from "@aws-sdk/signing-middleware";
11+
import {
12+
RDSDataConfiguration,
13+
RDSDataResolvedConfiguration
14+
} from "./RDSDataConfiguration";
15+
import { RDSRuntimeConfiguration } from "./runtimeConfig";
16+
import {
17+
RegionConfiguration,
18+
EndpointsConfig,
19+
ProtocolConfig
20+
} from "@aws-sdk/config-resolver";
21+
import { HttpOptions, MetadataBearer } from "@aws-sdk/types";
22+
import { Client as SmithyClient } from "@aws-sdk/smithy-client";
23+
24+
type InputTypesUnion = any;
25+
type OutputTypesUnion = MetadataBearer;
26+
27+
export class RDSDataClient extends SmithyClient<
28+
HttpOptions,
29+
InputTypesUnion,
30+
OutputTypesUnion
31+
> {
32+
readonly config: RDSDataResolvedConfiguration;
33+
34+
constructor(configuration: RDSDataConfiguration) {
35+
const intermediaConfig_0 = ProtocolConfig.resolve({
36+
...RDSRuntimeConfiguration,
37+
...configuration
38+
});
39+
let intermediaConfig_1 = RegionConfiguration.resolve(intermediaConfig_0);
40+
let intermediaConfig_2 = AwsAuthConfiguration.resolve(intermediaConfig_1);
41+
let intermediaConfig_3 = EndpointsConfig.resolve(intermediaConfig_2);
42+
let intermediaConfig_4 = RetryConfig.resolve(intermediaConfig_3);
43+
let intermediaConfig_5 = UserAgentConfig.resolve(intermediaConfig_4);
44+
super(intermediaConfig_0);
45+
this.config = intermediaConfig_5;
46+
this.middlewareStack.use(contentLengthPlugin(this.config));
47+
this.middlewareStack.use(retryPlugin(this.config));
48+
this.middlewareStack.use(awsAuthPlugin(this.config));
49+
this.middlewareStack.use(userAgentPlugin(this.config));
50+
}
51+
52+
destroy(): void {
53+
if (typeof this.config.httpHandler.destroy === "function") {
54+
this.config.httpHandler.destroy();
55+
}
56+
}
57+
}

clients/node/client-rds-data-node/commands/ExecuteStatementCommand.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ export class ExecuteStatementCommand extends Command<
3636
protocol: { handler }
3737
} = configuration;
3838

39-
this.use(serdePlugin(configuration, this.serialize, this.deserialize));
39+
this.middlewareStack.use(
40+
serdePlugin(configuration, this.serialize, this.deserialize)
41+
);
4042

4143
const stack = clientStack.concat(this.middlewareStack);
4244

packages/middleware-content-length/src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ export function contentLengthMiddleware(
4444
};
4545
}
4646

47-
export namespace ContentLength {
48-
export const setMiddleware = (options: {
49-
bodyLengthChecker: BodyLengthCalculator;
50-
}): Pluggable<any, any> => clientStack => {
47+
export const getContentLengthPlugin = (options: {
48+
bodyLengthChecker: BodyLengthCalculator;
49+
}): Pluggable<any, any> => ({
50+
applyToStack: clientStack => {
5151
clientStack.add(contentLengthMiddleware(options.bodyLengthChecker), {
5252
step: "build",
5353
tags: { SET_CONTENT_LENGTH: true }
5454
});
55-
};
56-
}
55+
}
56+
});

packages/middleware-serde/src/serdePlugin.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@ export function serdePlugin<
2121
serializer: RequestSerializer<any, SerializerRuntimeUtils>,
2222
deserializer: ResponseDeserializer<OutputType, any, DeserializerRuntimeUtils>
2323
): Pluggable<InputType, OutputType> {
24-
return (commandStack: MiddlewareStack<InputType, OutputType>) => {
25-
commandStack.add(deserializerMiddleware(config, deserializer), {
26-
step: "deserialize",
27-
tags: { DESERIALIZER: true }
28-
});
29-
commandStack.add(serializerMiddleware(config, serializer), {
30-
step: "serialize",
31-
tags: { SERIALIZER: true }
32-
});
24+
return {
25+
applyToStack: (commandStack: MiddlewareStack<InputType, OutputType>) => {
26+
commandStack.add(deserializerMiddleware(config, deserializer), {
27+
step: "deserialize",
28+
tags: { DESERIALIZER: true }
29+
});
30+
commandStack.add(serializerMiddleware(config, serializer), {
31+
step: "serialize",
32+
tags: { SERIALIZER: true }
33+
});
34+
}
3335
};
3436
}

packages/middleware-signing/src/configurations.ts

Lines changed: 43 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,58 @@ import {
22
RequestSigner,
33
Credentials,
44
Provider,
5-
HashConstructor,
6-
Pluggable
5+
HashConstructor
76
} from "@aws-sdk/types";
87
import { SignatureV4 } from "@aws-sdk/signature-v4";
9-
import { signingMiddleware } from "./middleware";
108

11-
export namespace AwsAuth {
12-
export interface Input {
13-
/**
14-
* The credentials used to sign requests.
15-
*/
16-
credentials?: Credentials | Provider<Credentials>;
9+
export interface AwsAuthConfigInput {
10+
/**
11+
* The credentials used to sign requests.
12+
*/
13+
credentials?: Credentials | Provider<Credentials>;
1714

18-
/**
19-
* The signer to use when signing requests.
20-
*/
21-
signer?: RequestSigner;
15+
/**
16+
* The signer to use when signing requests.
17+
*/
18+
signer?: RequestSigner;
2219

23-
/**
24-
* Whether to escape request path when signing the request
25-
*/
26-
signingEscapePath?: boolean;
27-
}
28-
interface PreviouslyResolved {
29-
credentialDefaultProvider: (input: any) => Provider<Credentials>;
30-
region: string | Provider<string>;
31-
signingName: string;
32-
sha256: HashConstructor;
33-
}
34-
export interface Resolved {
35-
credentials: Provider<Credentials>;
36-
signer: RequestSigner;
37-
signingEscapePath: boolean;
38-
}
39-
export function resolveConfig<T>(
40-
input: T & Input & PreviouslyResolved
41-
): T & Resolved {
42-
let credentials =
43-
input.credentials || input.credentialDefaultProvider(input as any);
44-
const normalizedCreds = normalizeProvider(credentials);
45-
const signingEscapePath = input.signingEscapePath || false;
46-
return {
47-
...input,
48-
signingEscapePath,
20+
/**
21+
* Whether to escape request path when signing the request
22+
*/
23+
signingEscapePath?: boolean;
24+
}
25+
interface PreviouslyResolved {
26+
credentialDefaultProvider: (input: any) => Provider<Credentials>;
27+
region: string | Provider<string>;
28+
signingName: string;
29+
sha256: HashConstructor;
30+
}
31+
export interface AwsAuthConfigResolved {
32+
credentials: Provider<Credentials>;
33+
signer: RequestSigner;
34+
signingEscapePath: boolean;
35+
}
36+
export function resolveAwsAuthConfig<T>(
37+
input: T & AwsAuthConfigInput & PreviouslyResolved
38+
): T & AwsAuthConfigResolved {
39+
let credentials =
40+
input.credentials || input.credentialDefaultProvider(input as any);
41+
const normalizedCreds = normalizeProvider(credentials);
42+
const signingEscapePath = input.signingEscapePath || false;
43+
return {
44+
...input,
45+
signingEscapePath,
46+
credentials: normalizedCreds,
47+
signer: new SignatureV4({
4948
credentials: normalizedCreds,
50-
signer: new SignatureV4({
51-
credentials: normalizedCreds,
52-
region: input.region,
53-
service: input.signingName,
54-
sha256: input.sha256,
55-
uriEscapePath: signingEscapePath
56-
})
57-
};
58-
}
59-
60-
export const setMiddleware = (
61-
options: AwsAuth.Resolved
62-
): Pluggable<any, any> => clientStack => {
63-
clientStack.add(signingMiddleware(options), {
64-
step: "finalizeRequest",
65-
tags: { SIGNATURE: true }
66-
});
49+
region: input.region,
50+
service: input.signingName,
51+
sha256: input.sha256,
52+
uriEscapePath: signingEscapePath
53+
})
6754
};
6855
}
6956

70-
//export separately for showing comment block in Intellisense
71-
export type AwsAuthInput = AwsAuth.Input;
72-
7357
function normalizeProvider<T>(input: T | Provider<T>): Provider<T> {
7458
if (typeof input === "object") {
7559
const promisified = Promise.resolve(input);

packages/middleware-signing/src/middleware.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import {
44
FinalizeRequestMiddleware,
55
FinalizeHandlerOutput
66
} from "@aws-sdk/types";
7-
import { AwsAuth } from "./configurations";
7+
import { AwsAuthConfigResolved } from "./configurations";
88
import { HttpRequest } from "@aws-sdk/protocol-http";
99

1010
export function signingMiddleware<Input extends object, Output extends object>(
11-
options: AwsAuth.Resolved
11+
options: AwsAuthConfigResolved
1212
): FinalizeRequestMiddleware<Input, Output> {
1313
return (
1414
next: FinalizeHandler<Input, Output>
@@ -23,3 +23,14 @@ export function signingMiddleware<Input extends object, Output extends object>(
2323
});
2424
};
2525
}
26+
27+
export const getAwsAuthPlugin = (
28+
options: AwsAuthConfigResolved
29+
): Pluggable<any, any> => ({
30+
applyToStack: clientStack => {
31+
clientStack.add(signingMiddleware(options), {
32+
step: "finalizeRequest",
33+
tags: { SIGNATURE: true }
34+
});
35+
}
36+
});

packages/middleware-stack/src/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import {
22
BuildHandlerOptions,
3-
FinalizeHandler,
43
FinalizeRequestHandlerOptions,
54
SerializeMiddleware,
65
FinalizeRequestMiddleware,
7-
BuildMiddleware,
86
Handler,
97
HandlerExecutionContext,
108
HandlerOptions,
@@ -14,7 +12,8 @@ import {
1412
Step,
1513
DeserializeMiddleware,
1614
DeserializeHandlerOptions,
17-
DeserializeHandler
15+
DeserializeHandler,
16+
Pluggable
1817
} from "@aws-sdk/types";
1918

2019
interface HandlerListEntry<Input extends object, Output extends object> {
@@ -70,6 +69,10 @@ export class MiddlewareStack<Input extends object, Output extends object> {
7069
});
7170
}
7271

72+
use(pluggable: Pluggable<Input, Output>) {
73+
pluggable.applyToStack(this);
74+
}
75+
7376
clone(): IMiddlewareStack<Input, Output> {
7477
const clone = new MiddlewareStack<Input, Output>();
7578
clone.entries.push(...this.entries);
Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,18 @@
1-
import { Pluggable } from "@aws-sdk/types";
2-
import { userAgentMiddleware } from "./middleware";
3-
4-
export namespace UserAgent {
5-
export interface Input {
6-
/**
7-
* The custom user agent header that would be appended to default one
8-
*/
9-
customUserAgent?: string;
10-
}
11-
export interface PreviouslyResolved {
12-
defaultUserAgent: string;
13-
}
14-
export interface Resolved {
15-
defaultUserAgent: string;
16-
customUserAgent?: string;
17-
}
18-
export function resolveConfig<T>(
19-
input: T & PreviouslyResolved & Input
20-
): T & Resolved {
21-
return input;
22-
}
23-
24-
export const setMiddleware = (
25-
config: UserAgent.Resolved
26-
): Pluggable<any, any> => clientStack => {
27-
clientStack.add(userAgentMiddleware(config), {
28-
step: "build",
29-
tags: { SET_USER_AGENT: true }
30-
});
31-
};
1+
export interface UserAgentConfigInput {
2+
/**
3+
* The custom user agent header that would be appended to default one
4+
*/
5+
customUserAgent?: string;
6+
}
7+
interface PreviouslyResolved {
8+
defaultUserAgent: string;
9+
}
10+
export interface UserAgentConfigResolved {
11+
defaultUserAgent: string;
12+
customUserAgent?: string;
13+
}
14+
export function resolveConfig<T>(
15+
input: T & PreviouslyResolved & UserAgentConfigInput
16+
): T & UserAgentConfigResolved {
17+
return input;
3218
}
33-
34-
export type UserAgentInput = UserAgent.Input;

packages/middleware-user-agent/src/middleware.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import {
66
Pluggable
77
} from "@aws-sdk/types";
88
import { HttpRequest } from "@aws-sdk/protocol-http";
9-
import { UserAgent } from "./configurations";
9+
import { UserAgentConfigResolved } from "./configurations";
1010

1111
const userAgentHeader = "User-Agent";
1212

13-
export function userAgentMiddleware(options: UserAgent.Resolved) {
13+
export function userAgentMiddleware(options: UserAgentConfigResolved) {
1414
return <Output extends MetadataBearer>(
1515
next: BuildHandler<any, any>
1616
): BuildHandler<any, any> => (
@@ -33,3 +33,14 @@ export function userAgentMiddleware(options: UserAgent.Resolved) {
3333
});
3434
};
3535
}
36+
37+
export const userAgentPlugin = (
38+
config: UserAgentConfigResolved
39+
): Pluggable<any, any> => ({
40+
applyToStack: clientStack => {
41+
clientStack.add(userAgentMiddleware(config), {
42+
step: "build",
43+
tags: { SET_USER_AGENT: true }
44+
});
45+
}
46+
});

0 commit comments

Comments
 (0)