mirror of
https://github.com/lucide-icons/lucide.git
synced 2026-12-03 16:59:42 +00:00
feat(docs|site): Add MCP waitlist page (#5249)
* First iteration * Adjust waitlist * Adjust page * Add background * minor adjustments * Adjust hero styling * Add api backend * Adjust backend * Extract base URL * fix final things * Final touch * Format code * update lockfile
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
import { defineEventHandler, getQuery, sendRedirect } from 'h3';
|
||||
import { waitlistProducts } from '../../theme/components/waitlist/products';
|
||||
import {
|
||||
verifyConfirmToken,
|
||||
type VerifyResult,
|
||||
type WaitlistSignup,
|
||||
} from '../../lib/waitlist/token.ts';
|
||||
import { addToWaitlist } from '../../lib/waitlist/resend.ts';
|
||||
|
||||
const CONFIRMED_PAGE = '/waitlist/confirmed';
|
||||
|
||||
function confirmedPageUrl(
|
||||
product: string | undefined,
|
||||
status?: 'invalid' | 'error',
|
||||
confirmed?: { signup: WaitlistSignup; contactId: string },
|
||||
) {
|
||||
const params = new URLSearchParams();
|
||||
|
||||
if (product && Object.hasOwn(waitlistProducts, product)) {
|
||||
params.set('product', product);
|
||||
}
|
||||
|
||||
if (status) {
|
||||
params.set('status', status);
|
||||
}
|
||||
|
||||
// Hidden fields for the survey. The Resend contact ID links a submission to the contact
|
||||
// without putting the email address in the URL.
|
||||
if (confirmed) {
|
||||
params.set('contactId', confirmed.contactId);
|
||||
if (confirmed.signup.useCase) params.set('use-case', confirmed.signup.useCase);
|
||||
}
|
||||
|
||||
const query = params.toString();
|
||||
return query ? `${CONFIRMED_PAGE}?${query}` : CONFIRMED_PAGE;
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/waitlist/confirm?token=…
|
||||
*
|
||||
* The link from the confirmation email. Verifies the signed token, creates the Resend contact
|
||||
* in the Waitlist segment, then redirects to the confirmed page. The page shows the thank-you
|
||||
* survey when only `product` is set, and the expired state when `status` is set.
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const { token } = getQuery(event);
|
||||
const result: VerifyResult =
|
||||
typeof token === 'string' ? verifyConfirmToken(token) : { valid: false };
|
||||
|
||||
// `in` narrows the union even without `strictNullChecks`, unlike checking `result.valid`.
|
||||
if (!('signup' in result)) {
|
||||
return sendRedirect(event, confirmedPageUrl(result.product, 'invalid'), 302);
|
||||
}
|
||||
|
||||
const { signup } = result;
|
||||
|
||||
if (!Object.hasOwn(waitlistProducts, signup.product)) {
|
||||
return sendRedirect(event, confirmedPageUrl(undefined, 'invalid'), 302);
|
||||
}
|
||||
|
||||
let contactId: string;
|
||||
try {
|
||||
contactId = await addToWaitlist(signup);
|
||||
} catch (error) {
|
||||
console.error('[waitlist] Could not add the contact to the waitlist', error);
|
||||
return sendRedirect(event, confirmedPageUrl(signup.product, 'error'), 302);
|
||||
}
|
||||
|
||||
return sendRedirect(
|
||||
event,
|
||||
confirmedPageUrl(signup.product, undefined, { signup, contactId }),
|
||||
302,
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,77 @@
|
||||
import { createError, defineEventHandler, getRequestURL, readBody } from 'h3';
|
||||
import { checkBotId } from 'botid/server';
|
||||
import { waitlistProducts } from '../../theme/components/waitlist/products';
|
||||
import { createConfirmToken } from '../../lib/waitlist/token.ts';
|
||||
import { sendConfirmationEmail } from '../../lib/waitlist/resend.ts';
|
||||
|
||||
const MAX_EMAIL_LENGTH = 254;
|
||||
const MAX_USE_CASE_LENGTH = 200;
|
||||
const MAX_SOURCE_LENGTH = 100;
|
||||
// Deliberately loose: the confirmation email is the real check.
|
||||
const EMAIL_PATTERN = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
|
||||
function asTrimmedString(value: unknown, maxLength: number) {
|
||||
return typeof value === 'string' ? value.trim().slice(0, maxLength) : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/waitlist
|
||||
*
|
||||
* Runs the spam checks and sends a confirmation email with a signed link. Nothing is stored yet:
|
||||
* the contact is only created in Resend once the link is clicked (see `confirm.get.ts`).
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody<Record<string, unknown> | undefined>(event).catch(() => undefined);
|
||||
|
||||
if (!body || typeof body !== 'object') {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Invalid request body' });
|
||||
}
|
||||
|
||||
// Honeypot: people never see this field. Answer as if it worked, so bots learn nothing.
|
||||
if (typeof body.website === 'string' && body.website !== '') {
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
const verification = await checkBotId({
|
||||
advancedOptions: { headers: event.node.req.headers },
|
||||
});
|
||||
|
||||
if (verification.isBot) {
|
||||
throw createError({ statusCode: 403, statusMessage: 'Access denied' });
|
||||
}
|
||||
|
||||
const email = asTrimmedString(body.email, MAX_EMAIL_LENGTH + 1).toLowerCase();
|
||||
const product = typeof body.product === 'string' ? body.product : '';
|
||||
|
||||
if (email.length > MAX_EMAIL_LENGTH || !EMAIL_PATTERN.test(email)) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Invalid email address' });
|
||||
}
|
||||
|
||||
if (!Object.hasOwn(waitlistProducts, product)) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Unknown product' });
|
||||
}
|
||||
|
||||
const token = createConfirmToken({
|
||||
email,
|
||||
product,
|
||||
useCase: asTrimmedString(body.use_case, MAX_USE_CASE_LENGTH),
|
||||
source: asTrimmedString(body.source, MAX_SOURCE_LENGTH),
|
||||
signedUpAt: new Date().toISOString(),
|
||||
});
|
||||
|
||||
const confirmUrl = new URL('/api/waitlist/confirm', getRequestURL(event).origin);
|
||||
confirmUrl.searchParams.set('token', token);
|
||||
|
||||
try {
|
||||
await sendConfirmationEmail({
|
||||
email,
|
||||
productName: waitlistProducts[product].name,
|
||||
confirmUrl: confirmUrl.href,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('[waitlist] Could not send the confirmation email', error);
|
||||
throw createError({ statusCode: 502, statusMessage: 'Could not send the confirmation email' });
|
||||
}
|
||||
|
||||
return { ok: true };
|
||||
});
|
||||
Reference in New Issue
Block a user