Skip to content

Commit

Permalink
Merge pull request #113 from timlrx/v2
Browse files Browse the repository at this point in the history
Add support for newsletter route handler
  • Loading branch information
timlrx authored Jul 16, 2023
2 parents 19cb0ea + fd22f66 commit c27df31
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"neat-tables-occur",
"pretty-worms-help",
"proud-brooms-laugh",
"unlucky-keys-join"
"unlucky-keys-join",
"weak-swans-heal"
]
}
5 changes: 5 additions & 0 deletions .changeset/weak-swans-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'pliny': patch
---

Add support for newsletter route handler
6 changes: 6 additions & 0 deletions packages/pliny/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# pliny

## 0.1.0-beta.5

### Patch Changes

- 3e31a82: Add support for newsletter route handler

## 0.1.0-beta.4

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/pliny/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "pliny",
"description": "Main entry point for pliny components",
"homepage": "https://github.com/timlrx/pliny",
"version": "0.1.0-beta.4",
"version": "0.1.0-beta.5",
"type": "module",
"exports": {
"./*": "./*",
Expand Down
5 changes: 3 additions & 2 deletions packages/pliny/src/newsletter/buttondown.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { NextApiRequest } from 'next'
import { NextRequest } from 'next/server'

export const buttondownHandler = async (req: NextApiRequest, res: NextApiResponse) => {
export const buttondownHandler = async (req: NextApiRequest | NextRequest) => {
const { email } = req.body
const API_KEY = process.env.BUTTONDOWN_API_KEY
const API_URL = 'https://api.buttondown.email/v1/'
Expand Down
5 changes: 3 additions & 2 deletions packages/pliny/src/newsletter/convertkit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { NextApiRequest } from 'next'
import { NextRequest } from 'next/server'

export const convertkitHandler = async (req: NextApiRequest, res: NextApiResponse) => {
export const convertkitHandler = async (req: NextApiRequest | NextRequest) => {
const { email } = req.body
const FORM_ID = process.env.CONVERTKIT_FORM_ID
const API_KEY = process.env.CONVERTKIT_API_KEY
Expand Down
5 changes: 3 additions & 2 deletions packages/pliny/src/newsletter/emailOctopus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { NextApiRequest } from 'next'
import { NextRequest } from 'next/server'

export const emailOctopusHandler = async (req: NextApiRequest, res: NextApiResponse) => {
export const emailOctopusHandler = async (req: NextApiRequest | NextRequest) => {
const { email } = req.body
const API_KEY = process.env.EMAILOCTOPUS_API_KEY
const LIST_ID = process.env.EMAILOCTOPUS_LIST_ID
Expand Down
83 changes: 72 additions & 11 deletions packages/pliny/src/newsletter/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { buttondownHandler } from './buttondown'
import { convertkitHandler } from './convertkit'
import { mailchimpHandler } from './mailchimp'
Expand All @@ -16,7 +18,7 @@ export interface NewsletterRequest extends NextApiRequest {

export type NewsletterResponse<T = any> = NextApiResponse<T>

async function NewsletterHandler(
async function NewsletterAPIHandler(
req: NextApiRequest,
res: NextApiResponse,
options: NewsletterConfig
Expand All @@ -29,22 +31,22 @@ async function NewsletterHandler(
let response: Response
switch (options.provider) {
case 'buttondown':
response = await buttondownHandler(req, res)
response = await buttondownHandler(req)
break
case 'convertkit':
response = await convertkitHandler(req, res)
response = await convertkitHandler(req)
break
case 'mailchimp':
response = await mailchimpHandler(req, res)
response = await mailchimpHandler(req)
break
case 'klaviyo':
response = await klaviyoHandler(req, res)
response = await klaviyoHandler(req)
break
case 'revue':
response = await revueHandler(req, res)
response = await revueHandler(req)
break
case 'emailoctopus':
response = await emailOctopusHandler(req, res)
response = await emailOctopusHandler(req)
break
default:
res.status(500).json({ error: `${options.provider} not supported` })
Expand All @@ -58,20 +60,79 @@ async function NewsletterHandler(
}
}

async function NewsletterRouteHandler(req: NextRequest, options: NewsletterConfig) {
const res = await req.json()
if (!res.email) {
return NextResponse.json({ error: 'Email is required' }, { status: 400 })
}
try {
let response: Response
switch (options.provider) {
case 'buttondown':
response = await buttondownHandler(req)
break
case 'convertkit':
response = await convertkitHandler(req)
break
case 'mailchimp':
response = await mailchimpHandler(req)
break
case 'klaviyo':
response = await klaviyoHandler(req)
break
case 'revue':
response = await revueHandler(req)
break
case 'emailoctopus':
response = await emailOctopusHandler(req)
break
default:
return NextResponse.json({ error: `${options.provider} not supported` }, { status: 500 })
}
if (response.status == 200) {
return NextResponse.json(
{ message: 'Successfully subscribed to the newsletter' },
{ status: response.status }
)
} else if (response.status >= 400) {
return NextResponse.json(
{ error: `There was an error subscribing to the list` },
{ status: response.status }
)
}
return NextResponse.json({ error: '' }, { status: 201 })
} catch (error) {
return NextResponse.json({ error: error.message || error.toString() }, { status: 500 })
}
}

export function NewsletterAPI(options: NewsletterConfig): any
export function NewsletterAPI(req: NextRequest, options: NewsletterConfig): any
export function NewsletterAPI(
req: NextApiRequest,
res: NextApiResponse,
options: NewsletterConfig
): any

export function NewsletterAPI(
...args: [NewsletterConfig] | [NextApiRequest, NextApiResponse, NewsletterConfig]
...args:
| [NewsletterConfig]
| [NextRequest, NewsletterConfig]
| [NextApiRequest, NextApiResponse, NewsletterConfig]
) {
if (args.length === 1) {
return async (req: NewsletterRequest, res: NewsletterResponse) =>
await NewsletterHandler(req, res, args[0])
return async (req: NewsletterRequest | NextRequest, res: NewsletterResponse) => {
// For route handlers, 2nd argument contains the 'params' property instead of a response object
if ('params' in res) {
return await NewsletterRouteHandler(req as NextRequest, args[0])
}
return await NewsletterAPIHandler(req as NewsletterRequest, res, args[0])
}
}

if (args.length === 2) {
return NewsletterRouteHandler(...args)
}

return NewsletterHandler(args[0], args[1], args[2])
return NewsletterAPIHandler(...args)
}
5 changes: 3 additions & 2 deletions packages/pliny/src/newsletter/klaviyo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { NextApiRequest } from 'next'
import { NextRequest } from 'next/server'

export const klaviyoHandler = async (req: NextApiRequest, res: NextApiResponse) => {
export const klaviyoHandler = async (req: NextApiRequest | NextRequest) => {
const { email } = req.body
const API_KEY = process.env.KLAVIYO_API_KEY
const LIST_ID = process.env.KLAVIYO_LIST_ID
Expand Down
5 changes: 3 additions & 2 deletions packages/pliny/src/newsletter/mailchimp.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { NextApiRequest } from 'next'
import { NextRequest } from 'next/server'
import mailchimp from '@mailchimp/mailchimp_marketing'

mailchimp.setConfig({
apiKey: process.env.MAILCHIMP_API_KEY,
server: process.env.MAILCHIMP_API_SERVER, // E.g. us1
})

export const mailchimpHandler = async (req: NextApiRequest, res: NextApiResponse) => {
export const mailchimpHandler = async (req: NextApiRequest | NextRequest) => {
const { email } = req.body

const response = await mailchimp.lists.addListMember(process.env.MAILCHIMP_AUDIENCE_ID, {
Expand Down
5 changes: 3 additions & 2 deletions packages/pliny/src/newsletter/revue.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { NextApiRequest } from 'next'
import { NextRequest } from 'next/server'

export const revueHandler = async (req: NextApiRequest, res: NextApiResponse) => {
export const revueHandler = async (req: NextApiRequest | NextRequest) => {
const { email } = req.body

const API_KEY = process.env.REVUE_API_KEY
Expand Down

0 comments on commit c27df31

Please sign in to comment.