Skip to content

Commit

Permalink
feat(config-resolver): add getRegionInfo helper functions
Browse files Browse the repository at this point in the history
Refs: aws#2696
  • Loading branch information
trivikr committed Aug 23, 2021
1 parent 18469ce commit 4e78b80
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/config-resolver/src/getRegionInfo/getRegionInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { getResolvedHostName } from "./getResolvedHostname";
import { getResolvedPartition } from "./getResolvedPartition";
import { RegionInfoOptions } from "./types";

export const getRegionInfo = (region: string, { regionHash, partitionHash }: RegionInfoOptions) => ({
signingService: "s3",
hostname: getResolvedHostName(region, { regionHash, partitionHash }),
partition: getResolvedPartition(region, { partitionHash }),
...(regionHash[region]?.signingRegion && {
signingRegion: regionHash[region].signingRegion,
}),
...(regionHash[region]?.signingService && {
signingService: regionHash[region].signingService,
}),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { getResolvedPartition } from "./getResolvedPartition";
import { RegionInfoOptions } from "./types";

export const getResolvedHostName = (region: string, { regionHash, partitionHash }: RegionInfoOptions) =>
regionHash[region]?.hostname ??
partitionHash[getResolvedPartition(region, { partitionHash })].hostname.replace("{region}", region);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { RegionInfoOptions } from "./types";

export const getResolvedPartition = (region: string, { partitionHash }: Omit<RegionInfoOptions, "regionHash">) =>
Object.keys(partitionHash).find((key) => partitionHash[key].regions.includes(region)) ?? "aws";
2 changes: 2 additions & 0 deletions packages/config-resolver/src/getRegionInfo/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./types";
export * from "./getRegionInfo";
19 changes: 19 additions & 0 deletions packages/config-resolver/src/getRegionInfo/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { RegionInfo } from "@aws-sdk/types";

export type RegionHash = { [key: string]: Partial<Omit<RegionInfo, "partition" | "path">> };
export type PartitionHash = { [key: string]: { regions: string[]; hostname: string } };

export interface RegionInfoOptions {
/**
* The hash of region with the information specific to that region.
* The information can include hostname, signingService and signingRegion.
*/
regionHash: RegionHash;

/**
* The hash of partition with the information specific to that partition.
* The information includes the list of regions belonging to that partition,
* and the hostname to be used for the partition.
*/
partitionHash: PartitionHash;
}
1 change: 1 addition & 0 deletions packages/config-resolver/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./CustomEndpointsConfig";
export * from "./EndpointsConfig";
export * from "./RegionConfig";
export * from "./getRegionInfo";

0 comments on commit 4e78b80

Please sign in to comment.