@readme/oas-to-har 30.0.3 → 31.0.0

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
@@ -1,9 +1,10 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
2
 
3
3
  var _chunkIN7TUPJQcjs = require('./chunk-IN7TUPJQ.cjs');
4
4
 
5
5
  // src/index.ts
6
6
  var _dataurls = require('@readme/data-urls');
7
+ var _oas = require('oas'); var _oas2 = _interopRequireDefault(_oas);
7
8
  var _extensions = require('oas/extensions');
8
9
  var _operation = require('oas/operation');
9
10
  var _types = require('oas/types');
@@ -50,7 +51,7 @@ function getSafeRequestBody(obj) {
50
51
  }
51
52
  return obj;
52
53
  }
53
- function getSubschemas(schema, opts) {
54
+ function getSubschemas(schema, api, opts) {
54
55
  let subSchemaDataSize = 0;
55
56
  if (opts.parentIsArray) {
56
57
  const parentData = get(opts.payload, opts.parentKey || "");
@@ -62,22 +63,67 @@ function getSubschemas(schema, opts) {
62
63
  let subschemas = [];
63
64
  if (subSchemaDataSize > 0) {
64
65
  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
- );
66
+ const foundSubschemas = getSubschemas(schema, api, {
67
+ ...opts,
68
+ parentIsArray: false,
69
+ parentKey: opts.parentKey ? [opts.parentKey, idx].join(".") : String(idx)
70
+ });
71
+ if (foundSubschemas) {
72
+ subschemas = subschemas.concat(foundSubschemas);
73
+ }
71
74
  }
72
75
  } else {
73
- subschemas = Object.entries(schema).map(([key, subschema]) => ({
74
- key: opts.parentKey ? [opts.parentKey, key].join(".") : key,
75
- schema: getSafeRequestBody(subschema)
76
- }));
76
+ let resolvedSchema = schema;
77
+ if (schema && _types.isRef.call(void 0, schema)) {
78
+ resolvedSchema = _utils.dereferenceRef.call(void 0, schema, api);
79
+ if (!resolvedSchema || _types.isRef.call(void 0, resolvedSchema)) {
80
+ return subschemas;
81
+ }
82
+ }
83
+ const baseKey = _nullishCoalesce(opts.parentKey, () => ( ""));
84
+ if (resolvedSchema.properties && typeof resolvedSchema.properties === "object") {
85
+ for (const [propName, propSchema] of Object.entries(resolvedSchema.properties)) {
86
+ if (propSchema && typeof propSchema === "object") {
87
+ const resolved = _types.isRef.call(void 0, propSchema) ? _utils.dereferenceRef.call(void 0, propSchema, api) : propSchema;
88
+ if (resolved && !_types.isRef.call(void 0, resolved)) {
89
+ subschemas.push({
90
+ key: baseKey ? [baseKey, propName].join(".") : propName,
91
+ schema: resolved
92
+ });
93
+ }
94
+ }
95
+ }
96
+ }
97
+ if (!("properties" in resolvedSchema) && typeof resolvedSchema === "object" && !("type" in resolvedSchema && resolvedSchema.type !== "object")) {
98
+ for (const [propName, propSchema] of Object.entries(resolvedSchema)) {
99
+ if (propSchema && (Array.isArray(propSchema) || typeof propSchema === "object" && propSchema !== null)) {
100
+ const raw = getSafeRequestBody(propSchema);
101
+ const resolved = _types.isRef.call(void 0, raw) ? _utils.dereferenceRef.call(void 0, raw, api) : raw;
102
+ const toPush = resolved && !_types.isRef.call(void 0, resolved) ? resolved : raw;
103
+ if (toPush && typeof toPush === "object") {
104
+ subschemas.push({
105
+ key: baseKey ? [baseKey, propName].join(".") : propName,
106
+ schema: toPush
107
+ });
108
+ }
109
+ }
110
+ }
111
+ }
112
+ if ("items" in resolvedSchema && resolvedSchema.items !== void 0 && resolvedSchema.items !== true) {
113
+ const itemsSchema = resolvedSchema.items;
114
+ const resolved = _types.isRef.call(void 0, itemsSchema) ? _utils.dereferenceRef.call(void 0, itemsSchema, api) : itemsSchema;
115
+ if (resolved && !_types.isRef.call(void 0, resolved)) {
116
+ subschemas.push({
117
+ key: baseKey,
118
+ schema: resolved,
119
+ parentIsArray: true
120
+ });
121
+ }
122
+ }
77
123
  }
78
124
  return subschemas;
79
125
  }
80
- function getTypedFormatsInSchema(format, schema, opts) {
126
+ function getTypedFormatsInSchema(format, schema, api, opts) {
81
127
  try {
82
128
  if (_optionalChain([schema, 'optionalAccess', _6 => _6.format]) === format) {
83
129
  if (opts.parentIsArray) {
@@ -93,33 +139,28 @@ function getTypedFormatsInSchema(format, schema, opts) {
93
139
  }
94
140
  } else if (opts.parentKey && get(opts.payload, opts.parentKey) !== void 0) {
95
141
  return opts.parentKey;
96
- } else if (opts.payload !== void 0) {
142
+ } else if (!opts.parentKey && opts.payload !== void 0) {
97
143
  return true;
98
144
  }
99
145
  return false;
100
146
  }
101
- const subschemas = getSubschemas(schema, opts);
147
+ const subschemas = getSubschemas(schema, api, opts);
102
148
  if (!subschemas) {
103
149
  return false;
104
150
  }
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
- });
151
+ return subschemas.flatMap(({ key, schema: subschema, parentIsArray: entryIsArray }) => {
152
+ if (_types.isRef.call(void 0, subschema)) {
153
+ const resolved = _utils.dereferenceRef.call(void 0, subschema, api);
154
+ if (resolved && !_types.isRef.call(void 0, resolved)) {
155
+ return resolved;
115
156
  }
116
- return getTypedFormatsInSchema(format, subschema.items, {
117
- payload: opts.payload,
118
- parentKey: key,
119
- parentIsArray: true
120
- });
157
+ return false;
121
158
  }
122
- return getTypedFormatsInSchema(format, subschema, { payload: opts.payload, parentKey: key });
159
+ return getTypedFormatsInSchema(format, subschema, api, {
160
+ payload: opts.payload,
161
+ parentKey: key,
162
+ parentIsArray: entryIsArray
163
+ });
123
164
  }).filter(Boolean);
124
165
  } catch (e) {
125
166
  return [];
@@ -145,6 +186,27 @@ function getParameterContentSchema(param, contentType) {
145
186
  }
146
187
  return null;
147
188
  }
189
+ function parseJsonStringsInBody(obj) {
190
+ if (typeof obj === "string") {
191
+ try {
192
+ const p = JSON.parse(obj);
193
+ return typeof p === "object" && p !== null ? parseJsonStringsInBody(p) : p;
194
+ } catch (e2) {
195
+ return obj;
196
+ }
197
+ }
198
+ if (Array.isArray(obj)) {
199
+ return obj.map(parseJsonStringsInBody);
200
+ }
201
+ if (obj !== null && typeof obj === "object") {
202
+ const out = {};
203
+ for (const [k, v] of Object.entries(obj)) {
204
+ out[k] = parseJsonStringsInBody(v);
205
+ }
206
+ return out;
207
+ }
208
+ return obj;
209
+ }
148
210
 
149
211
  // src/lib/style-formatting/style-serializer.ts
150
212
  var isRfc3986Reserved = (char) => ":/?#[]@!$&'()*+,;=".indexOf(char) > -1;
@@ -152,7 +214,7 @@ var isRfc3986Unreserved = (char) => /^[a-z0-9\-._~]+$/i.test(char);
152
214
  function isURIEncoded(value) {
153
215
  try {
154
216
  return decodeURIComponent(value) !== value;
155
- } catch (e2) {
217
+ } catch (e3) {
156
218
  return false;
157
219
  }
158
220
  }
@@ -524,7 +586,7 @@ function handleDeepObject(value, parameter) {
524
586
  });
