Skip to content

Commit

Permalink
Merge pull request #406 from mailgun/fix-source-on-is-not-function-fo…
Browse files Browse the repository at this point in the history
…r-t-variables

fix: Source.on is not a function for t:variables
  • Loading branch information
olexandr-mazepa authored Feb 16, 2024
2 parents bea29c0 + e647143 commit 0486635
Show file tree
Hide file tree
Showing 6 changed files with 24,678 additions and 7 deletions.
14 changes: 13 additions & 1 deletion dist/Types/Messages/Messages.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ export type CustomFile = {
[key: string]: unknown;
};
export type MessageAttachment = CustomFile | CustomFile[] | File | File[] | string | CustomFileData | CustomFileData[];
export type FormDataInputValue = MimeMessage | CustomFileData | string | string[] | boolean | MessageAttachment | undefined | number;
export type FormDataInputValue = MimeMessage | CustomFileData | string | string[] | boolean | MessageAttachment | undefined | number | JsonObject;
export type JsonPrimitive = string | number | boolean | null;
export type JsonArray = Json[];
export type JsonObject = {
[key: string]: Json;
};
export type JsonComposite = JsonArray | JsonObject;
export type Json = JsonPrimitive | JsonComposite;
export type MailgunMessageContent = AtLeastOneKeyPresent<{
/**
* Body of the message. (text version)
Expand Down Expand Up @@ -85,6 +92,11 @@ export type MailgunMessageData = MailgunMessageContent & {
* in the text part of the message in case of template sending
*/
't:text'?: boolean | 'yes' | 'no';
/**
* A valid JSON-encoded dictionary used as the input for template variable expansion.
* See [Templates](https://documentation.mailgun.com/en/latest/api-templates.html#api-templates) for more information.
*/
't:variables'?: string | JsonObject;
/**
* Tag string. See [Tagging](https://documentation.mailgun.com/en/latest/user_manual.html#tagging) for more information.
*/
Expand Down
14,697 changes: 14,694 additions & 3 deletions dist/mailgun.node.js

Large diffs are not rendered by default.

9,940 changes: 9,937 additions & 3 deletions dist/mailgun.web.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions lib/Classes/common/FormDataBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ class FormDataBuilder {
): void {
const addValueBasedOnFD = (fdKey: string, fdValue: FormDataInputValue): void => {
if (this.isFormDataPackage(formDataAcc)) {
if (typeof fdValue === 'object') {
// eslint-disable-next-line no-console
console.warn('The received value is an object. \n'
+ '"JSON.Stringify" will be used to avoid TypeError \n'
+ 'To remove this warning: \n'
+ 'Consider switching to built-in FormData or converting the value on your own.\n');
return formDataAcc.append(fdKey, JSON.stringify(fdValue));
}
return formDataAcc.append(fdKey, fdValue);
}
if (typeof fdValue === 'string') {
Expand Down
14 changes: 14 additions & 0 deletions lib/Types/Messages/Messages.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-use-before-define */
/**
* Ensures the object has least one key present and not undefined
*
Expand Down Expand Up @@ -41,6 +42,13 @@ export type FormDataInputValue =
| MessageAttachment
| undefined
| number // doc says it should be auto-converted https://developer.mozilla.org/en-US/docs/Web/API/FormData/append
| JsonObject

export type JsonPrimitive = string | number | boolean | null;
export type JsonArray = Json[];
export type JsonObject = { [key: string]: Json };
export type JsonComposite = JsonArray | JsonObject;
export type Json = JsonPrimitive | JsonComposite;

export type MailgunMessageContent = AtLeastOneKeyPresent<{
/**
Expand Down Expand Up @@ -120,6 +128,12 @@ export type MailgunMessageData = MailgunMessageContent & {
*/
't:text'?: boolean | 'yes' | 'no';

/**
* A valid JSON-encoded dictionary used as the input for template variable expansion.
* See [Templates](https://documentation.mailgun.com/en/latest/api-templates.html#api-templates) for more information.
*/
't:variables'?: string | JsonObject;

/**
* Tag string. See [Tagging](https://documentation.mailgun.com/en/latest/user_manual.html#tagging) for more information.
*/
Expand Down
12 changes: 12 additions & 0 deletions test/formDataBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ describe('FormDataBuilder', function () {
expect(data).include('Content-Disposition: form-data; name="attachment"; filename="file"');
expect(data).include('Content-Type: application/octet-stream');
});

it('Converts object to String value', async () => {
// Prevents TypeError: source.on is not a function for form-data package
const formDataWithValue = builder.createFormData({
't:variables': {
testProp: 'testValue'
}
}) as NodeFormData;
const data = formDataWithValue.getBuffer().toString();
expect(data).include('Content-Disposition: form-data; name="t:variables"');
expect(data).include('{"testProp":"testValue"}');
});
});

if (global.FormData) {
Expand Down

0 comments on commit 0486635

Please sign in to comment.