@kubb/oas 4.33.0 → 4.33.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/dist/index.cjs CHANGED
@@ -42,8 +42,6 @@ let node_fs = require("node:fs");
42
42
  node_fs = __toESM(node_fs);
43
43
  let node_path = require("node:path");
44
44
  node_path = __toESM(node_path);
45
- let _kubb_core_transformers = require("@kubb/core/transformers");
46
- let _kubb_core_utils = require("@kubb/core/utils");
47
45
  let _readme_openapi_parser = require("@readme/openapi-parser");
48
46
  let oas_types = require("oas/types");
49
47
  let oas_normalize = require("oas-normalize");
@@ -51,6 +49,177 @@ oas_normalize = __toESM(oas_normalize);
51
49
  let remeda = require("remeda");
52
50
  let swagger2openapi = require("swagger2openapi");
53
51
  swagger2openapi = __toESM(swagger2openapi);
52
+ //#region ../../internals/utils/src/casing.ts
53
+ /**
54
+ * Shared implementation for camelCase and PascalCase conversion.
55
+ * Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
56
+ * and capitalizes each word according to `pascal`.
57
+ *
58
+ * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
59
+ */
60
+ function toCamelOrPascal(text, pascal) {
61
+ return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
62
+ if (word.length > 1 && word === word.toUpperCase()) return word;
63
+ if (i === 0 && !pascal) return word.charAt(0).toLowerCase() + word.slice(1);
64
+ return word.charAt(0).toUpperCase() + word.slice(1);
65
+ }).join("").replace(/[^a-zA-Z0-9]/g, "");
66
+ }
67
+ /**
68
+ * Splits `text` on `.` and applies `transformPart` to each segment.
69
+ * The last segment receives `isLast = true`, all earlier segments receive `false`.
70
+ * Segments are joined with `/` to form a file path.
71
+ */
72
+ function applyToFileParts(text, transformPart) {
73
+ const parts = text.split(".");
74
+ return parts.map((part, i) => transformPart(part, i === parts.length - 1)).join("/");
75
+ }
76
+ /**
77
+ * Converts `text` to camelCase.
78
+ * When `isFile` is `true`, dot-separated segments are each cased independently and joined with `/`.
79
+ *
80
+ * @example
81
+ * camelCase('hello-world') // 'helloWorld'
82
+ * camelCase('pet.petId', { isFile: true }) // 'pet/petId'
83
+ */
84
+ function camelCase(text, { isFile, prefix = "", suffix = "" } = {}) {
85
+ if (isFile) return applyToFileParts(text, (part, isLast) => camelCase(part, isLast ? {
86
+ prefix,
87
+ suffix
88
+ } : {}));
89
+ return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
90
+ }
91
+ /**
92
+ * Converts `text` to PascalCase.
93
+ * When `isFile` is `true`, the last dot-separated segment is PascalCased and earlier segments are camelCased.
94
+ *
95
+ * @example
96
+ * pascalCase('hello-world') // 'HelloWorld'
97
+ * pascalCase('pet.petId', { isFile: true }) // 'pet/PetId'
98
+ */
99
+ function pascalCase(text, { isFile, prefix = "", suffix = "" } = {}) {
100
+ if (isFile) return applyToFileParts(text, (part, isLast) => isLast ? pascalCase(part, {
101
+ prefix,
102
+ suffix
103
+ }) : camelCase(part));
104
+ return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
105
+ }
106
+ //#endregion
107
+ //#region ../../internals/utils/src/reserved.ts
108
+ /**
109
+ * Returns `true` when `name` is a syntactically valid JavaScript variable name.
110
+ */
111
+ function isValidVarName(name) {
112
+ try {
113
+ new Function(`var ${name}`);
114
+ } catch {
115
+ return false;
116
+ }
117
+ return true;
118
+ }
119
+ //#endregion
120
+ //#region ../../internals/utils/src/urlPath.ts
121
+ /**
122
+ * Parses and transforms an OpenAPI/Swagger path string into various URL formats.
123
+ *
124
+ * @example
125
+ * const p = new URLPath('/pet/{petId}')
126
+ * p.URL // '/pet/:petId'
127
+ * p.template // '`/pet/${petId}`'
128
+ */
129
+ var URLPath = class {
130
+ /** The raw OpenAPI/Swagger path string, e.g. `/pet/{petId}`. */
131
+ path;
132
+ #options;
133
+ constructor(path, options = {}) {
134
+ this.path = path;
135
+ this.#options = options;
136
+ }
137
+ /** Converts the OpenAPI path to Express-style colon syntax, e.g. `/pet/{petId}` → `/pet/:petId`. */
138
+ get URL() {
139
+ return this.toURLPath();
140
+ }
141
+ /** Returns `true` when `path` is a fully-qualified URL (e.g. starts with `https://`). */
142
+ get isURL() {
143
+ try {
144
+ return !!new URL(this.path).href;
145
+ } catch {
146
+ return false;
147
+ }
148
+ }
149
+ /**
150
+ * Converts the OpenAPI path to a TypeScript template literal string.
151
+ *
152
+ * @example
153
+ * new URLPath('/pet/{petId}').template // '`/pet/${petId}`'
154
+ * new URLPath('/account/monetary-accountID').template // '`/account/${monetaryAccountId}`'
155
+ */
156
+ get template() {
157
+ return this.toTemplateString();
158
+ }
159
+ /** Returns the path and its extracted params as a structured `URLObject`, or as a stringified expression when `stringify` is set. */
160
+ get object() {
161
+ return this.toObject();
162
+ }
163
+ /** Returns a map of path parameter names, or `undefined` when the path has no parameters. */
164
+ get params() {
165
+ return this.getParams();
166
+ }
167
+ #transformParam(raw) {
168
+ const param = isValidVarName(raw) ? raw : camelCase(raw);
169
+ return this.#options.casing === "camelcase" ? camelCase(param) : param;
170
+ }
171
+ /** Iterates over every `{param}` token in `path`, calling `fn` with the raw token and transformed name. */
172
+ #eachParam(fn) {
173
+ for (const match of this.path.matchAll(/\{([^}]+)\}/g)) {
174
+ const raw = match[1];
175
+ fn(raw, this.#transformParam(raw));
176
+ }
177
+ }
178
+ toObject({ type = "path", replacer, stringify } = {}) {
179
+ const object = {
180
+ url: type === "path" ? this.toURLPath() : this.toTemplateString({ replacer }),
181
+ params: this.getParams()
182
+ };
183
+ if (stringify) {
184
+ if (type === "template") return JSON.stringify(object).replaceAll("'", "").replaceAll(`"`, "");
185
+ if (object.params) return `{ url: '${object.url}', params: ${JSON.stringify(object.params).replaceAll("'", "").replaceAll(`"`, "")} }`;
186
+ return `{ url: '${object.url}' }`;
187
+ }
188
+ return object;
189
+ }
190
+ /**
191
+ * Converts the OpenAPI path to a TypeScript template literal string.
192
+ * An optional `replacer` can transform each extracted parameter name before interpolation.
193
+ *
194
+ * @example
195
+ * new URLPath('/pet/{petId}').toTemplateString() // '`/pet/${petId}`'
196
+ */
197
+ toTemplateString({ prefix = "", replacer } = {}) {
198
+ return `\`${prefix}${this.path.split(/\{([^}]+)\}/).map((part, i) => {
199
+ if (i % 2 === 0) return part;
200
+ const param = this.#transformParam(part);
201
+ return `\${${replacer ? replacer(param) : param}}`;
202
+ }).join("")}\``;
203
+ }
204
+ /**
205
+ * Extracts all `{param}` segments from the path and returns them as a key-value map.
206
+ * An optional `replacer` transforms each parameter name in both key and value positions.
207
+ * Returns `undefined` when no path parameters are found.
208
+ */
209
+ getParams(replacer) {
210
+ const params = {};
211
+ this.#eachParam((_raw, param) => {
212
+ const key = replacer ? replacer(param) : param;
213
+ params[key] = key;
214
+ });
215
+ return Object.keys(params).length > 0 ? params : void 0;
216
+ }
217
+ /** Converts the OpenAPI path to Express-style colon syntax, e.g. `/pet/{petId}` → `/pet/:petId`. */
218
+ toURLPath() {
219
+ return this.path.replace(/\{([^}]+)\}/g, ":$1");
220
+ }
221
+ };
222
+ //#endregion
54
223
  //#region ../../node_modules/.pnpm/tslib@2.8.1/node_modules/tslib/tslib.es6.mjs
