npm package: @pitech-lib/config
Description: Reusable config package for Env and AWS Secrets Manager with Joi validation.
Latest version: 1.0.2

# NestJS Config Toolkit

A powerful, modular, and extensible configuration package for **NestJS applications**, supporting both **environment variables** and **AWS Secrets Manager** with built-in **schema validation**.

---

## How to Install

Install the core package and required dependencies based on your need:

```bash
npm install @pitech-lib/config joi dotenv @aws-sdk/client-secrets-manager
```

| Package                           | Purpose                              |
| --------------------------------- | ------------------------------------ |
| `@pitech-lib/config`              | This configuration toolkit           |
| `joi`                             | Schema validation                    |
| `dotenv`                          | Load `.env` files into `process.env` |
| `@aws-sdk/client-secrets-manager` | AWS SDK for retrieving secrets       |

---

## Using Environment Variables

To use `EnvConfig`, first define your schema using Joi, then register the loader in `ConfigModule.forRoot()`:

```ts
import { Module } from '@nestjs/common';
import Joi from 'joi';
import { ConfigModule } from '@pitech-lib/config';
import { AppController } from './app.controller';

@Module({
  imports: [
    ConfigModule.forRoot({
      env: {
        schema: Joi.object({
          NODE_ENV: Joi.string().valid('development', 'production').required(),
          PORT: Joi.number().required(),
        }),
      },
    }),
  ],
  controllers: [AppController],
})
export class AppModule {}
```

#### ✅ Usage in Controller

```ts
import { Controller, Get } from '@nestjs/common';
import { EnvConfig } from '@pitech-lib/config';

@Controller('config')
export class AppController {
  constructor(private readonly env: EnvConfig) {}

  @Get('env')
  getEnv() {
    return {
      NODE_ENV: this.env.get<string>('NODE_ENV'),
      PORT: this.env.get<number>('PORT'),
    };
  }
}
```

---

## Using AWS Secrets Manager

To pull configuration from AWS Secrets Manager, include the `aws` config along with an instance of `SecretsManagerClient`:

```ts
import { SecretsManagerClient } from '@aws-sdk/client-secrets-manager';

ConfigModule.forRoot({
  aws: {
    schema: Joi.object({
      DB_HOST: Joi.string().required(),
      DB_PASS: Joi.string().required(),
    }),
    awsClient: new SecretsManagerClient({ region: 'eu-west-1' }),
  },
});
```

#### Usage in Controller

```ts
import { Controller, Get } from '@nestjs/common';
import { AwsSecretManagerConfig } from '@pitech-lib/config';

@Controller('config')
export class AppController {
  constructor(private readonly aws: AwsSecretManagerConfig) {}

  @Get('aws')
  getAwsSecrets() {
    return {
      DB_HOST: this.aws.get<string>('DB_HOST'),
      DB_PASS: this.aws.get<string>('DB_PASS'),
    };
  }
}
```

---

## Using Both Loaders Together

```ts
@Module({
  imports: [
    ConfigModule.forRoot({
      env: {
        schema: Joi.object({
          NODE_ENV: Joi.string().required(),
        }),
      },
      aws: {
        schema: Joi.object({
          DB_HOST: Joi.string().required(),
        }),
        awsClient: new SecretsManagerClient({ region: 'us-east-1' }),
      },
    }),
  ],
})
export class AppModule {}
```

#### Usage in Controller

```ts
@Controller('config')
export class AppController {
  constructor(
    private readonly env: EnvConfig,
    private readonly aws: AwsSecretManagerConfig,
  ) {}

  @Get()
  getCombinedConfig() {
    return {
      NODE_ENV: this.env.get<string>('NODE_ENV'),
      DB_HOST: this.aws.get<string>('DB_HOST'),
    };
  }
}
```

---


## Advanced Usage

### Direct Manual Usage (e.g. scripts, CLI tools)

```ts
const config = new EnvConfig(schema);
await config.load();
const value = config.get<string>('NODE_ENV');
```