nestjs-nacos
TypeScript icon, indicating that this package has built-in type declarations

1.1.2 • Public • Published

Description

the nacos integration in nestjs

Installation

$ npm i --save nestjs-nacos

Usage

register config module before use

import { NacosConfigModule } from "nestjs-nacos";
import { Module } from "@nestjs/common";
import { AppController } from "./app.controller";
import { AppService } from "./app.service";

@Module({
  imports: [
    NacosConfigModule.register(
      {
        url: "xxx",
        // the namesapce is optional
        namespace: "xxx",
        // the timeout is optional, default 30000ms
        timeout: 4000,
      },
      // register nacos config module as a global module
      true
    ),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

import service in your project

import { NacosConfigService } from "nestjs-nacos";
import { Injectable } from "@nestjs/common";

@Injectable()
export class AppService {
  constructor(protected nacosConfigService: NacosConfigService) {
    this.nacosConfigService.subscribeKeyItem({
      dataId: "oss-config.development.json",
      // the group is optional
      group: "DEFAULT_GROUP",
      handler: (config: string) => {
        console.log(config);
      },
    });
  }

  getHello() {
    return this.nacosConfigService.getKeyItemConfig({
      dataId: "oss-config.development.json",
      // the group is optional
      group: "DEFAULT_GROUP",
    });
  }
}

API

initialize module options;

interface NacosConfigOptions {
  /**
   * nacos server address is required
   */
  url: string;
  /**
   * nacos gets the remote data timeout time
   */
  timeout?: number;
  /**
   * the namespace is optional
   */
  namespace?: string;
}

get config item or subscribe change

interface NacosConfigService {
  subscribeKeyItem({ dataId, group, handler }: NacosConfigSubscribeParams): void;
  getKeyItemConfig({ dataId, group }: NacosConfigGetItemParams): Promise<string>;
}

Readme

Keywords

Package Sidebar

Install

npm i nestjs-nacos

Weekly Downloads

8

Version

1.1.2

License

MIT

Unpacked Size

16.1 kB

Total Files

23

Last publish

Collaborators

  • sicau-yangxu