@shiftapi/next
v0.0.42
Published
Next.js integration for fully-typed TypeScript clients from shiftapi Go servers
Readme
@shiftapi/next
Next.js integration that generates fully-typed TypeScript clients from ShiftAPI Go servers. Get end-to-end type safety between your Go API and your Next.js app with zero manual type definitions.
How it works
- Reads your
shiftapi.config.ts(powered byshiftapi) - Extracts the OpenAPI 3.1 spec from your Go server at build time
- Generates TypeScript types using
openapi-typescript - Writes a typed
@shiftapi/clientmodule with a pre-configured openapi-fetch client - In dev mode, watches
.gofiles and regenerates types on changes - Auto-configures Next.js rewrites to proxy API requests to your Go server
Installation
npm install -D shiftapi @shiftapi/next
# or
pnpm add -D shiftapi @shiftapi/nextPeer dependency: next (v14+).
Setup
shiftapi.config.ts
Create a shiftapi.config.ts in your project root:
import { defineConfig } from "shiftapi";
export default defineConfig({
server: "./cmd/server",
});next.config.ts
import type { NextConfig } from "next";
import { withShiftAPI } from "@shiftapi/next";
const nextConfig: NextConfig = {};
export default withShiftAPI(nextConfig);Config options
Options are set in shiftapi.config.ts (see shiftapi):
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| server | string | (required) | Path to the Go server entry point (e.g. "./cmd/server") |
| baseUrl | string | "/" | Fallback base URL for the API client. Can be overridden via NEXT_PUBLIC_SHIFTAPI_BASE_URL. |
| url | string | "http://localhost:8080" | Address the Go server listens on. Used to auto-configure the dev proxy. |
Usage
Import the typed client in your frontend code:
import { client } from "@shiftapi/client";
const { data, error } = await client.GET("/greet", {
params: { query: { name: "World" } },
});
// `data` and `error` are fully typed based on your Go handler signaturesThe createClient factory is also exported if you need a custom instance:
import { createClient } from "@shiftapi/client";
const api = createClient({ baseUrl: "https://api.example.com" });What it auto-configures
- Next.js rewrites -- API requests are proxied to your Go server during development via
beforeFilesrewrites. - tsconfig.json -- A path mapping for
@shiftapi/clientis added so TypeScript resolves the generated types. - Go file watcher -- When
.gofiles change, the plugin restarts the Go server and regenerates types. - SSR support -- The generated client handles both server-side and client-side rendering, using the correct base URL in each environment.
License
MIT
