Skip to content

Commit

Permalink
fix(util-create-request): fix creating undefined request (#803)
Browse files Browse the repository at this point in the history
* fix(util-create-request): fix creating undefined request

Use command.resolveMiddleware() to generate request instead of
concatenating middleware stacks manually. This fixes gh issue
also include `build` stage to creating request. Because build
stage sometimes contains necessary middleware to build a correct
request. For example, S3's bucket endpoint middleware is located
in build stage. Host header is also necessary, if users want to
sign the generated request directly.

* fix: remove middleware-stack dev dep
  • Loading branch information
AllanZhengYP committed Jan 28, 2020
1 parent defd342 commit b6a96c0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
1 change: 0 additions & 1 deletion packages/util-create-request/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"tslib": "^1.8.0"
},
"devDependencies": {
"@aws-sdk/middleware-stack": "^1.0.0-alpha.1",
"@aws-sdk/protocol-http": "^1.0.0-alpha.6",
"@types/jest": "^24.0.12",
"@types/node": "^10.0.3",
Expand Down
6 changes: 5 additions & 1 deletion packages/util-create-request/src/foo.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ export const operationCommand: Command<
config: any,
options: any
) => {
return () => Promise.resolve({ output, response: {} });
const concatStack = stack.concat(operationCommand.middlewareStack);
return concatStack.resolve(
() => Promise.resolve({ output, response: {} }),
{} as any
);
}
};

Expand Down
2 changes: 1 addition & 1 deletion packages/util-create-request/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe("create-request", () => {
);
expect(request).toEqual({
...httpRequest,
body: "A1B2"
body: "A1B2C3"
});
});
});
27 changes: 11 additions & 16 deletions packages/util-create-request/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
HttpRequest,
MetadataBearer,
SerializeMiddleware
} from "@aws-sdk/types";
import { HttpRequest, MetadataBearer, BuildMiddleware } from "@aws-sdk/types";
import { MiddlewareStack } from "@aws-sdk/middleware-stack";
import { Client, Command } from "@aws-sdk/smithy-client";

Expand All @@ -14,27 +10,26 @@ export async function createRequest<
client: Client<any, InputTypesUnion, MetadataBearer, any>,
command: Command<InputType, OutputType, any, InputTypesUnion, MetadataBearer>
): Promise<HttpRequest> {
const presignMiddleware: SerializeMiddleware<
const interceptMiddleware: BuildMiddleware<
InputType,
OutputType
> = next => async args => {
return { output: { request: args.request } as any, response: undefined };
};
const clientStack = client.middlewareStack.clone();
const commandStack = command.middlewareStack.clone();
const concatenatedStack = clientStack.concat(commandStack) as MiddlewareStack<
InputType,
OutputType
>;

//add middleware to the last of 'build' step
concatenatedStack.add(presignMiddleware, {
step: "serialize",
clientStack.add(interceptMiddleware, {
step: "build",
priority: "low"
});

const handler = concatenatedStack.resolve((() => {}) as any, {
logger: {} as any
});
const handler = command.resolveMiddleware(
clientStack as MiddlewareStack<any, any>,
client.config,
undefined
);

//@ts-ignore
return await handler(command).then(output => output.output.request);
}

0 comments on commit b6a96c0

Please sign in to comment.