Hapi + Typescript = epic
After i created my K8s cluster i decided that now i want to have the best server to use as boilerplate.So i setup myself some goals i want to achieve with it:
- It has to use type-script.
- It has to have eslint
- It has to have swagger ( best dev documentation and validation out there )
- It has to auto-generate that swagger ( preferably link it with interfaces ) - yes, its possible! (sort of)
TL:DR; Example please - https://github.com/howtoclient/hapi-typescript
For my candidates i went with
- ExpressJs, its old, its simple and i worked with it a lot + it has every plugin at this point.
- HapiJs Its a simple framework with a large array of plugins and a vast eco-system
- NestJs Its a new framework oriented and built for Typescript and ease of use
NestJs: After some research i came to a conclusion that NestJs wasn't for me. Mainly because its structure is awkward and it uses decorators that greatly reduce my ability to debug it easily. Plus i don't have much experience using it so i am bias.
Express: As awesome as this library is, and yes it has full TS support via plugins, they still don't support async functions and the only way i could make it produce swagger is to pass it YAML with swagger structure which is way too much work and will produce tons of errors when i develop and forget to update it. Not to mention passing `interfaces` to it.
HapiJS: Even though my research ( fancy word for googling for 3 hours ) started shoddy i ended up finding more and more features of this framework that fit what i wanted to achieve. And to be fair i couldn't be happier with the results!
So lets get to it:
It has to use type-script: That part was surprisingly easy, hapi-js has @types/hapi that you need to install and you are basically done. That's it...
It has to have eslint: Recently a magic thing happened, eslint started supporting typescript so that easy as well.
It has to have swagger: Hapi-swagger, it utilizes @hapi/joi ( i used typesafe-joi ) for not only excellent validations but also for creating the swagger configurations for Payload and for Response
It has to auto-generate swagger: The reason i used typesafe-joi is specifically because it has the ability Joi.Literal<typeof joiSchemaModel>. Yes that means i can convert Joi schema to interface! It also means that i can now assign that interface to my handler, and give the parent schema to the swagger and.... It will generate swagger and interface automatically!
You can find this project in GitHub, interface swagger and everything
תודה רבה לך על השיתוף🙂 אני מזמינה אותך לקבוצה שלי: הקבוצה מחברת בין ישראלים ואנשי העולם במגוון תחומים. https://chat.whatsapp.com/BubG8iFDe2bHHWkNYiboeU