Skip to content

Commit

Permalink
chore: Remove deprecated use of json()
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmytton committed Dec 23, 2024
1 parent baad2a9 commit 4841bf1
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 34 deletions.
9 changes: 4 additions & 5 deletions app/routes/attack.test/route.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { LoaderFunctionArgs } from "@remix-run/node";
import { json } from "@remix-run/node";
import arcjet from "~/arcjet";

export async function loader(args: LoaderFunctionArgs) {
Expand All @@ -12,25 +11,25 @@ export async function loader(args: LoaderFunctionArgs) {
console.log("Arcjet decision: ", decision);

if (decision.isDenied() && decision.reason.isShield()) {
return json({ message: "Forbidden" }, { status: 403 });
return Response.json({ message: "Forbidden" }, { status: 403 });
} else if (decision.isErrored()) {
console.error("Arcjet error:", decision.reason);

if (decision.reason.message == "[unauthenticated] invalid key") {
return json(
return Response.json(
{
message:
"Invalid Arcjet key. Is the ARCJET_KEY environment variable set?",
},
{ status: 500 },
);
} else {
return json(
return Response.json(
{ message: "Internal server error: " + decision.reason.message },
{ status: 500 },
);
}
}

return json({ message: "Hello, world!" }, { status: 200 });
return Response.json({ message: "Hello, world!" }, { status: 200 });
}
15 changes: 7 additions & 8 deletions app/routes/bots.test/route.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { detectBot, fixedWindow } from "@arcjet/remix";
import type { LoaderFunctionArgs } from "@remix-run/node";
import { json } from "@remix-run/node";
import arcjet from "~/arcjet";

// Add rules to the base Arcjet instance outside of the handler function
Expand Down Expand Up @@ -30,36 +29,36 @@ export async function loader(args: LoaderFunctionArgs) {

// Use the IP analysis to customize the response based on the country
if (decision.ip.hasCountry() && decision.ip.country == "JP") {
return json({ message: "Konnichiwa!" }, { status: 200 });
return Response.json({ message: "Konnichiwa!" }, { status: 200 });
}

// Always deny requests from VPNs
if (decision.ip.isVpn()) {
return json({ message: "VPNs are forbidden" }, { status: 403 });
return Response.json({ message: "VPNs are forbidden" }, { status: 403 });
}

if (decision.isDenied() && decision.reason.isBot()) {
return json({ message: "Bots are forbidden" }, { status: 403 });
return Response.json({ message: "Bots are forbidden" }, { status: 403 });
} else if (decision.isDenied() && decision.reason.isRateLimit()) {
return json({ message: "Too many requests" }, { status: 429 });
return Response.json({ message: "Too many requests" }, { status: 429 });
} else if (decision.isErrored()) {
console.error("Arcjet error:", decision.reason);

if (decision.reason.message == "[unauthenticated] invalid key") {
return json(
return Response.json(
{
message:
"Invalid Arcjet key. Is the ARCJET_KEY environment variable set?",
},
{ status: 500 },
);
} else {
return json(
return Response.json(
{ message: "Internal server error: " + decision.reason.message },
{ status: 500 },
);
}
}

return json({ message: "Hello, world!" }, { status: 200 });
return Response.json({ message: "Hello, world!" }, { status: 200 });
}
18 changes: 10 additions & 8 deletions app/routes/rate-limit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type {
LoaderFunctionArgs,
MetaFunction,
} from "@remix-run/node";
import { json } from "@remix-run/node";
import {
Form,
Link,
Expand Down Expand Up @@ -104,7 +103,7 @@ export async function action(args: ActionFunctionArgs) {
const reset = decision.reason.resetTime;

if (reset === undefined) {
return json(
return Response.json(
{ message: "too many requests. Please try again later." },
{ status: 429, headers },
);
Expand All @@ -115,39 +114,42 @@ export async function action(args: ActionFunctionArgs) {
const minutes = Math.ceil(seconds / 60);

if (minutes > 1) {
return json(
return Response.json(
{
message: `too many requests. Please try again in ${minutes} minutes.`,
},
{ status: 429, headers },
);
} else {
return json(
return Response.json(
{
message: `too many requests. Please try again in ${seconds} seconds.`,
},
{ status: 429, headers },
);
}
} else {
return json({ message: "forbidden" }, { status: 403, headers });
return Response.json({ message: "forbidden" }, { status: 403, headers });
}
} else if (decision.isErrored()) {
console.error("Arcjet error:", decision.reason);
if (decision.reason.message == "[unauthenticated] invalid key") {
return json(
return Response.json(
{
message:
"invalid Arcjet key. Is the ARCJET_KEY environment variable set?",
},
{ status: 500, headers },
);
} else {
return json({ message: "internal server error" }, { status: 500 });
return Response.json(
{ message: "internal server error" },
{ status: 500 },
);
}
}

return json(
return Response.json(
{ message: `HTTP 200: OK. ${remaining} requests remaining. ${message}` },
{ status: 200, headers },
);
Expand Down
15 changes: 10 additions & 5 deletions app/routes/sensitive-info._index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { sensitiveInfo } from "@arcjet/remix";
import type { ActionFunctionArgs, MetaFunction } from "@remix-run/node";
import { json } from "@remix-run/node";
import {
Form,
Link,
Expand Down Expand Up @@ -53,10 +52,10 @@ export async function action(args: ActionFunctionArgs) {
const errors = {
supportMessage: "please do not include credit card numbers.",
};
return json({ errors, values }, { status: 400 });
return Response.json({ errors, values }, { status: 400 });
} else {
const errors = { supportMessage: "forbidden" };
return json({ errors, values }, { status: 403 });
return Response.json({ errors, values }, { status: 403 });
}
} else if (decision.isErrored()) {
console.error("Arcjet error:", decision.reason);
Expand All @@ -65,10 +64,16 @@ export async function action(args: ActionFunctionArgs) {
supportMessage:
"invalid Arcjet key. Is the ARCJET_KEY environment variable set?",
};
return json({ errors, values: { supportMessage } }, { status: 500 });
return Response.json(
{ errors, values: { supportMessage } },
{ status: 500 },
);
} else {
const errors = { supportMessage: "internal server error" };
return json({ errors, values: { supportMessage } }, { status: 500 });
return Response.json(
{ errors, values: { supportMessage } },
{ status: 500 },
);
}
}

Expand Down
15 changes: 7 additions & 8 deletions app/routes/signup._index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { protectSignup } from "@arcjet/remix";
import type { ActionFunctionArgs, MetaFunction } from "@remix-run/node";
import { json } from "@remix-run/node";
import {
Form,
Link,
Expand Down Expand Up @@ -93,13 +92,13 @@ export async function action(args: ActionFunctionArgs) {
}

const errors = { email: message };
return json({ errors, values }, { status: 400 });
return Response.json({ errors, values }, { status: 400 });
} else if (decision.reason.isRateLimit()) {
const reset = decision.reason.resetTime;

if (reset === undefined) {
const errors = { email: "too many requests. Please try again later." };
return json({ errors, values }, { status: 429 });
return Response.json({ errors, values }, { status: 429 });
}

// Calculate number of seconds between reset Date and now
Expand All @@ -110,16 +109,16 @@ export async function action(args: ActionFunctionArgs) {
const errors = {
email: `too many requests. Please try again in ${minutes} minutes.`,
};
return json({ errors, values }, { status: 429 });
return Response.json({ errors, values }, { status: 429 });
} else {
const errors = {
email: `too many requests. Please try again in ${seconds} seconds.`,
};
return json({ errors, values }, { status: 429 });
return Response.json({ errors, values }, { status: 429 });
}
} else {
const errors = { email: "forbidden" };
return json({ errors, values }, { status: 403 });
return Response.json({ errors, values }, { status: 403 });
}
} else if (decision.isErrored()) {
console.error("Arcjet error:", decision.reason);
Expand All @@ -128,10 +127,10 @@ export async function action(args: ActionFunctionArgs) {
email:
"invalid Arcjet key. Is the ARCJET_KEY environment variable set?",
};
return json({ errors, values: { email } }, { status: 500 });
return Response.json({ errors, values: { email } }, { status: 500 });
} else {
const errors = { email: "internal server error" };
return json({ errors, values: { email } }, { status: 500 });
return Response.json({ errors, values: { email } }, { status: 500 });
}
}

Expand Down

0 comments on commit 4841bf1

Please sign in to comment.