diff --git a/lib/apis.js b/lib/apis.js index adab8a3..5950d35 100644 --- a/lib/apis.js +++ b/lib/apis.js @@ -169,6 +169,11 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { next(); }); + // TODO delete these caches when config changes + var _stripe; + var _stripe_test; + var _mandrill; + var _mailchimp; myApp.use('/', function preHandler(req, res, next) { return getSiteConfig(clientUrih).then(function (siteConfig) { Object.defineProperty(req, 'getSiteMailer', { @@ -235,8 +240,54 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { */ // TODO allow third-party clients stripe ids destination // https://stripe.com/docs/connect/payments-fees - req.Stripe = require('stripe')(siteConfig['stripe.com'].live.secret); - req.StripeTest = require('stripe')(siteConfig['stripe.com'].test.secret); + Object.defineProperty(req, 'Stripe', { + enumerable: true + , configurable: false + , writable: false + , get: function () { + _stripe = _stripe || require('stripe')(siteConfig['stripe.com'].live.secret); + return _stripe; + } + }); + + Object.defineProperty(req, 'StripeTest', { + enumerable: true + , configurable: false + , writable: false + , get: function () { + _stripe_test = _stripe_test || require('stripe')(siteConfig['stripe.com'].test.secret); + return _stripe_test; + } + }); + + Object.defineProperty(req, 'Mandrill', { + enumerable: true + , configurable: false + , writable: false + , get: function () { + if (!_mandrill) { + var Mandrill = require('mandrill-api/mandrill'); + _mandrill = new Mandrill.Mandrill(siteConfig['mandrill.com'].apiKey); + _mandrill.messages.sendTemplateAsync = function (opts) { + return new PromiseA(function (resolve, reject) { + _mandrill.messages.sendTemplate(opts, resolve, reject); + }); + }; + } + return _mandrill; + } + }); + + Object.defineProperty(req, 'Mailchimp', { + enumerable: true + , configurable: false + , writable: false + , get: function () { + var Mailchimp = require('mailchimp-api-v3'); + _mailchimp = _mailchimp || new Mailchimp(siteConfig['mailchimp.com'].apiKey); + return _stripe_test; + } + }); req._walnutOriginalUrl = req.url; // "/path/api/com.example/hello".replace(/.*\/api\//, '').replace(/([^\/]*\/+)/, '/') => '/hello' @@ -245,6 +296,7 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { next(); }); }); + // // TODO handle /accounts/:accountId //