reformat with prettier with semicolons and tabs
This commit is contained in:
@ -1,21 +1,21 @@
|
||||
import getConfig from "next/config"
|
||||
import axios from "axios"
|
||||
import getConfig from "next/config";
|
||||
import got from "got";
|
||||
|
||||
const { serverRuntimeConfig } = getConfig()
|
||||
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 { 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 base64ApiKey = Buffer.from(`any:${apiKey}`).toString("base64");
|
||||
const headers = {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Basic ${base64ApiKey}`,
|
||||
}
|
||||
};
|
||||
|
||||
return axios.post(url, data, { headers })
|
||||
return got.post(url, { json: data, headers });
|
||||
}
|
||||
|
@ -1,59 +1,59 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next"
|
||||
import zod from "zod"
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import zod from "zod";
|
||||
|
||||
import type { ApiError } from "../_types"
|
||||
import appLogger from "../../../integrations/logger"
|
||||
import { addSubscriber } from "./_mailchimp"
|
||||
import type { ApiError } from "../_types";
|
||||
import appLogger from "../../../integrations/logger";
|
||||
import { addSubscriber } from "./_mailchimp";
|
||||
|
||||
type Response = {} | ApiError
|
||||
type Response = {} | ApiError;
|
||||
|
||||
const logger = appLogger.child({ route: "/api/newsletter/subscribe" })
|
||||
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 statusCode = 405;
|
||||
const apiError: ApiError = {
|
||||
statusCode,
|
||||
errorMessage: `Method ${req.method} Not Allowed`,
|
||||
}
|
||||
logger.error(apiError)
|
||||
};
|
||||
logger.error(apiError);
|
||||
|
||||
res.setHeader("Allow", ["POST"])
|
||||
res.status(statusCode).send(apiError)
|
||||
return
|
||||
res.setHeader("Allow", ["POST"]);
|
||||
res.status(statusCode).send(apiError);
|
||||
return;
|
||||
}
|
||||
|
||||
let body
|
||||
let body;
|
||||
try {
|
||||
body = bodySchema.parse(req.body)
|
||||
body = bodySchema.parse(req.body);
|
||||
} catch (error) {
|
||||
const statusCode = 400
|
||||
const statusCode = 400;
|
||||
const apiError: ApiError = {
|
||||
statusCode,
|
||||
errorMessage: "Body is malformed",
|
||||
}
|
||||
logger.error(error)
|
||||
};
|
||||
logger.error(error);
|
||||
|
||||
res.status(statusCode).send(apiError)
|
||||
return
|
||||
res.status(statusCode).send(apiError);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await addSubscriber(body.email)
|
||||
await addSubscriber(body.email);
|
||||
} catch (error) {
|
||||
console.log("error", error.response?.data)
|
||||
console.log("error", error.response?.data);
|
||||
|
||||
if (error.response?.data.title !== "Member Exists") {
|
||||
return res.status(error.response?.status ?? 400).end()
|
||||
return res.status(error.response?.status ?? 400).end();
|
||||
}
|
||||
}
|
||||
|
||||
res.status(200).end()
|
||||
res.status(200).end();
|
||||
}
|
||||
|
Reference in New Issue
Block a user