zod-codegen 1.0.0 → 1.0.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.
@@ -2,24 +2,37 @@ import { readFileSync } from 'node:fs';
2
2
  import { load } from 'js-yaml';
3
3
  import { OpenApiSpec } from '../types/openapi.js';
4
4
  export class SyncFileReaderService {
5
- readFile(path) {
6
- return readFileSync(path, 'utf8');
5
+ async readFile(path) {
6
+ // Check if path is a URL
7
+ try {
8
+ const url = new URL(path);
9
+ // If it's a valid URL, fetch it
10
+ const response = await fetch(url.toString());
11
+ if (!response.ok) {
12
+ throw new Error(`Failed to fetch ${path}: ${String(response.status)} ${response.statusText}`);
13
+ }
14
+ return await response.text();
15
+ }
16
+ catch {
17
+ // If URL parsing fails, treat it as a local file path
18
+ return readFileSync(path, 'utf8');
19
+ }
7
20
  }
8
21
  }
9
22
  export class OpenApiFileParserService {
10
23
  parse(input) {
11
- let parsedInput;
12
- if (typeof input === 'string') {
13
- try {
14
- parsedInput = JSON.parse(input);
15
- }
16
- catch {
17
- parsedInput = load(input);
18
- }
19
- }
20
- else {
21
- parsedInput = input;
22
- }
24
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
25
+ const parsedInput = typeof input === 'string'
26
+ ? (() => {
27
+ try {
28
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
29
+ return JSON.parse(input);
30
+ }
31
+ catch {
32
+ return load(input);
33
+ }
34
+ })()
35
+ : input;
23
36
  return OpenApiSpec.parse(parsedInput);
24
37
  }
25
38
  }
@@ -10,10 +10,7 @@ export class TypeScriptImportBuilderService {
10
10
  buildImports() {
11
11
  return [
12
12
  this.createImport('zod', {
13
- defaultImport: { z: false },
14
- }),
15
- this.createImport('path-to-regexp', {
16
- namedImports: { compile: false },
13
+ namedImports: { z: false },
17
14
  }),
18
15
  ];
19
16
  }
@@ -32,14 +29,10 @@ export class TypeScriptImportBuilderService {
32
29
  // Check if we have any imports at all
33
30
  const hasAnyImports = hasDefaultImport || namedImports;
34
31
  // For side effects imports, we can pass undefined as the import clause
35
- // For imports with bindings, we need to create the clause differently
32
+ // For imports with bindings, we need to create the clause using the factory
36
33
  return ts.factory.createImportDeclaration(undefined, hasAnyImports
37
- ? {
38
- kind: ts.SyntaxKind.ImportClause,
39
- isTypeOnly: false,
40
- name: hasDefaultImport && defaultImport ? ts.factory.createIdentifier(defaultImport) : undefined,
41
- namedBindings: namedImports,
42
- }
34
+ ? // eslint-disable-next-line @typescript-eslint/no-deprecated
35
+ ts.factory.createImportClause(false, hasDefaultImport && defaultImport ? ts.factory.createIdentifier(defaultImport) : undefined, namedImports ?? undefined)
43
36
  : undefined, ts.factory.createStringLiteral(target, true), undefined);
44
37
  }
45
38
  }
@@ -9,8 +9,20 @@ export class TypeScriptTypeBuilderService {
9
9
  case 'boolean':
10
10
  return ts.factory.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword);
11
11
  case 'unknown':
12
- default:
13
12
  return ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword);
13
+ default:
14
+ // Handle custom types (like Pet, Order, User, etc.) and array types
15
+ if (type.endsWith('[]')) {
16
+ const itemType = type.slice(0, -2);
17
+ return ts.factory.createArrayTypeNode(this.buildType(itemType));
18
+ }
19
+ // Handle Record types
20
+ if (type.startsWith('Record<')) {
21
+ // For now, return unknown for complex Record types
22
+ return ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword);
23
+ }
24
+ // Custom type name - create a type reference
25
+ return ts.factory.createTypeReferenceNode(ts.factory.createIdentifier(this.sanitizeIdentifier(type)), undefined);
14
26
  }
15
27
  }
