[Enhancement]: Properly type routes and validate all structures #125
Labels
awaiting-approval
Topic has not been approved or denied
enhancement
An update to an existing part of the codebase
Checked Existing
What enhancement would you like to see?
Currently routes remain untyped and only loose validation is done, mostly on the parts which we actually use. We don't really make many sanity checks for other values.
Additionally we have functions like
getValueFromHeaders
andgetValueFromQueryString
as hacky work arounds to the Express type system without actually understanding how it works.Instead we should be actually typing the routes and validating them using something like Zod.
Any other details to share? (OPTIONAL)
As an example, take the NNAS request for getting your access token:
This can be typed and validated like so:
Of course things like
TypedRequest
andTypedHandler
would be defined somewhere more global. This gives us intellisense and strict typing on the request data inside the route handler and automatically rejects requests which don't match the Zod schemas.However this comes with 2 caveats which I'm not sure how best to work around right now:
whatever.ts
files wherewhatever
comes from/v1/api/WHATEVER
. Each file can have MANY routes, and most do have many. If the Zod schemas are defined in these files as well then the files will get very large and unwieldy to work in. So maybe in their own files like we do for types?1600
but the server is expected to send back specific errors in some cases. For example if the username is missing from the request body during apassword
login then the server should send back error0002
, which can't currently happen.TypedHandler
is supposed to be generic so we can't directly have it handle specific errors on its own. But if we had another "error handler" function then routes would look something like:which might not be nice to work with? Idk, need options.
I wonder if it would be worth making this
TypedHandler
system it's own library as well, so we can use it in our other servers?The text was updated successfully, but these errors were encountered: