Updated members-api to export POJO

no-issue

Previously members-api exported a pre configured express router with the
paths and handlers defined. This did not allow for much control from the
parent application. This replaces this pattern by exposing middlewares,
which the parent application can mount where it sees fit.
This commit is contained in:
Fabien O'Carroll 2019-09-25 16:32:33 +07:00
parent d67ad13057
commit acf01e9065

View File

@ -94,9 +94,13 @@ module.exports = function MembersApi({
return encodeIdentityToken({sub: member.email}); return encodeIdentityToken({sub: member.email});
} }
const apiInstance = new Router(); const middleware = {
sendMagicLink: Router(),
createCheckoutSession: Router(),
handleStripeWebhook: Router()
};
apiInstance.post('/send-magic-link', body.json(), async function (req, res) { middleware.sendMagicLink.use(body.json(), async function (req, res) {
const email = req.body.email; const email = req.body.email;
if (!email) { if (!email) {
res.writeHead(400); res.writeHead(400);
@ -113,7 +117,7 @@ module.exports = function MembersApi({
} }
}); });
apiInstance.post('/create-stripe-checkout-session', ensureStripe, body.json(), async function (req, res) { middleware.createCheckoutSession.use(ensureStripe, body.json(), async function (req, res) {
const plan = req.body.plan; const plan = req.body.plan;
const identity = req.body.identity; const identity = req.body.identity;
@ -152,7 +156,7 @@ module.exports = function MembersApi({
res.end(JSON.stringify(sessionInfo)); res.end(JSON.stringify(sessionInfo));
}); });
apiInstance.post('/handle-stripe-webhook', ensureStripe, body.raw({type: 'application/json'}), async function (req, res) { middleware.handleStripeWebhook.use(ensureStripe, body.raw({type: 'application/json'}), async function (req, res) {
try { try {
const event = await stripe.parseWebhook(req.body, req.headers['stripe-signature']); const event = await stripe.parseWebhook(req.body, req.headers['stripe-signature']);
if (event.type !== 'checkout.session.completed') { if (event.type !== 'checkout.session.completed') {
@ -169,36 +173,41 @@ module.exports = function MembersApi({
res.writeHead(200); res.writeHead(200);
res.end(); res.end();
} catch (err) { } catch (err) {
common.logging.error(err);
res.writeHead(400); res.writeHead(400);
res.end(); res.end();
} }
}); });
apiInstance.getMemberDataFromMagicLinkToken = getMemberDataFromMagicLinkToken; const setLogger = common.logging.setLogger;
apiInstance.getMemberIdentityData = getMemberIdentityData;
apiInstance.getMemberIdentityToken = getMemberIdentityToken;
apiInstance.setLogger = common.logging.setLogger; const getPublicConfig = function () {
apiInstance.getPublicConfig = function () {
return Promise.resolve({ return Promise.resolve({
publicKey, publicKey,
issuer issuer
}); });
}; };
apiInstance.members = users; const bus = new (require('events').EventEmitter)();
apiInstance.bus = new (require('events').EventEmitter)();
if (stripe) { if (stripe) {
stripe.ready().then(() => { stripe.ready().then(() => {
apiInstance.bus.emit('ready'); bus.emit('ready');
}).catch((err) => { }).catch((err) => {
apiInstance.bus.emit('error', err); bus.emit('error', err);
}); });
} else { } else {
process.nextTick(() => apiInstance.bus.emit('ready')); process.nextTick(() => bus.emit('ready'));
} }
return apiInstance; return {
middleware,
getMemberDataFromMagicLinkToken,
getMemberIdentityToken,
getMemberIdentityData,
setLogger,
getPublicConfig,
bus,
members: users
};
}; };