Skip to content

Commit

Permalink
remove: convertkit
Browse files Browse the repository at this point in the history
  • Loading branch information
CaliCastle committed Jun 1, 2023
1 parent 9740a1e commit c4ae2ff
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 42 deletions.
46 changes: 14 additions & 32 deletions app/api/newsletter/route.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,17 @@
import { Ratelimit } from '@upstash/ratelimit'
import { type NextRequest, NextResponse } from 'next/server'
import { Resend } from 'resend'
import { z } from 'zod'

import { emailConfig } from '~/config/email'
import ConfirmSubscriptionEmail from '~/emails/confirm-subscription'
import { env } from '~/env.mjs'
import { redis } from '~/lib/redis'

const API_KEY = env.CONVERTKIT_API_KEY
const BASE_URL = 'https://api.convertkit.com/v3'

const newsletterFormSchema = z.object({
email: z.string().email().nonempty(),
formId: z.string().nonempty(),
})

function subscribeToForm({ formId, email }: { formId: string; email: string }) {
const url = [BASE_URL, 'forms', formId, 'subscribe'].join('/')

const headers = new Headers({
'Content-Type': 'application/json; charset=utf-8',
})

return fetch(url, {
method: 'POST',
headers,
body: JSON.stringify({
api_key: API_KEY,
email,
tags: [
// cali.so newsletter tag
3817600,
// Chinese newsletter tag
3817754,
],
}),
})
}

export const runtime = 'edge'

const ratelimit = new Ratelimit({
Expand All @@ -53,12 +29,18 @@ export async function POST(req: NextRequest) {
try {
const { data } = await req.json()
const parsed = newsletterFormSchema.parse(data)
const res = await subscribeToForm(parsed)
if (res.ok) {
return NextResponse.json({ status: 'success' })
}

return NextResponse.error()
// generate a random one-time token
const token = Math.random().toString(36).slice(2)

await new Resend(env.RESEND_API_KEY).sendEmail({
from: emailConfig.from,
to: parsed.email,
subject: '确认订阅 Cali 的动态吗?',
react: ConfirmSubscriptionEmail({ token }),
})

return NextResponse.json({ status: 'success' })
} catch (error) {
console.error('[Newsletter]', error)

Expand Down
7 changes: 7 additions & 0 deletions config/email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const emailConfig = {
from: '[email protected]',
baseUrl:
process.env.VERCEL_ENV === 'production'
? `https://cali.so`
: 'http://localhost:3000',
}
15 changes: 5 additions & 10 deletions emails/confirm-subscription.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react'

import { emailConfig } from '../config/email'
import {
Body,
Button,
Expand All @@ -16,15 +17,9 @@ import {
Text,
} from './_components'

const baseUrl =
process.env.VERCEL_ENV === 'production'
? `https://cali.so`
: 'http://localhost:3000'
const confirmLink = new URL('/confirm', baseUrl)
const confirmLink = new URL('/confirm', emailConfig.baseUrl)

const ConfirmSubscriptionEmail: React.FC<{
token?: string
}> = ({ token = 'fake-token' }) => {
const ConfirmSubscriptionEmail = ({ token = 'fake-token' }) => {
const previewText = `确认订阅 Cali 的动态吗?`
confirmLink.searchParams.set('token', token)
const link = confirmLink.toString()
Expand All @@ -38,7 +33,7 @@ const ConfirmSubscriptionEmail: React.FC<{
<Container className="mx-auto my-[40px] w-[465px] rounded-2xl border border-solid border-zinc-100 bg-white p-[20px]">
<Section className="mt-[24px]">
<Img
src={`${baseUrl}/subscription-email-header.jpg`}
src={`${emailConfig.baseUrl}/subscription-email-header.jpg`}
width="234"
height="121"
alt="Cali"
Expand Down Expand Up @@ -80,7 +75,7 @@ const ConfirmSubscriptionEmail: React.FC<{
<Hr className="mx-0 my-[20px] h-px w-full bg-zinc-100" />
<Section>
<Img
src={`${baseUrl}/icon.png`}
src={`${emailConfig.baseUrl}/icon.png`}
width="24"
height="24"
alt="Cali"
Expand Down

0 comments on commit c4ae2ff

Please sign in to comment.