Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client): Fix typescript issue in the Attachment model #1402

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/use-cases/attachments.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const msg = {
filename: 'some-attachment.txt',
type: 'plain/text',
disposition: 'attachment',
content_id: 'mytext'
contentId: 'mytext'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per sendgrid open api specification, it is content_id

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ThecontentId property is used internally in the Attachment class. You are correct that the API expects the content_id property, and that is exactly what we send. The toJSON method of the Mail class handles this conversion, transforming the internal camelCase structure into the snake_case format expected by the API.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sbansla could you take a look, please?

},
],
};
Expand All @@ -43,7 +43,7 @@ fs.readFile(('Document.pdf'), (err, data) => {
filename: 'some-attachment.pdf',
type: 'application/pdf',
disposition: 'attachment',
content_id: 'mytext',
contentId: 'mytext',
},
],
};
Expand Down Expand Up @@ -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',
},
],
};
Expand Down
2 changes: 2 additions & 0 deletions packages/helpers/classes/attachment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
}
13 changes: 13 additions & 0 deletions packages/helpers/classes/attachment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/helpers/classes/mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}
}

Expand Down
17 changes: 17 additions & 0 deletions packages/helpers/classes/mail.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '[email protected]',
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);
});
});
});

Expand Down
Loading