1.1.2 • Published 1 year ago

nestjs-nacos v1.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

介绍

nestjs 中集成 nacos

安装

$ npm i --save nestjs-nacos

使用

在模块中导入NacosConfigModule

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,
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

在项目中引入NacosConfigService

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

初始化参数

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

获取数据或者订阅变化

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

1 year ago

1.1.2

1 year ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago