@wptom/waitlist
v0.3.0
Published
A waitlist endpoint for any Astro site: validation, bot protection and storage in Supabase, with no dependencies.
Readme
@wptom/waitlist
A waitlist endpoint for Astro (or any runtime with the Fetch API): validation, bot protection, duplicate handling and storage in Supabase or a Mailchimp audience. Part of the WPTom integrations: the logic, and components that take your template's look.
// src/pages/api/waitlist.ts
import type { APIRoute } from 'astro';
import { createWaitlistHandler, memoryStore, supabaseStore } from '@wptom/waitlist';
export const prerender = false;
const handle = createWaitlistHandler({
store: process.env.SUPABASE_URL
? supabaseStore({ url: process.env.SUPABASE_URL, key: process.env.SUPABASE_SECRET_KEY! })
: memoryStore(), // development only
redirect: { success: '/waitlist/thanks' },
onJoin: async (entry) => {
// e.g. send a welcome email with @wptom/email
},
});
export const POST: APIRoute = ({ request }) => handle(request);<form action="/api/waitlist" method="post">
<input name="email" type="email" required />
<input name="source" type="hidden" value="home" />
<!-- bots fill this, people never see it -->
<input name="company_website" tabindex="-1" autocomplete="off" hidden />
<button>Join</button>
</form>- Plain form posts are redirected (
303) toredirect.success, or back with?error=…. fetchcalls withAccept: application/jsonget{ ok, alreadyJoined }or{ ok: false, error }.onJoinruns only for new signups; its errors are logged and never shown to the visitor.
Supabase table
Run once in the SQL editor:
create table public.waitlist (
email text primary key,
source text,
created_at timestamptz not null default now()
);
-- Row level security on and no policies: only your server's secret key can write.
alter table public.waitlist enable row level security;Use a secret API key from Project Settings → API Keys, only on the server.
Mailchimp
import { mailchimpStore } from '@wptom/waitlist';
const store = mailchimpStore({
apiKey: process.env.MAILCHIMP_API_KEY!, // Account → Extras → API keys; ends with the data center, e.g. -us21
audienceId: process.env.MAILCHIMP_AUDIENCE_ID!, // Audience → Settings → Audience name and defaults
doubleOptIn: false, // true: Mailchimp sends its confirmation email; the contact is "pending" until then
});Adds each signup to the audience as subscribed (or pending with double opt-in), tagged with
its source. An address already in the audience, whatever its status, is left as it is and
reported as added: false, so onJoin doesn't run again. A key without its data-center suffix
throws when the store is created.
Astro component
---
import SignupForm from '@wptom/waitlist/SignupForm.astro';
---
<SignupForm source="home" emailLabel="Email" placeholder="[email protected]" button="Join"
joining="Joining…" joined="You're on the list." alreadyJoined="You're already on the list."
invalidEmail="Check your email." error="Something went wrong." />Posts to /api/waitlist (action to change it) for a waitlist or a newsletter; tone="photo"
for a form over a photo.
Styled only with your site's CSS variables (--brand-bg, --brand-panel, --brand-text, --brand-muted, --brand-border, --brand-button, --brand-on-button, --brand-link, --brand-radius, --brand-button-radius, --font-heading, --font-mono), so it takes your look; every text is a prop, so editors can change it in the CMS.
No dependencies. MIT License.
