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

@nest-yalc-2/field-middleware

v2.2.18

Published

<div align="center"> <h1>@nest-yalc-2/field-middleware</h1> <p><em>Core enterprise module for the @nest-yalc-2/field-middleware integration within the Ferrox/YALC ecosystem.</em></p>

Readme

npm version License

🚀 Installation

npm install @nest-yalc-2/field-middleware
# or
yarn add @nest-yalc-2/field-middleware
# or
pnpm add @nest-yalc-2/field-middleware

GraphQL Field Middleware & Dynamic Property Transformers

The @nestjs-yalc/field-middleware package provides fine-grained property-level access control, dynamic field encryption, value maskers, and runtime formatting transformers for GraphQL schemas and REST DTO entities.


1. What It Is & Architectural Purpose

In GraphQL APIs and REST services, security and privacy requirements frequently demand hiding or transforming specific entity fields based on user permissions or compliance mandates (e.g., masking credit card numbers, masking email addresses for non-admins, decrypting PII data on read, or applying localized currency formatting).

@nestjs-yalc/field-middleware injects field middleware hooks into the NestJS execution context. It allows developers to attach declarative decorators (@FieldMiddleware(...), @MaskField(), @EncryptField()) directly to GraphQL ObjectType fields or class DTO properties without polluting business domain services with inline security checks.

┌────────────────────────────────────────────────────────────────────────┐
│                        GraphQL Query / DTO                             │
├────────────────────────────────────────────────────────────────────────┤
│  1. Incoming Request Context (User Role: 'GUEST')                      │
│  2. Resolving Field: 'user.email'                                      │
└──────────────────────────────────┬─────────────────────────────────────┘
                                   │
                                   ▼
┌────────────────────────────────────────────────────────────────────────┐
│                      YalcFieldMiddleware Engine                        │
├────────────────────────────────────────────────────────────────────────┤
│  Evaluate Field Decorators (@MaskField({ type: 'EMAIL' }))             │
│  User is GUEST -> Transform '[email protected]'                     │
│               -> Result: 'j***[email protected]'                           │
└──────────────────────────────────┬─────────────────────────────────────┘
                                   │
                                   ▼
┌────────────────────────────────────────────────────────────────────────┐
│                   Masked GraphQL / JSON Output                         │
└────────────────────────────────────────────────────────────────────────┘

2. What It Does & Key Capabilities

  • Declarative Field Protection: Attach middleware rules to fields using NestJS decorators.
  • Role-Based Property Masking: Dynamically mask Sensitive Data (PII, SSN, Credit Cards) depending on the requestor's JWT roles/permissions.
  • Property-Level Encryption: Transparently decrypt AES-256 encrypted database columns during GraphQL object resolution.
  • Computed Value Formatting: Apply localized date, currency, or string case transformations dynamically on read operations.

3. How It Works Under the Hood

Execution Flow Sequence

sequenceDiagram
    autonumber
    participant Client as GraphQL Client
    participant Resolver as Field Resolver
    participant Middleware as FieldMiddleware Pipeline
    participant Security as Auth Context Guard
    participant Output as Transformed Value

    Client->>Resolver: Query User { id, email, ssn }
    Resolver->>Middleware: Resolve 'ssn' Field Value
    Middleware->>Security: Inspect Request Context (User Roles)
    alt User Has 'ADMIN' Role
        Security-->>Middleware: Authorized
        Middleware-->>Output: Return Unmasked SSN ("123-45-6789")
    else User Has 'USER' / 'GUEST' Role
        Security-->>Middleware: Unauthorized for Raw Value
        Middleware->>Middleware: Apply SSN Mask ("***-**-6789")
        Middleware-->>Output: Return Masked Value
    end
    Output-->>Client: Deliver GraphQL JSON Payload

4. Why It Was Designed This Way

| Feature | Standard Manual Field Checks | @nestjs-yalc/field-middleware | | :--- | :--- | :--- | | Separation of Concerns | Controllers/Services littered with if (role === 'GUEST') mask() logic. | Zero business service clutter; declarative @Field() decorators. | | GraphQL Parity | Requires writing custom GraphQL field directives by hand. | Native NestJS field middleware integration with standard context. | | Consistency | Risk of forgetting field masks in newly created API routes. | Centralized middleware pipeline guarantees compliance rules. |


5. Practical Usage Guide & Extended Code Examples

5.1 Masking Sensitive PII Fields in GraphQL

import { Field, ObjectType } from '@nestjs/graphql';
import { MaskField, FieldMiddleware } from '@nestjs-yalc/field-middleware';

@ObjectType()
export class UserProfileType {
  @Field()
  id: string;

  @Field()
  @MaskField({ type: 'EMAIL', allowedRoles: ['ADMIN', 'SUPERUSER'] })
  email: string;

  @Field()
  @MaskField({ type: 'SSN', allowedRoles: ['COMPLIANCE_OFFICER'] })
  ssn: string;
}

5.2 Creating Custom Field Middleware Transformers

import { FieldMiddleware, MiddlewareContext, NextFn } from '@nestjs-yalc/field-middleware';

export const CurrencyFormatterMiddleware: FieldMiddleware = async (
  ctx: MiddlewareContext,
  next: NextFn,
) => {
  const value = await next();
  if (typeof value !== 'number') return value;

  const userLocale = ctx.context.req?.headers['accept-language'] || 'en-US';
  return new Intl.NumberFormat(userLocale, { style: 'currency', currency: 'USD' }).format(value);
};

6. Anti-Patterns: How NOT to Use It

[!CAUTION] Anti-Pattern 1: Performing Heavy Async I/O in Field Middleware Field middleware runs for every resolved property instance in a GraphQL query array. Executing database queries inside field middleware causes N+1 performance bottlenecks. Use DataLoaders for async field enrichment.


7. Pro-Tips & Best Practices

[!TIP] Pro-Tip 1: Combining with NestJS Guards Use Field Middleware for property-level transformation while keeping NestJS Guards responsible for overall endpoint routing access.


🔗 Cross-References

To see how this module integrates with the rest of the Ferrox architecture, refer to the following documentation:


📚 Ecosystem Documentation

This module is a core component of the Ferrox enterprise microservice architecture.

👉 Read the Full Documentation on Ferrox-Rust.dev