@goast/typescript 0.5.1 → 0.5.2

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.
@@ -1,5 +1,6 @@
1
1
  // @ts-check
2
2
 
3
+ import { FormData } from 'https://jslib.k6.io/formdata/0.0.2/index.js';
3
4
  import http from 'k6/http';
4
5
 
5
6
  /**
@@ -322,6 +323,13 @@ export class RequestBuilder {
322
323
  }
323
324
  }
324
325
  this._bodyContent = pairs.map((p) => `${encodeURIComponent(p[0])}=${encodeURIComponent(p[1])}`).join('&');
326
+ } else if (this._bodyContentType === 'multipart/form-data' && value !== null && typeof value === 'object') {
327
+ const fd = new FormData();
328
+ for (const key in value) {
329
+ fd.append(key, this.formDataValue(value[key]));
330
+ }
331
+ this._bodyContent = fd.body();
332
+ this._bodyContentType = 'multipart/form-data; boundary=' + fd.boundary;
325
333
  } else if (this._bodyContentType.indexOf('json') !== -1 && value !== null && typeof value === 'object') {
326
334
  this._bodyContent = JSON.stringify(value);
327
335
  } else {
@@ -339,10 +347,27 @@ export class RequestBuilder {
339
347
  if (value === null || value === undefined) {
340
348
  return null;
341
349
  }
342
- if (typeof value === 'object') {
343
- return JSON.stringify(value);
350
+ if (this.isFile(value)) {
351
+ return value;
344
352
  }
345
- return String(value);
353
+ return { data: JSON.stringify(value), content_type: 'application/json' };
354
+ }
355
+
356
+ /**
357
+ * @private
358
+ * @param {*} value
359
+ * @returns {boolean}
360
+ */
361
+ isFile(value) {
362
+ return (
363
+ typeof value === 'object' &&
364
+ value !== null &&
365
+ 'data' in value &&
366
+ (typeof value.data === 'string' || value.data instanceof ArrayBuffer) &&
367
+ (!('filename' in value) || typeof value.filename === 'string') &&
368
+ (!('content_type' in value) || typeof value.content_type === 'string') &&
369
+ Object.keys(value).every((key) => key === 'data' || key === 'filename' || key === 'content_type')
370
+ );
346
371
  }
347
372
 
348
373
  /**
@@ -403,7 +428,7 @@ export class RequestBuilder {
403
428
  }
404
429
 
405
430
  // Request content headers
406
- if (this._bodyContentType && this._bodyContentType !== 'multipart/form-data') {
431
+ if (this._bodyContentType) {
407
432
  httpHeaders['Content-Type'] = this._bodyContentType;
408
433
  }
409
434
 
@@ -1,5 +1,7 @@
1
1
  // @deno-types="npm:@types/k6/http"
2
2
  import http, { Params, Response } from 'k6/http';
3
+ // @ts-ignore
4
+ import { FormData } from 'https://jslib.k6.io/formdata/0.0.2/index.js';
3
5
 
4
6
  /**
5
7
  * Defines the options for appending a parameter
@@ -265,6 +267,13 @@ export class RequestBuilder {
265
267
  }
266
268
  }
267
269
  this._bodyContent = pairs.map((p) => `${encodeURIComponent(p[0])}=${encodeURIComponent(p[1])}`).join('&');
270
+ } else if (this._bodyContentType === 'multipart/form-data' && value !== null && typeof value === 'object') {
271
+ const fd = new FormData();
272
+ for (const key in value) {
273
+ fd.append(key, this.formDataValue(value[key]));
274
+ }
275
+ this._bodyContent = fd.body();
276
+ this._bodyContentType = 'multipart/form-data; boundary=' + fd.boundary;
268
277
  } else if (this._bodyContentType.indexOf('json') !== -1 && value !== null && typeof value === 'object') {
269
278
  this._bodyContent = JSON.stringify(value);
270
279
  } else {
@@ -277,10 +286,22 @@ export class RequestBuilder {
277
286
  if (value === null || value === undefined) {
278
287
  return null;
279
288
  }
280
- if (typeof value === 'object') {
281
- return JSON.stringify(value);
289
+ if (this.isFile(value)) {
290
+ return value;
282
291
  }
283
- return String(value);
292
+ return { data: JSON.stringify(value), content_type: 'application/json' };
293
+ }
294
+
295
+ private isFile(value: any): value is http.FileData {
296
+ return (
297
+ typeof value === 'object' &&
298
+ value !== null &&
299
+ 'data' in value &&
300
+ (typeof value.data === 'string' || value.data instanceof ArrayBuffer) &&
301
+ (!('filename' in value) || typeof value.filename === 'string') &&
302
+ (!('content_type' in value) || typeof value.content_type === 'string') &&
303
+ Object.keys(value).every((key) => key === 'data' || key === 'filename' || key === 'content_type')
304
+ );
284
305
  }
285
306
 
286
307
  /**
@@ -325,7 +346,7 @@ export class RequestBuilder {
325
346
  }
326
347
 
327
348
  // Request content headers
328
- if (this._bodyContentType && this._bodyContentType !== 'multipart/form-data') {
349
+ if (this._bodyContentType) {
329
350
  httpHeaders['Content-Type'] = this._bodyContentType;
330
351
  }
331
352
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goast/typescript",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "description": "Provides gOAst generators for generating TypeScript code from OpenAPI specifications.",
5
5
  "author": {
6
6
  "name": "Marc Schmidt (MaSch0212)",