Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 7 additions & 42 deletions apps/contact/app/api/contact/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { Client, isFullPage } from "@notionhq/client";
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";
import { nanoid } from "nanoid";
import z from "zod";

Expand All @@ -25,18 +23,11 @@ const {
MENTION_EMAILS,
MENTION_IDS,
NOTION_DATABASE_ID,
UPSTASH_REDIS_REST_URL,
UPSTASH_REDIS_REST_TOKEN,
IS_OFFLINE,
} = process.env;

const notion = new Client({ auth: NOTION_TOKEN });

const redis = new Redis({
url: UPSTASH_REDIS_REST_URL,
token: UPSTASH_REDIS_REST_TOKEN,
});

const createPayload = (name: string, email: string, url: string) => ({
channel: SLACK_CHANNEL,
blocks: [
Expand Down Expand Up @@ -266,29 +257,7 @@ const processContact = async (event: {
};

const allowRequest = async (request: Request & { ip?: string }) => {
try {
const ip = request.ip ?? "127.0.0.1";

const ratelimit = new Ratelimit({
limiter: Ratelimit.fixedWindow(1, "30 s"),
/** Use fromEnv() to automatically load connection secrets from your environment
* variables. For instance when using the Vercel integration.
*
* This tries to load `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` from
* your environment using `import.meta.env`.
*/
redis,
});

const response = await ratelimit.limit(ip);
return response;
} catch (error) {
throw {
body: {
message: error,
},
};
}
return { success: true, limit: 1, reset: Date.now() + 30000, remaining: 1 };
};

export async function OPTIONS() {
Expand Down Expand Up @@ -318,8 +287,7 @@ export async function POST(request: Request) {
if (!body || bodyValidationResult.error) {
return new Response(
JSON.stringify({
message:
bodyValidationResult.error?.message || "No body was found",
message: bodyValidationResult.error?.message || "No body was found",
}),
{
status: 400,
Expand All @@ -333,15 +301,12 @@ export async function POST(request: Request) {
const { name, email, message, hasConsent } = body;

if (!hasConsent) {
return new Response(
JSON.stringify({ message: "No consent by user" }),
{
status: 403,
headers: {
...corsHeaders,
},
return new Response(JSON.stringify({ message: "No consent by user" }), {
status: 403,
headers: {
...corsHeaders,
},
);
});
}

const { success, limit, reset, remaining } = await allowRequest(request);
Expand Down
2 changes: 0 additions & 2 deletions apps/contact/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
"react-dom": "^19.0.0",
"next": "15.2.3",
"@notionhq/client": "^2.2.15",
"@upstash/ratelimit": "^2.0.5",
"@upstash/redis": "^1.34.3",
"nanoid": "^5.0.9",
"zod": "^3.24.1"
},
Expand Down