Skip to content

Next.js Server & Client

Next.js App Router allows you to evaluate business rules across both server and client boundaries:

  1. Server Actions & Route Handlers: Guarantee secure backend authorization and business logic execution.
  2. React Server Components (RSC): Conditionally render UI features before sending HTML to the client.
  3. Client Components: Provide real-time instant feedback before form submission.

1. Route Handlers (app/api/**/route.ts)

Protect API endpoints from unauthorized actions or invalid business payloads:

app/api/payouts/route.ts
import { NextResponse, type NextRequest } from "next/server";
import { rule, checkRule } from "@qarar/sdk";
import { getSession } from "@/lib/auth";
// Define payout eligibility policy
const payoutEligibilityRule = rule("Merchant Payout Gate")
.id("merchant-payout-gate")
.where("merchant.status").eq("active")
.and("merchant.kycVerified").eq(true)
.and("payout.amount").lte(50000)
.and("payout.currency").in(["USD", "EUR", "GBP"])
.build();
export async function POST(req: NextRequest) {
const session = await getSession();
if (!session?.merchant) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const payload = await req.json();
const context = {
merchant: session.merchant,
payout: payload,
};
const decision = checkRule(payoutEligibilityRule, context);
if (!decision.result) {
return NextResponse.json(
{
error: "Forbidden",
message: "Payout request does not meet eligibility criteria",
reasons: decision.reasons,
},
{ status: 403 }
);
}
// Proceed with payout processing
return NextResponse.json({ status: "Payout Initiated", amount: payload.amount });
}

2. Server Actions Protection

Validate business rules directly inside Server Actions before running database mutations:

app/actions/upgradePlan.ts
"use server";
import { rule, checkRule } from "@qarar/sdk";
import { getCurrentUser } from "@/lib/session";
import { db } from "@/lib/db";
const upgradeEligibilityRule = rule("Self-Service Upgrade Policy")
.where("account.hasOverdueInvoices").eq(false)
.and("account.seats").gte(1)
.build();
export async function upgradeSubscription(targetPlan: string) {
const user = await getCurrentUser();
const account = await db.account.findUnique({ where: { id: user.accountId } });
const check = checkRule(upgradeEligibilityRule, { account });
if (!check.result) {
return {
success: false,
reasons: check.reasons,
};
}
await db.account.update({
where: { id: account.id },
data: { plan: targetPlan },
});
return { success: true };
}

3. Server Components Feature Flags

Conditionally render UI components without shipping private rule criteria to the browser:

app/dashboard/page.tsx
import { rule, checkRule } from "@qarar/sdk";
import { getCurrentUser } from "@/lib/session";
import { AdvancedAnalyticsWidget } from "@/components/AnalyticsWidget";
import { UpgradePrompt } from "@/components/UpgradePrompt";
const analyticsEntitlementRule = rule("Advanced Analytics Entitlement")
.where("user.role").eq("admin")
.or("account.plan").eq("enterprise")
.build();
export default async function DashboardPage() {
const user = await getCurrentUser();
const context = { user, account: user.account };
const canViewAnalytics = checkRule(analyticsEntitlementRule, context).result;
return (
<main className="p-8">
<h1>Executive Dashboard</h1>
{canViewAnalytics ? <AdvancedAnalyticsWidget /> : <UpgradePrompt />}
</main>
);
}