H
Hono3mo ago
Jing

Hono JS best practice for DRY

What are the best practices in Hono for writing APIs, specifically when one API (API B) partially replicates the functionality of another API (API A)? For example, if API A performs a specific task, and API B performs a different task but includes a part that is identical to the task in API A, what is the recommended approach to handle this overlap efficiently and maintainably?
4 Replies
Joaquim Ley
Joaquim Ley3mo ago
Well either create a shared lib that does the common logic or API B calls API A (but this introduces another set of issues, networking, availability, etc)
Aditya Mathur
Aditya Mathur3mo ago
There are multiple approaches to solving this issue, the one I use the most is splitting logic in middlewares and wrapper functions. You can check this out - https://hono.dev/docs/helpers/factory#factory-helper for creating middllewares. There is a page in docs which tells about the best practices but it is not related to this. Here it is - https://hono.dev/docs/guides/best-practices
Best Practices - Hono
Ultrafast web framework for Cloudflare Workers, Fastly Compute, Deno, Bun, Vercel, Node.js, and others. Fast, but not only fast.
Factory Helper - Hono
Ultrafast web framework for Cloudflare Workers, Fastly Compute, Deno, Bun, Vercel, Node.js, and others. Fast, but not only fast.
Aditya Mathur
Aditya Mathur3mo ago
But at the end of the day, it totally depends on your use case. Personally I would suggest, build the base version of the api then focus on optimising it else it can get tedious.
Jing
Jing2mo ago
Thank you, I appreciate both of your time answeing
Want results from more Discord servers?
Add your server