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

Node.js builtin FormData issue #380

Merged
merged 1 commit into from
Aug 16, 2023
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
2 changes: 1 addition & 1 deletion dist/Classes/common/FormDataBuilder.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ declare class FormDataBuilder {
private FormDataConstructor;
constructor(FormDataConstructor: InputFormData);
createFormData(data: any): NodeFormData | FormData;
private isNodeFormData;
private isFormDataPackage;
private getAttachmentOptions;
private addMimeDataToFD;
private addFilesToFD;
Expand Down
73 changes: 61 additions & 12 deletions dist/mailgun.node.js

Large diffs are not rendered by default.

73 changes: 61 additions & 12 deletions dist/mailgun.web.js

Large diffs are not rendered by default.

67 changes: 55 additions & 12 deletions lib/Classes/common/FormDataBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as NodeFormData from 'form-data';
import { InputFormData } from '../../Types/Common';
import { APIErrorOptions, InputFormData } from '../../Types/Common';
import APIError from './Error';

class FormDataBuilder {
private FormDataConstructor: InputFormData;
Expand All @@ -7,7 +8,7 @@
this.FormDataConstructor = FormDataConstructor;
}

public createFormData(data: any): NodeFormData | FormData {

Check warning on line 11 in lib/Classes/common/FormDataBuilder.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Unexpected any. Specify a different type
if (!data) {
throw new Error('Please provide data object');
}
Expand All @@ -31,8 +32,8 @@
return formData;
}

private isNodeFormData(formDataInstance: NodeFormData | FormData)
: formDataInstance is NodeFormData {
private isFormDataPackage(formDataInstance: NodeFormData | FormData)
: boolean {
return (<NodeFormData>formDataInstance).getHeaders !== undefined;
}

Expand Down Expand Up @@ -63,24 +64,47 @@
data: Buffer | Blob | string,
formDataInstance: NodeFormData | FormData
): void {
if (Buffer.isBuffer(data) || typeof data === 'string') {
if (typeof data === 'string') { // if string only two parameters should be used.
formDataInstance.append(key, data as string);
return;
}

if (this.isFormDataPackage(formDataInstance)) { // form-data package is used
const nodeFormData = formDataInstance as NodeFormData;
const preparedData = typeof data === 'string' ? Buffer.from(data) : data;
nodeFormData.append(key, preparedData, { filename: 'MimeMessage' });
} else {
const browserFormData = formDataInstance as FormData;
browserFormData.append(key, data, 'MimeMessage');
nodeFormData.append(key, data, { filename: 'MimeMessage' });
return;
}

if (typeof Blob !== undefined) { // either node > 18 or browser
const browserFormData = formDataInstance as FormData; // Browser compliant FormData
if (data instanceof Blob) {
browserFormData.append(key, data, 'MimeMessage');
return;
}
if (typeof Buffer !== 'undefined') { // node environment
if (Buffer.isBuffer(data)) {
const blobInstance = new Blob([data]);
browserFormData.append(key, blobInstance, 'MimeMessage');
return;
}
}
}

throw new APIError({
status: 400,
statusText: `Unknown data type for ${key} property`,
body: 'The mime data should have type of Buffer, String or Blob'
} as APIErrorOptions);
}

private addFilesToFD(
propertyName: string,
value: any,

Check warning on line 102 in lib/Classes/common/FormDataBuilder.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Unexpected any. Specify a different type
formDataInstance: NodeFormData | FormData
): void {
const appendFileToFD = (
originalKey: string,
obj: any,

Check warning on line 107 in lib/Classes/common/FormDataBuilder.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Unexpected any. Specify a different type
formData: NodeFormData | FormData
): void => {
const key = originalKey === 'multipleValidationFile' ? 'file' : originalKey;
Expand All @@ -88,11 +112,30 @@
const objData = isStreamData ? obj : obj.data;
// getAttachmentOptions should be called with obj parameter to prevent loosing filename
const options = this.getAttachmentOptions(obj);
if (this.isNodeFormData(formData)) {
formData.append(key, objData, options);
if (typeof objData === 'string') {
formData.append(key, objData as string);
return;
}

if (this.isFormDataPackage(formData)) {
const fd = formData as NodeFormData;
fd.append(key, objData, options);
return;
}
formData.append(key, objData, options.filename);

if (typeof Blob !== undefined) { // either node > 18 or browser
const browserFormData = formDataInstance as FormData; // Browser compliant FormData
if (objData instanceof Blob) {
browserFormData.append(key, objData, options.filename);
return;
}
if (typeof Buffer !== 'undefined') { // node environment
if (Buffer.isBuffer(objData)) {
const blobInstance = new Blob([objData]);
browserFormData.append(key, blobInstance, options.filename);
}
}
}
};

if (Array.isArray(value)) {
Expand All @@ -104,17 +147,17 @@
}
}

private isStream(data: any) {

Check warning on line 150 in lib/Classes/common/FormDataBuilder.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Unexpected any. Specify a different type
return typeof data === 'object' && typeof data.pipe === 'function';
}

private addCommonPropertyToFD(
key: string,
value: any,

Check warning on line 156 in lib/Classes/common/FormDataBuilder.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Unexpected any. Specify a different type
formDataAcc: NodeFormData | FormData
): void {
if (Array.isArray(value)) {
value.forEach(function (item: any) {

Check warning on line 160 in lib/Classes/common/FormDataBuilder.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Unexpected any. Specify a different type
formDataAcc.append(key, item);
});
} else if (value != null) {
Expand Down
Loading