The original Next.js middleware handler function.
A wrapped middleware function that handles contract violations.
// pages/api/protected-route.ts
import { withContractMiddleware } from "../../src/integrations/nextjs";
import { contract } from "../../src/core/contract";
import { auth } from "../../src/conditions/auth";
class MyApiHandler {
@contract({
requires: [auth("admin")],
})
async handleRequest(req: NextApiRequest, res: NextApiResponse) {
res.status(200).json({ message: "Welcome, admin!" });
}
}
export default withContractMiddleware(new MyApiHandler().handleRequest);
Helper function for Next.js Middleware integration. This function acts as a wrapper to handle contract violations within Next.js middleware. It catches
ContractViolationError
and responds with appropriate HTTP status codes or redirects.