UNPKG

11.9 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 refactoring 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 address.
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 * Note that construct metadata is not the same as CloudFormation resource metadata and is never written to the CloudFormation template.
161 * The metadata entries are written to the Cloud Assembly Manifest if the `treeMetadata` property is specified in the props of the App that contains this Construct.
162 *
163 * @param type a string denoting the type of metadata
164 * @param data the value of the metadata (can be a Token). If null/undefined, metadata will not be added.
165 * @param options options
166 */
167 addMetadata(type: string, data: any, options?: MetadataOptions): void;
168 /**
169 * All parent scopes of this construct.
170 *
171 * @returns a list of parent scopes. The last element in the list will always
172 * be the current construct and the first element will be the root of the
173 * tree.
174 */
175 get scopes(): IConstruct[];
176 /**
177 * Returns the root of the construct tree.
178 * @returns The root of the construct tree.
179 */
180 get root(): IConstruct;
181 /**
182 * Returns true if this construct or the scopes in which it is defined are
183 * locked.
184 */
185 get locked(): boolean;
186 /**
187 * Add an ordering dependency on another construct.
188 *
189 * An `IDependable`
190 */
191 addDependency(...deps: IDependable[]): void;
192 /**
193 * Return all dependencies registered on this node (non-recursive).
194 */
195 get dependencies(): IConstruct[];
196 /**
197 * Remove the child with the given name, if present.
198 *
199 * @returns Whether a child with the given name was deleted.
200 */
201 tryRemoveChild(childName: string): boolean;
202 /**
203 * Adds a validation to this construct.
204 *
205 * When `node.validate()` is called, the `validate()` method will be called on
206 * all validations and all errors will be returned.
207 *
208 * @param validation The validation object
209 */
210 addValidation(validation: IValidation): void;
211 /**
212 * Validates this construct.
213 *
214 * Invokes the `validate()` method on all validations added through
215 * `addValidation()`.
216 *
217 * @returns an array of validation error messages associated with this
218 * construct.
219 */
220 validate(): string[];
221 /**
222 * Locks this construct from allowing more children to be added. After this
223 * call, no more children can be added to this construct or to any children.
224 */
225 lock(): void;
226 /**
227 * Adds a child construct to this node.
228 *
229 * @param child The child construct
230 * @param childName The type name of the child construct.
231 * @returns The resolved path part name of the child
232 */
233 private addChild;
234}
235/**
236 * Represents the building block of the construct graph.
237 *
238 * All constructs besides the root construct must be created within the scope of
239 * another construct.
240 */
241export declare class Construct implements IConstruct {
242 /**
243 * Checks if `x` is a construct.
244 *
245 * Use this method instead of `instanceof` to properly detect `Construct`
246 * instances, even when the construct library is symlinked.
247 *
248 * Explanation: in JavaScript, multiple copies of the `constructs` library on
249 * disk are seen as independent, completely different libraries. As a
250 * consequence, the class `Construct` in each copy of the `constructs` library
251 * is seen as a different class, and an instance of one class will not test as
252 * `instanceof` the other class. `npm install` will not create installations
253 * like this, but users may manually symlink construct libraries together or
254 * use a monorepo tool: in those cases, multiple copies of the `constructs`
255 * library can be accidentally installed, and `instanceof` will behave
256 * unpredictably. It is safest to avoid using `instanceof`, and using
257 * this type-testing method instead.
258 *
259 * @returns true if `x` is an object created from a class which extends `Construct`.
260 * @param x Any object
261 */
262 static isConstruct(x: any): x is Construct;
263 /**
264 * The tree node.
265 */
266 readonly node: Node;
267 /**
268 * Creates a new construct node.
269 *
270 * @param scope The scope in which to define this construct
271 * @param id The scoped construct ID. Must be unique amongst siblings. If
272 * the ID includes a path separator (`/`), then it will be replaced by double
273 * dash `--`.
274 */
275 constructor(scope: Construct, id: string);
276 /**
277 * Returns a string representation of this construct.
278 */
279 toString(): string;
280}
281/**
282 * Implement this interface in order for the construct to be able to validate itself.
283 */
284export interface IValidation {
285 /**
286 * Validate the current construct.
287 *
288 * This method can be implemented by derived constructs in order to perform
289 * validation logic. It is called on all constructs before synthesis.
290 *
291 * @returns An array of validation error messages, or an empty array if there the construct is valid.
292 */
293 validate(): string[];
294}
295/**
296 * In what order to return constructs
297 */
298export declare enum ConstructOrder {
299 /**
300 * Depth-first, pre-order
301 */
302 PREORDER = 0,
303 /**
304 * Depth-first, post-order (leaf nodes first)
305 */
306 POSTORDER = 1
307}
308/**
309 * Options for `construct.addMetadata()`.
310 */
311export interface MetadataOptions {
312 /**
313 * Include stack trace with metadata entry.
314 * @default false
315 */
316 readonly stackTrace?: boolean;
317 /**
318 * A JavaScript function to begin tracing from.
319 *
320 * This option is ignored unless `stackTrace` is `true`.
321 *
322 * @default addMetadata()
323 */
324 readonly traceFromFunction?: any;
325}
326/**
327 * Creates a new root construct node.
328 *
329 * The root construct represents the top of the construct tree and is not contained within a parent scope itself.
330 * For root constructs, the id is optional.
331 */
332export declare class RootConstruct extends Construct {
333 /**
334 * Creates a new root construct node.
335 *
336 * @param id The scoped construct ID. Must be unique amongst siblings. If
337 * the ID includes a path separator (`/`), then it will be replaced by double
338 * dash `--`.
339 */
340 constructor(id?: string);
341}