api.onError((err, c) => { console.error(err) return c.text("Something went wrong", 500)})
Base Path
Set the base path where your API lives:
const api = app .router() .basePath("/api") // All API routes will be under /api/* .use(app.defaults.cors) .onError(app.defaults.errorHandler)
Make sure your API route folder corresponds with this path.
CORS Middleware
CORS is handled by default via:
.use(app.defaults.cors)
If you want to customize CORS, use Hono’s cors middleware and include the x-is-superjson header because Shapeless uses it internally for JSON serialization:
import { cors } from "hono/cors"const api = app .router() .use( cors({ allowHeaders: ["x-is-superjson"], exposeHeaders: ["x-is-superjson"], origin: "*", credentials: true, }), ) .onError(app.defaults.errorHandler)
Inferring Router Input and Output Types
Leverage Shapeless’s TypeScript helpers to infer procedure inputs and outputs for type-safe frontend calls:
import type { AppRouter } from "./index"import type { InferRouterInputs, InferRouterOutputs } from "@shapelesss/core"type Input = InferRouterInputs<AppRouter>type Output = InferRouterOutputs<AppRouter>// Usage example:type CreatePostInput = Input["post"]["create"]type CreatePostOutput = Output["post"]["create"]
This structure lets you organize your backend API cleanly, keep things type-safe end-to-end, and deploy anywhere with confidence.