Skip to content

Commit cad5a14

Browse files
painotpiTarun Paiscopsy
authored
Add support for Nodemailer (#32)
* Adding nodemailer * Adding config files for nodemailer folder setup * chore(release): 0.1.5 * chore(release): 0.1.4 * Adding nodemailer types * Reverting version bump on @notifire/core * Adding nodemailer test workflow Co-authored-by: Tarun Pai <[email protected]> Co-authored-by: Dima Grossman <[email protected]>
1 parent a8b1ca1 commit cad5a14

19 files changed

+6406
-0
lines changed

.github/workflows/test-nodemailer.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Test @notifire/nodemailer
2+
on:
3+
push:
4+
paths:
5+
- "providers/nodemailer/**"
6+
jobs:
7+
test:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
- uses: actions/setup-node@v2
12+
with:
13+
node-version: "14"
14+
- run: rm -rf build
15+
- run: yarn install
16+
- run: yarn bootstrap
17+
- run: yarn build
18+
- name: Test
19+
working-directory: "providers/nodemailer"
20+
run: yarn test

providers/nodemailer/.cspell.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"version": "0.1",
3+
"$schema": "https://raw.githubusercontent.com/streetsidesoftware/cspell/master/cspell.schema.json",
4+
"language": "en",
5+
"words": [
6+
"bitjson",
7+
"bitauth",
8+
"cimg",
9+
"circleci",
10+
"codecov",
11+
"commitlint",
12+
"dependabot",
13+
"editorconfig",
14+
"esnext",
15+
"execa",
16+
"exponentiate",
17+
"globby",
18+
"libauth",
19+
"mkdir",
20+
"prettierignore",
21+
"sandboxed",
22+
"transpiled",
23+
"typedoc",
24+
"untracked",
25+
"Sendgrid",
26+
"notifire"
27+
],
28+
"flagWords": [],
29+
"ignorePaths": [
30+
"package.json",
31+
"package-lock.json",
32+
"yarn.lock",
33+
"tsconfig.json",
34+
"node_modules/**"
35+
]
36+
}

providers/nodemailer/.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
end_of_line = lf
7+
indent_size = 2
8+
indent_style = space
9+
insert_final_newline = true
10+
max_line_length = 80
11+
trim_trailing_whitespace = true
12+
13+
[*.md]
14+
max_line_length = 0
15+
trim_trailing_whitespace = false

providers/nodemailer/.eslintrc.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../.eslintrc.json"
3+
}

