@kubb/plugin-zod 5.0.0-beta.25 → 5.0.0-beta.28

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.js CHANGED
@@ -126,6 +126,129 @@ function toRegExpString(text, func = "RegExp") {
126
126
  return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ""})`;
127
127
  }
128
128
  //#endregion
129
+ //#region ../../internals/utils/src/reserved.ts
130
+ /**
131
+ * JavaScript and Java reserved words.
132
+ * @link https://github.com/jonschlinkert/reserved/blob/master/index.js
133
+ */
134
+ const reservedWords = new Set([
135
+ "abstract",
136
+ "arguments",
137
+ "boolean",
138
+ "break",
139
+ "byte",
140
+ "case",
141
+ "catch",
142
+ "char",
143
+ "class",
144
+ "const",
145
+ "continue",
146
+ "debugger",
147
+ "default",
148
+ "delete",
149
+ "do",
150
+ "double",
151
+ "else",
152
+ "enum",
153
+ "eval",
154
+ "export",
155
+ "extends",
156
+ "false",
157
+ "final",
158
+ "finally",
159
+ "float",
160
+ "for",
161
+ "function",
162
+ "goto",
163
+ "if",
164
+ "implements",
165
+ "import",
166
+ "in",
167
+ "instanceof",
168
+ "int",
169
+ "interface",
170
+ "let",
171
+ "long",
172
+ "native",
173
+ "new",
174
+ "null",
175
+ "package",
176
+ "private",
177
+ "protected",
178
+ "public",
179
+ "return",
180
+ "short",
181
+ "static",
182
+ "super",
183
+ "switch",
184
+ "synchronized",
185
+ "this",
186
+ "throw",
187
+ "throws",
188
+ "transient",
189
+ "true",
190
+ "try",
191
+ "typeof",
192
+ "var",
193
+ "void",
194
+ "volatile",
195
+ "while",
196
+ "with",
197
+ "yield",
198
+ "Array",
199
+ "Date",
200
+ "hasOwnProperty",
201
+ "Infinity",
202
+ "isFinite",
203
+ "isNaN",
204
+ "isPrototypeOf",
205
+ "length",
206
+ "Math",
207
+ "name",
208
+ "NaN",
209
+ "Number",
210
+ "Object",
211
+ "prototype",
212
+ "String",
213
+ "toString",
214
+ "undefined",
215
+ "valueOf"
216
+ ]);
217
+ /**
218
+ * Returns `true` when `name` is a syntactically valid JavaScript variable name.
219
+ *
220
+ * @example
221
+ * ```ts
222
+ * isValidVarName('status') // true
223
+ * isValidVarName('class') // false (reserved word)
224
+ * isValidVarName('42foo') // false (starts with digit)
225
+ * ```
226
+ */
227
+ function isValidVarName(name) {
228
+ if (!name || reservedWords.has(name)) return false;
229
+ return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
230
+ }
231
+ /**
232
+ * Returns `name` when it's a syntactically valid JavaScript variable name,
233
+ * otherwise prefixes it with `_` so the result is a valid identifier.
234
+ *
235
+ * Useful for sanitizing OpenAPI schema names or operation IDs that start with
236
+ * a digit (e.g. `409`, `504AccountCancel`) before using them as exported
237
+ * variable, type, or function names.
238
+ *
239
+ * @example
240
+ * ```ts
241
+ * ensureValidVarName('409') // '_409'
242
+ * ensureValidVarName('504AccountCancel') // '_504AccountCancel'
243
+ * ensureValidVarName('Pet') // 'Pet'
244
+ * ensureValidVarName('class') // '_class'
245
+ * ```
246
+ */
247
+ function ensureValidVarName(name) {
248
+ if (!name || isValidVarName(name)) return name;
249
+ return `_${name}`;
250
+ }
251
+ //#endregion
129
252
  //#region src/components/Operations.tsx
130
253
  function Operations({ name, operations }) {
131
254
  const operationsJSON = operations.reduce((prev, acc) => {
@@ -800,11 +923,19 @@ const zodGenerator = defineGenerator({
800
923
  meta: meta.file.meta,
801
924
  banner: resolver.resolveBanner(ctx.meta, {
802
925
  output,
803
- config
926
+ config,
927
+ file: {
928
+ path: meta.file.path,
929
+ baseName: meta.file.baseName
930
+ }
804
931
  }),
805
932
  footer: resolver.resolveFooter(ctx.meta, {
806
933
  output,
807
- config
934
+ config,
935
+ file: {
936
+ path: meta.file.path,
937
+ baseName: meta.file.baseName
938
+ }
808
939
  }),
809
940
  children: [
810
941
  /* @__PURE__ */ jsx(File.Import, {
@@ -948,11 +1079,19 @@ const zodGenerator = defineGenerator({
948
1079
  meta: meta.file.meta,
949
1080
  banner: resolver.resolveBanner(ctx.meta, {
950
1081
  output,
951
- config
1082
+ config,
1083
+ file: {
1084
+ path: meta.file.path,
1085
+ baseName: meta.file.baseName
1086
+ }
952
1087
  }),
953
1088
  footer: resolver.resolveFooter(ctx.meta, {
954
1089
  output,
955
- config
1090
+ config,
1091
+ file: {
1092
+ path: meta.file.path,
1093
+ baseName: meta.file.baseName
1094
+ }
956
1095
  }),
957
1096
  children: [
958
1097
  /* @__PURE__ */ jsx(File.Import, {
@@ -1017,11 +1156,19 @@ const zodGenerator = defineGenerator({
1017
1156
  meta: meta.file.meta,
1018
1157
  banner: resolver.resolveBanner(ctx.meta, {
1019
1158
  output,
1020
- config
1159
+ config,
1160
+ file: {
1161
+ path: meta.file.path,
1162
+ baseName: meta.file.baseName
1163
+ }
1021
1164
  }),
1022
1165
  footer: resolver.resolveFooter(ctx.meta, {
1023
1166
  output,
1024
- config
1167
+ config,
1168
+ file: {
1169
+ path: meta.file.path,
1170
+ baseName: meta.file.baseName
1171
+ }
1025
1172
  }),
1026
1173
  children: [
1027
1174
  /* @__PURE__ */ jsx(File.Import, {
@@ -1059,19 +1206,20 @@ const resolverZod = defineResolver(() => {
1059
1206
  name: "default",
1060
1207
  pluginName: "plugin-zod",
1061
1208
  default(name, type) {
1062
- return camelCase(name, {
1209
+ const resolved = camelCase(name, {
1063
1210
  isFile: type === "file",
1064
1211
  suffix: type ? "schema" : void 0
1065
1212
  });
1213
+ return type === "file" ? resolved : ensureValidVarName(resolved);
1066
1214
  },
1067
1215
  resolveSchemaName(name) {
1068
- return camelCase(name, { suffix: "schema" });
1216
+ return ensureValidVarName(camelCase(name, { suffix: "schema" }));
1069
1217
  },
1070
1218
  resolveSchemaTypeName(name) {
1071
- return pascalCase(name, { suffix: "schema" });
1219
+ return ensureValidVarName(pascalCase(name, { suffix: "schema" }));
1072
1220
  },
1073
1221
  resolveTypeName(name) {
1074
- return pascalCase(name);
1222
+ return ensureValidVarName(pascalCase(name));
1075
1223
  },
1076
1224
  resolvePathName(name, type) {
1077
1225
  return this.default(name, type);