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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@thesaasdevkit/multitenancy-fastify

v1.0.18

Published

Fastify middleware adapter for multi-tenancy

Readme

@lanemc/multitenancy-fastify

Fastify middleware adapter for the Multi-Tenant SaaS Toolkit.

Installation

npm install @lanemc/multitenancy-fastify @lanemc/multitenancy-core

Usage

import fastify from 'fastify';
import { fastifyMultitenancy, requireRole, requirePermission } from '@lanemc/multitenancy-fastify';

const app = fastify();

// Register the multitenancy plugin
await app.register(fastifyMultitenancy, {
  resolution: {
    type: 'subdomain', // or 'header', 'token', 'custom'
    // headerName: 'x-tenant-id', // for header-based resolution
    // tokenClaim: 'tenant_id', // for token-based resolution
    // customResolver: async (request) => { ... } // for custom resolution
  },
  dataStore: myDataStore, // Your implementation of TenantDataStore
  onError: (error, request, reply) => {
    // Custom error handling
    reply.status(500).send({ error: error.message });
  },
  allowNoTenant: false // Whether to allow requests without tenant context
});

// Use in routes
app.get('/api/profile', {
  preHandler: requireTenantAuth
}, async (request, reply) => {
  // Access tenant context
  const tenant = request.tenant;
  const user = request.tenantUser;
  const roles = request.tenantRoles;
  
  return { tenant, user, roles };
});

// Require specific roles
app.post('/api/admin/users', {
  preHandler: [requireTenantAuth, requireRole('admin', 'super-admin')]
}, async (request, reply) => {
  // Only accessible by admin or super-admin
});

// Require specific permissions
app.delete('/api/documents/:id', {
  preHandler: [requireTenantAuth, requirePermission('documents.delete')]
}, async (request, reply) => {
  // Only accessible with documents.delete permission
});

Tenant Resolution Strategies

Subdomain-based

// Resolves tenant from subdomain (e.g., acme.app.com -> tenant: acme)
resolution: { type: 'subdomain' }

Header-based

// Resolves tenant from request header
resolution: { 
  type: 'header',
  headerName: 'x-tenant-id' // default
}

Token-based

// Resolves tenant from JWT token claim
resolution: { 
  type: 'token',
  tokenClaim: 'tenant_id' // default
}

Custom

// Custom resolution logic
resolution: { 
  type: 'custom',
  customResolver: async (request) => {
    // Your custom logic
    return tenantId;
  }
}

Context Access

Within route handlers and hooks running after the multitenancy plugin, you can access the tenant context:

import { tenantContext } from '@lanemc/multitenancy-core';

app.get('/api/data', async (request, reply) => {
  // Direct access from request
  const tenant = request.tenant;
  
  // Or use the context manager
  const currentTenant = tenantContext.getCurrentTenant();
  const currentUser = tenantContext.getCurrentUser();
  const hasRole = tenantContext.hasRole('admin');
  const hasPermission = tenantContext.hasPermission('data.read');
  
  // Your logic here
});

Error Handling

The plugin provides default error responses, but you can customize them:

await app.register(fastifyMultitenancy, {
  // ... other options
  onError: (error, request, reply) => {
    // Log the error
    request.log.error(error);
    
    // Custom error response
    if (error.message.includes('Tenant not found')) {
      reply.status(404).send({
        statusCode: 404,
        error: 'Not Found',
        message: 'Organization not found'
      });
    } else {
      reply.status(500).send({
        statusCode: 500,
        error: 'Internal Server Error',
        message: 'An error occurred'
      });
    }
  }
});

TypeScript Support

The plugin extends Fastify's type definitions to include tenant properties:

declare module 'fastify' {
  interface FastifyRequest {
    tenant?: Tenant;
    tenantUser?: User;
    tenantRoles?: string[];
    tenantPermissions?: string[];
  }
}

This means you get full TypeScript support when accessing tenant data from the request object.