Skip to content

Commit 342e6d6

Browse files
author
Chase Coalwell
committed
fix: get region from config
1 parent 028409e commit 342e6d6

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Provider } from "@aws-sdk/types";
2+
3+
export interface LocationConstraintInputConfig {}
4+
5+
interface PreviouslyResolved {
6+
region: string | Provider<string>;
7+
}
8+
9+
export interface LocationConstraintResolvedConfig {
10+
region: string | Provider<string>;
11+
}
12+
export function resolveLocationConstraintConfig<T>(
13+
input: T & LocationConstraintInputConfig & PreviouslyResolved
14+
): T & LocationConstraintResolvedConfig {
15+
return { ...input };
16+
}

packages/middleware-location-constraint/src/index.spec.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ describe("locationConstrainMiddleware", () => {
88
});
99

1010
it("should remove any CreateBucketConfiguration from requests directed at us-east-1", async () => {
11-
const handler = locationConstraintMiddleware()(next, {} as any);
11+
const handler = locationConstraintMiddleware({
12+
region: "us-east-1"
13+
})(next, {} as any);
1214
const input = {
1315
CreateBucketConfiguration: { LocationConstraint: "us-east-1" },
14-
foo: "bar",
15-
region: "us-east-1"
16+
foo: "bar"
1617
};
1718
await handler({ input });
1819

@@ -26,10 +27,11 @@ describe("locationConstrainMiddleware", () => {
2627
});
2728

2829
it("should apply a CreateBucketConfiguration with a LocationConstraint of the target region for requests directed outside of us-east-1", async () => {
29-
const handler = locationConstraintMiddleware()(next, {} as any);
30-
const input = {
31-
foo: "bar",
30+
const handler = locationConstraintMiddleware({
3231
region: "us-east-2"
32+
})(next, {} as any);
33+
const input = {
34+
foo: "bar"
3335
};
3436

3537
await handler({ input });
@@ -44,7 +46,9 @@ describe("locationConstrainMiddleware", () => {
4446
});
4547

4648
it("should do nothing if a LocationConstraint had already been set on a request directed outside of us-east-1", async () => {
47-
const handler = locationConstraintMiddleware()(next, {} as any);
49+
const handler = locationConstraintMiddleware({
50+
region: "us-east-2"
51+
})(next, {} as any);
4852
const input = {
4953
CreateBucketConfiguration: { LocationConstraint: "us-east-1" },
5054
foo: "bar"

packages/middleware-location-constraint/src/index.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ import {
77
MetadataBearer,
88
Pluggable
99
} from "@aws-sdk/types";
10+
import { LocationConstraintResolvedConfig } from "./configuration";
1011

11-
export function locationConstraintMiddleware(): InitializeMiddleware<any, any> {
12+
export function locationConstraintMiddleware(
13+
options: LocationConstraintResolvedConfig
14+
): InitializeMiddleware<any, any> {
1215
return <Output extends MetadataBearer>(
1316
next: InitializeHandler<any, Output>
1417
): InitializeHandler<any, Output> => async (
1518
args: InitializeHandlerArguments<any>
1619
): Promise<InitializeHandlerOutput<Output>> => {
17-
const { region, CreateBucketConfiguration } = args.input;
20+
const { CreateBucketConfiguration } = args.input;
1821
if (
1922
!CreateBucketConfiguration ||
2023
!CreateBucketConfiguration.LocationConstraint
@@ -23,10 +26,10 @@ export function locationConstraintMiddleware(): InitializeMiddleware<any, any> {
2326
...args,
2427
input: {
2528
...args.input,
26-
CreateBucketConfiguration: { LocationConstraint: region }
29+
CreateBucketConfiguration: { LocationConstraint: options.region }
2730
}
2831
};
29-
} else if (region === "us-east-1") {
32+
} else if (options.region === "us-east-1") {
3033
args = {
3134
...args,
3235
input: {
@@ -47,9 +50,12 @@ export const locationConstraintMiddlewareOptions: InitializeHandlerOptions = {
4750
};
4851

4952
export const getLocationConstraintPlugin = (
50-
unused: any
53+
config: LocationConstraintResolvedConfig
5154
): Pluggable<any, any> => ({
5255
applyToStack: clientStack => {
53-
locationConstraintMiddleware(), locationConstraintMiddlewareOptions;
56+
clientStack.add(
57+
locationConstraintMiddleware(config),
58+
locationConstraintMiddlewareOptions
59+
);
5460
}
5561
});

0 commit comments

Comments
 (0)