Class Resolver<EnvReg, EnvName, Var>

Resolves variable values for a specific environment.

The Resolver provides type-safe access to variable values based on the environment configuration and variable definitions. It handles both synchronous and asynchronous resolutions automatically.

const resolver = varReg.createResolver("local", { env: { DATABASE_URL: "test" } })

// Synchronous resolution
const isWorkflow = resolver.get("IS_WORKFLOW") // string

// Asynchronous resolution
const apiKey = await resolver.get("OPENAI_API_KEY") // Promise<string>

Type Parameters

Methods

  • Gets the value of a variable for the current environment.

    This method provides type-safe access to variable values. It automatically handles both synchronous and asynchronous resolutions based on the variable definition and environment configuration.

    Type Parameters

    • Name extends string

      The name of the variable to resolve

    • Async extends "sync" | "async"

      Whether the resolution is sync or async (inferred automatically)

    Parameters

    • name: Name

      The name of the variable to resolve

    Returns AsyncValue<Async, string>

    The resolved value (string for sync, Promise for async)

    When the variable is not found

    When no definition exists for the variable in the current environment

    When no resolution method is found for the variable

    When the resolved value is not a string

    const resolver = varReg.createResolver("local", { env: { DATABASE_URL: "test" } })

    // Get a hardcoded value
    const isWorkflow = resolver.get("IS_WORKFLOW") // "false"

    // Get a value from environment data
    const dbUrl = resolver.get("DATABASE_URL") // "test"

    // Get a dynamic value
    const bucket = resolver.get("DOCUMENT_BUCKET") // "my document bucket name"

    // Get an async value (requires await)
    const apiKey = await resolver.get("OPENAI_API_KEY") // "secret for OPENAI_API_KEY from aws"
  • Gets the values of all accessible variables for the current environment.

    This method returns an object mapping variable names to their resolved values. If any variable resolves asynchronously, the whole result is a Promise that resolves to the final object. For resolvers created with multiple possible environments (via createDynamicResolver), only variables that are defined in every possible environment are included for type safety.

    The same validation rules as in get apply to each variable: a runtime error is thrown if a variable resolves to undefined, null, or a non-string value, or if a resolution method cannot be found.

    Returns AsyncValue<
        MergeAsync<
            GetResolveSyncStatus<
                GetResolveFunction<
                    EnvReg,
                    EnvName,
                    Var,
                    GetName<
                        GetCommonVariableForEnv<EnvReg, EnvName, Var> & GetVariableForEnv<
                            EnvReg,
                            EnvName,
                            Var,
                        >,
                    >,
                >,
            >,
        >,
        { [Name in string]: string },
    >

    An object of variable values, or a Promise of that object if async

    const resolver = varReg.createResolver("env1", { env: { DATABASE_URL: "dev" } })
    const all = await resolver.getAll()
    // { DATABASE_URL: "dev", IS_WORKFLOW: "false", ... }
    const res = varReg.createDynamicResolver({
    env1: [{ env: { VAR4: "v1" } }],
    env2: [{ secrets: { VAR4: "v2" } }],
    }, () => process.env.NODE_ENV === "production" ? "env2" : "env1")

    // Only variables defined in both env1 and env2 are included
    const all = await res.getAll()
  • Gets the values of variables that use a specific resolution tag in another environment.

    This method allows you to get values that would be resolved with a specific tag in a different environment. It filters variables based on their resolution method in the target environment but resolves their values in the current environment.

    The method enforces several type safety constraints:

    • The target environment must be different from the current environment
    • The resolution tag must be valid for the target environment
    • All requested variables must be defined in the current environment

    The return type is Promise if any of the requested variables have async resolutions in the current environment, otherwise it's T. This ensures proper handling of both synchronous and asynchronous variable resolutions.

    Type Parameters

    • TargetEnvName extends string

      The name of the target environment to filter variables by

    • TargetTag extends string

      The resolution tag to filter variables by in the target environment

    • TargetVar = Extract<
          GetVariableForEnv<EnvReg, TargetEnvName, Var>,
          Variable<
              EnvReg,
              NamesWithTag<EnvReg, TargetEnvName, Var, TargetTag>,
              DefinedResolution<
                  EnvReg,
                  GetEnvName<EnvReg>,
                  DefinedResolutionType<EnvReg, GetEnvName<EnvReg>>,
              >,
          >,
      >

      The type of variables that use the specified tag in the target environment

    Parameters

    • envName: TargetEnvName

      The name of the target environment to filter variables by

    • tag: TargetTag

      The resolution tag to filter variables by in the target environment

    • ..._fail: GetName<TargetVar> extends GetName<
          GetCommonVariableForEnv<EnvReg, EnvName, Var>,
      >
          ? []
          : [never]

    Returns AsyncValue<
        MergeAsync<
            GetResolveSyncStatus<
                GetResolveFunction<
                    EnvReg,
                    EnvName,
                    Var,
                    Extract<
                        GetName<TargetVar>,
                        GetName<GetVariableForEnv<EnvReg, EnvName, Var>>,
                    >,
                >,
            >,
        >,
        { [Name in string]: string },
    >

    An object mapping variable names to their resolved values from the current environment. Returns a Promise if any variable resolves asynchronously.

    When a variable is not found

    When no definition exists for a variable in the current environment

    When no resolution method is found for a variable

    When a resolved value is not a string

    const resolver = varReg.createResolver("env1", { env: {} })

    // Get values that use "hardcoded" resolution in env2
    const values = resolver.getAllFor("env2", "hardcoded")
    // { VAR1: "value1", VAR2: "value2" }

    // If any variable has async resolution in env1, result is Promise
    const asyncValues = await resolver.getAllFor("env2", "hardcoded")
    // Promise<{ VAR1: string, VAR2: string }>
    // Type error: Cannot use "env1" as target (same as current environment)
    resolver.getAllFor("env1", "hardcoded")

    // Type error: "invalid-tag" is not a valid resolution tag for env2
    resolver.getAllFor("env2", "invalid-tag")

    // Type error: Some variables not available in current environment
    resolver.getAllFor("env2", "hardcoded") // if VAR2 not in env1