oas 36.0.3 → 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-BJCTM2ME.cjs → chunk-GS43VKJH.cjs} +319 -270
  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-RQZ2BPMU.js → chunk-WIVQX3DA.js} +198 -149
  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 -2
  31. package/dist/chunk-4WJUHXQH.js +0 -866
  32. package/dist/chunk-4WJUHXQH.js.map +0 -1
  33. package/dist/chunk-6MDVLJ3A.js.map +0 -1
  34. package/dist/chunk-BJCTM2ME.cjs.map +0 -1
  35. package/dist/chunk-LJWHPW4H.cjs +0 -866
  36. package/dist/chunk-LJWHPW4H.cjs.map +0 -1
  37. package/dist/chunk-RQZ2BPMU.js.map +0 -1
  38. package/dist/chunk-SCWW2SNX.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) {
@@ -552,7 +703,7 @@ function getOperationId(path, method, operation, opts = {}) {
552
703
  if (operationIdExists) {
553
704
  operationId = sanitize(operationId);
554
705
  }
555
- operationId = operationId.replace(/^[0-9]/g, (match) => `_${match}`);
706
+ operationId = operationId.replace(/^[0-9]/g, (match2) => `_${match2}`);
556
707
  operationId = operationId.charAt(0).toLowerCase() + operationId.slice(1);
557
708
  if (operationId.startsWith(currMethod)) {
558
709
  return operationId;
@@ -790,24 +941,6 @@ var Operation = class {
790
941
  * Flattened out arrays of both request and response headers that are utilized on this operation.
791
942
  */
792
943
  headers;
793
- /**
794
- * Internal storage array that the library utilizes to keep track of the times the
795
- * {@see Operation.dereference} has been called so that if you initiate multiple promises they'll
796
- * all end up returning the same data set once the initial dereference call completed.
797
- */
798
- promises;
799
- /**
800
- * Internal storage array that the library utilizes to keep track of its `dereferencing` state so
801
- * it doesn't initiate multiple dereferencing processes.
802
- */
803
- dereferencing;
804
- /**
805
- * Have the component schemas within this API definition been decorated with our
806
- * `x-readme-ref-name` extension?
807
- *
808
- * @see {@link decorateComponentSchemas}
809
- */
810
- schemasDecorated = false;
811
944
  constructor(oas, path, method, operation) {
812
945
  this.oas = oas;
813
946
  this.schema = operation;
@@ -823,12 +956,6 @@ var Operation = class {
823
956
  request: [],
824
957
  response: []
825
958
  };
826
- this.promises = [];
827
- this.dereferencing = {
828
- processing: false,
829
- complete: false,
830
- circularRefs: []
831
- };
832
959
  }
833
960
  /**
834
961
  * Retrieve the `summary` for this operation.
@@ -862,6 +989,35 @@ var Operation = class {
862
989
  }
863
990
  return void 0;
864
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
+ }
865
1021
  /**
866
1022
  * Retrieve the primary content type for this operation. If multiple exist, the first JSON-like
867
1023
  * type will be returned.
@@ -1245,15 +1401,7 @@ var Operation = class {
1245
1401
  *
1246
1402
  */
1247
1403
  getParametersAsJSONSchema(opts = {}) {
1248
- if (this.isDereferenced()) {
1249
- throw new Error(
1250
- "`.getParametersAsJSONSchema()` is not compatible with an operation or OpenAPI definition that has been run through `.dereference().`"
1251
- );
1252
- }
1253
- if (!this.schemasDecorated) {
1254
- decorateComponentSchemasWithRefName(this.api);
1255
- this.schemasDecorated = true;
1256
- }
1404
+ decorateComponentSchemasWithRefName(this.api);
1257
1405
  return getParametersAsJSONSchema(this, this.api, {
1258
1406
  includeDiscriminatorMappingRefs: true,
1259
1407
  ...opts
@@ -1273,15 +1421,7 @@ var Operation = class {
1273
1421
  * this content-type, the function will return null.
1274
1422
  */
1275
1423
  getResponseAsJSONSchema(statusCode, opts = {}) {
1276
- if (this.isDereferenced()) {
1277
- throw new Error(
1278
- "`.getResponseAsJSONSchema()` is not compatible with an operation or OpenAPI definition that has been run through `.dereference().`"
1279
- );
1280
- }
1281
- if (!this.schemasDecorated) {
1282
- decorateComponentSchemasWithRefName(this.api);
1283
- this.schemasDecorated = true;
1284
- }
1424
+ decorateComponentSchemasWithRefName(this.api);
1285
1425
  return getResponseAsJSONSchema(this, this.api, statusCode, {
1286
1426
  includeDiscriminatorMappingRefs: true,
1287
1427
  ...opts
@@ -1627,107 +1767,6 @@ var Operation = class {
1627
1767
  this.exampleGroups = groups;
1628
1768
  return groups;
1629
1769
  }
1630
- /**
1631
- * Dereference the current operation schema so it can be parsed free of worries of `$ref` schemas
1632
- * and circular structures.
1633
- *
1634
- */
1635
- async dereference(opts) {
1636
- if (this.dereferencing.complete) {
1637
- return Promise.resolve(true);
1638
- }
1639
- if (this.dereferencing.processing) {
1640
- return new Promise((resolve, reject) => {
1641
- this.promises.push({ resolve, reject });
1642
- });
1643
- }
1644
- this.dereferencing.processing = true;
1645
- if (!this.schemasDecorated) {
1646
- decorateComponentSchemasWithRefName(this.api);
1647
- this.schemasDecorated = true;
1648
- }
1649
- const { api, schema, promises } = this;
1650
- const circularRefs = /* @__PURE__ */ new Set();
1651
- const dereferencingOptions = getDereferencingOptions(circularRefs);
1652
- const parser = new $RefParser();
1653
- return parser.dereference(
1654
- "#/__INTERNAL__",
1655
- {
1656
- // Because `json-schema-ref-parser` will dereference this entire object as we only want
1657
- // to dereference this operation schema we're attaching it to the `__INTERNAL__` key, and
1658
- // later using that to extract our dereferenced schema. If we didn't do this then we run
1659
- // the risk of any keyword in `schema` being overriden by `paths` and `components`.
1660
- //
1661
- // This solution isn't the best and still requires us to send `json-schema-ref-parser`
1662
- // basically the entire API defintiion but because we don't know what `$ref` pointers in
1663
- // `schema` reference, we can't know which parts of full API definition we could safely
1664
- // exclude from this process.
1665
- __INTERNAL__: structuredClone(schema),
1666
- paths: api.paths ?? void 0,
1667
- components: api.components ?? void 0
1668
- },
1669
- {
1670
- ...dereferencingOptions,
1671
- dereference: {
1672
- ...dereferencingOptions.dereference,
1673
- /**
1674
- * Because we only want to dereference our `__INTERNAL__` schema, not the **entire**
1675
- * API definition if the parser attemps to dereference anything but that then we
1676
- * should bail out of that crawler.
1677
- *
1678
- * @fixme this may cause issues where a path references a schema within itself to be ignored.
1679
- */
1680
- excludedPathMatcher: (path) => {
1681
- if (path === "#/paths" || path.startsWith("#/paths/")) {
1682
- return true;
1683
- }
1684
- return path === "#/components" || path.startsWith("#/components/");
1685
- }
1686
- }
1687
- }
1688
- ).then((res) => {
1689
- const dereferenced = res;
1690
- this.schema = dereferenced.__INTERNAL__;
1691
- this.promises = promises;
1692
- this.dereferencing = {
1693
- processing: false,
1694
- complete: true,
1695
- // We need to convert our `Set` to an array in order to match the typings.
1696
- circularRefs: [...circularRefs]
1697
- };
1698
- if (opts?.cb) {
1699
- opts.cb();
1700
- }
1701
- }).then(() => {
1702
- return this.promises.map((deferred) => deferred.resolve());
1703
- }).catch((err) => {
1704
- this.dereferencing.processing = false;
1705
- this.promises.map((deferred) => deferred.reject(err));
1706
- throw err;
1707
- });
1708
- }
1709
- /**
1710
- * Determine if the current operation schema, or the OpenAPI definition it's part of, has been
1711
- * dereferenced or not with `.dereference()`.
1712
- *
1713
- * @see Operation.dereference
1714
- */
1715
- isDereferenced() {
1716
- return this.oas.isDereferenced() || this.dereferencing.processing || this.dereferencing.complete;
1717
- }
1718
- /**
1719
- * Retrieve any circular `$ref` pointers that maybe present within operation schema.
1720
- *
1721
- * This method requires that you first dereference the definition.
1722
- *
1723
- * @see Operation.dereference
1724
- */
1725
- getCircularReferences() {
1726
- if (!this.dereferencing.complete) {
1727
- throw new Error(".dereference() must be called first in order for this method to obtain circular references.");
1728
- }
1729
- return this.dereferencing.circularRefs;
1730
- }
1731
1770
  };
1732
1771
  var Callback = class extends Operation {
1733
1772
  /**
@@ -1851,6 +1890,16 @@ var Webhook = class extends Operation {
1851
1890
  };
1852
1891
 
1853
1892
  export {
1893
+ getUserVariable,
1894
+ stripTrailingSlash,
1895
+ normalizedURLFromServers,
1896
+ variablesFromServers,
1897
+ defaultVariablesFromServers,
1898
+ splitUrlFromServers,
1899
+ transformURLIntoRegex,
1900
+ generatePathMatches,
1901
+ filterPathMethods,
1902
+ findTargetPath,
1854
1903
  Operation,
1855
1904
  Callback,
1856
1905
  Webhook
@@ -1867,4 +1916,4 @@ export {
1867
1916
  * @license Apache-2.0
1868
1917
  * @see {@link https://github.com/swagger-api/swagger-ui/blob/master/src/core/plugins/samples/fn.js}
1869
1918
  */
1870
- //# sourceMappingURL=chunk-RQZ2BPMU.js.map
1919
+ //# sourceMappingURL=chunk-WIVQX3DA.js.map