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

najm-guard

v1.1.13

Published

Authorization guards plugin for Najm — class-based route protection with RBAC/PBAC support.

Readme

najm-guard

Authorization guards plugin for Najm — class-based route protection with RBAC/PBAC support.

Install

bun add najm-guard

How It Works

createGuard takes a guard class constructor and returns a decorator factory. The guard class must have a canActivate method. Guards run before route handlers; return true to allow, throw to deny. Return a plain object to populate guard context:

// Return { user: ... } to populate @User() in downstream handlers
return { user: verifiedUser };

Usage

Basic Guard

import { Server } from 'najm-core';
import { Controller, Get, User } from 'najm-core';
import { createGuard } from 'najm-guard';
import { Headers } from 'najm-core';
import { Ctx } from 'najm-core';

// 1. Define the guard class
class AuthGuard {
  async canActivate(@Headers('authorization') auth: string, @Ctx() ctx: any) {
    if (!auth) throw new Error('Unauthorized');
    const user = await this.verifyToken(auth);
    return { user };
  }

  private async verifyToken(token: string) {
    return { id: '1', email: '[email protected]' };
  }
}

// 2. Create the decorator factory
export const IsAuth = createGuard(AuthGuard);

// 3. Apply to a controller
@Controller('/api')
class ApiController {
  @Get('/profile')
  @IsAuth()
  profile(@User() user: any) {
    return { user };
  }
}

await new Server()
  .load(ApiController)
  .listen(3000);

Adding a Method-Level Guard

Class-level and method-level guards compose — both run, with class-level guards executing first. This lets you add extra checks to specific methods:

import { createGuard } from 'najm-guard';

class AdminGuard {
  async canActivate(@Headers('authorization') auth: string) {
    const isAdmin = await this.checkAdmin(auth);
    if (!isAdmin) throw new Error('Forbidden');
    return true;
  }
}

export const IsAdmin = createGuard(AdminGuard);

@Controller('/api')
@IsAuth()                           // runs first for all methods
class ApiController {
  @Get('/profile')
  profile(@User() user: any) {     // only @IsAuth() runs
    return { user };
  }

  @Post('/admin-only')
  @IsAdmin()                        // @IsAuth() runs first, then @IsAdmin()
  adminRoute() {
    return { secret: true };
  }
}

Composing Multiple Guards

Use composeGuards to combine multiple already-created decorator factories:

import { createGuard, composeGuards } from 'najm-guard';

export const IsAuth = createGuard(AuthGuard);
export const IsAdmin = createGuard(AdminGuard);
export const IsModerator = createGuard(ModGuard);

// AND logic: all must pass
export const IsAdminOrMod = composeGuards(IsAuth(), IsAdmin());
export const FullAccess = composeGuards(IsAuth(), IsAdmin(), IsModerator());

Guard Context Tokens

| Token | Purpose | Available via | |-------|---------|---------------| | USER | Authenticated user object | @User() | | OWNER | Resource owner | @Owner() | | INFO | Extra guard metadata | @Info() | | DATA | Arbitrary guard-passed data | @Data() | | FILTER | Query filter from guard | @Filter() | | ROLE | Role from RBAC guard | @Role() | | PERMISSIONS | Permissions array | @Permissions() |

Guard Class Method Parameters

Guard canActivate methods use Najm parameter decorators for injection:

class MyGuard {
  async canActivate(
    @Headers('authorization') auth: string,
    @Ctx() ctx: any,
    @Params('id') id: string,
  ) {
    // ...
    return true;
  }
}

Production Notes

  • Guards run before route handlers. Return true to allow, throw to deny.
  • Return a plain object { user, role, ... } from canActivate to populate context tokens
  • Class-level and method-level guards compose — both run in sequence, not mutually exclusively
  • composeGuards applies guards in order; all must pass for access to be granted
  • For PBAC, combine with najm-auth's Can, canRead, canCreate, etc.
  • Guards that need to share data with downstream handlers should return the data via the result object, not via direct ALS manipulation