/
index.ts
50 lines (46 loc) · 1.32 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
export interface ARN {
partition: string;
service: string;
region: string;
accountId: string;
resource: string;
}
/**
* Validate whether a string is an ARN.
*/
export const validate = (str: any): boolean =>
typeof str === "string" && str.indexOf("arn:") === 0 && str.split(":").length >= 6;
/**
* Parse an ARN string into structure with partition, service, region, accountId and resource values
*/
export const parse = (arn: string): ARN => {
const segments = arn.split(":");
if (segments.length < 6 || segments[0] !== "arn") throw new Error("Malformed ARN");
const [
,
//Skip "arn" literal
partition,
service,
region,
accountId,
...resource
] = segments;
return {
partition,
service,
region,
accountId,
resource: resource.join(":"),
};
};
type buildOptions = Omit<ARN, "partition"> & { partition?: string };
/**
* Build an ARN with service, partition, region, accountId, and resources strings
*/
export const build = (arnObject: buildOptions): string => {
const { partition = "aws", service, region, accountId, resource } = arnObject;
if ([service, region, accountId, resource].some((segment) => typeof segment !== "string")) {
throw new Error("Input ARN object is invalid");
}
return `arn:${partition}:${service}:${region}:${accountId}:${resource}`;
};