nextkit is a library for building e2e type-safe API routes for Next.js. I built it after frustration that I could not get strongly typed API routes in the client side. Inspired by trpc, I decided to create something similar.
nextkit works by defining a type-safe router where each http method has a single return type. We can then take the type of said router and import that into the frontend. This works fine and we never import any serverside code because we do the following
import type ProductsAPI from '../pages/api/product';
type Res = InferAPIResponseType<typeof ProductsAPI, 'GET'>;
So this works because we use a nice feature of TypeScript called “type only” imports.
