diff --git a/docs/use-cases/attachments.md b/docs/use-cases/attachments.md index baeceec32..5eb67fe2e 100644 --- a/docs/use-cases/attachments.md +++ b/docs/use-cases/attachments.md @@ -16,7 +16,7 @@ const msg = { filename: 'some-attachment.txt', type: 'plain/text', disposition: 'attachment', - content_id: 'mytext' + contentId: 'mytext' }, ], }; @@ -43,7 +43,7 @@ fs.readFile(('Document.pdf'), (err, data) => { filename: 'some-attachment.pdf', type: 'application/pdf', disposition: 'attachment', - content_id: 'mytext', + contentId: 'mytext', }, ], }; @@ -72,7 +72,7 @@ request(fileURl, { encoding: null }, (err, res, body) => { filename: 'some-attachment.pdf', type: 'application/pdf', disposition: 'attachment', - content_id: 'mytext', + contentId: 'mytext', }, ], }; diff --git a/packages/helpers/classes/attachment.d.ts b/packages/helpers/classes/attachment.d.ts index fba0f8be2..f81b022c1 100644 --- a/packages/helpers/classes/attachment.d.ts +++ b/packages/helpers/classes/attachment.d.ts @@ -30,4 +30,6 @@ export default class Attachment implements AttachmentData { setDisposition(disposition: string): void; setContentId(contentId: string): void; toJSON(): AttachmentJSON; + static create(data: AttachmentData | Attachment): Attachment; + static create(data: AttachmentData[] | Attachment[]): Attachment[]; } diff --git a/packages/helpers/classes/attachment.js b/packages/helpers/classes/attachment.js index 46beed47b..a50cf0f2c 100644 --- a/packages/helpers/classes/attachment.js +++ b/packages/helpers/classes/attachment.js @@ -181,6 +181,19 @@ class Attachment { //Return return toSnakeCase(json); } + + static create(data) { + if(Array.isArray(data)) { + return data.filter(Boolean).map(item => this.create(item)); + } + //Already instance of Attachment class? + if (data instanceof Attachment) { + return data; + } + + //Create instance + return new Attachment(data); + } } //Export class diff --git a/packages/helpers/classes/mail.js b/packages/helpers/classes/mail.js index b7dce14a2..221a5aa28 100644 --- a/packages/helpers/classes/mail.js +++ b/packages/helpers/classes/mail.js @@ -5,6 +5,7 @@ */ const EmailAddress = require('./email-address'); const Personalization = require('./personalization'); +const Attachment = require('./attachment'); const toCamelCase = require('../helpers/to-camel-case'); const toSnakeCase = require('../helpers/to-snake-case'); const deepClone = require('../helpers/deep-clone'); @@ -388,7 +389,7 @@ class Mail { if (!attachments.every(attachment => !attachment.disposition || typeof attachment.disposition === 'string')) { throw new Error('Expected the attachment\'s `disposition` field to be a string'); } - this.attachments = attachments; + this.attachments = Attachment.create(attachments); } } diff --git a/packages/helpers/classes/mail.spec.js b/packages/helpers/classes/mail.spec.js index 41aec6be9..83695553b 100644 --- a/packages/helpers/classes/mail.spec.js +++ b/packages/helpers/classes/mail.spec.js @@ -185,6 +185,23 @@ describe('Mail', function() { }], })).to.throw('filename'); }); + + it('properly handles attachment contentId', () => { + const contentId = 'test-content-id'; + const mail = new Mail({ + to: 'recipient@example.org', + attachments: [{ + disposition: 'inline', + content: 'test-content', + filename: 'name-that-file', + type: 'file-type', + contentId, + }], + }); + const mainJSON = mail.toJSON(); + const firstAttachment = mainJSON.attachments[0]; + expect(firstAttachment.content_id).to.equal(contentId); + }); }); });