UNPKG

6.69 kBTypeScriptView Raw
1import { IConstruct } from 'constructs';
2import { IResolvable, ITokenResolver } from './resolvable';
3import { TokenizedStringFragments } from './string-fragments';
4/**
5 * An enum-like class that represents the result of comparing two Tokens.
6 * The return type of {@link Token.compareStrings}.
7 */
8export declare class TokenComparison {
9 /**
10 * This means we're certain the two components are NOT
11 * Tokens, and identical.
12 */
13 static readonly SAME: TokenComparison;
14 /**
15 * This means we're certain the two components are NOT
16 * Tokens, and different.
17 */
18 static readonly DIFFERENT: TokenComparison;
19 /** This means exactly one of the components is a Token. */
20 static readonly ONE_UNRESOLVED: TokenComparison;
21 /** This means both components are Tokens. */
22 static readonly BOTH_UNRESOLVED: TokenComparison;
23 private constructor();
24}
25/**
26 * Represents a special or lazily-evaluated value.
27 *
28 * Can be used to delay evaluation of a certain value in case, for example,
29 * that it requires some context or late-bound data. Can also be used to
30 * mark values that need special processing at document rendering time.
31 *
32 * Tokens can be embedded into strings while retaining their original
33 * semantics.
34 */
35export declare class Token {
36 /**
37 * Returns true if obj represents an unresolved value
38 *
39 * One of these must be true:
40 *
41 * - `obj` is an IResolvable
42 * - `obj` is a string containing at least one encoded `IResolvable`
43 * - `obj` is either an encoded number or list
44 *
45 * This does NOT recurse into lists or objects to see if they
46 * containing resolvables.
47 *
48 * @param obj The object to test.
49 */
50 static isUnresolved(obj: any): boolean;
51 /**
52 * Return a reversible string representation of this token
53 *
54 * If the Token is initialized with a literal, the stringified value of the
55 * literal is returned. Otherwise, a special quoted string representation
56 * of the Token is returned that can be embedded into other strings.
57 *
58 * Strings with quoted Tokens in them can be restored back into
59 * complex values with the Tokens restored by calling `resolve()`
60 * on the string.
61 */
62 static asString(value: any, options?: EncodingOptions): string;
63 /**
64 * Return a reversible number representation of this token
65 */
66 static asNumber(value: any): number;
67 /**
68 * Return a reversible list representation of this token
69 */
70 static asList(value: any, options?: EncodingOptions): string[];
71 /**
72 * Return a resolvable representation of the given value
73 */
74 static asAny(value: any): IResolvable;
75 /** Compare two strings that might contain Tokens with each other. */
76 static compareStrings(possibleToken1: string, possibleToken2: string): TokenComparison;
77 private constructor();
78}
79/**
80 * Less oft-needed functions to manipulate Tokens
81 */
82export declare class Tokenization {
83 /**
84 * Un-encode a string potentially containing encoded tokens
85 */
86 static reverseString(s: string): TokenizedStringFragments;
87 /**
88 * Un-encode a string which is either a complete encoded token, or doesn't contain tokens at all
89 *
90 * It's illegal for the string to be a concatenation of an encoded token and something else.
91 */
92 static reverseCompleteString(s: string): IResolvable | undefined;
93 /**
94 * Un-encode a Tokenized value from a number
95 */
96 static reverseNumber(n: number): IResolvable | undefined;
97 /**
98 * Un-encode a Tokenized value from a list
99 */
100 static reverseList(l: string[]): IResolvable | undefined;
101 /**
102 * Reverse any value into a Resolvable, if possible
103 *
104 * In case of a string, the string must not be a concatenation.
105 */
106 static reverse(x: any, options?: ReverseOptions): IResolvable | undefined;
107 /**
108 * Resolves an object by evaluating all tokens and removing any undefined or empty objects or arrays.
109 * Values can only be primitives, arrays or tokens. Other objects (i.e. with methods) will be rejected.
110 *
111 * @param obj The object to resolve.
112 * @param options Prefix key path components for diagnostics.
113 */
114 static resolve(obj: any, options: ResolveOptions): any;
115 /**
116 * Return whether the given object is an IResolvable object
117 *
118 * This is different from Token.isUnresolved() which will also check for
119 * encoded Tokens, whereas this method will only do a type check on the given
120 * object.
121 */
122 static isResolvable(obj: any): obj is IResolvable;
123 /**
124 * Stringify a number directly or lazily if it's a Token. If it is an object (i.e., { Ref: 'SomeLogicalId' }), return it as-is.
125 */
126 static stringifyNumber(x: number): string;
127 private constructor();
128}
129/**
130 * Options for the 'reverse()' operation
131 */
132export interface ReverseOptions {
133 /**
134 * Fail if the given string is a concatenation
135 *
136 * If `false`, just return `undefined`.
137 *
138 * @default true
139 */
140 readonly failConcat?: boolean;
141}
142/**
143 * Options to the resolve() operation
144 *
145 * NOT the same as the ResolveContext; ResolveContext is exposed to Token
146 * implementors and resolution hooks, whereas this struct is just to bundle
147 * a number of things that would otherwise be arguments to resolve() in a
148 * readable way.
149 */
150export interface ResolveOptions {
151 /**
152 * The scope from which resolution is performed
153 */
154 readonly scope: IConstruct;
155 /**
156 * The resolver to apply to any resolvable tokens found
157 */
158 readonly resolver: ITokenResolver;
159 /**
160 * Whether the resolution is being executed during the prepare phase or not.
161 * @default false
162 */
163 readonly preparing?: boolean;
164 /**
165 * Whether to remove undefined elements from arrays and objects when resolving.
166 *
167 * @default true
168 */
169 readonly removeEmpty?: boolean;
170}
171/**
172 * Properties to string encodings
173 */
174export interface EncodingOptions {
175 /**
176 * A hint for the Token's purpose when stringifying it
177 */
178 readonly displayHint?: string;
179}
180export declare function isResolvableObject(x: any): x is IResolvable;
181/**
182 * Call the given function only if all given values are resolved
183 *
184 * Exported as a function since it will be used by TypeScript modules, but
185 * can't be exposed via JSII because of the generics.
186 */
187export declare function withResolved<A>(a: A, fn: (a: A) => void): void;
188export declare function withResolved<A, B>(a: A, b: B, fn: (a: A, b: B) => void): void;
189export declare function withResolved<A, B, C>(a: A, b: B, c: C, fn: (a: A, b: B, c: C) => void): void;