55
224
  var tslib_es6_exports = /* @__PURE__ */ __exportAll({
56
225
  __addDisposableResource: () => __addDisposableResource,
@@ -4502,7 +4671,7 @@ function parseFromConfig(config, oasClass = Oas) {
4502
4671
  }
4503
4672
  }
4504
4673
  if (Array.isArray(config.input)) return merge(config.input.map((input) => node_path.default.resolve(config.root, input.path)), { oasClass });
4505
- if (new _kubb_core_utils.URLPath(config.input.path).isURL) return parse(config.input.path, { oasClass });
4674
+ if (new URLPath(config.input.path).isURL) return parse(config.input.path, { oasClass });
4506
4675
  return parse(node_path.default.resolve(config.root, config.input.path), { oasClass });
4507
4676
  }
4508
4677
  /**
@@ -4616,7 +4785,7 @@ function resolveCollisions(schemasWithMeta) {
4616
4785
  const nameMapping = /* @__PURE__ */ new Map();
4617
4786
  const normalizedNames = /* @__PURE__ */ new Map();
4618
4787
  for (const item of schemasWithMeta) {
4619
- const normalized = (0, _kubb_core_transformers.pascalCase)(item.originalName);
4788
+ const normalized = pascalCase(item.originalName);
4620
4789
  if (!normalizedNames.has(normalized)) normalizedNames.set(normalized, []);
4621
4790
  normalizedNames.get(normalized).push(item);
4622
4791
  }
