-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp-aware-construct.ts
44 lines (37 loc) · 1.7 KB
/
app-aware-construct.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
import { Construct } from "constructs";
import { AppIdentity } from "../../constructs/core";
import type { AnyConstructor } from "./types";
export function GuAppAwareConstruct<TBase extends AnyConstructor>(BaseClass: TBase) {
class Mixin extends BaseClass {
/**
* The ID of the construct with the App suffix.
* This should be used in place of `id` when trying to reference the construct.
*/
readonly idWithApp: string;
// eslint-disable-next-line custom-rules/valid-constructors, @typescript-eslint/no-explicit-any -- mixin
protected constructor(...args: any[]) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- mixin
const [scope, id, props, ...rest] = args;
if (!AppIdentity.isAppIdentity(props)) {
throw new Error("Cannot use the GuAppAwareConstruct mixin without an AppIdentity");
}
const app: string = props.app;
const idWithApp = AppIdentity.suffixText({ app }, id as string);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- mixin
const newArgs = [scope, idWithApp, props, ...rest];
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- mixin
super(...newArgs);
this.idWithApp = idWithApp;
/*
Add the `App` tag to the construct.
Although not every resource can be tagged, it's still safe to make the call.
If AWS support tags on a new resource one day, our test suite will fail and we can celebrate!
See https://docs.aws.amazon.com/ARG/latest/userguide/supported-resources.html
*/
if (Construct.isConstruct(this)) {
AppIdentity.taggedConstruct({ app }, this);
}
}
}
return Mixin;
}