525
587
  }
526
588
  function handleExplode(value, parameter) {
527
- if (Array.isArray(value) && _optionalChain([parameter, 'access', _9 => _9.schema, 'optionalAccess', _10 => _10.type]) === "array" && parameter.style === "deepObject") {
589
+ if (Array.isArray(value) && _optionalChain([parameter, 'access', _7 => _7.schema, 'optionalAccess', _8 => _8.type]) === "array" && parameter.style === "deepObject") {
528
590
  const newObj = {};
529
591
  const deepObjs = handleDeepObject(value, parameter);
530
592
  deepObjs.forEach((obj) => {
@@ -585,7 +647,7 @@ function formatter(values, param, type, onlyIfExists = false) {
585
647
  } else if (param.required && param.content) {
586
648
  const contentType = getParameterContentType(param);
587
649
  const schema = contentType ? getParameterContentSchema(param, contentType) : null;
588
- value = _optionalChain([schema, 'optionalAccess', _11 => _11.default]);
650
+ value = _optionalChain([schema, 'optionalAccess', _9 => _9.default]);
589
651
  } else if (type === "path") {
590
652
  return param.name;
591
653
  }
@@ -607,7 +669,7 @@ function multipartBodyToFormatterParams(payload, oasMediaTypeObject, schema) {
607
669
  const encoding = oasMediaTypeObject.encoding;
608
670
  if (typeof payload === "object" && payload !== null) {
609
671
  return Object.keys(payload).map((key) => {
610
- if (!_optionalChain([schema, 'access', _12 => _12.properties, 'optionalAccess', _13 => _13[key]])) {
672
+ if (!_optionalChain([schema, 'access', _10 => _10.properties, 'optionalAccess', _11 => _11[key]])) {
611
673
  return false;
612
674
  }
613
675
  const paramEncoding = encoding ? encoding[key] : void 0;
@@ -630,7 +692,7 @@ var defaultFormDataTypes = Object.keys(_utils.jsonSchemaTypes).reduce((prev, cur
630
692
  }, {});
631
693
  function getResponseContentType(content) {
632
694
  const types = Object.keys(content) || [];
633
- if (_optionalChain([types, 'optionalAccess', _14 => _14.length])) {
695
+ if (_optionalChain([types, 'optionalAccess', _12 => _12.length])) {
634
696
  const jsonType = types.find((t) => _utils.matchesMimeType.json(t));
635
697
  if (jsonType) {
636
698
  return jsonType;
@@ -689,10 +751,11 @@ function encodeBodyForHAR(body) {
689
751
  function oasToHar(oas, operationSchema, values = {}, auth = {}, opts = { proxyUrl: "" }) {
690
752
  let operation;
691
753
  if (!operationSchema || typeof operationSchema.getParameters !== "function") {
754
+ const currentOas = _oas2.default.init(oas);
692
755
  operation = new (0, _operation.Operation)(
693
- oas,
694
- _optionalChain([operationSchema, 'optionalAccess', _15 => _15.path]) || "",
695
- _optionalChain([operationSchema, 'optionalAccess', _16 => _16.method]) || "",
756
+ currentOas,
757
+ _optionalChain([operationSchema, 'optionalAccess', _13 => _13.path]) || "",
758
+ _optionalChain([operationSchema, 'optionalAccess', _14 => _14.method]) || "",
696
759
  operationSchema || { path: "", method: "" }
697
760
  );
698
761
  } else {
@@ -742,24 +805,25 @@ function oasToHar(oas, operationSchema, values = {}, auth = {}, opts = { proxyUr
742
805
  }
743
806
  return formatter(formData, parameter, "path");
744
807
  });
745
- const queryStrings = _optionalChain([parameters, 'optionalAccess', _17 => _17.filter, 'call', _18 => _18((param) => param.in === "query")]);
746
- if (_optionalChain([queryStrings, 'optionalAccess', _19 => _19.length])) {
808
+ const queryStrings = _optionalChain([parameters, 'optionalAccess', _15 => _15.filter, 'call', _16 => _16((param) => param.in === "query")]);
809
+ if (_optionalChain([queryStrings, 'optionalAccess', _17 => _17.length])) {
747
810
  queryStrings.forEach((queryString) => {
748
811
  const value = formatter(formData, queryString, "query", true);
749
812
  appendHarValue(har.queryString, queryString.name, value);
750
813
  });
751
814
  }
752
- const cookies = _optionalChain([parameters, 'optionalAccess', _20 => _20.filter, 'call', _21 => _21((param) => param.in === "cookie")]);
753
- if (_optionalChain([cookies, 'optionalAccess', _22 => _22.length])) {
815
+ const cookies = _optionalChain([parameters, 'optionalAccess', _18 => _18.filter, 'call', _19 => _19((param) => param.in === "cookie")]);
816
+ if (_optionalChain([cookies, 'optionalAccess', _20 => _20.length])) {
754
817
  cookies.forEach((cookie) => {
755
818
  const value = formatter(formData, cookie, "cookie", true);
756
819
  appendHarValue(har.cookies, cookie.name, value);
757
820
  });
758
821
  }
759
822
  if (operation.schema.responses) {
760
- Object.keys(operation.schema.responses).some((response) => {
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;
823
+ Object.keys(operation.schema.responses).some((statusCode) => {
824
+ const response = operation.getResponseByStatusCode(statusCode);
825
+ if (!response) return false;
826
+ const content = response.content;
763
827
  if (!content) return false;
764
828
  if (Object.keys(formData.header || {}).find((h) => h.toLowerCase() === "accept")) return true;
765
829
  har.headers.push({
@@ -771,8 +835,8 @@ function oasToHar(oas, operationSchema, values = {}, auth = {}, opts = { proxyUr
771
835
  }
772
836
  let hasContentType = false;
773
837
  let contentType = operation.getContentType();
774
- const headers = _optionalChain([parameters, 'optionalAccess', _29 => _29.filter, 'call', _30 => _30((param) => param.in === "header")]);
775
- if (_optionalChain([headers, 'optionalAccess', _31 => _31.length])) {
838
+ const headers = _optionalChain([parameters, 'optionalAccess', _21 => _21.filter, 'call', _22 => _22((param) => param.in === "header")]);
839
+ if (_optionalChain([headers, 'optionalAccess', _23 => _23.length])) {
776
840
  headers.forEach((header) => {
777
841
  const value = formatter(formData, header, "header", true);
778
842
  if (typeof value === "undefined") return;
@@ -814,11 +878,11 @@ function oasToHar(oas, operationSchema, values = {}, auth = {}, opts = { proxyUr
814
878
  }
815
879
  let requestBody;
816
880
  if (operation.hasRequestBody()) {
817
- requestBody = _optionalChain([operation, 'access', _32 => _32.getParametersAsJSONSchema, 'call', _33 => _33(), 'optionalAccess', _34 => _34.find, 'call', _35 => _35((payload) => {
881
+ requestBody = _optionalChain([operation, 'access', _24 => _24.getParametersAsJSONSchema, 'call', _25 => _25(), 'optionalAccess', _26 => _26.find, 'call', _27 => _27((payload) => {
818
882
  return payload.type === (operation.isFormUrlEncoded() ? "formData" : "body");
819
883
  })]);
820
884
  }
821
- if (_optionalChain([requestBody, 'optionalAccess', _36 => _36.schema]) && Object.keys(requestBody.schema).length) {
885
+ if (_optionalChain([requestBody, 'optionalAccess', _28 => _28.schema]) && Object.keys(requestBody.schema).length) {
822
886
  const requestBodySchema = requestBody.schema;
823
887
  if (operation.isFormUrlEncoded()) {
824
888
  if (Object.keys(formData.formData || {}).length) {
@@ -853,11 +917,15 @@ function oasToHar(oas, operationSchema, values = {}, auth = {}, opts = { proxyUr
853
917
  return false;
854
918
  });
855
919
  if (cleanBody !== void 0) {
856
- const multipartParams = multipartBodyToFormatterParams(
857
- formData.body,
858
- operation.schema.requestBody.content["multipart/form-data"],
859
- safeBodySchema
860
- );
920
+ let multipartParams = [];
921
+ const multipartContent = operation.getRequestBody("multipart/form-data");
922
+ if (typeof multipartContent === "object" && multipartContent !== null && // `getRequestBody()` will return an array if there are multiple content types that
923
+ // match the one we're looking for but because we're looking for an exact
924
+ // `multipart/form-data` match `getRequestBody()` will only ever return a single
925
+ // object back for us.
926
+ !Array.isArray(multipartContent)) {
927
+ multipartParams = multipartBodyToFormatterParams(formData.body, multipartContent, safeBodySchema);
928
+ }
861
929
  if (multipartParams.length) {
862
930
  Object.keys(cleanBody).forEach((name) => {
863
931
  const param = multipartParams.find((multipartParam) => multipartParam.name === name);
@@ -877,7 +945,7 @@ function oasToHar(oas, operationSchema, values = {}, auth = {}, opts = { proxyUr
877
945
  }
878
946
  }
879
947
  }
880
- appendHarValue(_optionalChain([har, 'access', _37 => _37.postData, 'optionalAccess', _38 => _38.params]) || [], name, val, addtlData);
948
+ appendHarValue(_optionalChain([har, 'access', _29 => _29.postData, 'optionalAccess', _30 => _30.params]) || [], name, val, addtlData);
881
949
  });
882
950
  }
883
951
  });
@@ -888,28 +956,39 @@ function oasToHar(oas, operationSchema, values = {}, auth = {}, opts = { proxyUr
888
956
  if (hasSchemaType(requestBody.schema, "string") || hasSchemaType(requestBody.schema, "integer") || hasSchemaType(requestBody.schema, "number") || hasSchemaType(requestBody.schema, "boolean")) {
889
957
  har.postData.text = JSON.stringify(JSON.parse(cleanBody));
890
958
  } else {
891
- const jsonTypes = getTypedFormatsInSchema("json", requestBodySchema.properties, { payload: cleanBody });
959
+ const jsonTypes = getTypedFormatsInSchema("json", requestBodySchema, operation.api, {
960
+ payload: cleanBody
961
+ });
892
962
  if (Array.isArray(jsonTypes) && jsonTypes.length) {
893
963
  try {
894
964
  jsonTypes.forEach((prop) => {
895
965
  try {
896
966
  set(cleanBody, String(prop), JSON.parse(get(cleanBody, String(prop))));
897
- } catch (e3) {
967
+ } catch (e4) {
898
968
  }
899
969
  });
900
970
  if (typeof cleanBody.RAW_BODY !== "undefined") {
901
971
  cleanBody = cleanBody.RAW_BODY;
902
972
  }
903
973
  har.postData.text = JSON.stringify(cleanBody);
904
- } catch (e4) {
974
+ } catch (e5) {
905
975
  har.postData.text = stringify(formData.body);
906
976
  }
907
977
  } else {
908
- har.postData.text = encodeBodyForHAR(formData.body);
978
+ try {
979
+ const parsed = parseJsonStringsInBody(cleanBody);
980
+ if (typeof _optionalChain([parsed, 'optionalAccess', _31 => _31.RAW_BODY]) !== "undefined") {
981
+ har.postData.text = isPrimitive(parsed.RAW_BODY) ? String(parsed.RAW_BODY) : stringify(parsed.RAW_BODY);
982
+ } else {
983
+ har.postData.text = JSON.stringify(parsed);
984
+ }
985
+ } catch (e6) {
986
+ har.postData.text = encodeBodyForHAR(formData.body);
987
+ }
909
988
  }
910
989
  }
911
990
  }
912
- } catch (e5) {
991
+ } catch (e7) {
913
992
  har.postData = { mimeType: contentType, text: stringify(formData.body) };
914
993
  }
915
994
  } else {
@@ -917,14 +996,14 @@ function oasToHar(oas, operationSchema, values = {}, auth = {}, opts = { proxyUr
917
996
  }
918
997
  }
919
998
  }
920
- if ((_optionalChain([har, 'access', _39 => _39.postData, 'optionalAccess', _40 => _40.text]) || _optionalChain([requestBody, 'optionalAccess', _41 => _41.schema]) && Object.keys(requestBody.schema).length) && !hasContentType) {
999
+ if ((_optionalChain([har, 'access', _32 => _32.postData, 'optionalAccess', _33 => _33.text]) || _optionalChain([requestBody, 'optionalAccess', _34 => _34.schema]) && Object.keys(requestBody.schema).length) && !hasContentType) {
921
1000
  har.headers.push({
922
1001
  name: "content-type",
923
1002
  value: contentType
924
1003
  });
925
1004
  }
926
1005
  const securityRequirements = operation.getSecurity();
927
- if (_optionalChain([securityRequirements, 'optionalAccess', _42 => _42.length])) {
1006
+ if (_optionalChain([securityRequirements, 'optionalAccess', _35 => _35.length])) {
928
1007
  securityRequirements.forEach((schemes) => {
929
1008
  Object.keys(schemes).forEach((security) => {
930
1009
  const securityValue = _chunkIN7TUPJQcjs.configureSecurity.call(void 0, apiDefinition, auth, security);