oas 36.0.2 → 37.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.
Files changed (38) hide show
  1. package/README.md +0 -5
  2. package/dist/analyzer/index.cjs +83 -28
  3. package/dist/analyzer/index.cjs.map +1 -1
  4. package/dist/analyzer/index.d.cts +3 -10
  5. package/dist/analyzer/index.d.ts +3 -10
  6. package/dist/analyzer/index.js +81 -26
  7. package/dist/analyzer/index.js.map +1 -1
  8. package/dist/analyzer/types.d.cts +0 -1
  9. package/dist/analyzer/types.d.ts +0 -1
  10. package/dist/{chunk-UDN4U5TL.cjs → chunk-GS43VKJH.cjs} +320 -274
  11. package/dist/chunk-GS43VKJH.cjs.map +1 -0
  12. package/dist/{chunk-6MDVLJ3A.js → chunk-IEN4GZPF.js} +9 -23
  13. package/dist/chunk-IEN4GZPF.js.map +1 -0
  14. package/dist/{chunk-SCWW2SNX.cjs → chunk-UKD63LKG.cjs} +10 -24
  15. package/dist/chunk-UKD63LKG.cjs.map +1 -0
  16. package/dist/{chunk-35LEYZEY.js → chunk-WIVQX3DA.js} +199 -153
  17. package/dist/chunk-WIVQX3DA.js.map +1 -0
  18. package/dist/index.cjs +634 -7
  19. package/dist/index.cjs.map +1 -1
  20. package/dist/index.d.cts +38 -135
  21. package/dist/index.d.ts +38 -135
  22. package/dist/index.js +633 -6
  23. package/dist/index.js.map +1 -1
  24. package/dist/operation/index.cjs +3 -3
  25. package/dist/operation/index.js +2 -2
  26. package/dist/reducer/index.cjs +7 -7
  27. package/dist/reducer/index.js +1 -1
  28. package/dist/utils.cjs +2 -2
  29. package/dist/utils.js +1 -1
  30. package/package.json +1 -4
  31. package/dist/chunk-35LEYZEY.js.map +0 -1
  32. package/dist/chunk-6MDVLJ3A.js.map +0 -1
  33. package/dist/chunk-IXQKQM3K.js +0 -866
  34. package/dist/chunk-IXQKQM3K.js.map +0 -1
  35. package/dist/chunk-SCWW2SNX.cjs.map +0 -1
  36. package/dist/chunk-UDN4U5TL.cjs.map +0 -1
  37. package/dist/chunk-YKV73CBG.cjs +0 -866
  38. package/dist/chunk-YKV73CBG.cjs.map +0 -1
