For simplicity, define your procedures and middleware inside shapeless.ts. You can split into separate files if you prefer.
Procedures Overview
Shapeless provides a publicProcedure by default that anyone (authenticated or not) can call. You can build on this to create protected procedures with middleware.
// resources/shapeless.tsimport { shapeless } from "@shapelesss/core"interface Env { Bindings: { DATABASE_URL: string }}export const app = shapeless.init<Env>()/** * Public (unauthenticated) procedures * This is the base for creating procedures. */export const publicProcedure = app.procedure
Example: Authenticated Procedure
Here’s how to create a procedure that only authenticated users can access:
// resources/shapeless.tsimport { HTTPException } from "hono/http-exception"import { shapeless } from "@shapelesss/core"interface Env { Bindings: { DATABASE_URL: string }}export const app = shapeless.init<Env>()const authMiddleware = app.middleware(async ({ c, next }) => { // Example authentication check (replace with real logic) const isAuthenticated = true if (!isAuthenticated) { throw new HTTPException(401, { message: "Unauthorized, please sign in.", }) } // Attach user data to context await next({ user: { id: "123", name: "John Doe" } })})export const publicProcedure = app.procedureexport const privateProcedure = publicProcedure.use(authMiddleware)