protected parseRef()

in packages/utilities/mdaa-config/lib/config.ts [162:200]


  protected parseRef(value: string, refMatch: string[]): string | number {
    const refMap: { [refInner: string]: string | undefined } = {
      org: this.props.org,
      env: this.props.env,
      domain: this.props.domain,
      module_name: this.props.module_name,
      partition: this.props.scope ? Stack.of(this.props.scope).partition : undefined,
      region: this.props.scope ? Stack.of(this.props.scope).region : undefined,
      account: this.props.scope ? Stack.of(this.props.scope).account : undefined,
    };

    // In all other cases, return a recursively substituted string
    let toReturn: string = value;
    refMatch.forEach(ref => {
      let resolvedValue: string | undefined;
      const refInner = this.transformValue(ref).toString();

      if (refMap[refInner]) {
        resolvedValue = refMap[refInner];
      } else if (refInner.startsWith('context:')) {
        resolvedValue = this.parseContext(refInner) as string;
      } else if (refInner.startsWith('env_var:')) {
        const envVar = refInner.replace(/^env_var:/, '');
        resolvedValue = process.env[envVar];
      } else if (refInner.startsWith('resolve:ssm:')) {
        const ssmPath = refInner.replace(/^resolve:ssm:/, '');
        if (!this.props.scope) {
          throw new Error('Unable to resolve ssm param outside of a Construct');
        }

        resolvedValue = this.props.scope?.node.tryGetContext('@mdaaLookupSSMValues')
          ? StringParameter.valueFromLookup(Stack.of(this.props.scope), ssmPath)
          : StringParameter.valueForStringParameter(Stack.of(this.props.scope), ssmPath);
      }

      toReturn = resolvedValue ? toReturn.replace(`{{${ref}}}`, resolvedValue) : toReturn;
    });
    return toReturn;
  }