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-consul-kv-realtime

v0.1.10

Published

NestJS library for work with consul-kv

Downloads

121

Readme

NestJS library for work with consul-kv

npm version monthly downloads

Installation

npm i --save consul nestjs-consul-kv-realtime

Links

https://www.npmjs.com/package/ilink-console-tools - Console tools with method for save .env file to consul-kv and method for download from consul-kv to .env file.

https://github.com/i-link-pro-team/ilink/blob/develop/libs/nestjs-consul-kv-realtime/tests/e2e/nestjs-consul-kv-realtime.spec.ts - Tests for all exists feature of library.

Usage

Simple usage with global watchers for consul-kv

import { Module } from '@nestjs/common';
import { NestjsConsulKvRealtimeModule } from 'nestjs-consul-kv-realtime';

@Module({
  imports: [
    NestjsConsulKvRealtimeModule.forRootAsync({
      useFactory: async () => ({
        port: '8500',
        host: 'localhost',
        defaults: {
          token: `CONSUL_HTTP_TOKEN`,
        },
        watchers: [
          {
            interval: 1000,
            key: 'consul-key',
            callback: async (value: { key: string }) => {
              console.log('New value from consul:', value);
            },
          },
        ],
      }),
    }),
  ],
})
export class AppModule {}

Get value from consul-kv with service

import { Injectable } from '@nestjs/common';
import { NestjsConsulKvRealtimeService } from 'nestjs-consul-kv-realtime';

@Injectable()
export class MyService {
  constructor(
    private readonly nestjsConsulKvRealtimeService: NestjsConsulKvRealtimeService
  ) {}
  async method() {
    const value = await this.nestjsConsulKvRealtimeService.getValue(
      'consul-key'
    );
    console.log('Value from consul:', value);
    return value;
  }
}

Listen changes from consul-kv

import { Controller, Get } from '@nestjs/common';
import { NestjsConsulKvRealtimeService } from 'nestjs-consul-kv-realtime';
import { tap } from 'rxjs/operators';

@Controller()
export class MyController {
  constructor(
    private readonly nestjsConsulKvRealtimeService: NestjsConsulKvRealtimeService
  ) {}

  @Get()
  getConsulValue() {
    return this.nestjsConsulKvRealtimeService
      .listen({ key: 'consul-key' })
      .pipe(tap((value) => console.log('New value from consul:', value)));
  }
}

Use decorator for get consul-kv value

import { Injectable } from '@nestjs/common';
import { ConsulKeyValue } from 'nestjs-consul-kv-realtime';

@Injectable()
export class MyService {
  @ConsulKeyValue({
    key: 'consul-key',
  })
  value!: { key: string };

  async method() {
    console.log('Value from consul:', this.value);
    return this.value;
  }
}

Use decorator with factory for create custom object from consul-kv value

import { Injectable } from '@nestjs/common';
import { ConsulKeyValue } from 'nestjs-consul-kv-realtime';

class ExternalLibrary {
  public options?: { key1: string };
  private connected = false;

  async connect(options: { key1: string }) {
    this.options = options;
    this.connected = true;
  }

  async waitEndOfAllCurrentOperations() {
    return true;
  }

  async disconnect() {
    this.connected = false;
  }

  async isConnected() {
    return this.connected === true;
  }
}

@Injectable()
export class MyService {
  @ConsulKeyValue({
    interval: 500,
    key: 'file',
    factory: async (
      newConsulValue: { key1: string },
      oldValue?: ExternalLibrary
    ) => {
      if (oldValue) {
        await oldValue.waitEndOfAllCurrentOperations();
        await oldValue.disconnect();
      }
      const externalLibrary = new ExternalLibrary();
      await externalLibrary.connect(newConsulValue);
      return externalLibrary;
    },
  })
  externalLibrary!: ExternalLibrary;

  async isConnected() {
    const isConnected = this.externalLibrary.isConnected();
    console.log('Connection state:', isConnected);
    return isConnected;
  }
}

License

MIT