nuxt-mail 3.0.22 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -157,7 +157,7 @@ Or via index (in which case you do not need the `name` property):
157
157
 
158
158
  ```js
159
159
  this.$axios.$post('/mail/send', {
160
- config: 1, // resolves to 'support'
160
+ config: 1, // Resolves to 'support'
161
161
  from: 'John Doe',
162
162
  subject: 'Incredible',
163
163
  text: 'This is an incredible test message',
package/dist/index.js CHANGED
@@ -1,89 +1,71 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = _default;
7
-
8
- var _findIndex = _interopRequireDefault(require("@dword-design/functions/dist/find-index"));
9
-
10
- var _omit = _interopRequireDefault(require("@dword-design/functions/dist/omit"));
11
-
12
- var _some = _interopRequireDefault(require("@dword-design/functions/dist/some"));
13
-
14
- var _express = _interopRequireDefault(require("express"));
15
-
16
- var _nodemailer = _interopRequireDefault(require("nodemailer"));
17
-
18
- var _nuxtPushPlugins = _interopRequireDefault(require("nuxt-push-plugins"));
19
-
20
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
-
22
- function _default(moduleOptions) {
23
- var _options$message;
24
-
25
- const options = { ...this.options.mail,
1
+ import some from "@dword-design/functions/dist/some.js";
2
+ import { addServerHandler, addTemplate, createResolver, isNuxt3 as isNuxt3Try } from '@nuxt/kit';
3
+ import express from 'express';
4
+ import fs from 'fs-extra';
5
+ import nodemailer from 'nodemailer';
6
+ import nuxtPushPlugins from 'nuxt-push-plugins';
7
+ import parsePackagejsonName from 'parse-packagejson-name';
8
+ import P from 'path';
9
+ import send from "./send.js";
10
+ const resolver = createResolver(import.meta.url);
11
+ const packageConfig = fs.readJsonSync(resolver.resolve('../package.json'));
12
+ const moduleName = parsePackagejsonName(packageConfig.name).fullName;
13
+ export default function (moduleOptions, nuxt) {
14
+ nuxt = nuxt || this;
15
+ const options = {
16
+ ...nuxt.options.mail,
26
17
  ...moduleOptions
27
18
  };
28
-
29
19
  if (!options.smtp) {
30
20
  throw new Error('SMTP config is missing.');
31
21
  }
32
-
33
22
  if (Array.isArray(options.message) && options.message.length === 0 || !options.message) {
34
23
  throw new Error('You have to provide at least one config.');
35
24
  }
36
-
37
25
  if (!Array.isArray(options.message)) {
38
26
  options.message = [options.message];
39
27
  }
40
-
41
- if (_options$message = options.message, (0, _some.default)(c => !c.to && !c.cc && !c.bcc)(_options$message)) {
28
+ if (some(c => !c.to && !c.cc && !c.bcc)(options.message)) {
42
29
  throw new Error('You have to provide to/cc/bcc in all configs.');
43
30
  }
44
-
45
- const app = (0, _express.default)();
46
-
47
- const transport = _nodemailer.default.createTransport(options.smtp);
48
-
49
- app.use(_express.default.json());
50
- app.post('/send', async (req, res) => {
51
- req.body = {
52
- config: 0,
53
- ...req.body
54
- };
55
-
56
- try {
57
- var _req$body, _options$message$req$;
58
-
59
- if (typeof req.body.config === 'string') {
60
- var _options$message2;
61
-
62
- const configIndex = (_options$message2 = options.message, (0, _findIndex.default)(_ => _.name === req.body.config)(_options$message2));
63
-
64
- if (configIndex === -1) {
65
- throw new Error(`Message config with name '${req.body.config}' not found.`);
66
- }
67
-
68
- req.body.config = configIndex;
69
- } else if (!options.message[req.body.config]) {
70
- throw new Error(`Message config not found at index ${req.body.config}.`);
31
+ let isNuxt3 = true;
32
+ try {
33
+ isNuxt3 = isNuxt3Try();
34
+ } catch {
35
+ isNuxt3 = false;
36
+ }
37
+ if (isNuxt3) {
38
+ addTemplate({
39
+ filename: P.join(moduleName, 'options.js'),
40
+ getContents: () => `export default ${JSON.stringify(options, undefined, 2)}`,
41
+ write: true
42
+ });
43
+ addTemplate({
44
+ filename: P.join(moduleName, 'send.js'),
45
+ getContents: () => fs.readFile(resolver.resolve('./send.js'), 'utf8'),
46
+ write: true
47
+ });
48
+ nuxt.options.alias['#mail'] = P.resolve(nuxt.options.buildDir, moduleName);
49
+ addServerHandler({
50
+ handler: resolver.resolve('./server-handler.post.js'),
51
+ route: '/mail/send'
52
+ });
53
+ } else {
54
+ const app = express();
55
+ const transport = nodemailer.createTransport(options.smtp);
56
+ app.use(express.json());
57
+ app.post('/send', async (req, res) => {
58
+ try {
59
+ await send(req.body, options, transport);
60
+ } catch (error) {
61
+ return res.status(500).send(error.message);
71
62
  }
72
-
73
- await transport.sendMail({ ...(_req$body = req.body, (0, _omit.default)(['config', 'to', 'cc', 'bcc'])(_req$body)),
74
- ...(_options$message$req$ = options.message[req.body.config], (0, _omit.default)(['name'])(_options$message$req$))
75
- });
76
- } catch (error) {
77
- return res.status(400).send(error.message);
78
- }
79
-
80
- return res.sendStatus(200);
81
- });
82
- this.addServerMiddleware({
83
- handler: app,
84
- path: '/mail'
85
- });
86
- (0, _nuxtPushPlugins.default)(this, require.resolve("./plugin"));
87
- }
88
-
89
- module.exports = exports.default;
63
+ return res.sendStatus(200);
64
+ });
65
+ nuxt.addServerMiddleware({
66
+ handler: app,
67
+ path: '/mail'
68
+ });
69
+ }
70
+ nuxtPushPlugins(nuxt, resolver.resolve(`./plugin-nuxt${isNuxt3 ? 3 : 2}.js`));
71
+ }
@@ -0,0 +1,9 @@
1
+ export default ((context, inject) => inject('mail', {
2
+ send: async config => {
3
+ try {
4
+ await context.app.$axios.$post('/mail/send', config);
5
+ } catch (error) {
6
+ throw new Error(error.response.data);
7
+ }
8
+ }
9
+ }));
@@ -0,0 +1,13 @@
1
+ import { useFetch } from '#app';
2
+ export default ((context, inject) => inject('mail', {
3
+ send: async config => {
4
+ try {
5
+ await useFetch('/mail/send', {
6
+ body: config,
7
+ method: 'POST'
8
+ });
9
+ } catch (error) {
10
+ throw new Error(error.response.data);
11
+ }
12
+ }
13
+ }));
package/dist/send.js ADDED
@@ -0,0 +1,21 @@
1
+ import findIndex from "@dword-design/functions/dist/find-index.js";
2
+ import omit from "@dword-design/functions/dist/omit.js";
3
+ export default (async (body, options, transport) => {
4
+ body = {
5
+ config: 0,
6
+ ...body
7
+ };
8
+ if (typeof body.config === 'string') {
9
+ const configIndex = findIndex(_ => _.name === body.config)(options.message);
10
+ if (configIndex === -1) {
11
+ throw new Error(`Message config with name '${body.config}' not found.`);
12
+ }
13
+ body.config = configIndex;
14
+ } else if (!options.message[body.config]) {
15
+ throw new Error(`Message config not found at index ${body.config}.`);
16
+ }
17
+ await transport.sendMail({
18
+ ...omit(['config', 'to', 'cc', 'bcc'])(body),
19
+ ...omit(['name'])(options.message[body.config])
20
+ });
21
+ });
@@ -0,0 +1,16 @@
1
+ import { createError, defineEventHandler, readBody } from 'h3';
2
+ import nodemailer from 'nodemailer';
3
+ import options from '#mail/options.js';
4
+ import send from '#mail/send.js';
5
+ const transport = nodemailer.createTransport(options.smtp);
6
+ export default defineEventHandler(async event => {
7
+ try {
8
+ await send(await readBody(event), options, transport);
9
+ } catch (error) {
10
+ throw createError({
11
+ statusCode: 500,
12
+ statusMessage: error.message
13
+ });
14
+ }
15
+ return '';
16
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-mail",
3
- "version": "3.0.22",
3
+ "version": "3.1.0",
4
4
  "description": "Adds email sending capability to a Nuxt.js app. Adds a server route, an injected variable, and uses nodemailer to send emails.",
5
5
  "keywords": [
6
6
  "email",
@@ -23,6 +23,8 @@
23
23
  "funding": "https://github.com/sponsors/dword-design",
24
24
  "license": "MIT",
25
25
  "author": "Sebastian Landwehr <info@sebastianlandwehr.com>",
26
+ "type": "module",
27
+ "exports": "./dist/index.js",
26
28
  "main": "dist/index.js",
27
29
  "files": [
28
30
  "dist"
@@ -38,27 +40,27 @@
38
40
  },
39
41
  "dependencies": {
40
42
  "@dword-design/functions": "^4.0.0",
43
+ "@nuxt/kit": "^3.0.0",
41
44
  "express": "^4.17.1",
45
+ "fs-extra": "^11.1.0",
46
+ "h3": "^1.0.2",
42
47
  "nodemailer": "^6.4.11",
43
- "nuxt-push-plugins": "^2.0.0"
48
+ "nuxt-push-plugins": "^2.1.2",
49
+ "parse-packagejson-name": "^1.0.1"
44
50
  },
45
51
  "devDependencies": {
46
- "@dword-design/base": "^8.0.0",
47
- "@dword-design/proxyquire": "^2.0.0",
52
+ "@dword-design/base": "^9.1.9",
48
53
  "@dword-design/puppeteer": "^5.0.0",
49
54
  "@dword-design/tester": "^2.0.0",
50
- "@dword-design/tester-plugin-nodemailer-mock": "^1.0.0",
51
- "@dword-design/tester-plugin-puppeteer": "^2.0.0",
55
+ "@dword-design/tester-plugin-nuxt-config": "^1.1.3",
56
+ "@dword-design/tester-plugin-puppeteer": "^2.1.22",
52
57
  "@nuxtjs/axios": "^5.13.1",
53
- "axios": "^0.25.0",
58
+ "axios": "^0.27.2",
54
59
  "depcheck-package-name": "^2.0.0",
55
- "nodemailer-mock": "^1.5.4",
56
- "nuxt": "^2.15.3",
57
- "output-files": "^2.0.0",
58
- "with-local-tmp-dir": "^4.0.0"
60
+ "smtp-tester": "^2.0.1"
59
61
  },
60
62
  "engines": {
61
- "node": ">=12"
63
+ "node": ">=14"
62
64
  },
63
65
  "publishConfig": {
64
66
  "access": "public"
package/dist/plugin.js DELETED
@@ -1,19 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _default = (context, inject) => inject('mail', {
9
- send: async config => {
10
- try {
11
- await context.app.$axios.$post('/mail/send', config);
12
- } catch (error) {
13
- throw new Error(error.response.data);
14
- }
15
- }
16
- });
17
-
18
- exports.default = _default;
19
- module.exports = exports.default;