@readme/oas-to-har 27.3.3 → 28.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.
package/dist/index.cjs CHANGED
@@ -33,13 +33,126 @@ function set(object, path, value) {
33
33
  // src/lib/style-formatting/index.ts
34
34
  var _qs = require('qs'); var _qs2 = _interopRequireDefault(_qs);
35
35
 
36
+ // src/lib/utils.ts
37
+
38
+
39
+ function hasSchemaType(schema, discriminator) {
40
+ if (Array.isArray(schema.type)) {
41
+ return schema.type.includes(discriminator);
42
+ }
43
+ return schema.type === discriminator;
44
+ }
45
+ function getSafeRequestBody(obj) {
46
+ if ("oneOf" in obj) {
47
+ return getSafeRequestBody(obj.oneOf[0]);
48
+ } else if ("anyOf" in obj) {
49
+ return getSafeRequestBody(obj.anyOf[0]);
50
+ }
51
+ return obj;
52
+ }
53
+ function getSubschemas(schema, opts) {
54
+ let subSchemaDataSize = 0;
55
+ if (opts.parentIsArray) {
56
+ const parentData = get(opts.payload, opts.parentKey || "");
57
+ if (parentData === void 0 || !Array.isArray(parentData)) {
58
+ return false;
59
+ }
60
+ subSchemaDataSize = parentData.length;
61
+ }
62
+ let subschemas = [];
63
+ if (subSchemaDataSize > 0) {
64
+ for (let idx = 0; idx < subSchemaDataSize; idx += 1) {
65
+ subschemas = subschemas.concat(
66
+ Object.entries(schema).map(([key, subschema]) => ({
67
+ key: opts.parentKey ? [opts.parentKey, idx, key].join(".") : key,
68
+ schema: getSafeRequestBody(subschema)
69
+ }))
70
+ );
71
+ }
72
+ } else {
73
+ subschemas = Object.entries(schema).map(([key, subschema]) => ({
74
+ key: opts.parentKey ? [opts.parentKey, key].join(".") : key,
75
+ schema: getSafeRequestBody(subschema)
76
+ }));
77
+ }
78
+ return subschemas;
79
+ }
80
+ function getTypedFormatsInSchema(format, schema, opts) {
81
+ try {
82
+ if (_optionalChain([schema, 'optionalAccess', _6 => _6.format]) === format) {
83
+ if (opts.parentIsArray) {
84
+ const parentData = get(opts.payload, opts.parentKey || "");
85
+ if (parentData !== void 0 && Array.isArray(parentData)) {
86
+ return Object.keys(parentData).map((pdk) => {
87
+ const currentKey = [opts.parentKey, pdk].join(".");
88
+ if (get(opts.payload, currentKey) !== void 0) {
89
+ return currentKey;
90
+ }
91
+ return false;
92
+ }).filter(Boolean);
93
+ }
94
+ } else if (opts.parentKey && get(opts.payload, opts.parentKey) !== void 0) {
95
+ return opts.parentKey;
96
+ } else if (opts.payload !== void 0) {
97
+ return true;
98
+ }
99
+ return false;
100
+ }
101
+ const subschemas = getSubschemas(schema, opts);
102
+ if (!subschemas) {
103
+ return false;
104
+ }
105
+ return subschemas.flatMap(({ key, schema: subschema }) => {
106
+ if ("properties" in subschema) {
107
+ return getTypedFormatsInSchema(format, subschema.properties, { payload: opts.payload, parentKey: key });
108
+ } else if ("items" in subschema) {
109
+ if (_optionalChain([subschema, 'access', _7 => _7.items, 'optionalAccess', _8 => _8.properties])) {
110
+ return getTypedFormatsInSchema(format, subschema.items.properties, {
111
+ payload: opts.payload,
112
+ parentKey: key,
113
+ parentIsArray: true
114
+ });
115
+ }
116
+ return getTypedFormatsInSchema(format, subschema.items, {
117
+ payload: opts.payload,
118
+ parentKey: key,
119
+ parentIsArray: true
120
+ });
121
+ }
122
+ return getTypedFormatsInSchema(format, subschema, { payload: opts.payload, parentKey: key });
123
+ }).filter(Boolean);
124
+ } catch (e) {
125
+ return [];
126
+ }
127
+ }
128
+ function getParameterContentType(param) {
129
+ if (!("content" in param) || typeof param.content !== "object" || !param.content) {
130
+ return null;
131
+ }
132
+ const contentKeys = Object.keys(param.content);
133
+ if (contentKeys.length < 1) {
134
+ return null;
135
+ }
136
+ return _utils.getParameterContentType.call(void 0, contentKeys) || null;
137
+ }
138
+ function getParameterContentSchema(param, contentType) {
139
+ if (!("content" in param) || typeof param.content !== "object" || !param.content) {
140
+ return null;
141
+ }
142
+ const mediaTypeObject = param.content[contentType];
143
+ if (typeof mediaTypeObject === "object" && mediaTypeObject && "schema" in mediaTypeObject && mediaTypeObject.schema) {
144
+ return _types.isRef.call(void 0, mediaTypeObject.schema) ? null : mediaTypeObject.schema;
145
+ }
146
+ return null;
147
+ }
148
+
36
149
  // src/lib/style-formatting/style-serializer.ts
37
150
  var isRfc3986Reserved = (char) => ":/?#[]@!$&'()*+,;=".indexOf(char) > -1;
38
151
  var isRfc3986Unreserved = (char) => /^[a-z0-9\-._~]+$/i.test(char);
39
152
  function isURIEncoded(value) {
40
153
  try {
41
154
  return decodeURIComponent(value) !== value;
42
- } catch (e) {
155
+ } catch (e2) {
43
156
  return false;
44
157
  }
45
158
  }
@@ -349,6 +462,18 @@ function stylizeValue(value, parameter) {
349
462
  if (parameter.in === "header" && shouldNotStyleReservedHeader(parameter)) {
350
463
  return value;
351
464
  }
465
+ if (parameter.content && parameter.in === "query") {
466
+ const contentType = getParameterContentType(parameter);
467
+ if (!contentType) {
468
+ return void 0;
469
+ }
470
+ switch (contentType) {
471
+ case "application/json":
472
+ return encodeURIComponent(JSON.stringify(value));
473
+ default:
474
+ return encodeURIComponent(String(value));
475
+ }
476
+ }
352
477
  let style = parameter.style;
353
478
  if (!style) {
354
479
  if (parameter.in === "query") {
@@ -399,7 +524,7 @@ function handleDeepObject(value, parameter) {
399
524
  });
400
525
  }
401
526
  function handleExplode(value, parameter) {
402
- if (Array.isArray(value) && _optionalChain([parameter, 'access', _6 => _6.schema, 'optionalAccess', _7 => _7.type]) === "array" && parameter.style === "deepObject") {
527
+ if (Array.isArray(value) && _optionalChain([parameter, 'access', _9 => _9.schema, 'optionalAccess', _10 => _10.type]) === "array" && parameter.style === "deepObject") {
403
528
  const newObj = {};
404
529
  const deepObjs = handleDeepObject(value, parameter);
405
530
  deepObjs.forEach((obj) => {
@@ -432,10 +557,10 @@ function shouldExplode(parameter) {
432
557
  return (parameter.explode || parameter.explode !== false && parameter.style === "form" || // style: deepObject && explode: false doesn't exist so explode it always
433
558
  // https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#style-examples
434
559
  parameter.style === "deepObject") && // header and path doesn't explode into separate parameters like query and cookie do
435
- parameter.in !== "header" && parameter.in !== "path";
560
+ parameter.in !== "header" && parameter.in !== "path" && !parameter.content;
436
561
  }
437
562
  function formatStyle(value, parameter) {
438
- if (parameter.style === "deepObject" && (!value || typeof value !== "object" || parameter.explode === false)) {
563
+ if (!parameter.content && parameter.style === "deepObject" && (!value || typeof value !== "object" || parameter.explode === false)) {
439
564
  return void 0;
440
565
  }
441
566
  if (shouldExplode(parameter)) {
@@ -444,97 +569,6 @@ function formatStyle(value, parameter) {
444
569
  return stylizeValue(value, parameter);
445
570
  }
446
571
 
447
- // src/lib/utils.ts
448
- function hasSchemaType(schema, discriminator) {
449
- if (Array.isArray(schema.type)) {
450
- return schema.type.includes(discriminator);
451
- }
452
- return schema.type === discriminator;
453
- }
454
- function getSafeRequestBody(obj) {
455
- if ("oneOf" in obj) {
456
- return getSafeRequestBody(obj.oneOf[0]);
457
- } else if ("anyOf" in obj) {
458
- return getSafeRequestBody(obj.anyOf[0]);
459
- }
460
- return obj;
461
- }
462
- function getSubschemas(schema, opts) {
463
- let subSchemaDataSize = 0;
464
- if (opts.parentIsArray) {
465
- const parentData = get(opts.payload, opts.parentKey || "");
466
- if (parentData === void 0 || !Array.isArray(parentData)) {
467
- return false;
468
- }
469
- subSchemaDataSize = parentData.length;
470
- }
471
- let subschemas = [];
472
- if (subSchemaDataSize > 0) {
473
- for (let idx = 0; idx < subSchemaDataSize; idx += 1) {
474
- subschemas = subschemas.concat(
475
- Object.entries(schema).map(([key, subschema]) => ({
476
- key: opts.parentKey ? [opts.parentKey, idx, key].join(".") : key,
477
- schema: getSafeRequestBody(subschema)
478
- }))
479
- );
480
- }
481
- } else {
482
- subschemas = Object.entries(schema).map(([key, subschema]) => ({
483
- key: opts.parentKey ? [opts.parentKey, key].join(".") : key,
484
- schema: getSafeRequestBody(subschema)
485
- }));
486
- }
487
- return subschemas;
488
- }
489
- function getTypedFormatsInSchema(format, schema, opts) {
490
- try {
491
- if (_optionalChain([schema, 'optionalAccess', _8 => _8.format]) === format) {
492
- if (opts.parentIsArray) {
493
- const parentData = get(opts.payload, opts.parentKey || "");
494
- if (parentData !== void 0 && Array.isArray(parentData)) {
495
- return Object.keys(parentData).map((pdk) => {
496
- const currentKey = [opts.parentKey, pdk].join(".");
497
- if (get(opts.payload, currentKey) !== void 0) {
498
- return currentKey;
499
- }
500
- return false;
501
- }).filter(Boolean);
502
- }
503
- } else if (opts.parentKey && get(opts.payload, opts.parentKey) !== void 0) {
504
- return opts.parentKey;
505
- } else if (opts.payload !== void 0) {
506
- return true;
507
- }
508
- return false;
509
- }
510
- const subschemas = getSubschemas(schema, opts);
511
- if (!subschemas) {
512
- return false;
513
- }
514
- return subschemas.flatMap(({ key, schema: subschema }) => {
515
- if ("properties" in subschema) {
516
- return getTypedFormatsInSchema(format, subschema.properties, { payload: opts.payload, parentKey: key });
517
- } else if ("items" in subschema) {
518
- if (_optionalChain([subschema, 'access', _9 => _9.items, 'optionalAccess', _10 => _10.properties])) {
519
- return getTypedFormatsInSchema(format, subschema.items.properties, {
520
- payload: opts.payload,
521
- parentKey: key,
522
- parentIsArray: true
523
- });
524
- }
525
- return getTypedFormatsInSchema(format, subschema.items, {
526
- payload: opts.payload,
527
- parentKey: key,
528
- parentIsArray: true
529
- });
530
- }
531
- return getTypedFormatsInSchema(format, subschema, { payload: opts.payload, parentKey: key });
532
- }).filter(Boolean);
533
- } catch (e2) {
534
- return [];
535
- }
536
- }
537
-
538
572
  // src/index.ts
539
573
  function formatter(values, param, type, onlyIfExists = false) {
540
574
  if (param.style) {
@@ -548,6 +582,10 @@ function formatter(values, param, type, onlyIfExists = false) {
548
582
  value = void 0;
549
583
  } else if (param.required && param.schema && !_types.isRef.call(void 0, param.schema) && param.schema.default) {
550
584
  value = param.schema.default;
585
+ } else if (param.required && param.content) {
586
+ const contentType = getParameterContentType(param);
587
+ const schema = contentType ? getParameterContentSchema(param, contentType) : null;
588
+ value = _optionalChain([schema, 'optionalAccess', _11 => _11.default]);
551
589
  } else if (type === "path") {
552
590
  return param.name;
553
591
  }
@@ -569,7 +607,7 @@ function multipartBodyToFormatterParams(payload, oasMediaTypeObject, schema) {
569
607
  const encoding = oasMediaTypeObject.encoding;
570
608
  if (typeof payload === "object" && payload !== null) {
571
609
  return Object.keys(payload).map((key) => {
572
- if (!_optionalChain([schema, 'access', _11 => _11.properties, 'optionalAccess', _12 => _12[key]])) {
610
+ if (!_optionalChain([schema, 'access', _12 => _12.properties, 'optionalAccess', _13 => _13[key]])) {
573
611
  return false;
574
612
  }
575
613
  const paramEncoding = encoding ? encoding[key] : void 0;
@@ -592,7 +630,7 @@ var defaultFormDataTypes = Object.keys(_utils.jsonSchemaTypes).reduce((prev, cur
592
630
  }, {});
593
631
  function getResponseContentType(content) {
594
632
  const types = Object.keys(content) || [];
595
- if (_optionalChain([types, 'optionalAccess', _13 => _13.length])) {
633
+ if (_optionalChain([types, 'optionalAccess', _14 => _14.length])) {
596
634
  const jsonType = types.find((t) => _utils.matchesMimeType.json(t));
597
635
  if (jsonType) {
598
636
  return jsonType;
@@ -653,8 +691,8 @@ function oasToHar(oas, operationSchema, values = {}, auth = {}, opts = { proxyUr
653
691
  if (!operationSchema || typeof operationSchema.getParameters !== "function") {
654
692
  operation = new (0, _operation.Operation)(
655
693
  oas,
656
- _optionalChain([operationSchema, 'optionalAccess', _14 => _14.path]) || "",
657
- _optionalChain([operationSchema, 'optionalAccess', _15 => _15.method]) || "",
694
+ _optionalChain([operationSchema, 'optionalAccess', _15 => _15.path]) || "",
695
+ _optionalChain([operationSchema, 'optionalAccess', _16 => _16.method]) || "",
658
696
  operationSchema || { path: "", method: "" }
659
697
  );
660
698
  } else {
@@ -704,15 +742,15 @@ function oasToHar(oas, operationSchema, values = {}, auth = {}, opts = { proxyUr
704
742
  }
705
743
  return formatter(formData, parameter, "path");
706
744
  });
707
- const queryStrings = _optionalChain([parameters, 'optionalAccess', _16 => _16.filter, 'call', _17 => _17((param) => param.in === "query")]);
708
- if (_optionalChain([queryStrings, 'optionalAccess', _18 => _18.length])) {
745
+ const queryStrings = _optionalChain([parameters, 'optionalAccess', _17 => _17.filter, 'call', _18 => _18((param) => param.in === "query")]);
746
+ if (_optionalChain([queryStrings, 'optionalAccess', _19 => _19.length])) {
709
747
  queryStrings.forEach((queryString) => {
710
748
  const value = formatter(formData, queryString, "query", true);
711
749
  appendHarValue(har.queryString, queryString.name, value);
712
750
  });
713
751
  }
714
- const cookies = _optionalChain([parameters, 'optionalAccess', _19 => _19.filter, 'call', _20 => _20((param) => param.in === "cookie")]);
715
- if (_optionalChain([cookies, 'optionalAccess', _21 => _21.length])) {
752
+ const cookies = _optionalChain([parameters, 'optionalAccess', _20 => _20.filter, 'call', _21 => _21((param) => param.in === "cookie")]);
753
+ if (_optionalChain([cookies, 'optionalAccess', _22 => _22.length])) {
716
754
  cookies.forEach((cookie) => {
717
755
  const value = formatter(formData, cookie, "cookie", true);
718
756
  appendHarValue(har.cookies, cookie.name, value);
@@ -720,8 +758,8 @@ function oasToHar(oas, operationSchema, values = {}, auth = {}, opts = { proxyUr
720
758
  }
721
759
  if (operation.schema.responses) {
722
760
  Object.keys(operation.schema.responses).some((response) => {
723
- if (_types.isRef.call(void 0, _optionalChain([operation, 'access', _22 => _22.schema, 'access', _23 => _23.responses, 'optionalAccess', _24 => _24[response]]))) return false;
724
- const content = (_optionalChain([operation, 'access', _25 => _25.schema, 'access', _26 => _26.responses, 'optionalAccess', _27 => _27[response]])).content;
761
+ if (_types.isRef.call(void 0, _optionalChain([operation, 'access', _23 => _23.schema, 'access', _24 => _24.responses, 'optionalAccess', _25 => _25[response]]))) return false;
762
+ const content = (_optionalChain([operation, 'access', _26 => _26.schema, 'access', _27 => _27.responses, 'optionalAccess', _28 => _28[response]])).content;
725
763
  if (!content) return false;
726
764
  if (Object.keys(formData.header || {}).find((h) => h.toLowerCase() === "accept")) return true;
727
765
  har.headers.push({
@@ -733,8 +771,8 @@ function oasToHar(oas, operationSchema, values = {}, auth = {}, opts = { proxyUr
733
771
  }
734
772
  let hasContentType = false;
735
773
  let contentType = operation.getContentType();
736
- const headers = _optionalChain([parameters, 'optionalAccess', _28 => _28.filter, 'call', _29 => _29((param) => param.in === "header")]);
737
- if (_optionalChain([headers, 'optionalAccess', _30 => _30.length])) {
774
+ const headers = _optionalChain([parameters, 'optionalAccess', _29 => _29.filter, 'call', _30 => _30((param) => param.in === "header")]);
775
+ if (_optionalChain([headers, 'optionalAccess', _31 => _31.length])) {
738
776
  headers.forEach((header) => {
739
777
  const value = formatter(formData, header, "header", true);
740
778
  if (typeof value === "undefined") return;
@@ -780,7 +818,7 @@ function oasToHar(oas, operationSchema, values = {}, auth = {}, opts = { proxyUr
780
818
  return payload.type === (operation.isFormUrlEncoded() ? "formData" : "body");
781
819
  });
782
820
  }
783
- if (_optionalChain([requestBody, 'optionalAccess', _31 => _31.schema]) && Object.keys(requestBody.schema).length) {
821
+ if (_optionalChain([requestBody, 'optionalAccess', _32 => _32.schema]) && Object.keys(requestBody.schema).length) {
784
822
  const requestBodySchema = requestBody.schema;
785
823
  if (operation.isFormUrlEncoded()) {
786
824
  if (Object.keys(formData.formData || {}).length) {
@@ -839,7 +877,7 @@ function oasToHar(oas, operationSchema, values = {}, auth = {}, opts = { proxyUr
839
877
  }
840
878
  }
841
879
  }
842
- appendHarValue(_optionalChain([har, 'access', _32 => _32.postData, 'optionalAccess', _33 => _33.params]) || [], name, val, addtlData);
880
+ appendHarValue(_optionalChain([har, 'access', _33 => _33.postData, 'optionalAccess', _34 => _34.params]) || [], name, val, addtlData);
843
881
  });
844
882
  }
845
883
  });
@@ -879,14 +917,14 @@ function oasToHar(oas, operationSchema, values = {}, auth = {}, opts = { proxyUr
879
917
  }
880
918
  }
881
919
  }
882
- if ((_optionalChain([har, 'access', _34 => _34.postData, 'optionalAccess', _35 => _35.text]) || _optionalChain([requestBody, 'optionalAccess', _36 => _36.schema]) && Object.keys(requestBody.schema).length) && !hasContentType) {
920
+ if ((_optionalChain([har, 'access', _35 => _35.postData, 'optionalAccess', _36 => _36.text]) || _optionalChain([requestBody, 'optionalAccess', _37 => _37.schema]) && Object.keys(requestBody.schema).length) && !hasContentType) {
883
921
  har.headers.push({
884
922
  name: "content-type",
885
923
  value: contentType
886
924
  });
887
925
  }
888
926
  const securityRequirements = operation.getSecurity();
889
- if (_optionalChain([securityRequirements, 'optionalAccess', _37 => _37.length])) {
927
+ if (_optionalChain([securityRequirements, 'optionalAccess', _38 => _38.length])) {
890
928
  securityRequirements.forEach((schemes) => {
891
929
  Object.keys(schemes).forEach((security) => {
892
930
  const securityValue = _chunkIN7TUPJQcjs.configureSecurity.call(void 0, apiDefinition, auth, security);