Skip to content

Commit

Permalink
fix: examples
Browse files Browse the repository at this point in the history
  • Loading branch information
e-moran committed Jan 2, 2025
1 parent 2451659 commit 8b78916
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
9 changes: 5 additions & 4 deletions examples/nextjs-bot-categories/app/api/arcjet/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ export async function GET(req: Request) {
}

const headers = new Headers();
if (decision.reason.isBot()) {
const botReason = decision.results.map((rule) => rule.reason).find((reason) => reason.isBot());
if (typeof botReason !== "undefined") {
// WARNING: This is illustrative! Don't share this metadata with users;
// otherwise they may use it to subvert bot detection!
headers.set("X-Arcjet-Bot-Allowed", decision.reason.allowed.join(", "))
headers.set("X-Arcjet-Bot-Denied", decision.reason.denied.join(", "))
headers.set("X-Arcjet-Bot-Allowed", botReason.allowed.join(", "))
headers.set("X-Arcjet-Bot-Denied", botReason.denied.join(", "))

// We need to check that the bot is who they say they are.
if (decision.reason.isSpoofed()) {
if (botReason.isSpoofed()) {
return NextResponse.json(
{ error: "You are pretending to be a good bot!" },
{ status: 403, headers },
Expand Down
16 changes: 10 additions & 6 deletions examples/nextjs-clerk-rate-limit/app/api/arcjet/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,17 @@ export async function GET(req: NextRequest) {
);
}
}

let reset: Date | undefined;
let remaining: number | undefined;

if (decision.reason.isRateLimit()) {
reset = decision.reason.resetTime;
remaining = decision.reason.remaining;
}
const rateLimitReason = decision.results.map((rule) => rule.reason).find((reason) => reason.isRateLimit());
if (typeof rateLimitReason !== "undefined") {
if (rateLimitReason.isRateLimit()) {
reset = rateLimitReason.resetTime;
remaining = rateLimitReason.remaining;
}

return NextResponse.json({ message: "Hello World", reset, remaining });}
return NextResponse.json({ message: "Hello World", reset, remaining });
}
}
9 changes: 5 additions & 4 deletions examples/nextjs-openai/app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const runtime = "edge";
export async function POST(req: Request) {
const { messages } = await req.json();

// Estimate the number of tokens required to process the request
// Estimate the number of tokens required to process the request
const estimate = promptTokensEstimate({
messages,
});
Expand All @@ -59,8 +59,9 @@ export async function POST(req: Request) {
const decision = await aj.protect(req, { requested: estimate });
console.log("Arcjet decision", decision.conclusion);

if (decision.reason.isRateLimit()) {
console.log("Requests remaining", decision.reason.remaining);
const rateLimitReason = decision.results.map((rule) => rule.reason).find((reason) => reason.isRateLimit());
if (typeof rateLimitReason !== "undefined") {
console.log("Requests remaining", rateLimitReason.remaining);
}

// If the request is denied, return a 429
Expand All @@ -83,4 +84,4 @@ export async function POST(req: Request) {
});

return result.toDataStreamResponse();
}
}

0 comments on commit 8b78916

Please sign in to comment.