@@ -4,30 +4,39 @@ import type {
44 OpenAPISecurityRequirement ,
55 OpenAPISecurityTypes ,
66 OpenAPITag ,
7- } from " ./Core.types.ts" ;
7+ } from ' ./Core.types.ts' ;
88import {
99 createEndpointBuilder ,
1010 type EndpointBuilder ,
11- } from " ./EndpointBuilder.ts" ;
12- import { createEndpointPath , type EndpointPath } from " ./EndpointPath.ts" ;
13- import type { AllowedLicenses } from " ./Licenses.types.ts" ;
11+ } from ' ./EndpointBuilder.ts' ;
12+ import { createEndpointPath , type EndpointPath } from ' ./EndpointPath.ts' ;
13+ import type { AllowedLicenses } from ' ./Licenses.types.ts' ;
1414
1515class OpenAPI {
1616 private raw : OpenAPICore ;
1717 private endpoints : EndpointBuilder [ ] ;
18+ private debug : boolean ;
1819
19- constructor ( ) {
20+ constructor ( { debug = false } : { debug ?: boolean } = { } ) {
2021 this . raw = {
21- openapi : " 3.1.0" ,
22+ openapi : ' 3.1.0' ,
2223 info : {
23- title : " OpenAPI 3.1.0" ,
24- version : " 1.0.0" ,
24+ title : ' OpenAPI 3.1.0' ,
25+ version : ' 1.0.0' ,
2526 } ,
2627 } ;
2728 this . endpoints = [ ] ;
29+ this . debug = debug ;
30+ }
31+
32+ private log ( level : string , message : string ) : void {
33+ if ( this . debug ) {
34+ console . log ( `@murat/openapi [${ level } ] ${ message } ` ) ;
35+ }
2836 }
2937
3038 getJSON ( ) : OpenAPICore {
39+ this . log ( 'info' , 'Generating OpenAPI JSON' ) ;
3140 // Create a deep copy of the raw object
3241 const result = JSON . parse ( JSON . stringify ( this . raw ) ) as OpenAPICore ;
3342
@@ -40,8 +49,8 @@ class OpenAPI {
4049 for ( const endpoint of this . endpoints ) {
4150 if ( ! endpoint . path || ! endpoint . method ) {
4251 console . warn (
43- " Endpoint is missing path or method, skipping" ,
44- endpoint ,
52+ ' Endpoint is missing path or method, skipping' ,
53+ endpoint
4554 ) ;
4655 continue ;
4756 }
@@ -62,100 +71,117 @@ class OpenAPI {
6271
6372 // Add an array of endpoints
6473 addEndpoints ( endpoints : EndpointBuilder [ ] ) : this {
65- this . endpoints . push ( ...endpoints ) ;
74+ this . log ( 'info' , `Adding ${ endpoints . length } endpoints` ) ;
75+ endpoints . forEach ( this . addEndpoint . bind ( this ) ) ;
6676 return this ;
6777 }
6878
6979 // Add a single endpoint
7080 addEndpoint ( endpoint : EndpointBuilder ) : this {
7181 this . endpoints . push ( endpoint ) ;
82+ this . log ( 'info' , `Endpoint added: ${ endpoint . method } ${ endpoint . path } ` ) ;
83+
7284 return this ;
7385 }
7486
7587 setTitle ( title : string ) : this {
88+ this . log ( 'info' , `Setting title: ${ title } ` ) ;
7689 this . raw . info . title = title ;
7790 return this ;
7891 }
7992
8093 setVersion ( version : string ) : this {
94+ this . log ( 'info' , `Setting version: ${ version } ` ) ;
8195 this . raw . info . version = version ;
8296 return this ;
8397 }
8498
8599 setDescription ( description : string ) : this {
100+ this . log ( 'info' , `Setting description` ) ;
86101 this . raw . info . description = description ;
87102 return this ;
88103 }
89104
90105 setTermsOfService ( termsOfService : string ) : this {
106+ this . log ( 'info' , `Setting terms of service: ${ termsOfService } ` ) ;
91107 this . raw . info . termsOfService = termsOfService ;
92108 return this ;
93109 }
94110
95111 setContactName ( name : string ) : this {
112+ this . log ( 'info' , `Setting contact name: ${ name } ` ) ;
96113 this . raw . info . contact = this . raw . info . contact || { } ;
97114 this . raw . info . contact . name = name ;
98115 return this ;
99116 }
100117
101118 setContactUrl ( url : string ) : this {
119+ this . log ( 'info' , `Setting contact URL: ${ url } ` ) ;
102120 this . raw . info . contact = this . raw . info . contact || { } ;
103121 this . raw . info . contact . url = url ;
104122 return this ;
105123 }
106124
107125 setContactEmail ( email : string ) : this {
126+ this . log ( 'info' , `Setting contact email: ${ email } ` ) ;
108127 this . raw . info . contact = this . raw . info . contact || { } ;
109128 this . raw . info . contact . email = email ;
110129 return this ;
111130 }
112131
113132 setLicenseName ( name : string ) : this {
114- this . raw . info . license = this . raw . info . license || { name : "" } ;
133+ this . log ( 'info' , `Setting license name: ${ name } ` ) ;
134+ this . raw . info . license = this . raw . info . license || { name : '' } ;
115135 this . raw . info . license . name = name ;
116136 return this ;
117137 }
118138
119139 setLicenseUrl ( url : string ) : this {
120- this . raw . info . license = this . raw . info . license || { name : "" } ;
140+ this . log ( 'info' , `Setting license URL: ${ url } ` ) ;
141+ this . raw . info . license = this . raw . info . license || { name : '' } ;
121142 this . raw . info . license . url = url ;
122143 return this ;
123144 }
124145
125146 setLicenseIdentifier ( identifier : AllowedLicenses ) : this {
126- this . raw . info . license = this . raw . info . license || { name : "" } ;
147+ this . log ( 'info' , `Setting license identifier: ${ identifier } ` ) ;
148+ this . raw . info . license = this . raw . info . license || { name : '' } ;
127149 this . raw . info . license . identifier = identifier ;
128150 return this ;
129151 }
130152
131153 addEndpointPath ( endpointPath : EndpointPath ) : this {
154+ this . log ( 'info' , `Adding endpoint path: ${ endpointPath . path } ` ) ;
132155 if ( ! this . raw . paths ) {
133156 this . raw . paths = { } ;
134157 }
135158
136159 if ( this . raw . paths [ endpointPath . path ] ) {
137- console . warn ( `Path ${ endpointPath . path } already exists, Overwriting it. ` ) ;
160+ this . log ( 'warn' , `Path ${ endpointPath . path } already exists, overwriting it` ) ;
138161 }
139162
140163 this . raw . paths [ endpointPath . path ] = endpointPath . pathItem ;
141164 return this ;
142165 }
143166
144167 setJsonSchemaDialect ( dialect : string ) : this {
168+ this . log ( 'info' , `Setting JSON schema dialect: ${ dialect } ` ) ;
145169 this . raw . jsonSchemaDialect = dialect ;
146170 return this ;
147171 }
148172
149173 setServers ( servers : { url : string ; description ?: string } [ ] ) : this {
174+ this . log ( 'info' , `Setting ${ servers . length } servers` ) ;
150175 this . raw . servers = servers ;
151176 return this ;
152177 }
153178
154179 setSecurity ( scheme : {
155- type : " apiKey" | " http" | " oauth2" | " openIdConnect" ;
180+ type : ' apiKey' | ' http' | ' oauth2' | ' openIdConnect' ;
156181 name : string ;
157182 scopes ?: string [ ] ;
158183 } ) : this {
184+ this . log ( 'info' , `Setting security scheme: ${ scheme . name } (${ scheme . type } )` ) ;
159185 if ( ! this . raw . security ) {
160186 this . raw . security = [ ] as never ;
161187 }
@@ -169,6 +195,7 @@ class OpenAPI {
169195 }
170196
171197 setTag ( name : string , description ?: string ) : this {
198+ this . log ( 'info' , `Adding tag: ${ name } ` ) ;
172199 if ( ! this . raw . tags ) {
173200 this . raw . tags = [ ] ;
174201 }
@@ -183,6 +210,7 @@ class OpenAPI {
183210 }
184211
185212 setTags ( tags : string [ ] ) : this {
213+ this . log ( 'info' , `Adding ${ tags . length } tags` ) ;
186214 if ( ! this . raw . tags ) {
187215 this . raw . tags = [ ] ;
188216 }
@@ -193,6 +221,7 @@ class OpenAPI {
193221 }
194222
195223 setExternalDocs ( url : string , description ?: string ) : this {
224+ this . log ( 'info' , `Setting external docs: ${ url } ` ) ;
196225 this . raw . externalDocs = {
197226 url,
198227 description,
@@ -204,16 +233,17 @@ class OpenAPI {
204233 name : string ,
205234 scheme : {
206235 type : OpenAPISecurityTypes ;
207- scheme ?: string ; // Correct property for HTTP auth
208- bearerFormat ?: string ; // For JWT format specification
209- name ?: string ; // For apiKey type
210- in ?: string ; // For apiKey type
211- openIdConnectUrl ?: string ; // For openIdConnect type
236+ scheme ?: string ;
237+ bearerFormat ?: string ;
238+ name ?: string ;
239+ in ?: string ;
240+ openIdConnectUrl ?: string ;
212241 // deno-lint-ignore no-explicit-any
213- flows ?: Record < string , any > ; // For oauth2 type
242+ flows ?: Record < string , any > ;
214243 description ?: string ;
215- } ,
244+ }
216245 ) : this {
246+ this . log ( 'info' , `Adding security schema: ${ name } (${ scheme . type } )` ) ;
217247 if ( ! this . raw . components ) {
218248 this . raw . components = { securitySchemes : { } } ;
219249 }
0 commit comments