providers/nodemailer/.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.idea/*
2+
.nyc_output
3+
build
4+
node_modules
5+
test
6+
src/**.js
7+
coverage
8+
*.log
9+
package-lock.json

providers/nodemailer/.prettierignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# package.json is formatted by package managers, so we ignore it here
2+
package.json

providers/nodemailer/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4+
5+
### [0.1.4](https://github.com/notifirehq/nodemailer/compare/v0.1.5...v0.1.4) (2021-10-08)

providers/nodemailer/LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Dima Grossman
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

providers/nodemailer/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Nodejs Nodemailer Provider
2+
3+
A nodemailer email provider library for [@notifire/core](https://github.com/notifirehq/notifire)
4+
5+
## Usage
6+
7+
```javascript
8+
import { NodemailerProvider } from '@notifire/nodemailer';
9+
10+
const provider = new NodemailerProvider({
11+
from: process.env.NODEMAILER_FROM_EMAIL,
12+
host: process.env.NODEMAILER_HOST,
13+
user: process.env.NODEMAILER_USERNAME,
14+
password: process.env.NODEMAILER_PASSWORD,
15+
port: process.env.NODEMAILER_PORT,
16+
secure: process.env.NODEMAILER_SECURE,
17+
});
18+
```

providers/nodemailer/jest.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
2+
module.exports = {
3+
preset: 'ts-jest',
4+
testEnvironment: 'node',
5+
};

providers/nodemailer/package.json

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
{
2+
"name": "@notifire/nodemailer",
3+
"version": "0.1.4",
4+
"description": "A nodemailer wrapper for notifire",
5+
"main": "build/main/index.js",
6+
"typings": "build/main/index.d.ts",
7+
"module": "build/module/index.js",
8+
"private": false,
9+
"repository": "https://github.com/notifirehq/nodemailer",
10+
"license": "MIT",
11+
"keywords": [],
12+
"scripts": {
13+
"build": "run-p build:*",
14+
"build:main": "tsc -p tsconfig.json",
15+
"build:module": "tsc -p tsconfig.module.json",
16+
"fix": "run-s fix:*",
17+
"fix:prettier": "prettier \"src/**/*.ts\" --write",
18+
"fix:lint": "eslint src --ext .ts --fix",
19+
"test": "run-s build test:*",
20+
"test:lint": "eslint src --ext .ts",
21+
"test:prettier": "prettier \"src/**/*.ts\" --list-different",
22+
"test:spelling": "cspell \"{README.md,.github/*.md,src/**/*.ts}\"",
23+
"test:unit": "jest src",
24+
"check-cli": "run-s test diff-integration-tests check-integration-tests",
25+
"check-integration-tests": "run-s check-integration-test:*",
26+
"diff-integration-tests": "mkdir -p diff && rm -rf diff/test && cp -r test diff/test && rm -rf diff/test/test-*/.git && cd diff && git init --quiet && git add -A && git commit --quiet --no-verify --allow-empty -m 'WIP' && echo '\\n\\nCommitted most recent integration test output in the \"diff\" directory. Review the changes with \"cd diff && git diff HEAD\" or your preferred git diff viewer.'",
27+
"watch:build": "tsc -p tsconfig.json -w",
28+
"watch:test": "jest src --watch",
29+
"cov": "run-s build test:unit cov:html cov:lcov && open-cli coverage/index.html",
30+
"cov:html": "nyc report --reporter=html",
31+
"cov:lcov": "nyc report --reporter=lcov",
32+
"cov:send": "run-s cov:lcov && codecov",
33+
"cov:check": "nyc report && nyc check-coverage --lines 100 --functions 100 --branches 100",
34+
"doc": "run-s doc:html && open-cli build/docs/index.html",
35+
"doc:html": "typedoc src/ --exclude **/*.spec.ts --target ES6 --mode file --out build/docs",
36+
"doc:json": "typedoc src/ --exclude **/*.spec.ts --target ES6 --mode file --json build/docs/typedoc.json",
37+
"doc:publish": "gh-pages -m \"[ci skip] Updates\" -d build/docs",
38+
"version": "standard-version",
39+
"reset-hard": "git clean -dfx && git reset --hard && yarn",
40+
"prepare-release": "run-s reset-hard test version"
41+
},
42+
"engines": {
43+
"node": ">=10"
44+
},
45+
"dependencies": {
46+
"@notifire/core": "^0.1.4",
47+
"nodemailer": "^6.6.5"
48+
},
49+
"devDependencies": {
50+
"@istanbuljs/nyc-config-typescript": "^1.0.1",
51+
"@types/jest": "^27.0.1",
52+
"@types/nodemailer": "^6.4.4",
53+
"codecov": "^3.5.0",
54+
"cspell": "^4.1.0",
55+
"cz-conventional-changelog": "^3.3.0",
56+
"gh-pages": "^3.1.0",
57+
"jest": "^27.1.0",
58+
"npm-run-all": "^4.1.5",
59+
"nyc": "^15.1.0",
60+
"open-cli": "^6.0.1",
61+
"prettier": "^2.1.1",
62+
"standard-version": "^9.0.0",
63+
"ts-jest": "^27.0.5",
64+
"ts-node": "^9.0.0",
65+
"typedoc": "^0.19.0"
66+
},
67+
"files": [
68+
"build/main",
69+
"build/module",
70+
"!**/*.spec.*",
71+
"!**/*.json",
72+
"CHANGELOG.md",
73+
"LICENSE",
74+
"README.md"
75+
],
76+
"ava": {
77+
"failFast": true,
78+
"timeout": "60s",
79+
"typescript": {
80+
"rewritePaths": {
81+
"src/": "build/main/"
82+
}
83+
},
84+
"files": [
85+
"!build/module/**"
86+
]
87+
},
88+
"config": {
89+
"commitizen": {
90+
"path": "cz-conventional-changelog"
91+
}
92+
},
93+
"prettier": {
94+
"singleQuote": true
95+
},
96+
"nyc": {
97+
"extends": "@istanbuljs/nyc-config-typescript",
98+
"exclude": [
99+
"**/*.spec.js"
100+
]
101+
}
102+
}

