Skip to content

First Spec Ideias #1

@jrmmendes

Description

@jrmmendes

Spec: V1

The project goal is to have a consistent service that wraps process.env, allowing users to validate if the environment has all required variables and apply any format validations.

The intended usage is something as follows:

1 - Create a function that implements the ValidationFunction interface:

type ValidationFunction<Environment> = (env: ProcessEnv, ...args: any[]) => Result<Environment>;

The Environment type refers to the expect environment variables. The validation process
must use the Result pattern to return either the reason for validation error, or the environment object with all values of process.env wrapped.

2 - Create the environment definition:

class MyEnvironment {
  @IsDefined()
  @IsIn(['DEV', 'HML', 'PRD'])
  NODE_ENV: string;
  
  @IsDefined()
  @isInt()
  PORT: number;
} 

Here I'm using something like class-validator, so we need to include those decorators to implement the validation function (althrough it would be a Joi schema or anything else);

3 - Use the ConfigurationServiceFactory to get a ConfigurationService instance:

import { ConfigurationServiceFactory } from '@mjsdevs/config.ts';

const service = ConfigurationServiceFactory
  .create<MyEnvironment>({
    validationFunction,
    env: process.env,
  });

4 - Get variables using the instance:

service.get('NODE_ENV');

// pass optional type paramenter to convert value in some desired format
service.get<number>('PORT');

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions