Skip to content

Commit 10bf39b

Browse files
author
Ben Dechrai
committed
Move withRule out of handler if possible
Clean up formatting to match blog post
1 parent 57c3925 commit 10bf39b

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

examples/nextjs-14-permit/src/app/api/permissions/route.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import { NextResponse } from "next/server";
22
import { detectBot, slidingWindow } from "@arcjet/next";
33
import { currentUser } from "@clerk/nextjs/server";
4-
import { aj } from "@/lib/arcjet";
4+
import { arcjet } from "@/lib/arcjet";
55
import { permit } from "@/lib/permit";
66

7+
const aj = arcjet
8+
// Add a sliding window to limit requests to 2 per second
9+
.withRule(slidingWindow({ mode: "LIVE", max: 2, interval: 1 }))
10+
// Add bot detection to block automated requests
11+
.withRule(detectBot({ mode: "LIVE", block: ["AUTOMATED"] }));
12+
713
export async function GET(req: Request) {
814
let user = await currentUser();
915
if (!user) {
1016
return NextResponse.json({ canUpdate: false });
1117
}
1218

13-
const decision = await aj
14-
// Add a sliding window rule to limit the number of requests to 2 per second
15-
.withRule(slidingWindow({ mode: "LIVE", max: 2, interval: 1 }))
16-
// Add bot detection to block automated requests
17-
.withRule(detectBot({ mode: "LIVE", block: ["AUTOMATED"] }))
18-
// Request a decision from Arcjet with the user's ID as a fingerprint
19-
.protect(req, { fingerprint: user.id });
19+
// Request a decision from Arcjet with the user's ID as a fingerprint
20+
const decision = await aj.protect(req, { fingerprint: user.id });
2021

2122
// If the decision is denied then return an error response
2223
if (decision.isDenied()) {

examples/nextjs-14-permit/src/app/api/stats/route.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NextRequest, NextResponse } from "next/server";
22
import { detectBot, slidingWindow } from "@arcjet/next";
33
import { currentUser } from "@clerk/nextjs/server";
4-
import { aj } from "@/lib/arcjet";
4+
import { arcjet } from "@/lib/arcjet";
55
import { permit } from "@/lib/permit";
66
import { getLastFriday } from "@/lib/dateHelper";
77
import { getOrderCount, getToppings } from "@/data/stats";
@@ -13,8 +13,8 @@ async function getClient() {
1313
const user = await currentUser();
1414
if (!user) {
1515
return (
16-
aj
17-
// Add a sliding window rule to limit the number of requests to 5 per minute
16+
arcjet
17+
// Add a sliding window to limit requests to 5 per minute
1818
.withRule(slidingWindow({ mode: "LIVE", max: 5, interval: 60 }))
1919
// Add bot detection to block automated requests
2020
.withRule(detectBot({ mode: "LIVE", block: ["AUTOMATED"] }))
@@ -25,18 +25,21 @@ async function getClient() {
2525
// then give them a medium rate limit.
2626
const canUpdate = await permit.check(user.id, "update", "stats");
2727
if (!canUpdate) {
28-
return aj.withRule(
29-
// Add a sliding window rule to limit the number of requests to 10 per minute
30-
slidingWindow({ mode: "LIVE", max: 10, interval: 60 })
28+
return (
29+
arcjet
30+
// Add a sliding window to limit requests to 10 per minute
31+
.withRule(slidingWindow({ mode: "LIVE", max: 10, interval: 60 }))
3132
);
3233
}
3334

34-
// User is logged in and has permission to update stats, so give them no rate limit
35-
return aj;
35+
// User is logged in and has permission to update stats,
36+
// so give them no rate limit
37+
return arcjet;
3638
}
3739

3840
export async function GET(req: NextRequest) {
39-
// Get the user's ID if they are logged in, otherwise use their IP address as a fingerprint
41+
// Get the user's ID if they are logged in, otherwise use
42+
// their IP address as a fingerprint
4043
const user = await currentUser();
4144
const fingerprint: string = user ? user.id : req.ip!;
4245

examples/nextjs-14-permit/src/lib/arcjet.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import arcjet, { shield } from "@arcjet/next";
1+
import _arcjet, { shield } from "@arcjet/next";
22

3-
export const aj = arcjet({
3+
export const arcjet = _arcjet({
44
// Get your site key from https://app.arcjet.com
55
// and set it as an environment variable rather than hard coding.
66
// See: https://nextjs.org/docs/app/building-your-application/configuring/environment-variables

0 commit comments

Comments
 (0)