Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First Spec Ideias #1

Open
jrmmendes opened this issue Jun 10, 2021 · 1 comment
Open

First Spec Ideias #1

jrmmendes opened this issue Jun 10, 2021 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@jrmmendes
Copy link
Contributor

jrmmendes commented Jun 10, 2021

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');
@jrmmendes
Copy link
Contributor Author

jrmmendes commented Jun 10, 2021

This is a simple working prove of concept, but it's coupled to class-validator and class-transformer
https://gist.github.com/jrmmendes/8db56c52ab79c57e30735501a3a7ffcc

@jrmmendes jrmmendes added this to the Setup and first version milestone Jun 10, 2021
@jrmmendes jrmmendes added the documentation Improvements or additions to documentation label Jun 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant