Skip to content
Closed
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
99 changes: 26 additions & 73 deletions apps/contact/app/api/contact/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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 +24,12 @@ 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 @@ -137,16 +130,25 @@ const mentionPerson = ({ id, email }: { id: string; email: string }) => [
];

const getMentions = () => {
if (MENTION_EMAILS && MENTION_IDS) {
const emails = MENTION_EMAILS.split(",");
const ids = MENTION_IDS.split(",");

if (emails.length && ids.length) {
return ids.map((id, i) => ({
id,
email: emails[i],
}));
try {
if (
MENTION_EMAILS &&
MENTION_IDS &&
typeof MENTION_EMAILS === "string" &&
typeof MENTION_IDS === "string"
) {
const emails = MENTION_EMAILS.split(",").filter(Boolean);
const ids = MENTION_IDS.split(",").filter(Boolean);

if (emails.length && ids.length) {
return ids.map((id, i) => ({
id: id.trim(),
email: emails[i]?.trim() || "",
}));
}
}
} catch (error) {
console.error("Error in getMentions:", error);
}
return [];
};
Expand Down Expand Up @@ -265,32 +267,6 @@ 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,
},
};
}
};

export async function OPTIONS() {
return new Response(null, {
status: 200,
Expand Down Expand Up @@ -318,8 +294,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,31 +308,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,
},
},
);
}

const { success, limit, reset, remaining } = await allowRequest(request);

if (!success) {
return new Response(
JSON.stringify({
message: "Too many requests. Please try again in a minute",
}),
{
status: 429,
headers: {
...corsHeaders,
},
return new Response(JSON.stringify({ message: "No consent by user" }), {
status: 403,
headers: {
...corsHeaders,
},
);
});
}

await processContact({
Expand All @@ -375,9 +331,6 @@ export async function POST(request: Request) {
status: 200,
headers: {
...corsHeaders,
"X-RateLimit-Limit": limit.toString(),
"X-RateLimit-Remaining": remaining.toString(),
"X-RateLimit-Reset": reset.toString(),
},
},
);
Expand Down
2 changes: 1 addition & 1 deletion apps/website/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig } from 'astro/config';
import tailwind from '@astrojs/tailwind';
import react from '@astrojs/react';
import vercel from '@astrojs/vercel/serverless'
import vercel from '@astrojs/vercel'
import createRemarkPlugin from '@crocoder-dev/remark-plugin';
import image from '@astrojs/image';

Expand Down
1 change: 0 additions & 1 deletion apps/website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"module": "index.ts",
"type": "module",
"dependencies": {
"@astrojs/image": "^0.18.0",
"@astrojs/react": "^4.1.2",
"@astrojs/rss": "^4.0.11",
"@astrojs/tailwind": "^6.0.2",
Expand Down
Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion packages/tailwind/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"dependencies": {
"@tailwindcss/forms": "0.5.9",
"@tailwindcss/typography": "0.5.15",
"tailwind-bootstrap-grid": "6.0.0"
"tailwind-bootstrap-grid": "6.0.0",
"@tailwindcss/postcss": "^4.1.11"
}
}
9 changes: 5 additions & 4 deletions packages/tailwind/postcss.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
plugins: [
require('@tailwindcss/postcss'),
require('autoprefixer'),
],
};