Wrote a Blog Post
As much as I like Next.js, reusing code for your API routes is not straightforward, and the framework leaves you a ton of freedom.

In this article, I go into detail about writing Next.js middleware pipes for making it easy to reuse as much code as possible.

https://giancarlobuomprisco.com/next/middleware-pipes-nextjs