Skip to main content

Core API Methods

Suvidha uses CtxRequest which extends the standard Express.js Request object with a context property, for accessing request-specific data added by middlewares.
Main class for creating a new instance of Suvidha; requires a Handlers instance.
Refer to Handlers for more details.

Purpose

Validates and types the request body using Zod schemas.

Parameters

  • schema: Zod schema definition.
  • Returns: Chainable Suvidha instance.

Example

Behavior

  • Validates request body before handler execution.
  • Overrides default any type with schema inference.

Purpose

Validates and types URL path parameters.

Example

Access in Handler

Purpose

Validates and types query string parameters.

Example

Default Values

Purpose

Builds request context through chainable middleware. Each call merges new properties into the context while preserving type safety. Any exceptions thrown will be handled by onErr handler.
  • Order-Sensitive: Middleware executes in the order of declaration.
  • Type Accumulation: Each middleware’s return type merges with previous context.
  • Immutable Context: Each middleware gets current context in Readonly wrapper to prevent mutations.
  • Early Termination: Middleware can end response with res.send().
Merging is the same as using the ... spread operator, new_ctx = { ...current_ctx, ...ctx }

Middleware Signature

Example

Type Safety

Purpose

Defines the final request handler with full type safety.

Signature

Parameters

  • req is express’ Request object with added context.
  • res is express’ Request object.
  • next is express’ NextFunction to call next middleware.
Acts as an Express.js middleware. It executes the configured Suvidha middleware functions (both validation via .body(), .query(), .params() and custom logic via .use()) in the order they are defined. After successful execution of these middlewares, .next() calls the next middleware in the Express.js chain using the express’ next() function.It’s like handler() but with request handler as express’ next() function.

Example

The execution order of middlewares is the same as the order of declaration.
Execution order is query -> middyA -> middyB -> params -> handler.

Usage

Data Validation

Validate the request body using Zod schemas.

Writing Middlewares

middlewares take CtxRequest and Response as input arguments and return Context | Promise<Context>.
By specifying the constraint in type signature, we can build type-safe context through ordered middlewares - TypeScript enforces dependency sequence:
Reorder middlewares → Type error ← Context dependencies broken.

Loosely Coupled Business Logic

Handles responses using onComplete and onErr when using Suvidha with Handlers. This separates your core business logic from framework-specific response methods, keeping your code cleaner by delegating response handling to the handlers.

Suvidha as data validation middleware

To use Suvidha solely as data validation middleware with TypeScript inference, implement onSchemaErr method.

DRY Suvidha

If you have common middlewares across multiple routes, you can create a Suvidha instance and reuse it.

Adopting Suvidha in Existing Projects

Suvidha’s CtxRequest extends the standard Request object by adding a context property. Existing handlers that accept Request will also work with CtxRequest. The context can be accessed using CtxRequest.