Skip to content

Commit

Permalink
feat: add and apply accountIdDefaultMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Chase Coalwell committed Dec 10, 2019
1 parent 724370e commit 691abb3
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,15 @@ public List<RuntimeClientPlugin> getClientPlugins() {
.servicePredicate((m, s) -> testServiceId(s, "S3"))
.build(),
RuntimeClientPlugin.builder()
.withConventions(AwsDependency.ADD_GLACIER_API_VERSION.dependency,
.withConventions(AwsDependency.GLACIER_MIDDLEWARE.dependency,
"AddGlacierApiVersion", HAS_MIDDLEWARE)
.servicePredicate((m, s) -> testServiceId(s, "Glacier"))
.build(),
RuntimeClientPlugin.builder()
.withConventions(AwsDependency.GLACIER_MIDDLEWARE.dependency,
"AccountIdDefault", HAS_MIDDLEWARE)
.servicePredicate((m, s) -> testServiceId(s, "Glacier"))
.build(),
RuntimeClientPlugin.builder()
.withConventions(AwsDependency.SSEC_MIDDLEWARE.dependency, "Ssec", HAS_MIDDLEWARE)
.servicePredicate((m, s) -> testServiceId(s, "S3"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public enum AwsDependency implements SymbolDependencyContainer {
ACCEPTS_HEADER(NORMAL_DEPENDENCY, "@aws-sdk/middleware-sdk-api-gateway", "^0.1.0-preview.5"),
VALIDATE_BUCKET_NAME(NORMAL_DEPENDENCY, "@aws-sdk/middleware-sdk-s3", "^0.1.0-preview.2"),
ADD_EXPECT_CONTINUE(NORMAL_DEPENDENCY, "@aws-sdk/middleware-expect-continue", "^0.1.0-preview.5"),
ADD_GLACIER_API_VERSION(NORMAL_DEPENDENCY, "@aws-sdk/middleware-sdk-glacier", "^0.1.0-preview.7"),
GLACIER_MIDDLEWARE(NORMAL_DEPENDENCY, "@aws-sdk/middleware-sdk-glacier", "^0.1.0-preview.7"),
SSEC_MIDDLEWARE(NORMAL_DEPENDENCY, "@aws-sdk/middleware-ssec", "^0.1.0-preview.5");

public final String packageName;
Expand Down
39 changes: 39 additions & 0 deletions packages/middleware-sdk-glacier/src/account-id-default.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { accountIdDefaultMiddleware } from "./account-id-default";

describe("accountIdDefaultMiddleware", () => {
const next = jest.fn();

beforeEach(() => {
jest.clearAllMocks();
});

it("adds default accountId if not set on input", async () => {
const handler = accountIdDefaultMiddleware()(next, {} as any);
await handler({
input: {}
});

expect(next.mock.calls.length).toBe(1);
expect(next).toHaveBeenCalledWith({
input: {
accountId: "-"
}
});
});

it("does not change accountId input if set previously", async () => {
const handler = accountIdDefaultMiddleware()(next, {} as any);
await handler({
input: {
accountId: "1234"
}
});

expect(next.mock.calls.length).toBe(1);
expect(next).toHaveBeenCalledWith({
input: {
accountId: "1234"
}
});
});
});
40 changes: 40 additions & 0 deletions packages/middleware-sdk-glacier/src/account-id-default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
InitializeHandler,
InitializeHandlerArguments,
InitializeHandlerOptions,
InitializeHandlerOutput,
InitializeMiddleware,
MetadataBearer,
Pluggable
} from "@aws-sdk/types";

export function accountIdDefaultMiddleware(): InitializeMiddleware<any, any> {
return <Output extends MetadataBearer>(
next: InitializeHandler<any, Output>
): InitializeHandler<any, Output> => async (
args: InitializeHandlerArguments<any>
): Promise<InitializeHandlerOutput<Output>> => {
const { input } = args;
if (input.accountId === undefined) {
input.accountId = "-";
}
return next({ ...args, input });
};
}

export const accountIdDefaultMiddlewareOptions: InitializeHandlerOptions = {
step: "initialize",
tags: ["ACCOUNT_ID_DEFAULT"],
name: "accountIdDefaultMiddleware"
};

export const getAccountIdDefaultPlugin = (
unused: any
): Pluggable<any, any> => ({
applyToStack: clientStack => {
clientStack.add(
accountIdDefaultMiddleware(),
accountIdDefaultMiddlewareOptions
);
}
});
9 changes: 8 additions & 1 deletion packages/middleware-sdk-glacier/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { addGlacierApiVersionMiddleware } from "./index";
import {
accountIdDefaultMiddleware,
addGlacierApiVersionMiddleware
} from "./index";

describe("middleware-sdk-glacier package exports", () => {
it("addGlacierApiVersionMiddleware", () => {
expect(typeof addGlacierApiVersionMiddleware).toBe("function");
});

it("accountIdDefaultMiddleware", () => {
expect(typeof accountIdDefaultMiddleware).toBe("function");
});
});
1 change: 1 addition & 0 deletions packages/middleware-sdk-glacier/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./add-glacier-api-version";
export * from "./account-id-default";

0 comments on commit 691abb3

Please sign in to comment.