API
Guide to Suvidha’s APIs
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.
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
.