Skip to content

Commit df8176c

Browse files
committed
feat: update according to new mw stack & smithy client interface
1 parent b69cae6 commit df8176c

File tree

5 files changed

+79
-84
lines changed

5 files changed

+79
-84
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const base = require("../../jest.config.base.js");
2+
3+
module.exports = {
4+
...base
5+
};

packages/util-create-request/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
"license": "Apache-2.0",
1616
"dependencies": {
1717
"@aws-sdk/types": "^0.1.0-preview.7",
18+
"@aws-sdk/smithy-client": "^0.1.0-preview.1",
19+
"@aws-sdk/middleware-stack": "^0.1.0-preview.9",
1820
"tslib": "^1.8.0"
1921
},
2022
"devDependencies": {
2123
"@aws-sdk/middleware-stack": "^0.1.0-preview.9",
24+
"@aws-sdk/protocol-http": "^0.1.0-preview.1",
2225
"@types/jest": "^24.0.12",
2326
"@types/node": "^10.0.3",
2427
"jest": "^24.7.1",
Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
import {
2-
AWSClient,
3-
MetadataBearer,
4-
ClientResolvedConfigurationBase,
5-
Command,
6-
CommandInput,
7-
HttpRequest
8-
} from "@aws-sdk/types";
9-
import { Readable } from "stream";
1+
import { MetadataBearer } from "@aws-sdk/types";
102
import { MiddlewareStack } from "@aws-sdk/middleware-stack";
3+
import { Client, Command } from "@aws-sdk/smithy-client";
4+
import { HttpRequest } from "@aws-sdk/protocol-http";
115

12-
export interface OperationInput extends CommandInput {
6+
export interface OperationInput {
137
String: string;
148
}
159

@@ -26,52 +20,48 @@ const output: OperationOutput = { Data: "data", $metadata: {} };
2620

2721
const input: OperationInput = { String: "input" };
2822

29-
export const fooClient: AWSClient<
30-
InputTypesUnion,
31-
OutputTypesUnion,
32-
Readable
33-
> = {
23+
export const fooClient: Client<any, InputTypesUnion, OutputTypesUnion, any> = {
3424
config: {},
35-
middlewareStack: new MiddlewareStack<
36-
InputTypesUnion,
37-
OutputTypesUnion,
38-
Readable
39-
>(),
25+
middlewareStack: new MiddlewareStack<InputTypesUnion, OutputTypesUnion>(),
4026
send: (
4127
command: Command<
4228
InputTypesUnion,
43-
OperationInput,
4429
OutputTypesUnion,
45-
OperationOutput,
46-
ClientResolvedConfigurationBase<OutputTypesUnion, Readable>,
47-
Readable
30+
any,
31+
OperationInput,
32+
OperationOutput
4833
>
49-
) => command.resolveMiddleware(this.middlewareStack, this.config)({ input })
34+
) =>
35+
command.resolveMiddleware(
36+
this.middlewareStack,
37+
this.config,
38+
undefined
39+
)({ input })
5040
};
5141

5242
export const operationCommand: Command<
5343
InputTypesUnion,
54-
OperationInput,
5544
OutputTypesUnion,
56-
OperationOutput,
57-
ClientResolvedConfigurationBase<OutputTypesUnion, Readable>,
58-
Readable
45+
any,
46+
OperationInput,
47+
OperationOutput
5948
> = {
60-
middlewareStack: new MiddlewareStack<object, OutputTypesUnion, Readable>(),
61-
model: {} as any,
49+
middlewareStack: new MiddlewareStack<object, OutputTypesUnion>(),
6250
input: {} as any,
6351
resolveMiddleware: (
64-
stack: MiddlewareStack<InputTypesUnion, OutputTypesUnion, Readable>
52+
stack: MiddlewareStack<InputTypesUnion, OutputTypesUnion>,
53+
config: any,
54+
options: any
6555
) => {
66-
return () => Promise.resolve(output);
56+
return () => Promise.resolve({ output, response: {} });
6757
}
6858
};
6959

70-
export const httpRequest: HttpRequest<Readable> = {
60+
export const httpRequest = new HttpRequest({
7161
protocol: "https:",
7262
path: "/foo",
7363
hostname: "foo-service.us-east-1.amazonaws.com",
7464
headers: {},
7565
method: "GET",
7666
body: ""
77-
};
67+
});

packages/util-create-request/src/index.spec.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import {
88
} from "./foo.fixture";
99
import {
1010
MetadataBearer,
11-
AWSClient,
1211
SerializeHandlerArguments,
1312
BuildHandlerArguments,
1413
FinalizeHandlerArguments
1514
} from "@aws-sdk/types";
16-
import { Readable } from "stream";
15+
import { Client } from "@aws-sdk/smithy-client";
16+
import { HttpRequest } from "@aws-sdk/protocol-http";
1717

1818
describe("create-request", () => {
1919
it("should concat initialize and serialize middlewares from client and command", async () => {
@@ -32,7 +32,9 @@ describe("create-request", () => {
3232
);
3333
operationCommand.middlewareStack.add(
3434
next => (args: SerializeHandlerArguments<OperationInput>) => {
35-
args.request.body += "2";
35+
if (HttpRequest.isInstance(args.request)) {
36+
args.request.body += "2";
37+
}
3638
return next(args);
3739
},
3840
{
@@ -41,7 +43,9 @@ describe("create-request", () => {
4143
);
4244
operationCommand.middlewareStack.add(
4345
next => (args: BuildHandlerArguments<OperationInput>) => {
44-
args.request.body += "3";
46+
if (HttpRequest.isInstance(args.request)) {
47+
args.request.body += "3";
48+
}
4549
return next(args);
4650
},
4751
{
@@ -50,11 +54,13 @@ describe("create-request", () => {
5054
);
5155
operationCommand.middlewareStack.add(
5256
next => (args: FinalizeHandlerArguments<OperationInput>) => {
53-
args.request.body += "4";
57+
if (HttpRequest.isInstance(args.request)) {
58+
args.request.body += "4";
59+
}
5460
return next(args);
5561
},
5662
{
57-
step: "finalize"
63+
step: "finalizeRequest"
5864
}
5965
);
6066

@@ -73,7 +79,9 @@ describe("create-request", () => {
7379
);
7480
fooClient.middlewareStack.add(
7581
next => (args: SerializeHandlerArguments<OperationInput>) => {
76-
args.request.body += "B";
82+
if (HttpRequest.isInstance(args.request)) {
83+
args.request.body += "B";
84+
}
7785
return next(args);
7886
},
7987
{
@@ -82,7 +90,9 @@ describe("create-request", () => {
8290
);
8391
fooClient.middlewareStack.add(
8492
next => (args: BuildHandlerArguments<OperationInput>) => {
85-
args.request.body += "C";
93+
if (HttpRequest.isInstance(args.request)) {
94+
args.request.body += "C";
95+
}
8696
return next(args);
8797
},
8898
{
@@ -91,20 +101,22 @@ describe("create-request", () => {
91101
);
92102
fooClient.middlewareStack.add(
93103
next => (args: FinalizeHandlerArguments<OperationInput>) => {
94-
args.request.body += "D";
104+
if (HttpRequest.isInstance(args.request)) {
105+
args.request.body += "D";
106+
}
95107
return next(args);
96108
},
97109
{
98-
step: "finalize"
110+
step: "finalizeRequest"
99111
}
100112
);
101113
const request = await createRequest(
102-
fooClient as AWSClient<InputTypesUnion, MetadataBearer, Readable>,
114+
fooClient as Client<any, InputTypesUnion, MetadataBearer, any>,
103115
operationCommand
104116
);
105117
expect(request).toEqual({
106118
...httpRequest,
107-
body: "1A2B"
119+
body: "A1B2"
108120
});
109121
});
110122
});
Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,40 @@
11
import {
2-
CommandInput,
3-
FinalizeHandler,
4-
FinalizeHandlerArguments,
52
HttpRequest,
6-
AWSClient,
7-
Command,
83
MetadataBearer,
9-
MiddlewareStack
4+
SerializeMiddleware
105
} from "@aws-sdk/types";
6+
import { MiddlewareStack } from "@aws-sdk/middleware-stack";
7+
import { Client, Command } from "@aws-sdk/smithy-client";
118

129
export async function createRequest<
1310
InputTypesUnion extends object,
1411
InputType extends InputTypesUnion,
15-
StreamType,
1612
OutputType extends MetadataBearer = MetadataBearer
1713
>(
18-
client: AWSClient<InputTypesUnion, MetadataBearer, StreamType>,
19-
command: Command<
20-
InputTypesUnion,
14+
client: Client<any, InputTypesUnion, MetadataBearer, any>,
15+
command: Command<InputType, OutputType, any, InputTypesUnion, MetadataBearer>
16+
): Promise<HttpRequest> {
17+
const presignMiddleware: SerializeMiddleware<
2118
InputType,
22-
MetadataBearer,
23-
OutputType,
24-
any,
25-
StreamType
26-
>
27-
): Promise<HttpRequest<StreamType>> {
28-
const presignHandler: FinalizeHandler<
29-
any,
30-
HttpRequest<StreamType>,
31-
StreamType
32-
> = async (
33-
args: FinalizeHandlerArguments<CommandInput>
34-
): Promise<HttpRequest<StreamType>> => Promise.resolve(args.request);
19+
OutputType
20+
> = next => async args => {
21+
return { output: { request: args.request } as any, response: undefined };
22+
};
3523
const clientStack = client.middlewareStack.clone();
36-
const commandStack = (command.middlewareStack.clone() as unknown) as MiddlewareStack<
24+
const commandStack = command.middlewareStack.clone();
25+
const concatenatedStack = clientStack.concat(commandStack) as MiddlewareStack<
3726
InputType,
38-
any,
39-
StreamType
27+
OutputType
4028
>;
41-
const concatenatedStack = clientStack
42-
.concat(commandStack)
43-
.filter(middlewareStats => {
44-
return (
45-
middlewareStats.step === "initialize" ||
46-
middlewareStats.step === "serialize"
47-
);
48-
});
29+
//add middleware to the last of 'build' step
30+
concatenatedStack.add(presignMiddleware, {
31+
step: "serialize",
32+
priority: "low"
33+
});
4934

50-
const handler = concatenatedStack.resolve(presignHandler, {
51-
model: command.model,
35+
const handler = concatenatedStack.resolve((() => {}) as any, {
5236
logger: {} as any
5337
});
54-
return await handler(command);
38+
//@ts-ignore
39+
return await handler(command).then(output => output.output.request);
5540
}

0 commit comments

Comments
 (0)