This site runs best with JavaScript enabled.

Publishing my first NPM package

Ruairidh Wynne-McHardy

15 February, 2020


Writing my first NPM package

Recently, I had the opportunity to publish my first NPM package. At work, we are keen on contributing to open-source, and have released several packages which we use internally such as Synth - our design token system, and Midi - our collection of helper functions such as hooks, etc.

I'm working on our internal API just now, and we wanted the ability to send out notifications. I had been wanting to release my first package for a while, and so I created Hermes.

Hermes allows you to send out a notification via Slack, SMS, or email and can easily be extended to add other delivery methods.

Choosing Typescript

I also took the chance to write it purely in Typescript which was really good fun and caught a lot of bugs during the development process, which might not have surfaced until much later otherwise. I also enjoyed the fact that I was really forced to consider what I was passing between functions, and why with Typescript. It's something I'll work a lot more with in the future.

It was interesting to note the similarities between writing a GraphQL SDL and Typescript also. For example, defining a union type is identical:

export type NotificationChannel = 'slack' | 'email' | 'sms'

Send notifications in JavaScript (with Hermes)

Using Hermes is pretty easy, you simply send a notification like this:

import { sendNotification } from '@beatgig/hermes'
await sendNotification({
channels: ['email', 'slack', 'sms'],
email: {
to: user.email,
subject: 'Reset your Password',
template: TEMPLATE_RESET_PASSWORD,
templateData: {
link,
},
apiKey: process.env.MAILGUN_API_KEY,
domain: process.env.MAILGUN_DOMAIN
},
slack: {
channel: 'testing',
message: 'User #1234 has requested a password reset.'
emoji: ':fire:',
blocks: [...block data],
token: process.env.SLACK_TOKEN
},
sms: {
to: user.phone,
message: 'A password reset request was received for your account. If this was not you, please contact us immediately.',
accountSid: process.env.TWILIO_ACCOUNT_SID,
token: process.env.TWILIO_TOKEN
}
})

Each of the channels is optional and so you could send a much smaller version to Slack on its own:

import { sendNotification } from '@beatgig/hermes'
await sendNotification({
channels: ['slack'],
slack: {
channel: 'testing',
message: 'User #1234 has requested a password reset.'
emoji: ':fire:',
blocks: [...block data],
token: process.env.SLACK_TOKEN
},
})

Conclusion

Overall, it was very straightforward to create an NPM package and begin using it internally straightaway. My hope is that others will find this helpful for their own systems and that I'll start to see some PRs come in.

If you want to try Hermes out, then run yarn add @beatgig/hermes and let me know what you think!

Share article

Join the Newsletter



Ruairidh Wynne-McHardy © 2020