migrate to blitzjs
This commit is contained in:
21
app/api/newsletter/_mailchimp.ts
Normal file
21
app/api/newsletter/_mailchimp.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import getConfig from "next/config"
|
||||
import axios from "axios"
|
||||
|
||||
const { serverRuntimeConfig } = getConfig()
|
||||
|
||||
export async function addSubscriber(email: string) {
|
||||
const { apiKey, audienceId } = serverRuntimeConfig.mailChimp
|
||||
const region = apiKey.split("-")[1]
|
||||
const url = `https://${region}.api.mailchimp.com/3.0/lists/${audienceId}/members`
|
||||
const data = {
|
||||
email_address: email,
|
||||
status: "subscribed",
|
||||
}
|
||||
const base64ApiKey = Buffer.from(`any:${apiKey}`).toString("base64")
|
||||
const headers = {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Basic ${base64ApiKey}`,
|
||||
}
|
||||
|
||||
return axios.post(url, data, { headers })
|
||||
}
|
59
app/api/newsletter/subscribe.ts
Normal file
59
app/api/newsletter/subscribe.ts
Normal file
@ -0,0 +1,59 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next"
|
||||
import zod from "zod"
|
||||
|
||||
import type { ApiError } from "../_types"
|
||||
import appLogger from "../../../integrations/logger"
|
||||
import { addSubscriber } from "./_mailchimp"
|
||||
|
||||
type Response = {} | ApiError
|
||||
|
||||
const logger = appLogger.child({ route: "/api/newsletter/subscribe" })
|
||||
|
||||
const bodySchema = zod.object({
|
||||
email: zod.string().email(),
|
||||
})
|
||||
|
||||
export default async function subscribeToNewsletter(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse<Response>
|
||||
) {
|
||||
if (req.method !== "POST") {
|
||||
const statusCode = 405
|
||||
const apiError: ApiError = {
|
||||
statusCode,
|
||||
errorMessage: `Method ${req.method} Not Allowed`,
|
||||
}
|
||||
logger.error(apiError)
|
||||
|
||||
res.setHeader("Allow", ["POST"])
|
||||
res.status(statusCode).send(apiError)
|
||||
return
|
||||
}
|
||||
|
||||
let body
|
||||
try {
|
||||
body = bodySchema.parse(req.body)
|
||||
} catch (error) {
|
||||
const statusCode = 400
|
||||
const apiError: ApiError = {
|
||||
statusCode,
|
||||
errorMessage: "Body is malformed",
|
||||
}
|
||||
logger.error(error)
|
||||
|
||||
res.status(statusCode).send(apiError)
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
await addSubscriber(body.email)
|
||||
} catch (error) {
|
||||
console.log("error", error.response?.data)
|
||||
|
||||
if (error.response?.data.title !== "Member Exists") {
|
||||
return res.status(error.response?.status ?? 400).end()
|
||||
}
|
||||
}
|
||||
|
||||
res.status(200).end()
|
||||
}
|
Reference in New Issue
Block a user