16
28
  createProperty(name, type, isReadonly = false) {
@@ -24,14 +36,9 @@ export class TypeScriptTypeBuilderService {
24
36
  return ts.factory.createTypeParameterDeclaration(undefined, ts.factory.createIdentifier(name), undefined, undefined);
25
37
  }
26
38
  sanitizeIdentifier(name) {
27
- let sanitized = name.replace(/[^a-zA-Z0-9_]/g, '_');
28
- if (/^[0-9]/.test(sanitized)) {
29
- sanitized = '_' + sanitized;
30
- }
31
- if (sanitized.length === 0) {
32
- sanitized = '_';
33
- }
34
- return sanitized;
39
+ const sanitized = name.replace(/[^a-zA-Z0-9_]/g, '_');
40
+ const withPrefix = /^[0-9]/.test(sanitized) ? '_' + sanitized : sanitized;
41
+ return withPrefix.length === 0 ? '_' : withPrefix;
35
42
  }
36
43
  toCamelCase(word) {
37
44
  return word.charAt(0).toLowerCase() + word.slice(1);
@@ -54,7 +54,7 @@ const ServerVariable = z.object({
54
54
  enum: z.array(z.string()).optional(),
55
55
  });
56
56
  const Server = z.object({
57
- url: z.string().url(),
57
+ url: z.url(),
58
58
  description: z.string().optional(),
59
59
  variables: z.record(z.string(), ServerVariable).optional(),
60
60
  });
@@ -125,18 +125,18 @@ const Info = z.object({
125
125
  title: z.string().min(1),
126
126
  version: z.string().min(1),
127
127
  description: z.string().optional(),
128
- termsOfService: z.string().url().optional(),
128
+ termsOfService: z.url().optional(),
129
129
  contact: z
130
130
  .object({
131
131
  name: z.string().optional(),
132
- email: z.string().email().optional(),
133
- url: z.string().url().optional(),
132
+ email: z.email().optional(),
133
+ url: z.url().optional(),
134
134
  })
135
135
  .optional(),
136
136
  license: z
137
137
  .object({
138
138
  name: z.string().min(1),
139
- url: z.string().url().optional(),
139
+ url: z.url().optional(),
140
140
  })
141
141
  .optional(),
142
142
  });
@@ -148,7 +148,7 @@ const Tag = z.object({
148
148
  });
149
149
  const ExternalDocumentation = z.object({
150
150
  description: z.string().optional(),
151
- url: z.string().url(),
151
+ url: z.url(),
152
152
  });
153
153
  const Components = z.object({
154
154
  schemas: z.record(z.string(), SchemaProperties).optional(),
@@ -1,5 +1,6 @@
1
1
  import { beforeEach, describe, expect, it, vi } from 'vitest';
2
2
  import { Generator } from '../../src/generator.js';
3
+ import { Reporter } from '../../src/utils/reporter.js';
3
4
  describe('Generator', () => {
4
5
  let generator;
5
6
  let mockReporter;
package/eslint.config.mjs CHANGED
@@ -8,6 +8,8 @@ export default defineConfig([
8
8
  '**/*.mjs',
9
9
  'dist/**/*',
10
10
  'node_modules/**/*',
11
+ '**/*.test*.ts',
12
+ '**/coverage/*',
11
13
  ],
12
14
  },
13
15
  eslint.configs.recommended,
@@ -20,6 +22,14 @@ export default defineConfig([
20
22
  },
21
23
  },
22
24
  rules: {
25
+ 'no-var': 'error',
26
+ 'prefer-const': [
27
+ 'error',
28
+ {
29
+ destructuring: 'all',
30
+ ignoreReadBeforeAssign: false,
31
+ },
32
+ ],
23
33
  quotes: ['error', 'single', { avoidEscape: true }],
24
34
  'sort-imports': [
25
35
  'error',
@@ -28,6 +38,14 @@ export default defineConfig([
28
38
  ignoreDeclarationSort: true,
29
39
  },
30
40
  ],
41
+ '@typescript-eslint/no-deprecated': 'warn',
42
+ '@typescript-eslint/no-unused-vars': [
43
+ 'error',
44
+ {
45
+ argsIgnorePattern: '^_',
46
+ varsIgnorePattern: '^_',
47
+ },
48
+ ],
31
49
  },
32
50
  },
33
51
  ]);
package/package.json CHANGED
@@ -7,19 +7,19 @@
7
7
  "url": "https://github.com/julienandreu/zod-codegen/issues"
8
8
  },
9
9
  "dependencies": {
10
- "@apidevtools/swagger-parser": "^10.1.0",
10
+ "@apidevtools/swagger-parser": "^12.1.0",
11
11
  "debug": "^4.3.7",
12
- "js-yaml": "^4.1.0",
12
+ "js-yaml": "^4.1.1",
13
13
  "jsonpath": "^1.1.1",
14
14
  "loud-rejection": "^2.2.0",
15
15
  "openapi-types": "^12.1.3",
16
- "openapi-typescript": "^7.4.0",
16
+ "openapi-typescript": "^7.10.1",
17
17
  "path-to-regexp": "^8.2.0",
18
- "prettier": "^3.3.3",
19
- "typescript": "^5.7.2",
18
+ "prettier": "^3.6.2",
19
+ "typescript": "^5.9.3",
20
20
  "url-pattern": "^1.0.3",
21
21
  "yargs": "^18.0.0",
22
- "zod": "^3.23.8"
22
+ "zod": "^4.1.12"
23
23
  },
24
24
  "description": "A powerful TypeScript code generator that creates Zod schemas and type-safe clients from OpenAPI specifications",
25
25
  "keywords": [
@@ -34,30 +34,30 @@
34
34
  "validation"
35
35
  ],
36
36
  "devDependencies": {
37
- "@commitlint/cli": "^19.8.1",
38
- "@commitlint/config-conventional": "^19.8.1",
39
- "@eslint/js": "^9.17.0",
37
+ "@commitlint/cli": "^20.1.0",
38
+ "@commitlint/config-conventional": "^20.0.0",
39
+ "@eslint/js": "^9.39.1",
40
40
  "@semantic-release/changelog": "^6.0.3",
41
41
  "@semantic-release/git": "^10.0.1",
42
42
  "@types/debug": "^4.1.12",
43
43
  "@types/jest": "^30.0.0",
44
44
  "@types/js-yaml": "^4.0.9",
45
45
  "@types/jsonpath": "^0.2.4",
46
- "@types/node": "^22.10.2",
47
- "@types/yargs": "^17.0.33",
48
- "@vitest/coverage-v8": "^3.2.4",
49
- "eslint": "^9.17.0",
50
- "eslint-config-prettier": "^9.1.0",
46
+ "@types/node": "^24.10.1",
47
+ "@types/yargs": "^17.0.34",
48
+ "@vitest/coverage-v8": "^4.0.8",
49
+ "eslint": "^9.39.1",
50
+ "eslint-config-prettier": "^10.1.8",
51
51
  "husky": "^9.1.7",
52
- "lint-staged": "^16.1.6",
53
- "semantic-release": "^24.2.8",
52
+ "lint-staged": "^16.2.6",
53
+ "semantic-release": "^25.0.2",
54
54
  "ts-node": "^10.9.2",
55
- "typescript-eslint": "^8.18.1",
56
- "undici": "^6.21.0",
57
- "vitest": "^3.2.4"
55
+ "typescript-eslint": "^8.46.4",
56
+ "undici": "^7.16.0",
57
+ "vitest": "^4.0.8"
58
58
  },
59
59
  "optionalDependencies": {
60
- "undici": "^6.21.0"
60
+ "undici": "^7.16.0"
61
61
  },
62
62
  "homepage": "https://github.com/julienandreu/zod-codegen",
63
63
  "license": "MIT",
@@ -65,9 +65,9 @@
65
65
  "type": "module",
66
66
  "exports": {
67
67
  ".": {
68
- "types": "./dist/src/generator-v2.d.ts",
69
- "import": "./dist/src/generator-v2.js",
70
- "require": "./dist/src/generator-v2.js"
68
+ "types": "./dist/src/generator.d.ts",
69
+ "import": "./dist/src/generator.js",
70
+ "require": "./dist/src/generator.js"
71
71
  }
72
72
  },
73
73
  "repository": {
@@ -75,7 +75,7 @@
75
75
  "url": "git+ssh://git@github.com/julienandreu/zod-codegen.git"
76
76
  },
77
77
  "engines": {
78
- "node": ">=20.0.0"
78
+ "node": ">=24.11.1"
79
79
  },
80
80
  "scripts": {
81
81
  "build": "rm -rf dist && tsc --project tsconfig.json && cp -r src/assets dist/src/ && chmod +x ./dist/src/cli.js",
@@ -98,5 +98,5 @@
98
98
  "release": "semantic-release",
99
99
  "release:dry": "semantic-release --dry-run"
100
100
  },
101
- "version": "1.0.0"
101
+ "version": "1.0.2"
102
102
  }
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "zod-codegen",
3
- "version": "0.1.0-alpha.1",
4
- "description": "Zod code generator"
3
+ "version": "1.0.1",
4
+ "description": "A powerful TypeScript code generator that creates Zod schemas and type-safe clients from OpenAPI specifications"
5
5
  }
package/src/generator.ts CHANGED
@@ -24,7 +24,7 @@ export class Generator {
24
24
 
25
25
  async run(): Promise<number> {
26
26
  try {
27
- const rawSource = this.readFile();
27
+ const rawSource = await this.readFile();
28
28
  const openApiSpec = this.parseFile(rawSource);
29
29
  const generatedCode = this.generateCode(openApiSpec);
30
30
 
@@ -43,8 +43,8 @@ export class Generator {
43
43
  }
44
44
  }
45
45
 
46
- private readFile(): string {
47
- return this.fileReader.readFile(this.inputPath);
46
+ private async readFile(): Promise<string> {
47
+ return await this.fileReader.readFile(this.inputPath);
48
48
  }
49
49
 
50
50
  private parseFile(source: string): OpenApiSpecType {
@@ -1,4 +1,5 @@
1
- import {HttpClient, HttpError, HttpRequestConfig, HttpResponse} from '../types/http.js';
1
+ import type {HttpClient, HttpRequestConfig, HttpResponse} from '../types/http.js';
2
+ import {HttpError} from '../types/http.js';
2
3
 
3
4
  declare const globalThis: typeof global & {
4
5
  fetch?: typeof fetch;
@@ -167,13 +168,12 @@ export class FetchHttpClient implements HttpClient {
167
168
  }
168
169
 
169
170
  if (contentType.includes('text/')) {
170
- return (await response.text()) as unknown as TResponse;
171
+ return (await response.text()) as TResponse;
171
172
  }
172
173
 
173
174
  try {
174
175
  const text = await response.text();
175
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
176
- return text ? JSON.parse(text) : ({} as TResponse);
176
+ return text ? (JSON.parse(text) as TResponse) : ({} as TResponse);
177
177
  } catch {
178
178
  return {} as TResponse;
179
179
  }