mailjet / mailjet-apiv3-nodejs

[API v3] Official Mailjet API v3 NodeJS wrapper
https://dev.mailjet.com
MIT License
236 stars 69 forks source link

[WIP] Cannot read property 'MessageState' of undefined #242

Open alecrobertson1 opened 1 year ago

alecrobertson1 commented 1 year ago

I am using node-mailjet within a node project that is the basis of an AWS lambda.

I am importing the Message namespace from the node-mailjet package as follows:

import { Message } from 'node-mailjet'

When the application runs (transpiled using Webpack), Message is undefined and so when I refer to the MessageState enum on this namespace, the application fails. I require the MessageState enum so I can do error handling and logging.

This appears to be an issue with how node-mailjet is processing its namespaces, with the transpiled code not including this namespace or any of its exports and so they come back as undefined.

alecrobertson1 commented 1 year ago

I seem to have located the problem: https://github.com/mailjet/mailjet-apiv3-nodejs/blob/e9854f6cf2ab23c25a37b428ae714efd7e3748ca/webpack/webpack.common.config.js#L110

Only the default export is being exported, even though the types - including from within the Message namespace - are being exported from the index when declared in TypeScript:

https://github.com/mailjet/mailjet-apiv3-nodejs/blob/4024d94eba51fccda0ccab518112c73c70244743/lib/index.ts#L16-L18

I saw in the commit history that at one point it was considered to remove this line and it seems to have not been done. Why has this decision only been taken for the browser and not the Node version? Would it not be useful for these types to be available?

ai-wintermute commented 1 year ago

Hi @alecrobertson1 I'm not sure why it was done. But I've tried the import you mentioned and it works. Can you please share the whole code snippet?

alecrobertson1 commented 1 year ago

Hi @ai-wintermute I have created a very simple test project to demonstrate the problem, it is available at https://github.com/alecrobertson1/TestProject.

If you run the main file using node dist/main.js you will see the error as above:

TypeError: Cannot read property 'MessageState' of undefined

trevorblades commented 1 year ago

I'm running into the same problem here, trying to implement a simple subscribe/unsubscribe function in TypeScript:

import Mailjet, { BulkContactManagement } from "node-mailjet";
import type { ContactSubscription } from "node-mailjet";

const mailjet = new Mailjet({
  apiKey: process.env.MAILJET_API_KEY,
  apiSecret: process.env.MAILJET_API_SECRET,
});

const subscribeToNewsletter = (id: number, shouldSub = true) => {
  const contactLists: ContactSubscription.PostContactManageContactsListsBody = {
    ContactsLists: [
      {
        ListID: MAILJET_NEWSLETTER_ID,
        Action: shouldSub
          ? BulkContactManagement.ManageContactsAction.AddForce
          : BulkContactManagement.ManageContactsAction.UnSub,
      },
    ],
  };

  return mailjet
    .post("contact", { version: "v3" })
    .id(id)
    .action("managecontactslists")
    .request<ContactSubscription.PostContactManageContactsListsResponse>(
      contactLists
    );
};

The Action property in my contact list subscription body complains if I don't use the BulkContactManagement.ManageContactsAction enum, but if I try to compile and run this code, it fails because BulkContactManagement is not exported in the bundle from node-mailjet. So we get cannot read property "ManageContactsAction" of undefined.

Arsalanliaqat commented 1 year ago

I'm just reaching out to ask about the status of this issue. This issue impacts our work, and Is there any update or approximate timeline for a fix?

TypeError: Cannot read properties of undefined (reading 'TrackOpens') TypeError: Cannot read properties of undefined (reading 'TrackClicks') TypeError: Cannot read properties of undefined (reading 'Response')

ai-wintermute commented 1 year ago

Hi @Arsalanliaqat PR with the hotfix is ready but it breaks the default import functionality. That's why we're considering other options, and it takes time. Sorry for the inconvenience.

Jerome91410 commented 10 months ago

Hello,

Any news?

I'm facing same issue when I want to access to enum declared by SendEmail. I had to self declare the value in my own code...
image

Could you export them (the enums) outside the namespace as a workaround/hotfix??

Thank you