Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/composable.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { useNuxtApp } from '#imports';

export const useMail = () => useNuxtApp().$mail;
export const useMail = () => ({
send: async config => {
try {
await $fetch('/mail/send', { body: config, method: 'POST' });
} catch (error) {
throw new Error(error.response._data.statusMessage);
}
},
});
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ export default function (moduleOptions, nuxt) {
addImports([
{ from: resolver.resolve('./composable.js'), name: 'useMail' },
]);

nuxt.hook('nitro:config', nitroConfig => {
if (!nitroConfig.imports) {
nitroConfig.imports = { imports: [] };
}

nitroConfig.imports.imports.push({
from: resolver.resolve('./composable.js'),
name: 'useMail',
});
});
} else {
const app = express();
const transport = nodemailer.createTransport(options.smtp);
Expand Down
104 changes: 88 additions & 16 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default {
'nuxt.config.js': endent`
export default {
modules: [
['../src/index.js', { message: { bcc: 'johndoe@gmail.com' }, smtp: { port: 3001 } }],
['self', { message: { bcc: 'johndoe@gmail.com' }, smtp: { port: 3001 } }],
],
}
`,
Expand Down Expand Up @@ -83,13 +83,24 @@ export default {
this.browser = await puppeteer.launch();
this.page = await this.browser.newPage();
this.mailServer.removeAll();

await fs.outputFile(
'node_modules/self/package.json',
JSON.stringify({
exports: './src/index.js',
name: 'self',
type: 'module',
}),
);

await fs.copy('../src', 'node_modules/self/src');
},
async cc() {
await outputFiles({
'nuxt.config.js': endent`
export default {
modules: [
['../src/index.js', { message: { cc: 'johndoe@gmail.com' }, smtp: { port: 3001 } }],
['self', { message: { cc: 'johndoe@gmail.com' }, smtp: { port: 3001 } }],
],
}
`,
Expand Down Expand Up @@ -134,7 +145,7 @@ export default {
'nuxt.config.js': endent`
export default {
modules: [
['../src/index.js', {
['self', {
message: { bcc: 'bcc@gmail.com', cc: 'cc@gmail.com' },
smtp: { port: 3001 },
}],
Expand Down Expand Up @@ -186,7 +197,7 @@ export default {
'nuxt.config.js': endent`
export default {
modules: [
['../src/index.js', { message: { to: 'johndoe@gmail.com' }, smtp: { port: 3001 } }],
['self', { message: { to: 'johndoe@gmail.com' }, smtp: { port: 3001 } }],
],
}
`,
Expand Down Expand Up @@ -233,7 +244,7 @@ export default {
'nuxt.config.js': endent`
export default {
modules: [
['../src/index.js', {
['self', {
message: [{ to: 'foo@bar.com' }, { to: 'johndoe@gmail.com' }],
smtp: { port: 3001 },
}],
Expand Down Expand Up @@ -281,7 +292,7 @@ export default {
'nuxt.config.js': endent`
export default {
modules: [
['../src/index.js', {
['self', {
message: [
{ to: 'foo@bar.com' },
{ name: 'foo', to: 'johndoe@gmail.com' },
Expand Down Expand Up @@ -332,7 +343,7 @@ export default {
'nuxt.config.js': endent`
export default {
modules: [
['../src/index.js', {
['self', {
message: [{ to: 'foo@bar.com' }],
smtp: {},
}],
Expand Down Expand Up @@ -370,7 +381,7 @@ export default {
'nuxt.config.js': endent`
export default {
modules: [
['../src/index.js', { message: [{ to: 'foo@bar.com' }], smtp: {} }],
['self', { message: [{ to: 'foo@bar.com' }], smtp: {} }],
],
}
`,
Expand Down Expand Up @@ -405,7 +416,7 @@ export default {
'nuxt.config.js': endent`
export default {
modules: [
['../src/index.js', {
['self', {
message: { to: 'johndoe@gmail.com' },
smtp: { port: 3001 },
}],
Expand Down Expand Up @@ -454,7 +465,7 @@ export default {
endent`
export default {
modules: [
['../src/index.js', { smtp: {} }],
['self', { smtp: {} }],
],
}
`,
Expand All @@ -470,7 +481,7 @@ export default {
endent`
export default {
modules: [
['../src/index.js', { message: {}, smtp: {} }],
['self', { message: {}, smtp: {} }],
],
}
`,
Expand All @@ -485,7 +496,7 @@ export default {
'nuxt.config.js',
endent`
export default {
modules: ['../src/index.js'],
modules: ['self'],
}
`,
);
Expand Down Expand Up @@ -529,6 +540,8 @@ export default {
`,
});

await fs.remove('node_modules');

await fs.symlink(
P.join('..', 'node_modules', '.cache', 'nuxt2', 'node_modules'),
'node_modules',
Expand Down Expand Up @@ -576,6 +589,8 @@ export default {
`,
});

await fs.remove('node_modules');

await fs.symlink(
P.join('..', 'node_modules', '.cache', 'nuxt2', 'node_modules'),
'node_modules',
Expand Down Expand Up @@ -632,6 +647,8 @@ export default {
`,
});

await fs.remove('node_modules');

await fs.symlink(
P.join('..', 'node_modules', '.cache', 'nuxt2', 'node_modules'),
'node_modules',
Expand Down Expand Up @@ -686,6 +703,8 @@ export default {
`,
});

await fs.remove('node_modules');

await fs.symlink(
P.join('..', 'node_modules', '.cache', 'nuxt2', 'node_modules'),
'node_modules',
Expand Down Expand Up @@ -714,7 +733,7 @@ export default {
'nuxt.config.js': endent`
export default {
modules: [
['../src/index.js', {
['self', {
message: { to: 'johndoe@gmail.com' },
smtp: { port: 3001 },
}],
Expand Down Expand Up @@ -762,7 +781,7 @@ export default {
await outputFiles({
'nuxt.config.js': endent`
export default {
modules: ['../src/index.js'],
modules: ['self'],
runtimeConfig: {
mail: {
message: { to: 'johndoe@gmail.com' },
Expand Down Expand Up @@ -807,12 +826,65 @@ export default {
await kill(nuxt.pid);
}
},
async 'server route'() {
await outputFiles({
'nuxt.config.js': endent`
export default {
modules: [
['self', {
message: { to: 'johndoe@gmail.com' },
smtp: { port: 3001 },
}],
],
}
`,
'pages/index.vue': endent`
<template>
<div />
</template>

<script setup>
$fetch('/api/foo');
</script>
`,
'server/api/foo.js': endent`
import { defineEventHandler, useMail } from '#imports';

const mail = useMail();

export default defineEventHandler(() => mail.send({
from: 'a@b.de',
subject: 'Incredible',
text: 'This is an incredible test message',
to: 'foo@bar.de',
}));
`,
});

const nuxt = execaCommand('nuxt dev');

try {
await nuxtDevReady();

const [capture] = await Promise.all([
this.mailServer.captureOne('johndoe@gmail.com'),
this.page.goto('http://localhost:3000'),
]);

expect(capture.email.body).toEqual('This is an incredible test message');
expect(capture.email.headers.subject).toEqual('Incredible');
expect(capture.email.headers.from).toEqual('a@b.de');
expect(capture.email.headers.to).toEqual('johndoe@gmail.com');
} finally {
await kill(nuxt.pid);
}
},
async 'to, cc and bcc'() {
await outputFiles({
'nuxt.config.js': endent`
export default {
modules: [
['../src/index.js', {
['self', {
message: {
bcc: 'bcc@gmail.com',
cc: 'cc@gmail.com',
Expand Down Expand Up @@ -870,7 +942,7 @@ export default {
'nuxt.config.js': endent`
export default {
modules: [
['../src/index.js', {
['self', {
message: { to: 'johndoe@gmail.com' },
smtp: { port: 3001 },
}],
Expand Down
16 changes: 3 additions & 13 deletions src/plugin-nuxt3.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
import { defineNuxtPlugin } from '#imports';

export default defineNuxtPlugin(() => ({
provide: {
mail: {
send: async config => {
try {
await $fetch('/mail/send', { body: config, method: 'POST' });
} catch (error) {
throw new Error(error.response._data.statusMessage);
}
},
},
},
}));
import { useMail } from './composable.js';

export default defineNuxtPlugin(() => ({ provide: { mail: useMail() } }));