DEV Community

Cover image for Publish Nest JS Dynamic Package to Github
tkssharma
tkssharma

Posted on

Publish Nest JS Dynamic Package to Github

Publish Nest JS Dynamic Package to Github

In this video we are publishing Nest JS Dynamic Package to Github, Package contains a simple NestJS Package that gives dynamic random Numbers

'private package deployed to github'

'github repo to play with package'



// Package.
import { Inject } from "@nestjs/common";

// Internal.
import { RANDOM_NUMBER_CLIENT_MODULE_OPTIONS } from "./random-number.constants";
import { RandomNumberModuleOptions } from "./random-number.interface";

export class RandomNumberService {
  private readonly min: number = 0;
  private readonly max: number = 100;

  constructor(
    @Inject(RANDOM_NUMBER_CLIENT_MODULE_OPTIONS)
    private readonly options: RandomNumberModuleOptions
  ) {
    this.min = this.options.min;
    this.max = this.options.max;
  }

  generate(): number {
    const range = this.max - this.min;
    return this.min + Math.floor(Math.random() * range);
  }
}


Enter fullscreen mode Exit fullscreen mode

Nest JS Dynamic module



// Package.
import { DynamicModule, Global, Module, Provider, Type } from "@nestjs/common";

//Internal
import {
  RANDOM_NUMBER_CLIENT_TOKEN,
  RANDOM_NUMBER_CLIENT_MODULE_OPTIONS,
} from "./random-number.constants";
import {
  RandomNumberModuleOptions,
  RandomNumberModuleAsyncOptions,
  RandomNumberModuleFactory,
} from "./random-number.interface";
import { getRandomNumberModuleOptions } from "./utils";

//Code.
@Global()
@Module({})
export class RandomNumberModule {
  public static forRoot(options: RandomNumberModuleOptions): DynamicModule {
    const providers: Provider[] = [
      {
        provide: RANDOM_NUMBER_CLIENT_TOKEN,
        useValue: getRandomNumberModuleOptions(options),
      }
    ]
    return {
      module: RandomNumberModule,
      providers: providers,
      exports: providers,
    };
  }

  public static forRootAsync(
    options: RandomNumberModuleAsyncOptions
  ): DynamicModule {

    const provider: Provider = {
      inject: [RANDOM_NUMBER_CLIENT_MODULE_OPTIONS],
      provide: RANDOM_NUMBER_CLIENT_TOKEN,
      useFactory: async (options: RandomNumberModuleOptions) =>
        getRandomNumberModuleOptions(options),
    };

    return {
      module: RandomNumberModule,
      imports: options.imports,
      providers: [...this.createAsyncProviders(options), provider],
      exports: [provider],
    };
  }

  private static createAsyncProviders(
    options: RandomNumberModuleAsyncOptions
  ): Provider[] {
    if (options.useExisting || options.useFactory) {
      return [this.createAsyncOptionsProvider(options)];
    }

    const useClass = options.useClass as Type<RandomNumberModuleFactory>;

    return [
      this.createAsyncOptionsProvider(options),
      {
        provide: useClass,
        useClass,
      },
    ];
  }

  private static createAsyncOptionsProvider(
    options: RandomNumberModuleAsyncOptions
  ): Provider {
    if (options.useFactory) {
      return {
        provide: RANDOM_NUMBER_CLIENT_MODULE_OPTIONS,
        useFactory: options.useFactory,
        inject: options.inject || [],
      };
    }

    const inject = [
      (options.useClass ||
        options.useExisting) as Type<RandomNumberModuleFactory>,
    ];

    return {
      provide: RANDOM_NUMBER_CLIENT_MODULE_OPTIONS,
      useFactory: async (optionsFactory: RandomNumberModuleFactory) =>
        await optionsFactory.createApiModuleOptions(),
      inject,
    };
  }
}



Enter fullscreen mode Exit fullscreen mode

Please checkout My video on the channel to Learn More about the package

publishing to Github using Github actions

Info About playlist

PlalyList Link
https://www.youtube.com/watch?v=lVze5eT5DQQ&list=PLIGDNOJWiL18ucL7WGWeVjXmSzOVTRObE
GitHub Link
https://github.com/tkssharma/publish-packages

🚀 Publish Node JS Utility Package to GitHub
🚀 Publish React JS Component Package to GitHub
🚀 Publish React JS Component and deploy using CI GitHub Actions to GitHub
🚀 Publish Nest JS Utility Package to GitHub
🚀 Publish Nest JS Dynamic Package to GitHub

🚀🚀 How to Publish Private NPM Packages With Github Package Registry

We'll begin by first making our modules repository private.
Publishing private NPM packages using GitHub Package Registry involves several steps to configure your package and authenticate with GitHub. Here's a step-by-step guide:

🚀🚀 Prerequisites
Node.js and npm: Make sure you have Node.js and npm installed.
GitHub Account: Ensure you have a GitHub account and a repository where you want to host the package.
Repository Permissions: Ensure you have the necessary permissions to publish to the GitHub repository.

🚀🚀We will follow these steps

  • Create a GitHub Personal Access Token
  • Authenticating With NPM Using the GitHub Registry
  • Publish Your Package
  • Using Your Private Package in Another Project

Top comments (0)