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.
I have tried to keep it simple.
asyncHandler()
to avoidtry-catch
boilerplate.- extend Express’ Request interface globally to add
user
.
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:
_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 theHandlers
and use
middlewares are called including request handler.
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.