@readme/oas-to-har 17.0.8 → 17.1.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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,24 @@
1
+ ## <small>17.1.2 (2022-07-27)</small>
2
+
3
+ * fix: issue with filenames in data URLs not retaining their casing ([e8a7edb](https://github.com/readmeio/oas-to-har/commit/e8a7edb))
4
+
5
+
6
+
7
+ ## <small>17.1.1 (2022-07-27)</small>
8
+
9
+ * fix: support for filenames that have underscores in them (#110) ([98a3972](https://github.com/readmeio/oas-to-har/commit/98a3972)), closes [#110](https://github.com/readmeio/oas-to-har/issues/110)
10
+
11
+
12
+
13
+ ## 17.1.0 (2022-07-26)
14
+
15
+ * docs: fixing weird code formatting in the readme ([85e80a8](https://github.com/readmeio/oas-to-har/commit/85e80a8))
16
+ * chore(deps): bump terser from 4.8.0 to 4.8.1 (#108) ([b0ffb8e](https://github.com/readmeio/oas-to-har/commit/b0ffb8e)), closes [#108](https://github.com/readmeio/oas-to-har/issues/108)
17
+ * chore(deps): bumping out of date deps ([c667505](https://github.com/readmeio/oas-to-har/commit/c667505))
18
+ * fix: keep arrays of binary files as separate params in postData (#109) ([545c0eb](https://github.com/readmeio/oas-to-har/commit/545c0eb)), closes [#109](https://github.com/readmeio/oas-to-har/issues/109)
19
+
20
+
21
+
1
22
  ## <small>17.0.8 (2022-07-07)</small>
2
23
 
3
24
  * fix: weird mess with instanceof checks not always working (#107) ([80c965e](https://github.com/readmeio/oas-to-har/commit/80c965e)), closes [#107](https://github.com/readmeio/oas-to-har/issues/107)
package/README.md CHANGED
@@ -26,18 +26,18 @@ console.log(oasToHar(spec, spec.operation('/pets', 'post')));
26
26
 
27
27
  ```json
28
28
  {
29
- log: {
30
- entries: [
29
+ "log": {
30
+ "entries": [
31
31
  {
32
- request: {
33
- cookies: [],
34
- headers: [],
35
- headersSize: 0,
36
- queryString: [],
37
- bodySize: 0,
38
- method: 'POST',
39
- url: 'http://petstore.swagger.io/v2/pets',
40
- httpVersion: 'HTTP/1.1'
32
+ "request": {
33
+ "cookies": [],
34
+ "headers": [],
35
+ "headersSize": 0,
36
+ "queryString": [],
37
+ "bodySize": 0,
38
+ "method": "POST",
39
+ "url": "http://petstore.swagger.io/v2/pets",
40
+ "httpVersion": "HTTP/1.1"
41
41
  }
42
42
  }
43
43
  ]
@@ -47,9 +47,9 @@ console.log(oasToHar(spec, spec.operation('/pets', 'post')));
47
47
 
48
48
  ### `oasToHar(oas, operationSchema, values, auth, opts) => Object`
49
49
 
50
- - `oas` *{Oas}*: Instance of our [oas/tooling](https://npm.im/oas) class.
51
- - `operationSchema` *{Object\|Operation}*: Can either be an object with `path` and `method` properties (that exist in the supplied OAS), or an instance of our `Operation` class from [oas/tooling](https://npm.im/oas) - accessed through `new Oas(spec).operation(path, method)`.
52
- - `values` *{Object}*: A object of payload data, with key-value data therein, that should be used to construct the request. Available data you can define here:
50
+ - `oas` _{Oas}_: Instance of our [oas/tooling](https://npm.im/oas) class.
51
+ - `operationSchema` _{Object\|Operation}_: Can either be an object with `path` and `method` properties (that exist in the supplied OAS), or an instance of our `Operation` class from [oas/tooling](https://npm.im/oas) - accessed through `new Oas(spec).operation(path, method)`.
52
+ - `values` _{Object}_: A object of payload data, with key-value data therein, that should be used to construct the request. Available data you can define here:
53
53
  - `path`
54
54
  - `query`
55
55
  - `body`
@@ -57,5 +57,5 @@ console.log(oasToHar(spec, spec.operation('/pets', 'post')));
57
57
  - `formData`
58
58
  - `header`
59
59
  - `server` &mdash; If the supplied OAS has multiple severs or server variables you can use this to set which server and variables to use. Shape of it should be: `{ selected: Integer, variables: { ...key-values }}`. `selected` should coorespond to index of the `servers` array in your OAS.
60
- - `auth` *{Object}*: Authentication information for the request.
61
- - `opts.proxyUrl` *{Boolean}*: Boolean to toggle if composed HAR objects should have their `url` be sent through our CORS-friendly proxy. Defaults to `false`.
60
+ - `auth` _{Object}_: Authentication information for the request.
61
+ - `opts.proxyUrl` _{Boolean}_: Boolean to toggle if composed HAR objects should have their `url` be sent through our CORS-friendly proxy. Defaults to `false`.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@readme/oas-to-har",
3
3
  "description": "Utility to transform an OAS operation into a HAR representation",
4
- "version": "17.0.8",
4
+ "version": "17.1.2",
5
5
  "main": "src/index.js",
6
6
  "author": "Jon Ursenbach <jon@ursenba.ch>",
7
7
  "license": "ISC",
@@ -24,9 +24,9 @@
24
24
  "test": "nyc mocha"
25
25
  },
26
26
  "dependencies": {
27
+ "@readme/data-urls": "^1.0.1",
27
28
  "@readme/oas-extensions": "^14.3.0",
28
29
  "oas": "^18.3.1",
29
- "parse-data-url": "^4.0.1",
30
30
  "qs": "^6.10.5",
31
31
  "remove-undefined-objects": "^2.0.0"
32
32
  },
@@ -35,10 +35,10 @@
35
35
  "@commitlint/config-conventional": "^17.0.2",
36
36
  "@jsdevtools/host-environment": "^2.1.2",
37
37
  "@jsdevtools/karma-config": "^3.1.7",
38
- "@readme/eslint-config": "^8.7.6",
39
- "@readme/oas-examples": "^5.3.0",
38
+ "@readme/eslint-config": "^9.0.0",
39
+ "@readme/oas-examples": "^5.4.1",
40
40
  "chai": "^4.3.6",
41
- "eslint": "^8.17.0",
41
+ "eslint": "^8.20.0",
42
42
  "har-validator": "^5.1.5",
43
43
  "husky": "^8.0.1",
44
44
  "mocha": "^10.0.0",
package/src/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  const extensions = require('@readme/oas-extensions');
2
2
  const { Operation, utils } = require('oas');
3
- const parseDataUrl = require('parse-data-url');
3
+ const { parse: parseDataUrl } = require('@readme/data-urls');
4
4
  const { default: removeUndefinedObjects } = require('remove-undefined-objects');
5
5
 
6
6
  const configureSecurity = require('./lib/configure-security');
@@ -34,6 +34,13 @@ function formatter(values, param, type, onlyIfExists) {
34
34
  // Handle file uploads. Specifically arrays of file uploads which need to be formatted very
35
35
  // specifically.
36
36
  if (param.schema && param.schema.type === 'array' && param.schema.items && param.schema.items.format === 'binary') {
37
+ if (Array.isArray(value)) {
38
+ // If this is array of binary data then we shouldn't do anything because we'll prepare them
39
+ // separately in the HAR in order to preserve `fileName` and `contentType` data within
40
+ // `postData.params`. If we don't then the HAR we generate for this data will be invalid.
41
+ return value;
42
+ }
43
+
37
44
  return JSON.stringify(value);
38
45
  }
39
46
 
@@ -353,12 +360,31 @@ module.exports = (
353
360
  har.postData.mimeType = 'multipart/form-data';
354
361
  har.postData.params = [];
355
362
 
356
- // Discover all `{ type: string, format: binary }` properties the schema. If there are
357
- // any, then that means that we're dealing with a `multipart/form-data` request and
358
- // need to treat the payload as `postData.params`.
359
- const binaryTypes = Object.keys(requestBody.schema.properties).filter(
360
- key => requestBody.schema.properties[key].format === 'binary'
361
- );
363
+ /**
364
+ * Discover all `{ type: string, format: binary }` properties, or arrays containing the
365
+ * same, within the request body. If there are any, then that means that we're dealing
366
+ * with a `multipart/form-data` request and need to treat the payload as
367
+ * `postData.params` and supply filenames and content types for the files (if they're
368
+ * available).
369
+ *
370
+ * @example `{ type: string, format: binary }`
371
+ * @example `{ type: array, items: { type: string, format: binary } }`
372
+ */
373
+ const binaryTypes = Object.keys(requestBody.schema.properties).filter(key => {
374
+ if (requestBody.schema.properties[key].format === 'binary') {
375
+ return true;
376
+ } else if (
377
+ requestBody.schema.properties[key].type === 'array' &&
378
+ requestBody.schema.properties[key].items &&
379
+ typeof requestBody.schema.properties[key].items === 'object' &&
380
+ requestBody.schema.properties[key].items !== null &&
381
+ requestBody.schema.properties[key].items.format === 'binary'
382
+ ) {
383
+ return true;
384
+ }
385
+
386
+ return false;
387
+ });
362
388
 
363
389
  if (cleanBody !== undefined) {
364
390
  const multipartParams = multipartBodyToFormatterParams(
@@ -369,25 +395,30 @@ module.exports = (
369
395
  Object.keys(cleanBody).forEach(name => {
370
396
  const param = multipartParams.find(multipartParam => multipartParam.name === name);
371
397
 
372
- const value = formatter(formData, param, 'body', true);
373
-
374
398
  // If we're dealing with a binary type, and the value is a valid data URL we should
375
399
  // parse out any available filename and content type to send along with the
376
400
  // parameter to interpreters like `fetch-har` can make sense of it and send a usable
377
401
  // payload.
378
402
  const addtlData = {};
379
403
 
380
- if (binaryTypes.includes(name)) {
381
- const parsed = parseDataUrl(value);
382
- if (parsed) {
383
- addtlData.fileName = 'name' in parsed ? parsed.name : 'unknown';
384
- if ('contentType' in parsed) {
385
- addtlData.contentType = parsed.contentType;
404
+ let value = formatter(formData, param, 'body', true);
405
+ if (!Array.isArray(value)) {
406
+ value = [value];
407
+ }
408
+
409
+ value.forEach(val => {
410
+ if (binaryTypes.includes(name)) {
411
+ const parsed = parseDataUrl(val);
412
+ if (parsed) {
413
+ addtlData.fileName = 'name' in parsed ? parsed.name : 'unknown';
414
+ if ('contentType' in parsed) {
415
+ addtlData.contentType = parsed.contentType;
416
+ }
386
417
  }
387
418
  }
388
- }
389
419
 
390
- appendHarValue(har.postData.params, name, value, addtlData);
420
+ appendHarValue(har.postData.params, name, val, addtlData);
421
+ });
391
422
  });
392
423
  }
393
424
  } else {