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: Source.on is not a function for t:variables #406

Merged
Merged
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
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 @@
| 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 @@ -107,7 +115,7 @@
*
* You can post multiple `inline` values.
*/
inline?: any;

Check warning on line 118 in lib/Types/Messages/Messages.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

/**
* Use this parameter to send a message to specific version of a template
Expand All @@ -120,6 +128,12 @@
*/
'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
Loading