Skip to content

@smitejs/env ​

Declarative environment variables for Smite: provider, zod validation, collect-mode manifest.

bash
yarn add @smitejs/env

8 documented symbols · version 2.0.0-SNAPSHOT.0 · source

Providers ​

typeEnvProvider ​

ts
type EnvProvider = (key: string) => Promise<unknown>;

Source of raw environment values: (key) => Promise<raw>.

packages/env/src/index.ts:6

Registration ​

interfaceEnvBuilder ​

ts
interface EnvBuilder<Specs extends EnvSpecs> {
    readonly withProvider: (provider: EnvProvider, options?: EnvProviderOptions) => EnvInstance<Specs>;
}

Builder returned by register; attach a provider to get an instance.

packages/env/src/index.ts:52

typeEnvSpec ​

ts
type EnvSpec<Value = unknown> = {
    readonly key: string;
    readonly validation: z.ZodType<Value>;
};

A declared variable: the raw provider key plus its zod validation.

packages/env/src/index.ts:13

typeEnvSpecs ​

ts
type EnvSpecs = Readonly<Record<string, EnvSpec>>;

A map of declared name to its spec.

packages/env/src/index.ts:23

functionregister ​

ts
function register<Specs extends EnvSpecs>(entries: Specs): EnvBuilder<Specs>;

Declares a set of env variables and returns a builder that becomes a scoped instance once a provider is attached. Fails on duplicate names. In collect mode each variable is also registered as an env.var node.

ts
const instance = env
  .register({
    databaseUrl: { key: "DATABASE_URL", validation: z.string() },
  })
  .withProvider(from({ DATABASE_URL: "postgres://x" }));
const url = await instance.databaseUrl;
ts
"postgres://x"
ts
const instance = env
  .register({
    port: { key: "PORT", validation: z.coerce.number().default(3000) },
  })
  .withProvider(async () => undefined);
const port = await instance.port;
ts
3000
ts
const instance = env
  .register({
    debug: { key: "DEBUG", validation: z.string().optional() },
  })
  .withProvider(async () => undefined);
const debug = await instance.debug;

packages/env/src/index.ts:79

Resolution ​

typeEnvInstance ​

ts
type EnvInstance<Specs extends EnvSpecs> = {
    readonly [Name in keyof Specs]: Promise<z.infer<Specs[Name]["validation"]>>;
};

The resolved instance: one lazily-resolved property per declared name.

packages/env/src/index.ts:30

typeEnvProviderOptions ​

ts
type EnvProviderOptions = {
    readonly cache?: boolean;
};

Per-instance options passed to EnvBuilder.withProvider.

packages/env/src/index.ts:39

Surface ​

constenv ​

ts
const env: {
    register: typeof register;
};

The env factory: declare variables, attach a provider, read values.

ts
const instance = env
  .register({
    databaseUrl: { key: "DATABASE_URL", validation: z.string() },
  })
  .withProvider(from({ DATABASE_URL: "postgres://x" }));
const url = await instance.databaseUrl;
ts
"postgres://x"

packages/env/src/index.ts:136

Released under the MIT License.

/ search•[ ] prev/next•j/k scroll•h home•t theme•api/smitejs-env