@@ -1,4 +1,5 @@
1
1
  import {
2
+ SERVER_VARIABLE_REGEX,
2
3
  applyDiscriminatorOneOfToUsedSchemas,
3
4
  cloneObject,
4
5
  collectRefsInSchema,
@@ -6,7 +7,6 @@ import {
6
7
  dereferenceRef,
7
8
  dereferenceRefDeep,
8
9
  filterRequiredRefsToReferenced,
9
- getDereferencingOptions,
10
10
  getParametersAsJSONSchema,
11
11
  getSchemaVersionString,
12
12
  isObject,
@@ -15,7 +15,7 @@ import {
15
15
  mergeReferencedSchemasIntoRoot,
16
16
  supportedMethods,
17
17
  toJSONSchema
18
- } from "./chunk-6MDVLJ3A.js";
18
+ } from "./chunk-IEN4GZPF.js";
19
19
  import {
20
20
  getExtension
21
21
  } from "./chunk-S27IGTVG.js";
@@ -23,8 +23,159 @@ import {
23
23
  isRef
24
24
  } from "./chunk-XG4HGNCN.js";
25
25
 
26
- // src/operation/index.ts
27
- import { $RefParser } from "@apidevtools/json-schema-ref-parser";
26
+ // src/lib/urls.ts
27
+ import { match, pathToRegexp } from "path-to-regexp";
28
+
29
+ // src/lib/get-user-variable.ts
30
+ function getUserVariable(user, property, selectedApp) {
31
+ let key = user;
32
+ if ("keys" in user && Array.isArray(user.keys) && user.keys.length) {
33
+ if (selectedApp) {
34
+ key = user.keys.find((k) => k.name === selectedApp);
35
+ } else {
36
+ key = user.keys[0];
37
+ }
38
+ }
39
+ return key?.[property] || user[property] || null;
40
+ }
41
+
42
+ // src/lib/urls.ts
43
+ function stripTrailingSlash(url) {
44
+ if (url[url.length - 1] === "/") {
45
+ return url.slice(0, -1);
46
+ }
47
+ return url;
48
+ }
49
+ function ensureProtocol(url) {
50
+ if (url.match(/^\/\//)) {
51
+ return `https:${url}`;
52
+ }
53
+ if (!url.match(/\/\//)) {
54
+ return `https://${url}`;
55
+ }
56
+ return url;
57
+ }
58
+ function normalizedURLFromServers(servers, selected) {
59
+ const exampleDotCom = "https://example.com";
60
+ let url;
61
+ try {
62
+ url = servers?.[selected]?.url;
63
+ if (!url) throw new Error("no url");
64
+ url = stripTrailingSlash(url);
65
+ if (url.startsWith("/") && !url.startsWith("//")) {
66
+ const urlWithOrigin = new URL(exampleDotCom);
67
+ urlWithOrigin.pathname = url;
68
+ url = urlWithOrigin.href;
69
+ }
70
+ } catch {
71
+ url = exampleDotCom;
72
+ }
73
+ return ensureProtocol(url);
74
+ }
75
+ function variablesFromServers(servers, selected = 0) {
76
+ return servers?.[selected]?.variables || {};
77
+ }
78
+ function defaultVariablesFromServers(servers, selected = 0, user = {}) {
79
+ const variables = variablesFromServers(servers, selected);
80
+ const defaults = {};
81
+ Object.keys(variables).forEach((key) => {
82
+ defaults[key] = getUserVariable(user, key) || variables[key].default || "";
83
+ });
84
+ return defaults;
85
+ }
86
+ function splitUrlFromServers(servers, selected = 0) {
87
+ const url = normalizedURLFromServers(servers, selected);
88
+ const variables = variablesFromServers(servers, selected);
89
+ return url.split(/({.+?})/).filter(Boolean).map((part, i) => {
90
+ const isVariable = part.match(/[{}]/);
91
+ const value = part.replace(/[{}]/g, "");
92
+ const key = `${value}-${i}`;
93
+ if (!isVariable) {
94
+ return {
95
+ type: "text",
96
+ value,
97
+ key
98
+ };
99
+ }
100
+ const variable = variables?.[value];
101
+ return {
102
+ type: "variable",
103
+ value,
104
+ key,
105
+ description: variable?.description,
106
+ enum: variable?.enum
107
+ };
108
+ });
109
+ }
110
+ function transformURLIntoRegex(url) {
111
+ return stripTrailingSlash(url.replace(SERVER_VARIABLE_REGEX, "([-_a-zA-Z0-9:.[\\]]+)"));
112
+ }
113
+ function normalizePath(path) {
114
+ return path.replace(/({?){(.*?)}(}?)/g, (str, ...args) => {
115
+ return `:${args[1].replace("-", "")}`;
116
+ }).replace(/::/, "\\::").split("?")[0];
117
+ }
118
+ function generatePathMatches(paths, pathName, origin) {
119
+ const prunedPathName = pathName.split("?")[0];
120
+ const matches = Object.keys(paths).map((path) => {
121
+ const cleanedPath = normalizePath(path);
122
+ let matchResult;
123
+ try {
124
+ const matchStatement = match(cleanedPath, { decode: decodeURIComponent });
125
+ matchResult = matchStatement(prunedPathName);
126
+ } catch {
127
+ return false;
128
+ }
129
+ const slugs = {};
130
+ if (matchResult && Object.keys(matchResult.params).length) {
131
+ Object.keys(matchResult.params).forEach((param) => {
132
+ slugs[`:${param}`] = matchResult.params[param];
133
+ });
134
+ }
135
+ return {
136
+ url: {
137
+ origin,
138
+ path: cleanedPath.replace(/\\::/, "::"),
139
+ nonNormalizedPath: path,
140
+ slugs
141
+ },
142
+ operation: paths[path],
143
+ match: matchResult
144
+ };
145
+ }).filter((item) => item !== false);
146
+ return matches.filter((p) => p.match);
147
+ }
148
+ function filterPathMethods(pathMatches, targetMethod) {
149
+ const regExp = pathToRegexp(targetMethod);
150
+ return pathMatches.map((p) => {
151
+ const captures = Object.keys(p.operation).filter((r) => regExp.regexp.exec(r));
152
+ if (captures.length) {
153
+ const method = captures[0];
154
+ p.url.method = method.toUpperCase();
155
+ return {
156
+ url: p.url,
157
+ operation: p.operation[method]
158
+ };
159
+ }
160
+ return false;
161
+ }).filter((item) => Boolean(item));
162
+ }
163
+ function findTargetPath(pathMatches) {
164
+ if (!pathMatches.length) {
165
+ return void 0;
166
+ }
167
+ let minCount = Object.keys(pathMatches[0].url.slugs).length;
168
+ let found;
169
+ for (let m = 0; m < pathMatches.length; m += 1) {
170
+ const selection = pathMatches[m];
171
+ const paramCount = Object.keys(selection.url.slugs).length;
172
+ if (paramCount <= minCount) {
173
+ minCount = paramCount;
174
+ found = selection;
175
+ }
176
+ }
177
+ return found;
178
+ }
28
179
 
29
180
  // src/operation/lib/dedupe-common-parameters.ts
30
181
  function dedupeCommonParameters(parameters, commonParameters) {
@@ -42,7 +193,6 @@ function dedupeCommonParameters(parameters, commonParameters) {
42
193
 
43
194
  // src/samples/index.ts
44
195
  import mergeJSONSchemaAllOf from "json-schema-merge-allof";
45
- import memoize from "memoizee";
46
196
 
47
197
  // src/samples/utils.ts
48
198
  function usesPolymorphism(schema) {
@@ -255,8 +405,6 @@ function sampleFromResolvedSchema(schema, opts, seenRefs) {
255
405
  }
256
406
  return primitive(schema);
257
407
  }
258
- var memo = memoize(sampleFromSchema);
259
- var samples_default = memo;
260
408
 
261
409
  // src/operation/lib/get-mediatype-examples.ts
262
410
  function getMediaTypeExamples(mediaType, mediaTypeObject, definition, opts = {}) {
@@ -311,7 +459,7 @@ function getMediaTypeExamples(mediaType, mediaTypeObject, definition, opts = {})
311
459
  if (!matches_mimetype_default.xml(mediaType)) {
312
460
  return [
313
461
  {
314
- value: samples_default(structuredClone(mediaTypeObject.schema), {
462
+ value: sampleFromSchema(structuredClone(mediaTypeObject.schema), {
315
463
  ...opts,
316
464
  definition
317
465
  })
@@ -555,7 +703,7 @@ function getOperationId(path, method, operation, opts = {}) {
555
703
  if (operationIdExists) {
556
704
  operationId = sanitize(operationId);
557
705
  }
558
- operationId = operationId.replace(/^[0-9]/g, (match) => `_${match}`);
706
+ operationId = operationId.replace(/^[0-9]/g, (match2) => `_${match2}`);
559
707
  operationId = operationId.charAt(0).toLowerCase() + operationId.slice(1);
560
708
  if (operationId.startsWith(currMethod)) {
561
709
  return operationId;
@@ -793,24 +941,6 @@ var Operation = class {
793
941
  * Flattened out arrays of both request and response headers that are utilized on this operation.
794
942
  */
795
943
  headers;
796
- /**
797
- * Internal storage array that the library utilizes to keep track of the times the
798
- * {@see Operation.dereference} has been called so that if you initiate multiple promises they'll
799
- * all end up returning the same data set once the initial dereference call completed.
800
- */
801
- promises;
802
- /**
803
- * Internal storage array that the library utilizes to keep track of its `dereferencing` state so
804
- * it doesn't initiate multiple dereferencing processes.
805
- */
806
- dereferencing;
807
- /**
808
- * Have the component schemas within this API definition been decorated with our
809
- * `x-readme-ref-name` extension?
810
- *
811
- * @see {@link decorateComponentSchemas}
812
- */
813
- schemasDecorated = false;
814
944
  constructor(oas, path, method, operation) {
815
945
  this.oas = oas;
816
946
  this.schema = operation;
@@ -826,12 +956,6 @@ var Operation = class {
826
956
  request: [],
827
957
  response: []
828
958
  };
829
- this.promises = [];
830
- this.dereferencing = {
831
- processing: false,
832
- complete: false,
833
- circularRefs: []
834
- };
835
959
  }
836
960
  /**
837
961
  * Retrieve the `summary` for this operation.
@@ -865,6 +989,35 @@ var Operation = class {
865
989
  }
866
990
  return void 0;
867
991
  }
992
+ /**
993
+ * Retrieve the server objects that apply to this operation, using OpenAPI server precedence:
994
+ * operation-level servers, then path-item servers, then root-level servers.
995
+ */
996
+ getServers() {
997
+ if (this.schema.servers?.length) {
998
+ return this.schema.servers;
999
+ }
1000
+ if (this.api.paths?.[this.path]) {
1001
+ const pathItem = dereferenceRef(this.api.paths[this.path], this.api);
1002
+ if (pathItem && !isRef(pathItem) && pathItem.servers?.length) {
1003
+ return pathItem.servers;
1004
+ }
1005
+ }
1006
+ return this.api.servers || [];
1007
+ }
1008
+ url(selected = 0, variables) {
1009
+ const url = normalizedURLFromServers(this.getServers(), selected);
1010
+ return this.oas.replaceUrl(url, variables || this.defaultVariables(selected)).trim();
1011
+ }
1012
+ variables(selected = 0) {
1013
+ return variablesFromServers(this.getServers(), selected);
1014
+ }
1015
+ defaultVariables(selected = 0) {
1016
+ return defaultVariablesFromServers(this.getServers(), selected, this.oas.user);
1017
+ }
1018
+ splitUrl(selected = 0) {
1019
+ return splitUrlFromServers(this.getServers(), selected);
1020
+ }
868
1021
  /**
869
1022
  * Retrieve the primary content type for this operation. If multiple exist, the first JSON-like
870
1023
  * type will be returned.
@@ -1248,15 +1401,7 @@ var Operation = class {
1248
1401
  *
1249
1402
  */
1250
1403
  getParametersAsJSONSchema(opts = {}) {
1251
- if (this.isDereferenced()) {
1252
- throw new Error(
1253
- "`.getParametersAsJSONSchema()` is not compatible with an operation or OpenAPI definition that has been run through `.dereference().`"
1254
- );
1255
- }
1256
- if (!this.schemasDecorated) {
1257
- decorateComponentSchemasWithRefName(this.api);
1258
- this.schemasDecorated = true;
1259
- }
1404
+ decorateComponentSchemasWithRefName(this.api);
1260
1405
  return getParametersAsJSONSchema(this, this.api, {
1261
1406
  includeDiscriminatorMappingRefs: true,
1262
1407
  ...opts
@@ -1276,15 +1421,7 @@ var Operation = class {
1276
1421
  * this content-type, the function will return null.
1277
1422
  */
1278
1423
  getResponseAsJSONSchema(statusCode, opts = {}) {
1279
- if (this.isDereferenced()) {
1280
- throw new Error(
1281
- "`.getResponseAsJSONSchema()` is not compatible with an operation or OpenAPI definition that has been run through `.dereference().`"
1282
- );
1283
- }
1284
- if (!this.schemasDecorated) {
1285
- decorateComponentSchemasWithRefName(this.api);
1286
- this.schemasDecorated = true;
1287
- }
1424
+ decorateComponentSchemasWithRefName(this.api);
1288
1425
  return getResponseAsJSONSchema(this, this.api, statusCode, {
1289
1426
  includeDiscriminatorMappingRefs: true,
1290
1427
  ...opts
@@ -1630,107 +1767,6 @@ var Operation = class {
1630
1767
  this.exampleGroups = groups;
1631
1768
  return groups;
1632
1769
  }
1633
- /**
1634
- * Dereference the current operation schema so it can be parsed free of worries of `$ref` schemas
1635
- * and circular structures.
1636
- *
1637
- */
1638
- async dereference(opts) {
1639
- if (this.dereferencing.complete) {
1640
- return Promise.resolve(true);
1641
- }
1642
- if (this.dereferencing.processing) {
1643
- return new Promise((resolve, reject) => {
1644
- this.promises.push({ resolve, reject });
1645
- });
1646
- }
1647
- this.dereferencing.processing = true;
1648
- if (!this.schemasDecorated) {
1649
- decorateComponentSchemasWithRefName(this.api);
1650
- this.schemasDecorated = true;
1651
- }
1652
- const { api, schema, promises } = this;
1653
- const circularRefs = /* @__PURE__ */ new Set();
1654
- const dereferencingOptions = getDereferencingOptions(circularRefs);
1655
- const parser = new $RefParser();
1656
- return parser.dereference(
1657
- "#/__INTERNAL__",
1658
- {
1659
- // Because `json-schema-ref-parser` will dereference this entire object as we only want
1660
- // to dereference this operation schema we're attaching it to the `__INTERNAL__` key, and
1661
- // later using that to extract our dereferenced schema. If we didn't do this then we run
1662
- // the risk of any keyword in `schema` being overriden by `paths` and `components`.
1663
- //
1664
- // This solution isn't the best and still requires us to send `json-schema-ref-parser`
1665
- // basically the entire API defintiion but because we don't know what `$ref` pointers in
1666
- // `schema` reference, we can't know which parts of full API definition we could safely
1667
- // exclude from this process.
1668
- __INTERNAL__: structuredClone(schema),
1669
- paths: api.paths ?? void 0,
1670
- components: api.components ?? void 0
1671
- },
1672
- {
1673
- ...dereferencingOptions,
1674
- dereference: {
1675
- ...dereferencingOptions.dereference,
1676
- /**
1677
- * Because we only want to dereference our `__INTERNAL__` schema, not the **entire**
1678
- * API definition if the parser attemps to dereference anything but that then we
1679
- * should bail out of that crawler.
1680
- *
1681
- * @fixme this may cause issues where a path references a schema within itself to be ignored.
1682
- */
1683
- excludedPathMatcher: (path) => {
1684
- if (path === "#/paths" || path.startsWith("#/paths/")) {
1685
- return true;
1686
- }
1687
- return path === "#/components" || path.startsWith("#/components/");
1688
- }
1689
- }
1690
- }
1691
- ).then((res) => {
1692
- const dereferenced = res;
1693
- this.schema = dereferenced.__INTERNAL__;
1694
- this.promises = promises;
1695
- this.dereferencing = {
1696
- processing: false,
1697
- complete: true,
1698
- // We need to convert our `Set` to an array in order to match the typings.
1699
- circularRefs: [...circularRefs]
1700
- };
1701
- if (opts?.cb) {
1702
- opts.cb();
1703
- }
1704
- }).then(() => {
1705
- return this.promises.map((deferred) => deferred.resolve());
1706
- }).catch((err) => {
1707
- this.dereferencing.processing = false;
1708
- this.promises.map((deferred) => deferred.reject(err));
1709
- throw err;
1710
- });
1711
- }
1712
- /**
1713
- * Determine if the current operation schema, or the OpenAPI definition it's part of, has been
1714
- * dereferenced or not with `.dereference()`.
1715
- *
1716
- * @see Operation.dereference
1717
- */
1718
- isDereferenced() {
1719
- return this.oas.isDereferenced() || this.dereferencing.processing || this.dereferencing.complete;
1720
- }
1721
- /**
1722
- * Retrieve any circular `$ref` pointers that maybe present within operation schema.
1723
- *
1724
- * This method requires that you first dereference the definition.
1725
- *
1726
- * @see Operation.dereference
1727
- */
1728
- getCircularReferences() {
1729
- if (!this.dereferencing.complete) {
1730
- throw new Error(".dereference() must be called first in order for this method to obtain circular references.");
1731
- }
1732
- return this.dereferencing.circularRefs;
1733
- }
1734
1770
  };
1735
1771
  var Callback = class extends Operation {
1736
1772
  /**
@@ -1854,6 +1890,16 @@ var Webhook = class extends Operation {
1854
1890
  };
1855
1891
 
1856
1892
  export {
1893
+ getUserVariable,
1894
+ stripTrailingSlash,
1895
+ normalizedURLFromServers,
1896
+ variablesFromServers,
1897
+ defaultVariablesFromServers,
1898
+ splitUrlFromServers,
1899
+ transformURLIntoRegex,
1900
+ generatePathMatches,
1901
+ filterPathMethods,
1902
+ findTargetPath,
1857
1903
  Operation,
1858
1904
  Callback,
1859
1905
  Webhook
@@ -1870,4 +1916,4 @@ export {
1870
1916
  * @license Apache-2.0
1871
1917
  * @see {@link https://github.com/swagger-api/swagger-ui/blob/master/src/core/plugins/samples/fn.js}
1872
1918
  */
1873
- //# sourceMappingURL=chunk-35LEYZEY.js.map
1919
+ //# sourceMappingURL=chunk-WIVQX3DA.js.map