Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to use composable on server #251

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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: '[email protected]' }, smtp: { port: 3001 } }],
['self', { message: { bcc: '[email protected]' }, 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: '[email protected]' }, smtp: { port: 3001 } }],
['self', { message: { cc: '[email protected]' }, 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: '[email protected]', cc: '[email protected]' },
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: '[email protected]' }, smtp: { port: 3001 } }],
['self', { message: { to: '[email protected]' }, 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: '[email protected]' }, { to: '[email protected]' }],
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: '[email protected]' },
{ name: 'foo', to: '[email protected]' },
Expand Down Expand Up @@ -332,7 +343,7 @@ export default {
'nuxt.config.js': endent`
export default {
modules: [
['../src/index.js', {
['self', {
message: [{ to: '[email protected]' }],
smtp: {},
}],
Expand Down Expand Up @@ -370,7 +381,7 @@ export default {
'nuxt.config.js': endent`
export default {
modules: [
['../src/index.js', { message: [{ to: '[email protected]' }], smtp: {} }],
['self', { message: [{ to: '[email protected]' }], smtp: {} }],
],
}
`,
Expand Down Expand Up @@ -405,7 +416,7 @@ export default {
'nuxt.config.js': endent`
export default {
modules: [
['../src/index.js', {
['self', {
message: { to: '[email protected]' },
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: '[email protected]' },
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: '[email protected]' },
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: '[email protected]' },
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: '[email protected]',
subject: 'Incredible',
text: 'This is an incredible test message',
to: '[email protected]',
}));
`,
});

const nuxt = execaCommand('nuxt dev');

try {
await nuxtDevReady();

const [capture] = await Promise.all([
this.mailServer.captureOne('[email protected]'),
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('[email protected]');
expect(capture.email.headers.to).toEqual('[email protected]');
} 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: '[email protected]',
cc: '[email protected]',
Expand Down Expand Up @@ -870,7 +942,7 @@ export default {
'nuxt.config.js': endent`
export default {
modules: [
['../src/index.js', {
['self', {
message: { to: '[email protected]' },
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() } }));
Loading