Why Suvidha?
Building type-safe Express APIs without boilerplate
Suvidha is designed for type safety and an improved developer experience with Express.js.
Let’s take a look at the typical flow of request and response handling.
Authentication
Validates user identity by checking the provided JWT token, API key, or session cookie before proceeding.
Authorization
Verifies if the authenticated user has the required permissions to access the requested resource.
Validation
Ensures all request data is properly formatted and meets the required validation rules.
Resource
Executes the main business logic to handle the request and prepare the appropriate response.
Below is an example implementation of it in express.js.
I have tried to keep it simple.
Popular solutions:
asyncHandler()
to avoidtry-catch
boilerplate.- extend Express’ Request interface globally to add
user
.
It’s patchwork to add user
property to Request
interface globally.
Types are not inferred by typescript, middlewares and request handlers act independently,
and on a leap of faith that everyone is doing their job right.
Suvidha is a wrapper around your request handler, that addresses all these type-safety issues.
Re-writing the above example using Suvidha:
If you noticed, we never used _res: Response
anywhere to send response,
because that’s handled by Handlers
for us. If you want to send a response manually,
you can still do res.send()
and it will work fine.
To understand how Suvidha
facilitates this, let’s look at the flow.
Flow
Lines are highlighted wherever the Handlers
and use
middlewares are called including request handler.
That’s pretty much what Suvidha
is. It just facilitates the flow by adding a layer of abstraction and type-safety.
Handlers
have the control over how you process the request and response.