0.7.1 • Published 4 years ago

@dimensionfourcloud/boot v0.7.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

NestCloud - Boot

Description

NestCloud component for getting local configurations and environment values when the app bootstrap.

中文文档

Installation

$ npm i --save @dimensionfourcloud/boot

Quick Start

Import Module

import { Module } from '@nestjs/common';
import { BootModule } from '@dimensionfourcloud/boot';

const env = process.env.NODE_ENV;

@Module({
  imports: [BootModule.register(__dirname, `bootstrap-${env}.yml`)],
})
export class ApplicationModule {}

Configurations

eg: bootstrap-development.yml.

web:
  name: example-service
  port: 3000

Usage

There are two ways to get configurations,

  1. Inject Boot instance:
import { Injectable } from '@nestjs/common';
import { InjectBoot, Boot } from '@dimensionfourcloud/boot';

@Injectable()
export class TestService {
  constructor(@InjectBoot() private readonly boot: Boot) {}

  getPort() {
      return this.boot.get('web.port', 3000);
  }
}
  1. Inject value:
import { Injectable } from '@nestjs/common';
import { BootValue } from '@dimensionfourcloud/boot';

@Injectable()
export class TestService {
  @BootValue('service.port', 3000)
  private readonly port: number;

  getPort() {
      return this.port;
  }
}

Template Compile.

Dependency handlebars.js.

template:

process.env.SERVICE_ID = 'your-service-id';
process.env.SERVICE_NAME = 'your-service-name';
service:
  id: ${{ SERVICE_ID }}
  name: ${{ SERVICE_NAME }}
  port: 3000
  address: http://${{ service.name }}:${{ service.port }}

result:

service:
  id: your-service-id
  name: your-service-name
  port: 3000
  address: http://your-service-name:3000

API

class BootModule

static register(path: string, filename: string): DynamicModule

Register boot module.

fieldtypedescription
pathstringthe config file path
filenamestringthe config filename

class Boot

get<T>(path: string, defaults?: T): T

Get configurations

fieldtypedescription
pathstringpath of configurations
defaultsanydefault value if the specific configuration is not exist

getEnv(): string

Get current NODE_ENV value, if not set, it will return 'development'.

getFilename(): string

Get the current config filename.

getConfigPath(): string

Get the config file path.

getFullConfigPath(): string

Get the config file path with filename.

Decorator

InjectBoot(): PropertyDecorator

Inject Boot instance.

BootValue(path?: string, defaultValue?: any): PropertyDecorator

Inject configuration to class attribute.

Stay in touch

License

NestCloud is MIT licensed.