postalsys / imapflow

IMAP Client library for EmailEngine Email API (https://emailengine.app)
https://imapflow.com
Other
350 stars 59 forks source link

Wrong typescript types for interface MessageStructureObject (dispositionParameters and parameters) #171

Closed totolicious closed 6 months ago

totolicious commented 6 months ago

Describe the bug The properties dispositionParameters and parameters of interface MessageStructureObject have wrong types. Currently, these properties have the string type, although both the documentation and the actual data returned by the library fetch method will return objects with string properties for both dispositionParameters and parameters.

Example bodyStructure (of type MessageStructureObject) of a FetchMessageObject.

  {
    childNodes: [
      {
        part: '1',
        type: 'text/plain',
        parameters: { charset: 'us-ascii' }, // this is not `string`, it's `{ [k: string]: string }`
        encoding: '7bit',
        size: 240,
        lineCount: 14
      },
      {
        part: '2',
        childNodes: [
          {
            part: '2.1',
            type: 'text/html',
            parameters: { charset: 'us-ascii' }, // this is not `string`, it's `{ [k: string]: string }`
            encoding: '7bit',
            size: 275,
            lineCount: 0
          },
          {
            part: '2.2',
            type: 'text/plain',
            parameters: { // this is not `string`, it's `{ [k: string]: string }`
              charset: 'us-ascii',
              name: 'text.txt',
              'x-unix-mode': '0644'
            },
            encoding: '7bit',
            size: 4,
            lineCount: 0,
            disposition: 'attachment',
            dispositionParameters: { filename: 'text.txt' } // this is not `string`, it's `{ [k: string]: string }`
          },
          {
            part: '2.3',
            type: 'text/html',
            parameters: { charset: 'us-ascii' }, // this is not `string`, it's `{ [k: string]: string }`
            encoding: 'quoted-printable',
            size: 1649,
            lineCount: 25
          }
        ],
        type: 'multipart/mixed',
        parameters: {
          boundary: 'Apple-Mail=_93BD19B2-1CAC-40C2-8C71-C9B1F761ACB3'
        }
      }
    ],
    type: 'multipart/alternative',
    parameters: { boundary: 'Apple-Mail=_FF0CF4B2-FCE4-4FBA-8C38-3062EF73E91A' } // this is not `string`, it's `{ [k: string]: string }`
  }

The problem & the fix: The types found in DefinitelyTyped are wrong. See this for the fix: https://github.com/DefinitelyTyped/DefinitelyTyped/commit/e8dd2e509036c91c0970daf96377c2d2df8cf136 I'm 100% sure how to contribute with the fix though 😅

To Reproduce

  1. Steps to reproduce the behavior:

To see the actual values returned by the lib:

const client = new ImapFlow({
    host: "127.0.0.1",
    auth: {
        user: "test",
        accessToken: "test",
    },
    port: 993,
    logger,
});

await client.getMailboxLock('INBOX');

const emailAsyncIterator = client.fetch(
      '1:*',
      {
        uid: true,
        size: true,
        envelope: true,
        bodyStructure: true,
        internalDate: true,
      },
      {
        uid: true,
      },
    );

for await (const emailSummary of emailAsyncIterator) {
  // look for an attachment (disposition: 'attachment') then look at dispositionParameters
  // look for a node with parameters (parameters: { something: 'here'})
  // both dispositionParameters and parameters are objects
  console.log(JSON.stringify(emailSummary, null, 2));
}
  1. Documentation clearly says that these are objects

  2. @types/imapflow types: MessageStructureObject['dispositionParameters'] : https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/imapflow/index.d.ts#L362 MessageStructureObject['parameters']: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/imapflow/index.d.ts#L356

Expected behavior The two string should be { [k: string]: string }

Screenshots Dump of an example bodyStructure image

Typescript Error when accessing parameters or dispositionParameters image

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

andris9 commented 6 months ago

DefinitelyTyped typings are community-maintained. Initially, I published the typings file generated from JSDoc comments with the ImapFlow package. Unfortunately, the auto-generated typings were incorrect. I did not want to manually edit the typings file every time I published a new ImapFlow version, so I removed the reference from package.json. Then someone else took over and started publishing typings to the DeifnitelyTyped repository. As I have nothing to do with these typings, I can't help solving this issue.

Flamenco commented 3 months ago

@andris9 You can add the types to this project, so DefinitelyTyped is not even needed.

Flamenco commented 3 months ago

This solved typescript errors for me, although you could go into more details based on disposition type.

Screenshot 2024-05-30 at 12 02 33 PM