Openstatus
www.openstatus.dev
1import type { Variables } from "@/types";
2import {
3 type Workspace,
4 workspacePlanHierarchy,
5} from "@openstatus/db/src/schema";
6import type { Context, Next } from "hono";
7import { OpenStatusApiError } from "../errors";
8
9/**
10 * Checks if the workspace has a minimum required plan to access the endpoint
11 */
12export function minPlanMiddleware({ plan }: { plan: Workspace["plan"] }) {
13 return async (c: Context<{ Variables: Variables }, "/*">, next: Next) => {
14 const workspace = c.get("workspace");
15
16 if (workspacePlanHierarchy[workspace.plan] < workspacePlanHierarchy[plan]) {
17 throw new OpenStatusApiError({
18 code: "PAYMENT_REQUIRED",
19 message: "You need to upgrade your plan to access this feature",
20 });
21 }
22
23 await next();
24 };
25}