1- import { version } from " ../package.json" ;
2- import { zodToTs } from " zod-to-ts" ;
3- import ts from " typescript" ;
4- import prettier from " prettier" ;
1+ import { version } from ' ../package.json' ;
2+ import { zodToTs } from ' zod-to-ts' ;
3+ import ts from ' typescript' ;
4+ import prettier from ' prettier' ;
55
66// Helper to print TypeScript node as string
77function printNode ( node : ts . Node ) : string {
88 const printer = ts . createPrinter ( { newLine : ts . NewLineKind . LineFeed } ) ;
9- const sourceFile = ts . createSourceFile (
10- "temp.ts" ,
11- "" ,
12- ts . ScriptTarget . Latest ,
13- false ,
14- ts . ScriptKind . TS
15- ) ;
9+ const sourceFile = ts . createSourceFile ( 'temp.ts' , '' , ts . ScriptTarget . Latest , false , ts . ScriptKind . TS ) ;
1610 return printer . printNode ( ts . EmitHint . Unspecified , node , sourceFile ) ;
1711}
1812
1913// Import all schemas
20- import {
21- ResourceSchema ,
22- TimestampsSchema ,
23- } from "../src/types/schemas/base.schema" ;
14+ import { ResourceSchema , TimestampsSchema } from '../src/types/schemas/base.schema' ;
2415
25- import { LoginResponseSchema } from " ../src/types/schemas/auth.schema" ;
16+ import { LoginResponseSchema } from ' ../src/types/schemas/auth.schema' ;
2617
27- import {
28- SelfSchema ,
29- ResourceLimitsSchema ,
30- } from "../src/types/schemas/user.schema" ;
18+ import { SelfSchema , ResourceLimitsSchema } from '../src/types/schemas/user.schema' ;
3119
3220import {
3321 EnvironmentSchema ,
3422 EnvironmentCreateInputSchema ,
3523 EnvironmentUpdateInputSchema ,
3624 EnvironmentValueSchema ,
37- EnvironmentValueUpdateInputSchema ,
38- } from " ../src/types/schemas/environment.schema" ;
25+ EnvironmentValueUpdateInputSchema
26+ } from ' ../src/types/schemas/environment.schema' ;
3927
40- import {
41- CronSchema ,
42- CronCreateInputSchema ,
43- CronUpdateInputSchema ,
44- } from "../src/types/schemas/cron.schema" ;
28+ import { CronSchema , CronCreateInputSchema , CronUpdateInputSchema } from '../src/types/schemas/cron.schema' ;
4529
46- import {
47- DomainSchema ,
48- DomainCreateInputSchema ,
49- } from "../src/types/schemas/domain.schema" ;
30+ import { DomainSchema , DomainCreateInputSchema } from '../src/types/schemas/domain.schema' ;
5031
5132import {
5233 WorkerSchema ,
5334 WorkerCreateInputSchema ,
5435 WorkerUpdateInputSchema ,
55- WorkerLanguageSchema ,
56- } from " ../src/types/schemas/worker.schema" ;
36+ WorkerLanguageSchema
37+ } from ' ../src/types/schemas/worker.schema' ;
5738
5839// Schema definitions to generate
5940const schemas = [
6041 // Base schemas
61- { schema : ResourceSchema , name : " Resource" } ,
62- { schema : TimestampsSchema , name : " Timestamps" } ,
42+ { schema : ResourceSchema , name : ' Resource' } ,
43+ { schema : TimestampsSchema , name : ' Timestamps' } ,
6344
6445 // Auth
65- { schema : LoginResponseSchema , name : " LoginResponse" } ,
46+ { schema : LoginResponseSchema , name : ' LoginResponse' } ,
6647
6748 // User
68- { schema : SelfSchema , name : " Self" } ,
69- { schema : ResourceLimitsSchema , name : " ResourceLimits" } ,
49+ { schema : SelfSchema , name : ' Self' } ,
50+ { schema : ResourceLimitsSchema , name : ' ResourceLimits' } ,
7051
7152 // Environment
72- { schema : EnvironmentSchema , name : " Environment" } ,
73- { schema : EnvironmentCreateInputSchema , name : " EnvironmentCreateInput" } ,
74- { schema : EnvironmentUpdateInputSchema , name : " EnvironmentUpdateInput" } ,
75- { schema : EnvironmentValueSchema , name : " EnvironmentValue" } ,
53+ { schema : EnvironmentSchema , name : ' Environment' } ,
54+ { schema : EnvironmentCreateInputSchema , name : ' EnvironmentCreateInput' } ,
55+ { schema : EnvironmentUpdateInputSchema , name : ' EnvironmentUpdateInput' } ,
56+ { schema : EnvironmentValueSchema , name : ' EnvironmentValue' } ,
7657 {
7758 schema : EnvironmentValueUpdateInputSchema ,
78- name : " EnvironmentValueUpdateInput" ,
59+ name : ' EnvironmentValueUpdateInput'
7960 } ,
8061
8162 // Cron
82- { schema : CronSchema , name : " Cron" } ,
83- { schema : CronCreateInputSchema , name : " CronCreateInput" } ,
84- { schema : CronUpdateInputSchema , name : " CronUpdateInput" } ,
63+ { schema : CronSchema , name : ' Cron' } ,
64+ { schema : CronCreateInputSchema , name : ' CronCreateInput' } ,
65+ { schema : CronUpdateInputSchema , name : ' CronUpdateInput' } ,
8566
8667 // Domain
87- { schema : DomainSchema , name : " Domain" } ,
88- { schema : DomainCreateInputSchema , name : " DomainCreateInput" } ,
68+ { schema : DomainSchema , name : ' Domain' } ,
69+ { schema : DomainCreateInputSchema , name : ' DomainCreateInput' } ,
8970
9071 // Worker
91- { schema : WorkerSchema , name : " Worker" } ,
92- { schema : WorkerCreateInputSchema , name : " WorkerCreateInput" } ,
93- { schema : WorkerUpdateInputSchema , name : " WorkerUpdateInput" } ,
94- { schema : WorkerLanguageSchema , name : " WorkerLanguage" } ,
72+ { schema : WorkerSchema , name : ' Worker' } ,
73+ { schema : WorkerCreateInputSchema , name : ' WorkerCreateInput' } ,
74+ { schema : WorkerUpdateInputSchema , name : ' WorkerUpdateInput' } ,
75+ { schema : WorkerLanguageSchema , name : ' WorkerLanguage' }
9576] ;
9677
9778async function generateTypes ( ) {
98- console . log ( " 🔄 Generating TypeScript types from Zod schemas..." ) ;
79+ console . log ( ' 🔄 Generating TypeScript types from Zod schemas...' ) ;
9980
10081 // Clean dist directory
10182 const distDir = `${ import . meta. dir } /../dist` ;
102- console . log ( " 🧹 Cleaning dist directory..." ) ;
83+ console . log ( ' 🧹 Cleaning dist directory...' ) ;
10384 try {
10485 await Bun . $ `rm -rf ${ distDir } ` ;
10586 } catch ( error ) {
@@ -111,30 +92,28 @@ async function generateTypes() {
11192 let idCounter = 0 ;
11293 const auxiliaryTypeStore = {
11394 nextId : ( ) => `T${ idCounter ++ } ` ,
114- definitions : new Map ( ) ,
95+ definitions : new Map ( )
11596 } ;
11697
117- console . log ( " 📋 First pass: building types map..." ) ;
98+ console . log ( ' 📋 First pass: building types map...' ) ;
11899 for ( const { schema, name } of schemas ) {
119100 try {
120101 const { node } = zodToTs ( schema , { auxiliaryTypeStore } ) ;
121102 let typeStr = printNode ( node ) ;
122103 // Normalize whitespace for better matching
123- typeStr = typeStr . replace ( / \s + / g, " " ) . trim ( ) ;
104+ typeStr = typeStr . replace ( / \s + / g, ' ' ) . trim ( ) ;
124105 typesMap . set ( typeStr , name ) ;
125106 } catch ( error ) {
126107 console . error ( `❌ Failed to generate type for ${ name } :` , error ) ;
127108 }
128109 }
129110
130111 // SECOND PASS: Replace nested types with references
131- console . log ( " 🔄 Second pass: replacing nested types..." ) ;
112+ console . log ( ' 🔄 Second pass: replacing nested types...' ) ;
132113 const finalTypesMap = new Map < string , string > ( ) ;
133114
134115 // Sort types by length (longest first) for better matching
135- const sortedTypes = Array . from ( typesMap . entries ( ) ) . sort (
136- ( [ a ] , [ b ] ) => b . length - a . length
137- ) ;
116+ const sortedTypes = Array . from ( typesMap . entries ( ) ) . sort ( ( [ a ] , [ b ] ) => b . length - a . length ) ;
138117
139118 for ( const [ typeStr , name ] of typesMap ) {
140119 let updatedTypeStr = typeStr ;
@@ -143,18 +122,15 @@ async function generateTypes() {
143122 for ( const [ otherTypeStr , otherName ] of sortedTypes ) {
144123 if ( otherName !== name && updatedTypeStr . includes ( otherTypeStr ) ) {
145124 // Use a more precise replacement to avoid partial matches
146- updatedTypeStr = updatedTypeStr . replaceAll (
147- otherTypeStr ,
148- `I${ otherName } `
149- ) ;
125+ updatedTypeStr = updatedTypeStr . replaceAll ( otherTypeStr , `I${ otherName } ` ) ;
150126 }
151127 }
152128
153129 finalTypesMap . set ( name , updatedTypeStr ) ;
154130 }
155131
156132 // THIRD PASS: Recursive replacement until no more changes
157- console . log ( " 🔁 Third pass: recursive replacement..." ) ;
133+ console . log ( ' 🔁 Third pass: recursive replacement...' ) ;
158134 let changed = true ;
159135 let iterations = 0 ;
160136 const maxIterations = 10 ;
@@ -168,10 +144,7 @@ async function generateTypes() {
168144
169145 for ( const [ otherTypeStr , otherName ] of sortedTypes ) {
170146 if ( otherName !== name && updatedTypeStr . includes ( otherTypeStr ) ) {
171- const newStr = updatedTypeStr . replaceAll (
172- otherTypeStr ,
173- `I${ otherName } `
174- ) ;
147+ const newStr = updatedTypeStr . replaceAll ( otherTypeStr , `I${ otherName } ` ) ;
175148 if ( newStr !== updatedTypeStr ) {
176149 updatedTypeStr = newStr ;
177150 changed = true ;
@@ -214,46 +187,43 @@ export type Dictionary<T> = Record<string, T>;
214187 }
215188
216189 // Format with prettier
217- console . log ( " 🎨 Formatting with prettier..." ) ;
190+ console . log ( ' 🎨 Formatting with prettier...' ) ;
218191 const formatted = await prettier . format ( output , {
219- parser : " typescript" ,
192+ parser : ' typescript' ,
220193 semi : true ,
221194 singleQuote : true ,
222- trailingComma : " all" ,
195+ trailingComma : ' all'
223196 } ) ;
224197
225198 // Write types file
226199 await Bun . write ( `${ distDir } /types.d.ts` , formatted ) ;
227200
228201 // Generate package.json for npm publish
229202 const packageJson = {
230- name : " @openworkers/api-types" ,
203+ name : ' @openworkers/api-types' ,
231204 version,
232- license : " MIT" ,
233- type : " module" ,
205+ license : ' MIT' ,
206+ type : ' module' ,
234207 private : false ,
235- main : " ./types.d.ts" ,
236- types : " ./types.d.ts" ,
208+ main : ' ./types.d.ts' ,
209+ types : ' ./types.d.ts' ,
237210 exports : {
238- "." : " ./types.d.ts" ,
211+ '.' : ' ./types.d.ts'
239212 } ,
240- description : " TypeScript types for OpenWorkers API" ,
241- keywords : [ " openworkers" , " types" , " typescript" ] ,
213+ description : ' TypeScript types for OpenWorkers API' ,
214+ keywords : [ ' openworkers' , ' types' , ' typescript' ] ,
242215 repository : {
243- type : " git" ,
244- url : " git+https://github.com/openworkers/openworkers-api.git" ,
216+ type : ' git' ,
217+ url : ' git+https://github.com/openworkers/openworkers-api.git'
245218 } ,
246219 publishConfig : {
247- access : " public" ,
248- registry : " https://registry.npmjs.org/" ,
249- provenance : true ,
250- } ,
220+ access : ' public' ,
221+ registry : ' https://registry.npmjs.org/' ,
222+ provenance : true
223+ }
251224 } ;
252225
253- await Bun . write (
254- `${ distDir } /package.json` ,
255- JSON . stringify ( packageJson , null , 2 )
256- ) ;
226+ await Bun . write ( `${ distDir } /package.json` , JSON . stringify ( packageJson , null , 2 ) ) ;
257227
258228 // Copy LICENSE
259229 const license = await Bun . file ( `${ import . meta. dir } /../LICENSE` ) . text ( ) ;
0 commit comments