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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@usageflow/nestjs

v0.4.1

Published

UsageFlow plugin for NestJS applications

Readme

@usageflow/nestjs

NestJS module for UsageFlow API tracking. Easily monitor and analyze your NestJS API usage with real-time tracking and allocation management.

npm version License: MIT

Installation

npm install @usageflow/nestjs

Quick Start

// app.module.ts
import { Module } from '@nestjs/common';
import { UsageFlowModule } from '@usageflow/nestjs';

@Module({
  imports: [
    UsageFlowModule.forRoot({
      apiKey: 'YOUR_API_KEY',
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Configuration

UsageFlowModule.forRoot

Configures the UsageFlow module with your API key. The module automatically tracks all routes and handles route configuration through the UsageFlow dashboard.

UsageFlowModule.forRoot({
  apiKey: string; // Your UsageFlow API key (required)
})

Examples

Basic usage:

UsageFlowModule.forRoot({
  apiKey: 'YOUR_API_KEY',
});

Route configuration (which routes to track, whitelist, etc.) is managed through the UsageFlow dashboard, not through code parameters. The middleware is automatically applied to all routes.

Advanced Configuration

Using UsageFlowService Directly

You can inject UsageFlowService into your services or controllers:

import { Injectable } from '@nestjs/common';
import { UsageFlowService } from '@usageflow/nestjs';

@Injectable()
export class MyService {
  constructor(private readonly usageFlowService: UsageFlowService) {}

  async someMethod() {
    // Access UsageFlowService methods
    const apiKey = this.usageFlowService.getApiKey();
    // ...
  }
}

Using UsageFlowRouteScanner

The module includes a route scanner that automatically detects routes in your NestJS application:

import { Injectable } from '@nestjs/common';
import { UsageFlowRouteScanner } from '@usageflow/nestjs';

@Injectable()
export class MyService {
  constructor(private readonly routeScanner: UsageFlowRouteScanner) {}

  async someMethod() {
    // Match a route pattern
    const pattern = this.routeScanner.match('GET', '/api/users/123');
    // Returns: '/api/users/:id' (if matched)
  }
}

Features

  • NestJS Module Architecture: Native NestJS module with dependency injection
  • Automatic Route Scanning: Automatically scans and matches NestJS routes
  • Request Metadata Collection: Collects comprehensive request metadata including headers, query params, path params, and body
  • Response Tracking: Tracks response status codes and duration
  • WebSocket Communication: Uses WebSocket for real-time communication with UsageFlow API
  • Connection Pooling: Maintains a pool of WebSocket connections for better performance
  • Header Sanitization: Automatically sanitizes sensitive headers (authorization, API keys)
  • Error Handling: Gracefully handles errors and provides meaningful error messages
  • Express and Fastify Support: Works with both Express and Fastify adapters

Request Metadata

The middleware automatically collects the following metadata for each request:

  • HTTP method
  • Route pattern (detected from NestJS routes)
  • Raw URL
  • Client IP (with X-Forwarded-For support)
  • User agent
  • Timestamp
  • Headers (sanitized)
  • Query parameters
  • Path parameters
  • Request body
  • Response status code
  • Request duration

Route Pattern Detection

The module uses multiple methods to detect route patterns:

  1. Route Scanner: Scans the Express/Fastify router stack to build a route map
  2. Request Route Path: Uses req.route.path when available
  3. Router Stack Traversal: Searches through the router stack for matching routes
  4. Parameter Reconstruction: Reconstructs route patterns from path parameters

Error Handling

If an allocation request fails, the middleware will:

  1. Return a 400 status code
  2. Include an error message in the response
  3. Set blocked: true in the response body
// Error response format
{
  message: "Error message",
  blocked: true
}

Ledger ID Generation

The middleware automatically generates ledger IDs based on:

  1. HTTP method and route pattern
  2. Configured identity fields from UsageFlow policies
  3. Identity field locations (path params, query params, body, bearer token, headers)

Requirements

  • Node.js >= 18.0.0
  • NestJS >= 10.0.0
  • TypeScript >= 5.0.0

Dependencies

  • @usageflow/core: Core UsageFlow functionality
  • @nestjs/common: NestJS common utilities
  • @nestjs/core: NestJS core functionality

Development

# Install dependencies
npm install

# Build the package
npm run build

# Run tests
npm test

License

MIT

Support

For issues, questions, or contributions, please visit our GitHub repository.