mailgun.js 4.0.0 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,4 +4,4 @@
4
4
 
5
5
  /*! https://mths.be/punycode v1.3.2 by @mathias */
6
6
 
7
- /*! mailgun.js v3.7.3 */
7
+ /*! mailgun.js v4.0.0 */
package/lib/request.ts CHANGED
@@ -50,7 +50,7 @@ class Request {
50
50
  private url: string;
51
51
  private timeout: number;
52
52
  private headers: any;
53
- private formData: InputFormData;
53
+ private FormDataConstructor: InputFormData;
54
54
 
55
55
  constructor(options: RequestOptions, formData: InputFormData) {
56
56
  this.username = options.username;
@@ -58,7 +58,7 @@ class Request {
58
58
  this.url = options.url as string;
59
59
  this.timeout = options.timeout;
60
60
  this.headers = options.headers || {};
61
- this.formData = formData;
61
+ this.FormDataConstructor = formData;
62
62
  }
63
63
 
64
64
  async request(method: string, url: string, inputOptions?: any): Promise<APIResponse> {
@@ -166,49 +166,82 @@ class Request {
166
166
  }
167
167
 
168
168
  createFormData(data: any): NodeFormData | FormData {
169
+ const formData: NodeFormData | FormData = Object.keys(data)
170
+ .filter(function (key) { return data[key]; })
171
+ .reduce((formDataAcc: NodeFormData | FormData, key) => {
172
+ const fileKeys = ['attachment', 'inline', 'file'];
173
+ if (fileKeys.includes(key)) {
174
+ this.addFilesToFD(key, data[key], formDataAcc);
175
+ return formDataAcc;
176
+ }
177
+
178
+ if (key === 'message') { // mime message
179
+ this.addMimeDataToFD(key, data[key], formDataAcc);
180
+ return formDataAcc;
181
+ }
182
+
183
+ this.addCommonPropertyToFD(key, data[key], formDataAcc);
184
+ return formDataAcc;
185
+ }, new this.FormDataConstructor());
186
+ return formData;
187
+ }
188
+
189
+ private addMimeDataToFD(
190
+ key: string,
191
+ data: Buffer | Blob,
192
+ formDataInstance: NodeFormData | FormData
193
+ ): void {
194
+ if (isNodeFormData(formDataInstance)) {
195
+ if (Buffer.isBuffer(data)) {
196
+ formDataInstance.append(key, data, { filename: 'MimeMessage' });
197
+ }
198
+ } else {
199
+ formDataInstance.append(key, data as Blob, 'MimeMessage');
200
+ }
201
+ }
202
+
203
+ private addFilesToFD(
204
+ propertyName: string,
205
+ value: any,
206
+ formDataInstance: NodeFormData | FormData
207
+ ): void {
169
208
  const appendFileToFD = (
170
209
  key: string,
171
210
  obj: any,
172
- formDataInstance: NodeFormData | FormData
211
+ formData: NodeFormData | FormData
173
212
  ): void => {
174
213
  const isStreamData = isStream(obj);
175
214
  const objData = isStreamData ? obj : obj.data;
215
+ // getAttachmentOptions should be called with obj parameter to prevent loosing filename
176
216
  const options = getAttachmentOptions(obj);
177
- if (isNodeFormData(formDataInstance)) {
178
- formDataInstance.append(key, objData, options);
217
+ if (isNodeFormData(formData)) {
218
+ formData.append(key, objData, options);
179
219
  return;
180
220
  }
181
- formDataInstance.append(key, objData, options.filename);
221
+ formData.append(key, objData, options.filename);
182
222
  };
183
223
 
184
- const formData: NodeFormData | FormData = Object.keys(data)
185
- .filter(function (key) { return data[key]; })
186
- .reduce((formDataAcc: NodeFormData | FormData, key) => {
187
- if (key === 'attachment' || key === 'inline' || key === 'file') {
188
- const obj = data[key];
189
-
190
- if (Array.isArray(obj)) {
191
- obj.forEach(function (item) {
192
- appendFileToFD(key, item, formDataAcc);
193
- });
194
- } else {
195
- appendFileToFD(key, obj, formDataAcc);
196
- }
197
-
198
- return formDataAcc;
199
- }
224
+ if (Array.isArray(value)) {
225
+ value.forEach(function (item) {
226
+ appendFileToFD(propertyName, item, formDataInstance);
227
+ });
228
+ } else {
229
+ appendFileToFD(propertyName, value, formDataInstance);
230
+ }
231
+ }
200
232
 
201
- if (Array.isArray(data[key])) {
202
- data[key].forEach(function (item: any) {
203
- formDataAcc.append(key, item);
204
- });
205
- } else if (data[key] != null) {
206
- formDataAcc.append(key, data[key]);
207
- }
208
- return formDataAcc;
209
- // eslint-disable-next-line new-cap
210
- }, new this.formData());
211
- return formData;
233
+ private addCommonPropertyToFD(
234
+ key: string,
235
+ value: any,
236
+ formDataAcc: NodeFormData | FormData
237
+ ): void {
238
+ if (Array.isArray(value)) {
239
+ value.forEach(function (item: any) {
240
+ formDataAcc.append(key, item);
241
+ });
242
+ } else if (value != null) {
243
+ formDataAcc.append(key, value);
244
+ }
212
245
  }
213
246
 
214
247
  put(url: string, data: any, options?: any): Promise<APIResponse> {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mailgun.js",
3
- "version": "4.0.0",
3
+ "version": "4.0.1",
4
4
  "main": "dist/mailgun.node.js",
5
5
  "browser": "dist/mailgun.web.js",
6
6
  "types": "dist/index.d.ts",
@@ -105,7 +105,8 @@
105
105
  "commitUrlFormat": "https://github.com/mailgun/mailgun.js/commits/{{hash}}",
106
106
  "compareUrlFormat": "https://github.com/mailgun/mailgun.js/compare/{{previousTag}}...{{currentTag}}",
107
107
  "scripts": {
108
- "prerelease": "npm test && webpack --config ./webpack/webpack.release.config.js --progress --color && git add -A dist"
108
+ "prerelease": "npm test && webpack --config ./webpack/webpack.release.config.js --progress --color && git add -A dist",
109
+ "posttag": "git push && git push --tags && rm -rf build"
109
110
  }
110
111
  }
111
112
  }