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

@nestjsx/common

v1.0.0

Published

A set of NestJs decorators, interfaces and configuration for application modules bootstraping

Downloads

16

Readme

@nestjsx/common

A set of NestJs decorators, interfaces and configuration for application modules bootstraping.

Table of Contents

Install

npm i --save @nestjsx/common

Decorators

@nestjsx/common provides two module decorators: @Module() and @AppRootModule(). They use glob to import module's files. Generally it looks like this:

/*.+(names){.ts,.js}

The default list of files names can be found under Configuration section.

@Module() decorator

It allows to inject controllers, providers, import entities or models and export providers with minimum effort. To make this happen, a file that exports a module class should be placed in a root of module directory. e.g. we have heroes module where we have heroes.module.ts and some files alongside with it or under some subdirectories of this module: heroes.service.ts, heroes.controller.ts, hero.entity.ts. In this case, heroes.module.ts can be like this:

import { Module } from '@nestjsx/common';

@Module()
export class HeroesModule {}

The @Module() decorator will inject, import and export those components. Of course, we can override any decorator's config property as if we use native NestJS @Module() decorator (docs).

Entities (models) can be imported if one of these has been installed: @nestjs/typeorm or @nestjs/mongoose. Also the orm package name must be specified in the Configuration. e.g.

...
"packages": {
  "orm": {
    "name": "@nestjs/typeorm"
  }
}

@AppRootModule() decorator

It allows to import application modules and can be used as a NestJS root module. e.g. we have a module file app.module.ts in our src directory:

import { AppRootModule } from '@nestjsx/common';

@AppRootModule()
export class AppModule {}

It will import modules and then we can use it the bootstrap function:

...
const app = await NestFactory.create(AppModule);

For the sake of the idea of module's functionality incapsulation, @AppRootModule will not import all modules, but only those with the names that are specified in the Configuration. By default, they are ["global-module", "api-module"] and can be changed in the configuration to meet your requirements.

@AppRootModule can also import global providers like APP_GUARD, APP_INTERCEPTOR, APP_FILTER and APP_PIPE. e.g. we have a file http-filter.app-provider.ts (or we can call it http.app-filter.ts) somewhere in our src directory with the following:

import { APP_FILTER } from '@nestjs/core';
import { NestjsxAppProvider } from '@nestjsx/common';

export const AppHttpFilterProvider = {
  order: 0,
  provider: {
    provide: APP_FILTER,
    useClass: HttpFilter,
  },
} as NestjsxAppProvider;

Since these files can be created anywhere in our project, we want to make sure that these global filters, guards, interceptors and pipes will be executed in a right order. To do so, we need to set an order property for each of them.

Files with those global components must be named according to globalsPrefix specified in the Configuration (app as a default) and can be changed accordingly.

Configuration

By default, configuration looks like this:

"bootstrap": {
  "globalsPrefix": "app",
  "exportProviders": true,
  "files": {
    "modules": ["global-module", "api-module"],
    "providers": ["provider", "providers", "service", "helper", "gateway"],
    "controllers": ["controller"],
    "guards": ["guard"],
    "interceptors": ["interceptor"],
    "filters": ["filter"],
    "pipes": ["pipe"],
    "entities": ["entity"]
  }
},
"packages": {
  "orm": {
    "name": null
  }
}

Any of this params can be changed in the configuration file:

.nestrc

You can create it in your project root or any other appropriate place (please refer to rc npm package for more details).