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

@cheep/nestjs

v1.0.4

Published

An adapter to use the @cheep/microservices library in the NestJS framework.

Downloads

19

Readme

@cheep/nestjs

An adapter to use the @cheep/microservices library in the NestJS framework.

Setup

App Level

Import the CheepMicroservicesModule.forRoot in a top-level module, once per application/microservice. This establishes the primary transport for all feature modules in this process.

import {
  CheepTransportModule,
  NestTransportUtils,
} from '@cheep/nestjs'
// ... more imports!
@Module({
  imports: [
    CheepMicroservicesModule.forRoot({
      // MemoryTransport is included by default in @cheep/transport,
      // others are also available!
      transport: new MemoryTransport(
        {
          // optional metadata config goes here
        },
        // this is the set of external plugins / functional callbacks cheep will utilise
        // we provide a default object for your convenience, but you can override it
        NestTransportUtils,
      ),
    }),
    /* ...other providers */
  ],
})
export class AppModule {}

Feature module

Defines an individual microservice API namespace.

First, create some services which you would like to expose functions from, then, delcare your API in pure Typescript (this file will disappear after compilation)

// user.api.ts
/**
 * *NOTE* use of import type - the api definition file should never
 * import anything other than types.
 *
 * UserQueryService and UserCommandService are normal
 * NestJS Injectable services
 */
import type { UserCommandService } from './user.command.service'
import type { UserQueryService } from './user.query.service'

/**
 * UserApi
 * a declarative type of the public api of this module,
 * consumed by other modules and used to ensure all handlers
 * are adequately provided in this module
 */
export type UserApi = ApiWithExecutableKeys<
  // this is the shape of the api itself, can be either imported classes
  // or function definitions
  {
    Query: {
      User: UserQueryService
    }
    Command: {
      User: UserCommandService
    }
    Event: {
      created: (user: User) => void
    }
  },
  // this is a union of the top level keys from the object which can be "executed",
  // meaning they can be awaited for a response
  'Query' | 'Command'
>

/**
 * UserRemoteApi
 * a union type of the api types that are consumed by this module
 * it's helpful to use import(...) syntax here to allow tools like NX
 * to differentiate the dependencies, as these dependencies will disappear
 * after typescript compilation (keeping your bundle size small!)
 */
export type UserRemoteApi =
  | import('../groups/group.api.ts').GroupApi
  | import('../accounts/account.api.ts').AccountApi

With those types defined, import the CheepMicroservicesModule.forModule in your module

// user.module.ts

@Module({
  imports: [
    // call for module with this module's api, and the remote api union to be consumed
    CheepMicroservicesModule.forModule<UserApi, UserRemoteApi>({
      // this registers your services as handlers of the various API routes available from
      // the intersection of remote and local api types
      handlers:{
        Query: {
          User: UserQueryService
        },
        Command:{
          User: UserCommandService
        }
      }
      // for any events which will be subscribed with observables, include them in this
      // map by setting them to true; you can set specific routes, or whole subtrees
      listenEventsFrom: {
        Event:{
          Group: true
        }
      },
    }),
    /* ...other providers */
  ],
  // be sure to also provide the query and command handler services to Nest!
  providers: [UserQueryService, UserCommandService],
})
export class UserModule {}

Usage

Once the setup is complete, you may consume the exported services from CheepMicroservicesModule.forModule using CheepApi in other modules

CheepApi

The CheepApi is for executing queries or commands on remote modules, such as from a gateway controller which is not in the same module/microservice.

Usage of the API is transparent, and has instant type safety to changes in the remote module.

// gateway.controller.ts

@Controller()
export class GatewayService implements OnApplicationBootstrap {
  constructor(
    // ConsumedApis is an intersection type = UserApi & GroupApi
    private api: CheepApi<ConsumedApis>,
  ) {}

  @Get('users')
  async getUsers() {
    // calling a query
    return this.api.execute.Query.User.getAll()
  }

  @Get('user/create')
  async createUser() {
    // calling commands
    const id = await this.api.execute.Command.User.create({
      user: {
        email: faker.internet.email(),
        name: faker.name.findName(),
      },
    })

    return this.api.execute.Query.User.getById({ id })
  }

  @Get('groups')
  async getGroups() {
    // the query object has keys for each unique namespace
    return this.api.execute.Query.Group.getAll()
  }

  @Get('group/create')
  async createGroup() {
    // the command object has keys for each unique namespace
    const id = await this.api.execute.Command.Group.create({
      group: {
        name: faker.commerce.department(),
        color: faker.random.arrayElement(['red', 'blue']),
      },
    })

    return this.api.execute.Query.Group.getById({ id })
  }

  /**
   * This is to show that *private* members of remote apis may still be
   * called without type safety, just like private functions in JS at runtime
   *
   */
  @Get('test')
  async test() {
    return await this.api.execute.Command.User['thisIsPrivate']() // will be *any*
  }
}