# core/resolvers/environment.resolver.ts

> 46 lines of code and 12 definitions.

Tree: Secrets
Language: typescript
Layer: infrastructure
Canonical: https://banes-lab.com/anatomy/secrets#file-secrets-core-resolvers-environment-resolver-ts
Source text: https://banes-lab.com/source/secrets/core/resolvers/environment.resolver.ts.txt

Listed in [core/resolvers](https://banes-lab.com/api/source/secrets/core/resolvers.md).

## Definitions

- `requiredOf` (lexical_declaration, line 36)
- `storedValueOf` (lexical_declaration, line 24)
- `declaredOf` (lexical_declaration, line 16)
- `vaultEntryOf` (lexical_declaration, line 12)
- `optionalTextOf` (lexical_declaration, line 53, exported)
- `portOf` (lexical_declaration, line 45, exported)
- `textOf` (lexical_declaration, line 49, exported)
- `DECLARED` (lexical_declaration, line 8)
- `ENTRY_BY_KEY` (lexical_declaration, line 10)
- `entry` (lexical_declaration, line 25)
- `declared` (lexical_declaration, line 37)
- `value` (lexical_declaration, line 38)

## Contained in

- [core/resolvers](https://banes-lab.com/anatomy/secrets/folder-secrets-core-resolvers.md)

## Uses

- [configuration/strings/environment.strings.ts](https://banes-lab.com/source/secrets/configuration/strings/environment.strings.ts.md)
- [core/adapters/environment.adapter.ts](https://banes-lab.com/source/secrets/core/adapters/environment.adapter.ts.md)
- [core/validators/environment.validator.ts](https://banes-lab.com/source/secrets/core/validators/environment.validator.ts.md)

## Grounds

- [Secrets Management](https://banes-lab.com/records/architecture/secrets-management.md)

## Linked from

- [configuration/strings](https://banes-lab.com/anatomy/secrets/folder-secrets-configuration-strings.md)
- [core/adapters](https://banes-lab.com/anatomy/secrets/folder-secrets-core-adapters.md)
- [core/resolvers](https://banes-lab.com/anatomy/secrets/folder-secrets-core-resolvers.md)
- [core/validators](https://banes-lab.com/anatomy/secrets/folder-secrets-core-validators.md)

## Source

```typescript
import type { EnvironmentEntry, EnvironmentScope, OptionalKey, PortKey, TextKey } from "#types/environment.types";
import { VAULT_FOLDER, VAULT_PATH_SEPARATOR } from "#configuration/constants/environment.constants";
import { fieldLabels, revealField } from "#core/adapters/environment.adapter";
import { keyMissing, valueMalformed } from "#configuration/strings/environment.strings";
import { ENVIRONMENT_ENTRIES } from "#configuration/schemas/environment.schema";
import { isValueOfKind } from "#core/validators/environment.validator";

const DECLARED: readonly EnvironmentEntry[] = ENVIRONMENT_ENTRIES;

const ENTRY_BY_KEY: ReadonlyMap<string, EnvironmentEntry> = new Map(DECLARED.map((entry) => [entry.key, entry]));

const vaultEntryOf = function vaultEntryOf(scope: EnvironmentScope): string {
    return VAULT_FOLDER + VAULT_PATH_SEPARATOR + scope;
};

const declaredOf = function declaredOf(key: string): EnvironmentEntry {
    const declared = ENTRY_BY_KEY.get(key);
    if (declared === undefined) {
        throw new Error(keyMissing(key, VAULT_FOLDER));
    }
    return declared;
};

const storedValueOf = function storedValueOf(declared: EnvironmentEntry): string | null {
    const entry = vaultEntryOf(declared.scope);
    if (!fieldLabels(entry).has(declared.key)) {
        return null;
    }
    const value = revealField(entry, declared.key);
    if (!isValueOfKind(declared.kind, value)) {
        throw new Error(valueMalformed(declared.key, declared.kind));
    }
    return value;
};

const requiredOf = function requiredOf(key: PortKey | TextKey): string {
    const declared = declaredOf(key);
    const value = storedValueOf(declared);
    if (value === null) {
        throw new Error(keyMissing(key, vaultEntryOf(declared.scope)));
    }
    return value;
};

export const portOf = function portOf(key: PortKey): number {
    return Number(requiredOf(key));
};

export const textOf = function textOf(key: TextKey): string {
    return requiredOf(key);
};

export const optionalTextOf = function optionalTextOf(key: OptionalKey): string | null {
    return storedValueOf(declaredOf(key));
};
```
