UNPKG

11.1 kBTypeScriptView Raw
1import { IDependable } from './dependency';
2import { MetadataEntry } from './metadata';
3/**
4 * Represents a construct.
5 */
6export interface IConstruct extends IDependable {
7 /**
8 * The tree node.
9 */
10 readonly node: Node;
11}
12/**
13 * Represents the construct node in the scope tree.
14 */
15export declare class Node {
16 private readonly host;
17 /**
18 * Separator used to delimit construct path components.
19 */
20 static readonly PATH_SEP = "/";
21 /**
22 * Returns the node associated with a construct.
23 * @param construct the construct
24 *
25 * @deprecated use `construct.node` instead
26 */
27 static of(construct: IConstruct): Node;
28 /**
29 * Returns the scope in which this construct is defined.
30 *
31 * The value is `undefined` at the root of the construct scope tree.
32 */
33 readonly scope?: IConstruct;
34 /**
35 * The id of this construct within the current scope.
36 *
37 * This is a scope-unique id. To obtain an app-unique id for this construct, use `addr`.
38 */
39 readonly id: string;
40 private _locked;
41 private readonly _children;
42 private readonly _context;
43 private readonly _metadata;
44 private readonly _dependencies;
45 private _defaultChild;
46 private readonly _validations;
47 private _addr?;
48 constructor(host: Construct, scope: IConstruct, id: string);
49 /**
50 * The full, absolute path of this construct in the tree.
51 *
52 * Components are separated by '/'.
53 */
54 get path(): string;
55 /**
56 * Returns an opaque tree-unique address for this construct.
57 *
58 * Addresses are 42 characters hexadecimal strings. They begin with "c8"
59 * followed by 40 lowercase hexadecimal characters (0-9a-f).
60 *
61 * Addresses are calculated using a SHA-1 of the components of the construct
62 * path.
63 *
64 * To enable refactorings of construct trees, constructs with the ID `Default`
65 * will be excluded from the calculation. In those cases constructs in the
66 * same tree may have the same addreess.
67 *
68 * @example c83a2846e506bcc5f10682b564084bca2d275709ee
69 */
70 get addr(): string;
71 /**
72 * Return a direct child by id, or undefined
73 *
74 * @param id Identifier of direct child
75 * @returns the child if found, or undefined
76 */
77 tryFindChild(id: string): IConstruct | undefined;
78 /**
79 * Return a direct child by id
80 *
81 * Throws an error if the child is not found.
82 *
83 * @param id Identifier of direct child
84 * @returns Child with the given id.
85 */
86 findChild(id: string): IConstruct;
87 /**
88 * Returns the child construct that has the id `Default` or `Resource"`.
89 * This is usually the construct that provides the bulk of the underlying functionality.
90 * Useful for modifications of the underlying construct that are not available at the higher levels.
91 *
92 * @throws if there is more than one child
93 * @returns a construct or undefined if there is no default child
94 */
95 get defaultChild(): IConstruct | undefined;
96 /**
97 * Override the defaultChild property.
98 *
99 * This should only be used in the cases where the correct
100 * default child is not named 'Resource' or 'Default' as it
101 * should be.
102 *
103 * If you set this to undefined, the default behavior of finding
104 * the child named 'Resource' or 'Default' will be used.
105 */
106 set defaultChild(value: IConstruct | undefined);
107 /**
108 * All direct children of this construct.
109 */
110 get children(): IConstruct[];
111 /**
112 * Return this construct and all of its children in the given order
113 */
114 findAll(order?: ConstructOrder): IConstruct[];
115 /**
116 * This can be used to set contextual values.
117 * Context must be set before any children are added, since children may consult context info during construction.
118 * If the key already exists, it will be overridden.
119 * @param key The context key
120 * @param value The context value
121 */
122 setContext(key: string, value: any): void;
123 /**
124 * Retrieves a value from tree context if present. Otherwise, would throw an error.
125 *
126 * Context is usually initialized at the root, but can be overridden at any point in the tree.
127 *
128 * @param key The context key
129 * @returns The context value or throws error if there is no context value for this key
130 */
131 getContext(key: string): any;
132 /**
133 * Retrieves the all context of a node from tree context.
134 *
135 * Context is usually initialized at the root, but can be overridden at any point in the tree.
136 *
137 * @param defaults Any keys to override the retrieved context
138 * @returns The context object or an empty object if there is discovered context
139 */
140 getAllContext(defaults?: object): any;
141 /**
142 * Retrieves a value from tree context.
143 *
144 * Context is usually initialized at the root, but can be overridden at any point in the tree.
145 *
146 * @param key The context key
147 * @returns The context value or `undefined` if there is no context value for this key.
148 */
149 tryGetContext(key: string): any;
150 /**
151 * An immutable array of metadata objects associated with this construct.
152 * This can be used, for example, to implement support for deprecation notices, source mapping, etc.
153 */
154 get metadata(): MetadataEntry[];
155 /**
156 * Adds a metadata entry to this construct.
157 * Entries are arbitrary values and will also include a stack trace to allow tracing back to
158 * the code location for when the entry was added. It can be used, for example, to include source
159 * mapping in CloudFormation templates to improve diagnostics.
160 *
161 * @param type a string denoting the type of metadata
162 * @param data the value of the metadata (can be a Token). If null/undefined, metadata will not be added.
163 * @param options options
164 */
165 addMetadata(type: string, data: any, options?: MetadataOptions): void;
166 /**
167 * All parent scopes of this construct.
168 *
169 * @returns a list of parent scopes. The last element in the list will always
170 * be the current construct and the first element will be the root of the
171 * tree.
172 */
173 get scopes(): IConstruct[];
174 /**
175 * Returns the root of the construct tree.
176 * @returns The root of the construct tree.
177 */
178 get root(): IConstruct;
179 /**
180 * Returns true if this construct or the scopes in which it is defined are
181 * locked.
182 */
183 get locked(): boolean;
184 /**
185 * Add an ordering dependency on another construct.
186 *
187 * An `IDependable`
188 */
189 addDependency(...deps: IDependable[]): void;
190 /**
191 * Return all dependencies registered on this node (non-recursive).
192 */
193 get dependencies(): IConstruct[];
194 /**
195 * Remove the child with the given name, if present.
196 *
197 * @returns Whether a child with the given name was deleted.
198 * @experimental
199 */
200 tryRemoveChild(childName: string): boolean;
201 /**
202 * Adds a validation to this construct.
203 *
204 * When `node.validate()` is called, the `validate()` method will be called on
205 * all validations and all errors will be returned.
206 *
207 * @param validation The validation object
208 */
209 addValidation(validation: IValidation): void;
210 /**
211 * Validates this construct.
212 *
213 * Invokes the `validate()` method on all validations added through
214 * `addValidation()`.
215 *
216 * @returns an array of validation error messages associated with this
217 * construct.
218 */
219 validate(): string[];
220 /**
221 * Locks this construct from allowing more children to be added. After this
222 * call, no more children can be added to this construct or to any children.
223 */
224 lock(): void;
225 /**
226 * Adds a child construct to this node.
227 *
228 * @param child The child construct
229 * @param childName The type name of the child construct.
230 * @returns The resolved path part name of the child
231 */
232 private addChild;
233}
234/**
235 * Represents the building block of the construct graph.
236 *
237 * All constructs besides the root construct must be created within the scope of
238 * another construct.
239 */
240export declare class Construct implements IConstruct {
241 /**
242 * Checks if `x` is a construct.
243 *
244 * Use this method instead of `instanceof` to properly detect `Construct`
245 * instances, even when the construct library is symlinked.
246 *
247 * Explanation: in JavaScript, multiple copies of the `constructs` library on
248 * disk are seen as independent, completely different libraries. As a
249 * consequence, the class `Construct` in each copy of the `constructs` library
250 * is seen as a different class, and an instance of one class will not test as
251 * `instanceof` the other class. `npm install` will not create installations
252 * like this, but users may manually symlink construct libraries together or
253 * use a monorepo tool: in those cases, multiple copies of the `constructs`
254 * library can be accidentally installed, and `instanceof` will behave
255 * unpredictably. It is safest to avoid using `instanceof`, and using
256 * this type-testing method instead.
257 *
258 * @returns true if `x` is an object created from a class which extends `Construct`.
259 * @param x Any object
260 */
261 static isConstruct(x: any): x is Construct;
262 /**
263 * The tree node.
264 */
265 readonly node: Node;
266 /**
267 * Creates a new construct node.
268 *
269 * @param scope The scope in which to define this construct
270 * @param id The scoped construct ID. Must be unique amongst siblings. If
271 * the ID includes a path separator (`/`), then it will be replaced by double
272 * dash `--`.
273 */
274 constructor(scope: Construct, id: string);
275 /**
276 * Returns a string representation of this construct.
277 */
278 toString(): string;
279}
280/**
281 * Implement this interface in order for the construct to be able to validate itself.
282 */
283export interface IValidation {
284 /**
285 * Validate the current construct.
286 *
287 * This method can be implemented by derived constructs in order to perform
288 * validation logic. It is called on all constructs before synthesis.
289 *
290 * @returns An array of validation error messages, or an empty array if there the construct is valid.
291 */
292 validate(): string[];
293}
294/**
295 * In what order to return constructs
296 */
297export declare enum ConstructOrder {
298 /**
299 * Depth-first, pre-order
300 */
301 PREORDER = 0,
302 /**
303 * Depth-first, post-order (leaf nodes first)
304 */
305 POSTORDER = 1
306}
307/**
308 * Options for `construct.addMetadata()`.
309 */
310export interface MetadataOptions {
311 /**
312 * Include stack trace with metadata entry.
313 * @default false
314 */
315 readonly stackTrace?: boolean;
316 /**
317 * A JavaScript function to begin tracing from.
318 *
319 * This option is ignored unless `stackTrace` is `true`.
320 *
321 * @default addMetadata()
322 */
323 readonly traceFromFunction?: any;
324}