feat(medusa-plugin-mailchimp): Migrate to TS + add update endpoint (#4040)
* - Refactored the .js files to .ts files - Added typing - Used 'WrapHandler' util instead of specific middleware in plugin - Added additional PUT method to the /mailchimp/subscribe endpoint. - Updated documentation where relevant. * Added changeset * Updated yarn.lock * Used camelcase for method args in services/mailchimp.ts --------- Co-authored-by: Tom Rupke <tom@adapptive.nl> Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
co-authored by
Tom Rupke
Oliver Windall Juhl
parent
5c01b4e6b6
commit
ddc90db530
+1
-1
@@ -1,7 +1,7 @@
|
||||
import { Router } from "express"
|
||||
import routes from "./routes"
|
||||
|
||||
export default (rootDirectory) => {
|
||||
export default function (rootDirectory: string): Router | Router[] {
|
||||
const app = Router()
|
||||
|
||||
routes(app, rootDirectory)
|
||||
@@ -1 +0,0 @@
|
||||
export default (fn) => (...args) => fn(...args).catch(args[2])
|
||||
@@ -1,5 +0,0 @@
|
||||
import { default as wrap } from "./await-middleware"
|
||||
|
||||
export default {
|
||||
wrap,
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
import { Router } from "express"
|
||||
import bodyParser from "body-parser"
|
||||
import middlewares from "../middleware"
|
||||
import { getConfigFile, parseCorsOrigins } from "medusa-core-utils"
|
||||
import cors from "cors"
|
||||
|
||||
const route = Router()
|
||||
|
||||
export default (app, rootDirectory) => {
|
||||
const { configModule } = getConfigFile(rootDirectory, `medusa-config`)
|
||||
const config = (configModule && configModule.projectConfig) || {}
|
||||
|
||||
const storeCors = config.store_cors || ""
|
||||
|
||||
route.use(
|
||||
cors({
|
||||
origin: parseCorsOrigins(storeCors),
|
||||
credentials: true,
|
||||
})
|
||||
)
|
||||
|
||||
app.use("/mailchimp", route)
|
||||
|
||||
route.post(
|
||||
"/subscribe",
|
||||
bodyParser.json(),
|
||||
middlewares.wrap(require("./subscribe-newsletter").default)
|
||||
)
|
||||
return app
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { Router } from "express"
|
||||
import * as cors from "cors"
|
||||
import * as bodyParser from "body-parser"
|
||||
import configLoader from "@medusajs/medusa/dist/loaders/config"
|
||||
import { parseCorsOrigins } from "medusa-core-utils"
|
||||
import { wrapHandler } from "@medusajs/medusa"
|
||||
import {
|
||||
update as subscribeNewsletterUpdate,
|
||||
add as subscribeNewsletterAdd,
|
||||
} from "./subscribe-newsletter"
|
||||
|
||||
const router = Router()
|
||||
|
||||
export default (app: Router, rootDirectory) => {
|
||||
const config = configLoader(rootDirectory)
|
||||
|
||||
const corsOptions = {
|
||||
origin: parseCorsOrigins(config.projectConfig.store_cors || ""),
|
||||
credentials: true,
|
||||
}
|
||||
|
||||
app.use("/mailchimp", router)
|
||||
|
||||
router.use(cors(corsOptions))
|
||||
|
||||
router.post(
|
||||
"/subscribe",
|
||||
bodyParser.json(),
|
||||
wrapHandler(subscribeNewsletterAdd)
|
||||
)
|
||||
|
||||
router.put(
|
||||
"/subscribe",
|
||||
bodyParser.json(),
|
||||
wrapHandler(subscribeNewsletterUpdate)
|
||||
)
|
||||
|
||||
return app
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
export default async (req, res) => {
|
||||
const mailchimpService = req.scope.resolve("mailchimpService")
|
||||
await mailchimpService.subscribeNewsletter(
|
||||
req.body.email,
|
||||
req.body.data || {}
|
||||
)
|
||||
res.sendStatus(200)
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { Request, Response } from "express"
|
||||
|
||||
export async function add(req: Request, res: Response) {
|
||||
const mailchimpService = req.scope.resolve("mailchimpService")
|
||||
await mailchimpService.subscribeNewsletterAdd(
|
||||
req.body.email,
|
||||
req.body.data || {}
|
||||
)
|
||||
res.sendStatus(200)
|
||||
}
|
||||
|
||||
export async function update(req: Request, res: Response) {
|
||||
const mailchimpService = req.scope.resolve("mailchimpService")
|
||||
await mailchimpService.subscribeNewsletterUpdate(
|
||||
req.body.email,
|
||||
req.body.data || {}
|
||||
)
|
||||
res.sendStatus(200)
|
||||
}
|
||||
Reference in New Issue
Block a user