dword-design / nuxt-mail

Adds email sending capability to a Nuxt.js app. Adds a server route, an injected variable, and uses nodemailer to send emails.
Other
239 stars 18 forks source link

Legacy plugin requires update for Nuxt 3 #138

Closed silverbackdan closed 1 year ago

silverbackdan commented 1 year ago

Hi and thanks for your great work. I had the issue described here: https://github.com/dword-design/nuxt-mail/issues/137

Setting the type to module in the package.json does work, however I get a warning that the plugin with legacy Nuxt 2 format is likely to be broken in future and should be updated.

I think perhaps a new version will be required for Nuxt 3 to get around these issues and implement the module in a way to future proof it for a while longer.

Were you aware of this and are there plans to make these changes?

[warn] [nuxt] You are using a plugin with legacy Nuxt 2 format (context, inject) which is likely to be broken. In the future they will be ignored: (context, inject) => inject('mail', {
send: async config => {
  try {
       await __vite_ssr_import_0__.useFetch('/mail/send', {
         body: config,
         method: 'POST'
       }, 'XXX');
   } catch (error) {
      throw new Error(error.response.data);
    }
  }
})
silverbackdan commented 1 year ago

As a side note, I also thought it'd be good to have a way in which we can implement the middleware with customisations. SO perhaps a way to disable the route and use the middleware still. This is because I want to use recaptcha as well. Looking into how I'm going to impliement this now, and perhaps I can just override the server route manually. I'll see.

dword-design commented 1 year ago

@silverbackdan Should be fixed. Can you provide an example for your suggestion and open a new issue?

silverbackdan commented 1 year ago

That's great thanks. For full type definitions if wanted I ended up with something like this for the plugin

import { createError, defineEventHandler, readBody } from "h3";
import nodemailer from "nodemailer";

import options from "#mail/options.js";
import send from "#mail/send.js";
// @ts-ignore
import omit from "@dword-design/functions/dist/omit.js";

const transport = nodemailer.createTransport(options.smtp);

export default defineEventHandler(async (event) => {
  try {
    await send(await readBody(event), options, transport);
  } catch (error) {
    if (error instanceof Error) {
      throw createError({ statusCode: 500, statusMessage: error.message });
    }
    throw createError({
      statusCode: 500,
      statusMessage: "An unknown error occurred",
    });
  }

  return "ok";
});

I'll open a new issue for discussion on the other suggestion

dword-design commented 1 year ago

Glad to take a PR with typings for Typescript!

silverbackdan commented 1 year ago

Great, I'll try and spare some time to help contribute. Helpful package, thanks for your work and fast responses.

silverbackdan commented 1 year ago

Just a curious one, is there a reason why you use @dword-design/functions/dist/omit.js instead of lodash? Is the functionality different?

dword-design commented 1 year ago

@silverbackdan I started @dword-design/functions as a collection of useful functions but most of them are around lodash, so actually there is no big reason anymore to have it. Maybe I'll drop it soon.

silverbackdan commented 1 year ago

Cool, thanks for clarifying :)