SendGrid plugin (#77)

Closes #73 and #77
This commit is contained in:
Oliver Windall Juhl
2020-07-03 18:00:54 +02:00
committed by GitHub
parent 081c5278cb
commit d6fc477636
22 changed files with 6037 additions and 23 deletions
@@ -0,0 +1,10 @@
import { Router } from "express"
import routes from "./routes"
export default (container) => {
const app = Router()
routes(app)
return app
}
@@ -0,0 +1 @@
export default (fn) => (...args) => fn(...args).catch(args[2])
@@ -0,0 +1,5 @@
import { default as wrap } from "./await-middleware"
export default {
wrap,
}
@@ -0,0 +1,16 @@
import { Router } from "express"
import bodyParser from "body-parser"
import middlewares from "../middleware"
const route = Router()
export default (app) => {
app.use("/sendgrid", route)
route.post(
"/send",
bodyParser.raw({ type: "application/json" }),
middlewares.wrap(require("./send-email").default)
)
return app
}
@@ -0,0 +1,28 @@
import { Validator, MedusaError } from "medusa-core-utils"
export default async (req, res) => {
const schema = Validator.object().keys({
template_id: Validator.string().required(),
from: Validator.string().required(),
to: Validator.string().required(),
data: Validator.object().optional().default({}),
})
const { value, error } = schema.validate(req.body)
if (error) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
}
try {
const sendgridService = req.scope.resolve("sendgridService")
await sendgridService.sendEmail(
value.template_id,
value.from,
value.to,
value.data
)
res.sendStatus(200)
} catch (err) {
throw err
}
}