providers/nodemailer/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './lib/nodemailer.provider';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const sendMailMock = jest.fn().mockReturnValue(() => {
2+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3+
return {} as any;
4+
});
5+
6+
import { NodemailerProvider } from './nodemailer.provider';
7+
8+
jest.mock('nodemailer', () => {
9+
return {
10+
createTransport: jest.fn().mockReturnValue({
11+
sendMail: sendMailMock,
12+
}),
13+
};
14+
});
15+
16+
test('should trigger nodemailer correctly', async () => {
17+
const provider = new NodemailerProvider({
18+
19+
host: 'test.test.email',
20+
21+
password: 'test123',
22+
port: 587,
23+
secure: false,
24+
});
25+
26+
await provider.sendMessage({
27+
28+
subject: 'test subject',
29+
html: '<div> Mail Content </div>',
30+
});
31+
32+
expect(sendMailMock).toHaveBeenCalled();
33+
expect(sendMailMock).toHaveBeenCalledWith({
34+
35+
html: '<div> Mail Content </div>',
36+
subject: 'test subject',
37+
38+
});
39+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { ChannelTypeEnum, IEmailOptions, IEmailProvider } from '@notifire/core';
2+
import nodemailer, { Transporter } from 'nodemailer';
3+
4+
export class NodemailerProvider implements IEmailProvider {
5+
id = 'nodemailer';
6+
channelType = ChannelTypeEnum.EMAIL as ChannelTypeEnum.EMAIL;
7+
private transports: Transporter;
8+
9+
constructor(
10+
private config: {
11+
from: string;
12+
host: string;
13+
port: number;
14+
secure: boolean;
15+
user: string;
16+
password: string;
17+
}
18+
) {
19+
this.transports = nodemailer.createTransport({
20+
host: this.config.host,
21+
port: this.config.port,
22+
secure: this.config.secure,
23+
auth: {
24+
user: this.config.user,
25+
pass: this.config.password,
26+
},
27+
});
28+
}
29+
30+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
31+
async sendMessage(options: IEmailOptions): Promise<any> {
32+
return await this.transports.sendMail({
33+
from: options.from || this.config.from,
34+
to: options.to,
35+
subject: options.subject,
36+
html: options.html,
37+
});
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* If you import a dependency which does not include its own type definitions,
3+
* TypeScript will try to find a definition for it by following the `typeRoots`
4+
* compiler option in tsconfig.json. For this project, we've configured it to
5+
* fall back to this folder if nothing is found in node_modules/@types.
6+
*
7+
* Often, you can install the DefinitelyTyped
8+
* (https://github.com/DefinitelyTyped/DefinitelyTyped) type definition for the
9+
* dependency in question. However, if no one has yet contributed definitions
10+
* for the package, you may want to declare your own. (If you're using the
11+
* `noImplicitAny` compiler options, you'll be required to declare it.)
12+
*
13+
* This is an example type definition which allows import from `module-name`,
14+
* e.g.:
15+
* ```ts
16+
* import something from 'module-name';
17+
* something();
18+
* ```
19+
*/
20+
declare module 'module-name' {}

providers/nodemailer/tsconfig.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "build/main",
5+
"rootDir": "src",
6+
"types": ["node", "jest"]
7+
},
8+
"include": ["src/**/*.ts"],
9+
"exclude": ["node_modules/**"]
10+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "./tsconfig",
3+
"compilerOptions": {
4+
"target": "esnext",
5+
"outDir": "build/module",
6+
"module": "esnext"
7+
},
8+
"exclude": [
9+
"node_modules/**"
10+
]
11+
}

0 commit comments

Comments
 (0)