A 100% typesafe library for sending mail using the Node.js Sendgrid SDK.
🔥 Never forget a variable in an email again.
It runs in 3 steps:
- Fetches your Sendgrid Templates using your own ApiKey
- Detects and extracts the variables from the templates
- Generate typings
There is no extra dependency, @sendgrid/mail is a peer-dependency. This package's purpose is only to add typings to the official SendGrid Library.
If you do not use the official Sendgrid Javascript SDK, you need to install it.
(npm install --save @sendgrid/mail
)
# For npm
npm install --save sendgrid-ts-mail
# For yarn
yarn add sendgrid-ts-mail
# For pnpm
pnpm add sendgrid-ts-mail
# For bun
bun add sendgrid-ts-mail
# For npm & yarn
npx sendgrid-ts-mail generate
# For pnpm
pnpx sendgrid-ts-mail generate
-h / --help
: Display usage
-e / --env {path}
: Path to .env file. Defaults to './.env'
-k / --key {variable_name}
: Specify a custom key name for looking up the SENDGRID_API_KEY in the environment file. Defaults to 'SENDGRID_API_KEY'
When updating an email in Sendgrid's interface, it is advised to re-generate the typings if you added/removed variables.
import sgMail from 'sendgrid-ts-mail';
sgMail.send({
from: '[email protected]',
templateId: 'd-xxxxx', // Type-safe
dynamicTemplateData: {
firstname: 'John', // Type-safe
lastname: 'Doe', // Type-safe
business: 'Acme Corp' // Type-safe
},
});
It is advised to add the typings generation before building your project:
npx sendgrid-ts-mail generate && npm run build
- Improve the variable detection (read from the IFs, etc)
- Batch the getSendgridTemplate(:id) to improve performance.
Here are some projects that use sendgrid-ts-mail
:
All type of contributions are appreciated.
npm start
: Runnpm run build
in watch modenpm run build
: Generate bundles and typingsnpm run test
: Run test suite oncenpm run test:watch
: Run test suite in interactive watch modenpm run test:prod
: Run linting and generate coveragenpm run report-coverage
: Report the coverage with coveralls
Made with ❤️ by @thibaut-trouve and all these wonderful contributors (emoji key):
Thibaut Trouvé 💻 🔧 🚇 |
This project follows the all-contributors specification. Contributions of any kind are welcome!