Suvidha

Validation • Context • Control

Type-Safe Middleware

Chain middleware like LEGO blocks, with type inferred context propagation.

Data Validation

Zod-powered type safety for body, params, and queries—without cluttering your routes.

Framework Independence

Keep your business logic clean. No Express.js baggage.

Explicit Control

Fine-grained control over request/response lifecycle.

is a lightweight, type-safe Express.js library that adds powerful validation, middleware context management, and response handling.

  • No Rewrites - Adopt incrementally in existing Express apps.
  • TypeScript Native - Inferred types end “guess what’s in req” games.

Installation

The library is available on the npm registry.

npm install suvidha

Quickstart

import express from "express";
import { Suvidha, DefaultHandlers, Formatter } from "suvidha";
import { UserSchema } from "./dto";
import { authenticate, roleCheck } from "./middlewares";
import { createUserHandler } from "./controller";

const app = express();
app.use(express.json());

// Configure Suvidha with default handlers
const suvidha = () => Suvidha.create(new DefaultHandlers());

// Protected user creation endpoint
app.post(
    "/users",
    suvidha()
        .use(authenticate)
        .use(roleCheck) // check permissions
        .body(UserSchema) // Validate request body
        .handler(async (req) => {
            // All validation/security passed
            const newUser = req.body; // type of newUser: z.infer<typeof UserSchema>
            const { role } = req.context.user; // type of role: string
            return createUserHandler(newUser, role); // Execute business logic
        }),
);

app.listen(3000);

sample response

{
    "status": "success",
    "data": {
        "id": "67adc39ea1ff4e9d60273236"
    },
    "meta": {}
}