Suvidha uses CtxRequest which extends the standard Express.js Request object with a context property,
for accessing request-specific data added by middlewares.
Copy
/** * Extends the standard Express.js `Request` object with a `context` property. * This allows you to store and access request-specific data throughout the * request lifecycle, particularly useful for middleware communication and * data sharing between middleware and request handler. * * @template C The type of the context object. Defaults to an empty object `{}`. * * @extends Express.js Request object */interface CtxRequest<C extends Context = {}, ...> extends Request<...> { context: C;}
Suvidha(handlers: Handlers)
Main class for creating a new instance of Suvidha; requires a Handlers instance.
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.
Key Characteristics
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 }
req is express’ Request object with added context.
res is express’ Request object.
next is express’ NextFunction to call next middleware.
.next()
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.
middlewares take CtxRequest and Response as input arguments and return Context | Promise<Context>.
Copy
// Middleware expects { user: User } in the context.// specify the constraint in type signature, that way// Suvidha will ensure that middleware is called// only when expected context data is available.type ExpectedContext = { user: User };const middleware = (req: CtxRequest<ExpectedContext>, res: Response) => { // business logic return { // return context that will be merged with the current context };};
TypeScript will throw an error because middleware expects { user: User } in context,
but the context is {} because the authenticate middleware has not been called yet.
typescript error [2345]
Copy
Property 'user' is missing in type '{}' but required in type 'ExpectedContext'.
By specifying the constraint in type signature, we can build type-safe context through ordered middlewares - TypeScript enforces dependency sequence:
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’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.