💡 Using Next.js as a platform for a well-structured REST API #62806
Unanswered
finom
asked this question in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
As you know, Next.js is positioned as a React framework, but its back-end capabilities are limited by the design of the route.ts file. There are multiple solutions to this problem, such as tRPC or GraphQL, but I didn't find them satisfactory. My goal was to create a REST API with the structured approach introduced in NestJS, while using Next.js as the core framework. I don't consider these technologies bad; in fact, they are amazing tools used by millions of developers. It's purely a matter of preference. Perhaps I'm too conservative, but consider that the first website I built was based on the
<frameset>
HTML tag long-long time ago.Over the last year, I've been working on a solution that allows Next.js to be used as a full-stack framework. I attempted to merge Next.js and NestJS into a unified system using Next.js middleware, but as you know it's too limited and don't allow to do many of the things that come to your mind. After much hard work and continuous thought, I am proud to present the final iteration of my solution to this challenge: Vovk.ts - REST for Next. This is an add-on over the public Next.js App Router API that leverages the so-called Optional Catch-all Segment to initialize decorated controller classes inspired by NestJS.
This library includes a CLI that generates a client library in node_modules with same-named variable names, which can be imported from any platform supporting the standard
fetch
function. This encompasses client components, server components, React Native apps, and other JavaScript environments. To create a new controller, you need to follow three steps.After that, you simply add more decorated static methods to extend the controller with additional features. As you might expect, the endpoints can be called using the standard
fetch
method, like so:It's also worth mentioning that the compiled library can be easily bundled with Webpack and distributed through NPM, CDN, or any other suitable method, since it acts as a minimal function that calls
fetch
.The main difference from other libraries is that Vovk.ts is specifically designed for the Next.js App Router. It avoids introducing complex abstractions and does not modify the request object, acting merely as a wrapper over Next.js route handlers. If you're familiar with using them, you already know how to handle operations like retrieving the JSON body, Request Body FormData, search query, making a redirect, using cookies, accessing headers, etc.
In order to make the client recognise types properly, you should use
VovkRequest
that modifies return types forreq.json
andreq.nextUrl.searchParams.get
. This also illiminates a need to useas
to convert the return type.At the example above query
id
usesstring
type but it also may be typed with a flavoured type.Vovk.ts comes with a bunch of examples covering the most common use-cases, which you can find here. Besides basic examples, you may also find it interesting to see how to implement a text streaming endpoint and a real use-case of OpenAI chat. Instead of using workarounds provided by the AI SDK, you can delegate the async iterable using the
yield*
syntax:and on the front-end you can use use disposable async iterable to iterate over the delegated items:
In other words, you can use any third-party AI library on server-side that uses generators to transcribe them into a client-side code.
The last but not least thing is that the generated library is fully customizable. You can override its behavior by adding custom flags, auth, make it return custom data, or tightly integrate with your application state. This part is explained in the documentation.
Vovk.ts comes with other cool features:
But that's a different story. In this brief post, I wanted to highlight the core capabilities provided by the library.
Thank you for reading. If you have any questions, I would be happy to answer them.
Vovk.ts on Github
Beta Was this translation helpful? Give feedback.
All reactions