# API Reference **Classes** Name|Description ----|----------- [Construct](#constructs-construct)|Represents the building block of the construct graph. [Dependable](#constructs-dependable)|Trait for IDependable. [DependencyGroup](#constructs-dependencygroup)|A set of constructs to be used as a dependable. [Node](#constructs-node)|Represents the construct node in the scope tree. **Structs** Name|Description ----|----------- [MetadataEntry](#constructs-metadataentry)|An entry in the construct metadata table. [MetadataOptions](#constructs-metadataoptions)|Options for `construct.addMetadata()`. **Interfaces** Name|Description ----|----------- [IConstruct](#constructs-iconstruct)|Represents a construct. [IDependable](#constructs-idependable)|Trait marker for classes that can be depended upon. [IValidation](#constructs-ivalidation)|Implement this interface in order for the construct to be able to validate itself. **Enums** Name|Description ----|----------- [ConstructOrder](#constructs-constructorder)|In what order to return constructs. ## class Construct Represents the building block of the construct graph. All constructs besides the root construct must be created within the scope of another construct. __Implements__: [IConstruct](#constructs-iconstruct), [IDependable](#constructs-idependable) ### Initializer Creates a new construct node. ```ts new Construct(scope: Construct, id: string) ``` * **scope** ([Construct](#constructs-construct)) The scope in which to define this construct. * **id** (string) The scoped construct ID. ### Properties Name | Type | Description -----|------|------------- **node** | [Node](#constructs-node) | The tree node. ### Methods #### toString() Returns a string representation of this construct. ```ts toString(): string ``` __Returns__: * string #### *static* isConstruct(x) Checks if `x` is a construct. Use this method instead of `instanceof` to properly detect `Construct` instances, even when the construct library is symlinked. Explanation: in JavaScript, multiple copies of the `constructs` library on disk are seen as independent, completely different libraries. As a consequence, the class `Construct` in each copy of the `constructs` library is seen as a different class, and an instance of one class will not test as `instanceof` the other class. `npm install` will not create installations like this, but users may manually symlink construct libraries together or use a monorepo tool: in those cases, multiple copies of the `constructs` library can be accidentally installed, and `instanceof` will behave unpredictably. It is safest to avoid using `instanceof`, and using this type-testing method instead. ```ts static isConstruct(x: any): boolean ``` * **x** (any) Any object. __Returns__: * boolean ## class Dependable 🔹 Trait for IDependable. Traits are interfaces that are privately implemented by objects. Instead of showing up in the public interface of a class, they need to be queried explicitly. This is used to implement certain framework features that are not intended to be used by Construct consumers, and so should be hidden from accidental use. ### Initializer ```ts new Dependable() ``` ### Properties Name | Type | Description -----|------|------------- **dependencyRoots**🔹 | Array<[IConstruct](#constructs-iconstruct)> | The set of constructs that form the root of this dependable. ### Methods #### *static* get(instance)⚠️ Return the matching Dependable for the given class instance. ```ts static get(instance: IDependable): Dependable ``` * **instance** ([IDependable](#constructs-idependable)) *No description* __Returns__: * [Dependable](#constructs-dependable) #### *static* implement(instance, trait)🔹 Turn any object into an IDependable. ```ts static implement(instance: IDependable, trait: Dependable): void ``` * **instance** ([IDependable](#constructs-idependable)) *No description* * **trait** ([Dependable](#constructs-dependable)) *No description* #### *static* of(instance)🔹 Return the matching Dependable for the given class instance. ```ts static of(instance: IDependable): Dependable ``` * **instance** ([IDependable](#constructs-idependable)) *No description* __Returns__: * [Dependable](#constructs-dependable) ## class DependencyGroup 🔹 A set of constructs to be used as a dependable. This class can be used when a set of constructs which are disjoint in the construct tree needs to be combined to be used as a single dependable. __Implements__: [IDependable](#constructs-idependable) ### Initializer ```ts new DependencyGroup(...deps: IDependable[]) ``` * **deps** ([IDependable](#constructs-idependable)) *No description* ### Methods #### add(...scopes)🔹 Add a construct to the dependency roots. ```ts add(...scopes: IDependable[]): void ``` * **scopes** ([IDependable](#constructs-idependable)) *No description* ## class Node Represents the construct node in the scope tree. ### Initializer ```ts new Node(host: Construct, scope: IConstruct, id: string) ``` * **host** ([Construct](#constructs-construct)) *No description* * **scope** ([IConstruct](#constructs-iconstruct)) *No description* * **id** (string) *No description* ### Properties Name | Type | Description -----|------|------------- **addr** | string | Returns an opaque tree-unique address for this construct. **children** | Array<[IConstruct](#constructs-iconstruct)> | All direct children of this construct. **dependencies** | Array<[IConstruct](#constructs-iconstruct)> | Return all dependencies registered on this node (non-recursive). **id** | string | The id of this construct within the current scope. **locked** | boolean | Returns true if this construct or the scopes in which it is defined are locked. **metadata** | Array<[MetadataEntry](#constructs-metadataentry)> | An immutable array of metadata objects associated with this construct. **path** | string | The full, absolute path of this construct in the tree. **root** | [IConstruct](#constructs-iconstruct) | Returns the root of the construct tree. **scopes** | Array<[IConstruct](#constructs-iconstruct)> | All parent scopes of this construct. **defaultChild**? | [IConstruct](#constructs-iconstruct) | Returns the child construct that has the id `Default` or `Resource"`.
__*Optional*__ **scope**? | [IConstruct](#constructs-iconstruct) | Returns the scope in which this construct is defined.
__*Optional*__ *static* **PATH_SEP** | string | Separator used to delimit construct path components. ### Methods #### addDependency(...deps) Add an ordering dependency on another construct. An `IDependable` ```ts addDependency(...deps: IDependable[]): void ``` * **deps** ([IDependable](#constructs-idependable)) *No description* #### addMetadata(type, data, options?) Adds a metadata entry to this construct. Entries are arbitrary values and will also include a stack trace to allow tracing back to the code location for when the entry was added. It can be used, for example, to include source mapping in CloudFormation templates to improve diagnostics. ```ts addMetadata(type: string, data: any, options?: MetadataOptions): void ``` * **type** (string) a string denoting the type of metadata. * **data** (any) the value of the metadata (can be a Token). * **options** ([MetadataOptions](#constructs-metadataoptions)) options. * **stackTrace** (boolean) Include stack trace with metadata entry. __*Default*__: false * **traceFromFunction** (any) A JavaScript function to begin tracing from. __*Default*__: addMetadata() #### addValidation(validation) Adds a validation to this construct. When `node.validate()` is called, the `validate()` method will be called on all validations and all errors will be returned. ```ts addValidation(validation: IValidation): void ``` * **validation** ([IValidation](#constructs-ivalidation)) The validation object. #### findAll(order?) Return this construct and all of its children in the given order. ```ts findAll(order?: ConstructOrder): Array ``` * **order** ([ConstructOrder](#constructs-constructorder)) *No description* __Returns__: * Array<[IConstruct](#constructs-iconstruct)> #### findChild(id) Return a direct child by id. Throws an error if the child is not found. ```ts findChild(id: string): IConstruct ``` * **id** (string) Identifier of direct child. __Returns__: * [IConstruct](#constructs-iconstruct) #### getContext(key) Retrieves a value from tree context if present. Otherwise, would throw an error. Context is usually initialized at the root, but can be overridden at any point in the tree. ```ts getContext(key: string): any ``` * **key** (string) The context key. __Returns__: * any #### lock() Locks this construct from allowing more children to be added. After this call, no more children can be added to this construct or to any children. ```ts lock(): void ``` #### setContext(key, value) This can be used to set contextual values. Context must be set before any children are added, since children may consult context info during construction. If the key already exists, it will be overridden. ```ts setContext(key: string, value: any): void ``` * **key** (string) The context key. * **value** (any) The context value. #### tryFindChild(id) Return a direct child by id, or undefined. ```ts tryFindChild(id: string): IConstruct ``` * **id** (string) Identifier of direct child. __Returns__: * [IConstruct](#constructs-iconstruct) #### tryGetContext(key) Retrieves a value from tree context. Context is usually initialized at the root, but can be overridden at any point in the tree. ```ts tryGetContext(key: string): any ``` * **key** (string) The context key. __Returns__: * any #### tryRemoveChild(childName)🔹 Remove the child with the given name, if present. ```ts tryRemoveChild(childName: string): boolean ``` * **childName** (string) *No description* __Returns__: * boolean #### validate() Validates this construct. Invokes the `validate()` method on all validations added through `addValidation()`. ```ts validate(): Array ``` __Returns__: * Array #### *static* of(construct)⚠️ Returns the node associated with a construct. ```ts static of(construct: IConstruct): Node ``` * **construct** ([IConstruct](#constructs-iconstruct)) the construct. __Returns__: * [Node](#constructs-node) ## interface IConstruct __Implemented by__: [Construct](#constructs-construct) __Obtainable from__: [Node](#constructs-node).[findChild](#constructs-node#constructs-node-findchild)(), [Node](#constructs-node).[tryFindChild](#constructs-node#constructs-node-tryfindchild)() Represents a construct. ### Properties Name | Type | Description -----|------|------------- **node** | [Node](#constructs-node) | The tree node. ## interface IDependable __Implemented by__: [Construct](#constructs-construct), [DependencyGroup](#constructs-dependencygroup) Trait marker for classes that can be depended upon. The presence of this interface indicates that an object has an `IDependable` implementation. This interface can be used to take an (ordering) dependency on a set of constructs. An ordering dependency implies that the resources represented by those constructs are deployed before the resources depending ON them are deployed. ## interface IValidation Implement this interface in order for the construct to be able to validate itself. ### Methods #### validate() Validate the current construct. This method can be implemented by derived constructs in order to perform validation logic. It is called on all constructs before synthesis. ```ts validate(): Array ``` __Returns__: * Array ## struct MetadataEntry An entry in the construct metadata table. Name | Type | Description -----|------|------------- **data** | any | The data. **type** | string | The metadata entry type. **trace**? | Array | Stack trace at the point of adding the metadata.
__*Default*__: no trace information ## struct MetadataOptions Options for `construct.addMetadata()`. Name | Type | Description -----|------|------------- **stackTrace**? | boolean | Include stack trace with metadata entry.
__*Default*__: false **traceFromFunction**? | any | A JavaScript function to begin tracing from.
__*Default*__: addMetadata() ## enum ConstructOrder In what order to return constructs. Name | Description -----|----- **PREORDER** |Depth-first, pre-order. **POSTORDER** |Depth-first, post-order (leaf nodes first).