This library provides an easy-to-use notification system that supports sending notifications via email and SMS using third-party providers.
Disclaimer: This library is currently in alpha version and should not be used in production environments. Use it at your own risk.
- Features
- Installation
- Usage
- Third-Party Providers
- Local Development
- Publishing to
npm
,yarn
andpnpm
- Send Email notifications
- Send SMS notifications
- Centralized notification management
- Flexible architecture for extending to other notification channels
You can install this library using any of the following package managers:
npm install @santospatrick/notify
yarn add @santospatrick/notify
pnpm add @santospatrick/notify
Here’s a basic example of how to send an email using the NotificationManager
from the library:
First, create a file named notificationConfig.ts
to configure the NotificationManager
:
// notificationConfig.ts
import { NotificationManager, EmailProviders } from '@santospatrick/notify';
// Create a new instance of NotificationManager
const notificationManager = new NotificationManager();
// Setup email service to use Twilio SendGrid (more to come!)
notificationManager.addEmailService({
provider: EmailProviders.SENDGRID,
apiKey: 'YOUR_API_KEY_HERE', // Your Twilio SendGrid API key that starts with "SG."
});
export { notificationManager };
Now, create another file named sendEmail.ts
to use the notificationManager
object for sending an email:
// sendEmail.ts
import { notificationManager } from './notificationConfig';
// Send notification
notificationManager
.send({
from: 'info@santospatrick.com', // Needs to be a "Single Sender" verified in SendGrid.
to: 'info@santospatrick.com',
subject: 'Hello from my application!',
html: '<strong>Hi there! Using @santospatrick/notify lib.</strong>',
})
.then(() => {
console.log('Email sent!');
})
.catch((error) => {
console.error(error);
});
This library uses SendGrid to send email notifications. To set up the SendGrid API key:
- Sign up for a Twilio SendGrid account.
- Go to
Settings > Sender Authentication > Verify a Single Sender > "Create New Sender"
. - Fill out the form and click "Create".
- Go to
Settings > API Keys > "Create API Key"
. - Give it a name and select "Restricted Access", click "Mail Send" and enable "Mail Send" (this is the only permission needed).
- Click "Create & View" and copy the API key.
To test the library locally while developing, you can use npm link
. This allows you to symlink your local development version of the library into another project.
- In the root directory of your library, run:
npm link
This will create a global symlink to your library.
- In the project where you want to test the library, run:
npm link @santospatrick/notify
This will create a symlink from the global @santospatrick/notify
to the node_modules
of your project.
-
Make changes to this library and run
yarn build
to compile the TypeScript code. -
Run your project and test the changes.
To publish the library to npm, follow these steps:
- Ensure your code is ready for publishing by running the build command:
yarn build
- Update the version number in the
package.json
file.
{
"version": "0.0.3" // Next version to be published
}
- Once the build is successful, publish the package to npm with public access:
npm publish --access public
This will publish your library to the npm registry, making it available for others to install and use.
Made with ❤️ by Patrick Santos