npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-typed-config

v0.2.2

Published

type-safe nestjs config module & service

Downloads

913

Readme

nestjs-typed-config (ENG)

KOR

nestjs-typed-config is type-safe nestjs config module & service. You can use ConfigService with type-safety, without additional type-casting.

What you need to do is just define your joi schema with typing, and pass it through module and service.

You can use all options in ConfigModule.forRoot, and you can also use all features in ConfigService. This will make your migration from ConfigModule to TypedConfigModule easier. Plus, you can also use ConfigService from @nestjs/config without any additional changes. TypedConfigModule also provide dependency injection for original ConfigService, so you can migrate your code step by step.

You can see detailed usage example in example code.

install

npm install nestjs-typed-config

createTypedConfig

By calling createTypedConfig, you can create TypedConfigService & TypedConfigModule. And then, you change your all ConfigModule to generated TypedConfigModule. Now, you can use TypedConfigService instead of ConfigService.

Below code is example of generating TypedConfigService and TypedConfigModule. You should write below code in your own project.

// typed-config.ts
import { createTypedConfig } from 'nestjs-typed-config';
import * as Joi from 'joi';

export const { TypedConfigService, TypedConfigModule } = createTypedConfig({
  DB_PASSWORD: Joi.string().required(),
  DB_PORT: Joi.number().required(),
});

export type TypedConfigService = InstanceType<typeof TypedConfigService>; // must declare this! 

Resolving Joi schema

If you just want to use TypedConfigService, you will not need this. It transforms joi schema type to plain object type.

import { ResolveJoiSchema } from 'nestjs-typed-config';
import * as Joi from 'joi';

const envObject = {
  NODE_ENV: Joi.string().required(),
  PORT: Joi.number().required(),
};

const envSchema = Joi.object<typeof envObject>(envObject);

type EnvType = ResolveJoiSchema<typeof envSchema>; // EnvType will be { NODE_ENV: string; PORT: number; }

example

For detailed example, see example code.

You should write below code to generate TypedConfig in your own project.

// src/typed-config.ts
import { createTypedConfig } from 'nestjs-typed-config';
import * as Joi from 'joi';

// parameter should be composed with joi. not wrapped with joi.object()
export const { TypedConfigService, TypedConfigModule } = createTypedConfig({
  DB_PASSWORD: Joi.string().required(),
  DB_PORT: Joi.number().required(),
});

export type TypedConfigService = InstanceType<typeof TypedConfigService>; // must declare this!

Import TypedConfigModule from src/typed-config.ts instead of ConfigModule from @nestjs/config.

// src/app.module.ts
import { TypedConfigModule } from './typed-config';

@Module({
  imports: [
    TypedConfigModule.forRoot({
      isGlobal: true,
    }),
  ],
})
export class AppModule {}

Import TypedConfigService from src/typed-config.ts instead of ConfigService from @nestjs/config.

// src/app.service.ts
import { TypedConfigService } from './typed-config';

@Injectable()
export class AppService {
  constructor(private readonly configService: TypedConfigService) {} // use TypedConfigService instead of ConfigService

  foo() {
    const nodeEnv = configService.get('NODE_ENV'); // infer type as string
    const port = configService.get('PORT'); // infer type as number
    const host = configService.get('HOST'); // compile error. HOST is not in schema
    const port2: boolean = configService.get('PORT'); // compile error. number is not assignable to boolean
  }
}