nest-cloud / nestcloud

A NodeJS micro-service solution, writing by Typescript language and NestJS framework.
MIT License
422 stars 55 forks source link

Config package does not expose Config class as stated in documentation #32

Open djedlajn opened 4 years ago

djedlajn commented 4 years ago

Hi,

First of all thanks for all the work awesome stuff.

But I have noticed that the documentation and actual packages are out of sync. In this case @nestcloud/config.

It does not export Config class as stated in documentation so it cant be properly injected. Is this a bug because the documentation has Config class in the exports and I have checked and could not find it in exports.

djedlajn commented 4 years ago

Example

import { Injectable } from '@nestjs/common';
import { InjectConfig, Config } from '@nestcloud/config';

import { JwtModuleOptions, JwtOptionsFactory } from '@nestjs/jwt';

@Injectable()
export class JwtConfigService implements JwtOptionsFactory {
  // Cant inject due to lack of Config
  constructor(@InjectConfig() private readonly config: Config) {}
  createJwtOptions(): Promise<JwtModuleOptions> | JwtModuleOptions {
    const opts = this.config.get<JwtModuleOptions>('app.auth.jwtSettings');
    return {
      secret: opts.secret,
      signOptions: { expiresIn: '24h' },
    };
  }
}
djedlajn commented 4 years ago

So after digging a bit trough code I finally made it work.

For it to work you will be needing your preferred mesh in my example consul alongside with config module. With the following example.

consul.config - KV store

service:
  port: 50060
  auth:
    jwt:
      secret: "jk#oz2nBR!en#$Dg%qHh3RV!s$F$&&qN&5!m!3&^YuqVLN@tcCoMas7#%Z@kY4XJ"
      signOptions:
        expiresIn: "24h"
        algorithm: "ES256"

config.yaml - For bootstraping application

consul:
  host: localhost
  port: 8500
config:
  key: proj/config/${{ service.name }}
  name: proj/config/${{ service.name }}
service:
  discoveryHost: localhost
  healthCheck:
    timeout: 1s
    interval: 10s
    tcp: ${{ service.discoveryHost }}:${{ service.port }}
  maxRetry: 5
  retryInterval: 5000
  tags: ['v1.0.0', 'microservice']
  name: service-name
  port: 50054

Note for config module to work you need to have a name property set under the config section of BOOT file otherwise, it will complain.

After that it's pretty much done and you can proceed to use it as follows.

import { Injectable } from '@nestjs/common';
import { InjectConfig } from '@nestcloud/config';
import { JwtModuleOptions, JwtOptionsFactory } from '@nestjs/jwt';
import { IConfig } from '@nestcloud/common';

@Injectable()
export class JwtConfigService implements JwtOptionsFactory {
  constructor(@InjectConfig() private readonly config: IConfig) {}
  createJwtOptions(): Promise<JwtModuleOptions> | JwtModuleOptions {
    const opts = this.config.get<JwtModuleOptions>('service.auth.jwt');
    console.log('OPTS', opts);

    return opts;
  }
}

For those wondering nestcloud/common now holds all the required interfaces/classes needed for instantiation such as IBoot and IConfig.

And finally @miaowing I think it would be good to create a section for open docs I bet localizing and improving documentation would be greatly beneficial to the project as a whole. I can make PR and fill up the gaps with the config module as well as provide some examples for the consul module if that is okay.

P.S Also I think breaking changes since it is pre 1.x.x release should be somehow incorporated to build and deploy process so users can know what's been deprecated removed or renamed and how to go with it without digging into code until documentation catches up.

koolamusic commented 3 years ago

Seconding this as I had the same issues.

koolamusic commented 3 years ago

@miaowing happy to help with documentation, and with sample projects