sendgrid / sendgrid-nodejs

The Official Twilio SendGrid Led, Community Driven Node.js API Library
https://sendgrid.com
MIT License
3k stars 782 forks source link

MailDataRequired requires MailContent? #1057

Open 0x80 opened 4 years ago

0x80 commented 4 years ago

Issue Summary

I'm running into a problem with Typescript because it doesn't let me build an email without a content field. Is the MailDataRequired correct?

I never used to send content with my emails. Only substitutions. The content / templates live on your servers.

Code Snippet

export type MailDataRequired = MailData & (
    { text: string } | { html: string } | { templateId: string } | { content: MailContent[] & { 0: MailContent } });

Exception/Log

# paste exception/log here

Technical details:

0x80 commented 4 years ago

BTW: 6.5.0 doesn't seem to have this weird { 0: MailContent } requirement, but still content is required.

jonbcampos-alto commented 4 years ago

watching this one too. updated my package and now not sure about this

childish-sambino commented 4 years ago

Considering this a duplicate of https://github.com/sendgrid/sendgrid-nodejs/issues/1056, unless I'm mistaken.

1041 introduced in the 6.5.4 release required one of text, html, or content. It was later pointed out that templateId by itself should also be allowed. This was fixed as part #1053 but has yet to be released.

rodrigomf24 commented 4 years ago

@childish-sambino this is still an issue on 6.5.4 the type is defined like this:

type MailDataRequired = MailData & (
  { text: string } | { html: string } | { content: MailContent[] & { 0: MailContent } });

That is making content a required property, is there a reason for that when MailData defines it as a conditional property? I fixed it by passing:

{...mailData, content: undefined}
childish-sambino commented 4 years ago

1041 introduced in the 6.5.4 release required one of text, html, or content.

@rodrigomf24 Does mailData not have one of those?

rodrigomf24 commented 4 years ago

@childish-sambino no just a templateId and dynamicTemplateData

childish-sambino commented 4 years ago

@rodrigomf24

It was later pointed out that templateId by itself should also be allowed. This was fixed as part #1053 but has yet to be released.

saveraa commented 4 years ago

Is this fix released?

saveraa commented 4 years ago

I just got version 7.0.0 and I still see this error

TSError: ⨯ Unable to compile TypeScript: src/email-templates/verification-email-template.ts(15,25): error TS2769: No overload matches this call. Overload 1 of 2, '(data: MailDataRequired, isMultiple?: boolean | undefined, cb?: ((err: Error | ResponseError, result: [ClientResponse, {}]) => void) | undefined): Promise<...>', gave the following error.

saveraa commented 4 years ago

If this has changed in 7.0.0, where is latest documentation for the send api please? Can't find much here. @childish-sambino

Thanks.

childish-sambino commented 4 years ago

Yes, this was released in 6.5.5

isaachinman commented 4 years ago

@childish-sambino What do you think about exporting MailDataRequired from the @sendgrid/mail package as well?

childish-sambino commented 4 years ago

@isaachinman I've no issue with that. If you want to submit a PR I can review it.

isaachinman commented 4 years ago

@childish-sambino I had a look around, and am unfamiliar with the module approach of export = being taken here. I'm not clear on how this can be expanded without introducing breaking changes.

Happy to help if you can point me in the right direction.

childish-sambino commented 4 years ago

Think this line can just be updated: https://github.com/sendgrid/sendgrid-nodejs/blob/master/packages/mail/src/mail.d.ts#L37

Like this maybe:

declare const mail: MailService & { MailService: typeof MailService, MailDataRequired: typeof MailDataRequired };
isaachinman commented 4 years ago

No, that won't work as MailDataRequired is already a type. The use of declare const makes things slightly complicated.

childish-sambino commented 4 years ago

Then just , MailDataRequired: MailDataRequired };?

isaachinman commented 4 years ago

No, what I am saying is that literally adds a MailDataRequired attribute to the default export. If you tried out your latest suggestion, you'd get a refers to a value, but is being used as a type error.

I haven't seen any other packages managing types with this sort of module approach, so I can't really offer you any suggestions. You need a way of supporting named exports.

Normally I'd expect to see something like this:

export default mail
export { MailDataRequired }
childish-sambino commented 4 years ago

@isaachinman Think this (hack) should fix it: https://github.com/sendgrid/sendgrid-nodejs/pull/1102

yenicelik commented 3 years ago

still having the same issue, and proper workaround?

mkmrcodes commented 2 years ago

Still same issue with version 7.6.0

peterj commented 2 years ago

Not perfect, but it allows you to go past the error:

// @ts-expect-error: https://github.com/sendgrid/sendgrid-nodejs/issues/1057
await sgMail.sendMultiple(msg);
HubbardJacob commented 2 years ago

Was also having this issue with 7.6.0. Had to force compiler to ignore it.

alzayatn commented 2 years ago

Also having the same issue with version 7.6.1

For reference I was digging around and I see:

send(data: MailDataRequired | MailDataRequired[], isMultiple?: boolean, cb?: (err: Error | ResponseError, result: [ClientResponse, {}]) => void): Promise<[ClientResponse, {}]>;

Where MailDataRequired is:

export type MailDataRequired = MailData & (
    { text: string } | { html: string } | { templateId: string } | { content: MailContent[] & { 0: MailContent } });

but conflicts with MailData where content is optional and the types mismatch:


export interface MailData {
  ...
  content?: MailContent[],
  ...
}

Can this be resolved? I'd rather not have to ignore it...

childish-sambino commented 2 years ago

This issue has been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog.

mgara commented 2 years ago

Hello, any workaround for this ? thanks !