aws-cdk-lib
Version:
Version 2 of the AWS Cloud Development Kit library
3 lines (2 loc) • 2.87 kB
JavaScript
;var _a;Object.defineProperty(exports,"__esModule",{value:!0}),exports.ContextProvider=void 0;const jsiiDeprecationWarnings=require("../../.warnings.jsii.js"),JSII_RTTI_SYMBOL_1=Symbol.for("jsii.rtti"),cxapi=require("../../cx-api"),constructs_1=require("constructs"),annotations_1=require("./annotations"),stack_1=require("./stack"),token_1=require("./token");class ContextProvider{constructor(){}static getKey(scope,options){try{jsiiDeprecationWarnings.aws_cdk_lib_GetContextKeyOptions(options)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.getKey),error}const stack=stack_1.Stack.of(scope),props=options.includeEnvironment??!0?{account:stack.account,region:stack.region,...options.props}:options.props??{};if(Object.values(props).find(x=>token_1.Token.isUnresolved(x)))throw new Error(`Cannot determine scope for context provider ${options.provider}.
This usually happens when one or more of the provider props have unresolved tokens`);const propStrings=propsToArray(props);return{key:`${options.provider}:${propStrings.join(":")}`,props}}static getValue(scope,options){try{jsiiDeprecationWarnings.aws_cdk_lib_GetContextValueOptions(options)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.getValue),error}const stack=stack_1.Stack.of(scope);if(token_1.Token.isUnresolved(stack.account)||token_1.Token.isUnresolved(stack.region))throw new Error(`Cannot retrieve value from context provider ${options.provider} since account/region are not specified at the stack level. Configure "env" with an account and region when you define your stack.See https://docs.aws.amazon.com/cdk/latest/guide/environments.html for more details.`);const{key,props}=this.getKey(scope,options),value=constructs_1.Node.of(scope).tryGetContext(key),providerError=extractProviderError(value);return value===void 0||providerError!==void 0?(stack.reportMissingContextKey({key,provider:options.provider,props}),providerError!==void 0&&annotations_1.Annotations.of(scope).addError(providerError),{value:options.dummyValue}):{value}}}exports.ContextProvider=ContextProvider,_a=JSII_RTTI_SYMBOL_1,ContextProvider[_a]={fqn:"aws-cdk-lib.ContextProvider",version:"2.70.0"};function extractProviderError(value){if(typeof value=="object"&&value!==null)return value[cxapi.PROVIDER_ERROR_KEY]}function colonQuote(xs){return xs.replace("$","$$").replace(":","$:")}function propsToArray(props,keyPrefix=""){const ret=[];for(const key of Object.keys(props))if(props[key]!==void 0)switch(typeof props[key]){case"object":{ret.push(...propsToArray(props[key],`${keyPrefix}${key}.`));break}case"string":{ret.push(`${keyPrefix}${key}=${colonQuote(props[key])}`);break}default:{ret.push(`${keyPrefix}${key}=${JSON.stringify(props[key])}`);break}}return ret.sort(),ret}