index.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. const services = require('./services.json');
  3. const normalized = {};
  4. Object.keys(services).forEach(key => {
  5. let service = services[key];
  6. normalized[normalizeKey(key)] = normalizeService(service);
  7. [].concat(service.aliases || []).forEach(alias => {
  8. normalized[normalizeKey(alias)] = normalizeService(service);
  9. });
  10. [].concat(service.domains || []).forEach(domain => {
  11. normalized[normalizeKey(domain)] = normalizeService(service);
  12. });
  13. });
  14. function normalizeKey(key) {
  15. return key.replace(/[^a-zA-Z0-9.-]/g, '').toLowerCase();
  16. }
  17. function normalizeService(service) {
  18. let filter = ['domains', 'aliases'];
  19. let response = {};
  20. Object.keys(service).forEach(key => {
  21. if (filter.indexOf(key) < 0) {
  22. response[key] = service[key];
  23. }
  24. });
  25. return response;
  26. }
  27. /**
  28. * Resolves SMTP config for given key. Key can be a name (like 'Gmail'), alias (like 'Google Mail') or
  29. * an email address (like 'test@googlemail.com').
  30. *
  31. * @param {String} key [description]
  32. * @returns {Object} SMTP config or false if not found
  33. */
  34. module.exports = function(key) {
  35. key = normalizeKey(key.split('@').pop());
  36. return normalized[key] || false;
  37. };