@@ -4650,6 +4819,12 @@ function resolveCollisions(schemasWithMeta) {
4650
4819
  }
4651
4820
  //#endregion
4652
4821
  //#region src/Oas.ts
4822
+ /**
4823
+ * Prefix used to create synthetic `$ref` values for anonymous (inline) discriminator schemas.
4824
+ * The suffix is the schema index within the discriminator's `oneOf`/`anyOf` array.
4825
+ * @example `#kubb-inline-0`
4826
+ */
4827
+ const KUBB_INLINE_REF_PREFIX = "#kubb-inline-";
4653
4828
  var Oas = class extends oas.default {
4654
4829
  #options = { discriminator: "strict" };
4655
4830
  document;
@@ -4748,7 +4923,7 @@ var Oas = class extends oas.default {
4748
4923
  }
4749
4924
  } else {
4750
4925
  const discriminatorValue = getDiscriminatorValue(schemaItem);
4751
- if (discriminatorValue) existingMapping[discriminatorValue] = `#kubb-inline-${index}`;
4926
+ if (discriminatorValue) existingMapping[discriminatorValue] = `${KUBB_INLINE_REF_PREFIX}${index}`;
4752
4927
  }
4753
4928
  });
4754
4929
  };
@@ -4955,6 +5130,25 @@ var Oas = class extends oas.default {
4955
5130
  }
4956
5131
  };
4957
5132
  //#endregion
5133
+ //#region src/resolveServerUrl.ts
5134
+ /**
5135
+ * Resolves an OpenAPI server URL by substituting `{variable}` placeholders
5136
+ * with values from `overrides` (user-provided) or the spec-defined defaults.
5137
+ *
5138
+ * Throws if an override value is not in the variable's `enum` list.
5139
+ */
5140
+ function resolveServerUrl(server, overrides) {
5141
+ if (!server.variables) return server.url;
5142
+ let url = server.url;
5143
+ for (const [key, variable] of Object.entries(server.variables)) {
5144
+ const value = overrides?.[key] ?? (variable.default != null ? String(variable.default) : void 0);
5145
+ if (value === void 0) continue;
5146
+ if (variable.enum?.length && !variable.enum.some((e) => String(e) === value)) throw new Error(`Invalid server variable value '${value}' for '${key}' when resolving ${server.url}. Valid values are: ${variable.enum.join(", ")}.`);
5147
+ url = url.replaceAll(`{${key}}`, value);
5148
+ }
5149
+ return url;
5150
+ }
5151
+ //#endregion
4958
5152
  //#region src/types.ts
4959
5153
  const HttpMethods = {
4960
5154
  GET: "get",
@@ -4968,6 +5162,7 @@ const HttpMethods = {
4968
5162
  };
4969
5163
  //#endregion
4970
5164
  exports.HttpMethods = HttpMethods;
5165
+ exports.KUBB_INLINE_REF_PREFIX = KUBB_INLINE_REF_PREFIX;
4971
5166
  exports.Oas = Oas;
4972
5167
  exports.getDefaultValue = getDefaultValue;
4973
5168
  exports.isAllOptional = isAllOptional;
@@ -4981,6 +5176,7 @@ exports.isRequired = isRequired;
4981
5176
  exports.merge = merge;
4982
5177
  exports.parse = parse;
4983
5178
  exports.parseFromConfig = parseFromConfig;
5179
+ exports.resolveServerUrl = resolveServerUrl;
4984
5180
  exports.validate = validate;
4985
5181
 
4986
5182
  //# sourceMappingURL=index.cjs.map