DEV Community

Cover image for Why you should wrap your (JavaScript) dependencies
Hugo Di Francesco
Hugo Di Francesco

Posted on • Originally published at codewithhugo.com on

2 2

Why you should wrap your (JavaScript) dependencies

An email sending example.

This was sent out on the Code with Hugo newsletter.
Subscribe to get the latest posts right in your inbox (before anyone else).

Example using SendGrid

On a very basic level, we want to be able to send emails.

In code terms, we want a send function. What’s nice to have is a way to abstract the message creation, that’s a makeMailMessage function.

See the following code:

const sgMail = require('@sendgrid/mail');

const {
  SENDGRID_API_KEY,
  EMAIL_OVERRIDE_TO_ADDRESS,
  EMAIL_FROM_ADDRESS
} = process.env;

sgMail.setApiKey(SENDGRID_API_KEY);

function makeMailMessage ({
  to,
  from = EMAIL_FROM_ADDRESS,
  subject,
  text,
  html
}) {
  return {
    to: EMAIL_OVERRIDE_TO_ADDRESS || to,
    from,
    subject,
    ...(html ? { html } : { text })
  };
}
function send (message) {
  return sgMail.send(message);
}
module.exports = {
  makeMailMessage,
  send
};
Enter fullscreen mode Exit fullscreen mode

How do we consume this?

function doSomeSending(name, email) {
  const message = makeMailMessage({
    from: 'hi@codewithhugo.com',
    to: email,
    subject: 'Welcome to Code with Hugo',
    text: `Hi ${name}, welcome to Code with Hugo.`
  });
  return send(message); 
}
Enter fullscreen mode Exit fullscreen mode

Switching to another email provider

Say we want to switch to another provider, namely Mailgun. Ideally, we don’t want to change any of the application code.

const {
  MAILGUN_API_KEY,
  EMAIL_DOMAIN,
  EMAIL_OVERRIDE_TO_ADDRESS,
  EMAIL_FROM_ADDRESS
} = process.env;
const mailgun = require('mailgun-js')({
  apiKey: MAILGUN_API_KEY,
  domain: EMAIL_DOMAIN
});

function makeMailMessage ({
  to,
  from = EMAIL_FROM_ADDRESS,
  subject,
  text,
  html
}) {
  return {
    to: EMAIL_OVERRIDE_TO_ADDRESS || to,
    from,
    subject,
    ...(html ? { html } : { text })
  };
}

function send (message) {
  return new Promise((resolve, reject) => {
    mailgun.messages().send(message, (error, body) => {
      if (error) {
        return reject(error);
      }
      resolve(body);
    });
  });
}

module.exports = {
  makeMailMessage,
  send
};
Enter fullscreen mode Exit fullscreen mode

The API for our mail module stays the same, but we’ve changed the provider under the hood 👍 .

Samuel Zeller

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay