5.3.14 • Published 7 months ago

@pitininja/envious v5.3.14

Weekly downloads
-
License
AGPL-3.0-or-later
Repository
-
Last release
7 months ago

Envious

npm version

Environment variable parsing using Typebox

Install

npm i @pitininja/envious

Usage

Refer to the official Typebox documentation for how to write a Typebox schema.

import { envious } from '@pitininja/envious';
import { Type } from '@sinclair/typebox';

const env = envious(
    Type.Object({
        STRING_VAR: Type.String(),
        NUMBER_VAR: Type.Integer(),
        BOOLEAN_VAR_WITH_DEFAULT: Type.Boolean({ default: false }),
        OPTIONAL_VAR: Type.Optional(Type.String())
    })
);

Previously dotenv was used to load environment variables. This is not longer the case in v5, since environment variables can be natively loaded with Node.js / TSX / Vitest / etc.

# Example loading environment variables with Node.js
node --env-file=.env index.js
# Example loading environment variables with TSX
tsx --env-file=.env index.ts

Formats

String formats can be loaded in the formats property of the options parameter, as an object with the format name as key and a regex or a validation function as value.

import { envious } from '@pitininja/envious';
import { Type } from '@sinclair/typebox';
import dayjs from 'dayjs';

const env = envious(
    Type.Object({
        EXAMPLE_URI: Type.String({ format: 'uri' }),
        EXAMPLE_DATE: Type.String({ format: 'date' })
    }),
    {
        formats: {
            uri: /^(?:[a-z][a-z0-9+\-.]*:)(?:\/?\/)?[^\s]*$/i,
            date: (val) => dayjs(val).isValid()
        }
    }
);

Errors

If something goes wrong, Envious will throw an error of class EnviousError.

An EnviousError error contains a message and a list of ValueError (an error class taken from Typebox).

Here is a simple example of how to handle Envious errors :

import { envious, EnviousError } from '@pitininja/envious';
import { Type } from '@sinclair/typebox';

try {
    const env = envious(
        Type.Object({
            STRING_VAR: Type.String()
        })
    );
} catch (err: unknown) {
    if (err instanceof EnviousError) {
        console.log('Message:', err.message); // Error message
        console.log('Errors:', err.errors); // Typebox's ValueError array
    }
}

Logging

In addition to throwing an EnviousError, errors can be logged in the console. To do so, use the logErrors option :

import { envious, EnviousError } from '@pitininja/envious';
import { Type } from '@sinclair/typebox';

const env = envious(
    Type.Object({
        STRING_VAR: Type.String()
    }),
    {
        logErrors: true
    }
);

You can also provide your own logging function :

import { envious, EnviousError } from '@pitininja/envious';
import { Type } from '@sinclair/typebox';

const env = envious(
    Type.Object({
        STRING_VAR: Type.String()
    }),
    {
        logErrors: true,
        logger: (message) => {
            myCustomLogger(message)
        }
    }
);
5.3.3

11 months ago

5.3.2

11 months ago

5.3.1

11 months ago

5.3.0

11 months ago

5.1.0

12 months ago

4.1.7

1 year ago

4.0.0

1 year ago

5.3.14

7 months ago

5.3.13

8 months ago

5.3.12

9 months ago

5.3.11

9 months ago

5.3.10

10 months ago

5.2.1

11 months ago

5.2.0

11 months ago

5.0.1

12 months ago

5.0.0

1 year ago

4.1.4

1 year ago

4.1.3

1 year ago

4.1.6

1 year ago

4.1.5

1 year ago

4.1.0

1 year ago

4.1.2

1 year ago

4.1.1

1 year ago

5.3.9

10 months ago

5.3.8

10 months ago

5.3.7

11 months ago

5.3.6

11 months ago

5.3.5

11 months ago

5.3.4

11 months ago

3.3.5

1 year ago

4.0.0-beta.1

1 year ago

3.3.4

1 year ago

3.3.3

1 year ago

3.3.2

1 year ago

3.3.1

1 year ago

3.3.0

1 year ago

3.1.11

1 year ago

3.1.10

1 year ago

3.1.9

1 year ago

3.1.8

1 year ago

3.2.0

1 year ago

3.1.7

2 years ago

3.1.6

2 years ago

3.1.5

2 years ago

3.1.4

2 years ago

3.1.3

2 years ago

3.1.2

2 years ago

1.1.0

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.0.0-beta.1

2 years ago

3.0.0-beta.3

2 years ago

3.0.0-beta.2

2 years ago

1.0.7

2 years ago

3.0.0

2 years ago

3.0.0-beta

2 years ago

3.1.1-beta.0

2 years ago

2.0.0

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago