@@ -24,29 +24,25 @@ import * as fs from 'fs'
2424import * as yaml from 'js-yaml'
2525
2626/**
27- * Scan the API folders in the specification to locate examples
27+ * Scan the API folders in the specification to locate examples
2828 * for all the API endpoints. Then add the examples to the model.
2929 */
3030export default class ExamplesProcessor {
31-
3231 specsFolder : string
3332
34- constructor ( specsFolder : string )
35- {
33+ constructor ( specsFolder : string ) {
3634 this . specsFolder = specsFolder
3735 }
3836
39- // Add request and response examples for all the endpoints in the model.
37+ // Add request and response examples for all the endpoints in the model.
4038 // Note that the 'jsonSpec' is a parameter that is passed to a 'Step'.
4139 // We don't need that parameter for the the 'addExamples' functionality.
4240 async addExamples ( model : model . Model , jsonSpec : Map < string , JsonSpec > ) : Promise < model . Model > {
4341 const requestExamplesProcessor = new RequestExamplesProcessor ( model , this . specsFolder )
4442 const responseExamplesProcessor = new ResponseExamplesProcessor ( model , this . specsFolder )
4543 for ( const endpoint of model . endpoints ) {
46- if ( endpoint . request != null )
47- requestExamplesProcessor . addExamples ( endpoint . request )
48- if ( endpoint . response != null )
49- responseExamplesProcessor . addExamples ( endpoint . response )
44+ if ( endpoint . request != null ) { requestExamplesProcessor . addExamples ( endpoint . request ) }
45+ if ( endpoint . response != null ) { responseExamplesProcessor . addExamples ( endpoint . response ) }
5046 }
5147 return model
5248 }
@@ -65,18 +61,18 @@ class BaseExamplesProcessor {
6561 }
6662
6763 // Log a 'warning' message.
68- warning ( message : string ) : void {
69- console . warn ( " === [ExamplesProcessor]: " + message )
64+ warning ( message : string ) : void {
65+ console . warn ( ' === [ExamplesProcessor]: ' + message )
7066 }
7167
72- // Get all the subfolders in a folder.
68+ // Get all the subfolders in a folder.
7369 getSubfolders ( folderPath : string ) : string [ ] {
7470 const entries = fs . readdirSync ( folderPath , { withFileTypes : true } )
7571 const folders = entries
7672 . filter ( entry => entry . isDirectory ( ) )
7773 . map ( entry => entry . name )
7874 return folders
79- }
75+ }
8076
8177 // Get all the files in a folder.
8278 getFilesInFolder ( folderPath : string ) : string [ ] {
@@ -90,39 +86,41 @@ class BaseExamplesProcessor {
9086 // Check if a path exists and is a directory.
9187 isDirectory ( path : string ) : boolean {
9288 try {
93- const stats = fs . statSync ( path ) ;
94- return stats . isDirectory ( ) ;
89+ const stats = fs . statSync ( path )
90+ return stats . isDirectory ( )
9591 } catch ( error ) {
9692 if ( error . code === 'ENOENT' ) {
9793 // Path does not exist
98- return false ;
94+ return false
9995 } else {
10096 // Other error, rethrow
101- throw error ;
97+ throw error
10298 }
10399 }
104100 }
105101
106102 // Given the spec location of a request or response,
107103 // return the path to the examples folder for that
108104 // request or response.
109- getExamplesFolder ( specLocation : string ) : string | undefined {
105+ getExamplesFolder ( specLocation : string ) : string | undefined {
110106 const specDir = path . dirname ( specLocation )
111107 const specPath = path . join ( this . specsFolder , specDir )
112- const examplesFolder = path . join ( specPath , "examples" )
113- if ( this . isDirectory ( examplesFolder ) )
114- return examplesFolder
108+ const examplesFolder = path . join ( specPath , 'examples' )
109+ if ( this . isDirectory ( examplesFolder ) ) {
110+ return examplesFolder
111+ }
115112 return undefined
116113 }
117114
118115 // Given an examples request or response folder, return all the
119116 // valid example files in that folder.
120117 getExampleFiles ( folder : string ) : string [ ] {
121- if ( ! this . isDirectory ( folder ) )
118+ if ( ! this . isDirectory ( folder ) ) {
122119 return [ ]
123- // Currently we only allow YAML example files.
120+ }
121+ // Currently we only allow YAML example files.
124122 const exampleFiles = this . getFilesInFolder ( folder )
125- . filter ( file => file . endsWith ( '.yml' ) || file . endsWith ( '.yaml' ) )
123+ . filter ( file => file . endsWith ( '.yml' ) || file . endsWith ( '.yaml' ) )
126124 if ( exampleFiles . length === 0 ) {
127125 this . warning ( `No example files found in ${ folder } ` )
128126 return [ ]
@@ -132,34 +130,33 @@ class BaseExamplesProcessor {
132130
133131 // Look up all the example files in a folder. Use the filename without extension
134132 // as the name of the example, and the YAML content as the example value.
135- // Return a map of example names to example values.
136- getExampleMap ( folder : string ) : Map < string , model . Example > {
133+ // Return a map of example names to example values.
134+ getExampleMap ( folder : string ) : Map < string , model . Example > {
137135 const exampleFiles = this . getExampleFiles ( folder )
138136 const examples = new Map < string , model . Example > ( )
139137 for ( const fileName of exampleFiles ) {
140- const filePath = path . join ( folder , fileName )
138+ const filePath = path . join ( folder , fileName )
141139 const exampleFileContent = fs . readFileSync ( filePath , 'utf8' )
142140 const exampleName = path . basename ( fileName , path . extname ( fileName ) )
143141 const example : model . Example = yaml . load ( exampleFileContent )
144- // Some of the example files set their 'value' as a JSON string,
142+ // Some of the example files set their 'value' as a JSON string,
145143 // and some files set their 'value' as an object. For consistency,
146144 // if the value is not a JSON string, convert it to a JSON string.
147145 if ( typeof example . value !== 'string' ) {
148146 // Convert to prettified JSON string
149- example . value = JSON . stringify ( example . value , null , 2 )
150- }
147+ example . value = JSON . stringify ( example . value , null , 2 )
148+ }
151149 examples [ exampleName ] = example
152150 }
153151 return examples
154- }
152+ }
155153}
156154
157155/*
158156 * Class to add the examples for an API request
159157 */
160158class RequestExamplesProcessor extends BaseExamplesProcessor {
161-
162- // Traverse all the types in the model to find a type that is
159+ // Traverse all the types in the model to find a type that is
163160 // of type 'request' and has the same name and namespace as the request.
164161 getRequestDefinition ( model : model . Model , request : model . TypeName ) : model . Request {
165162 for ( const type of model . types ) {
@@ -174,32 +171,34 @@ class RequestExamplesProcessor extends BaseExamplesProcessor {
174171
175172 // Given the spec location, return the request examples folder, if it exists.
176173 getExamplesRequestSubfolder ( examplesSubfolder : string ) : string | undefined {
177- const subFolder = path . join ( examplesSubfolder , " request" )
178- if ( this . isDirectory ( subFolder ) )
174+ const subFolder = path . join ( examplesSubfolder , ' request' )
175+ if ( this . isDirectory ( subFolder ) ) {
179176 return subFolder
177+ }
180178 return undefined
181179 }
182180
183181 // Find all the request examples for this request and add them to the model.
184182 addExamples ( request : model . TypeName ) : void {
185183 const requestDefinition = this . getRequestDefinition ( this . model , request )
186184 const examplesFolder = this . getExamplesFolder ( requestDefinition . specLocation )
187- if ( ! examplesFolder )
185+ if ( examplesFolder === undefined ) {
188186 return
189- // Get the request examples subfolder.
187+ }
188+ // Get the request examples subfolder.
190189 const examplesRequestSubfolder = this . getExamplesRequestSubfolder ( examplesFolder )
191190 // If there is an examples/request folder, add the request examples to the model.
192- if ( examplesRequestSubfolder )
191+ if ( examplesRequestSubfolder !== undefined ) {
193192 requestDefinition . examples = this . getExampleMap ( examplesRequestSubfolder )
193+ }
194194 }
195195}
196196
197197/*
198198 * Class to add the examples for an API response
199199 */
200200class ResponseExamplesProcessor extends BaseExamplesProcessor {
201-
202- // Traverse all the types in the model to find a type that is
201+ // Traverse all the types in the model to find a type that is
203202 // of type 'response' and has the same name and namespace as the response.
204203 getResponseDefinition ( model : model . Model , response : model . TypeName ) : model . Response {
205204 for ( const type of model . types ) {
@@ -209,51 +208,54 @@ class ResponseExamplesProcessor extends BaseExamplesProcessor {
209208 }
210209 }
211210 }
212- throw new Error ( `Can't find the request definiton for ${ response . namespace } .${ response . name } ` )
211+ throw new Error ( `Can't find the response definiton for ${ response . namespace } .${ response . name } ` )
213212 }
214213
215214 // Given the spec location, return the response example folders if they exists.
216215 // A response example folder can be of either of these forms:
217216 // response
218217 // {nnn}_response
219218 // Where {nnn} is the HTTP response code. If the folder is named 'response',
220- // assume that the response code is 200, otherwise pick up the response code
219+ // assume that the response code is 200, otherwise pick up the response code
221220 // from the folder name.
222- // Return a map of status code to the folder path.
221+ // Return a map of status code to the folder path.
223222 getExamplesResponseSubfolderMap ( examplesSubfolder : string ) : Map < string , string > | undefined {
224223 const subfolders = this . getSubfolders ( examplesSubfolder )
225224 // If we have a "response" subfolder, stop there and return.
226- // We should not have a mix of response and {nnn}_response folders.
227- if ( " response" in subfolders ) {
228- const response_subfolder = path . join ( examplesSubfolder , " response" )
229- return new Map ( [ [ " 200" , response_subfolder ] ] )
225+ // We should not have a mix of response and {nnn}_response folders.
226+ if ( ' response' in subfolders ) {
227+ const responseSubfolder = path . join ( examplesSubfolder , ' response' )
228+ return new Map ( [ [ ' 200' , responseSubfolder ] ] )
230229 }
231230 // Look for subfolders of the format '{nnn}_response'.
232- const rspSubfolders = subfolders . filter ( folder => folder . endsWith ( " _response" ) )
231+ const rspSubfolders = subfolders . filter ( folder => folder . endsWith ( ' _response' ) )
233232 const responseTypeMap = new Map < string , string > ( )
234233 for ( const rspSubfolder of rspSubfolders ) {
235234 const match = rspSubfolder . match ( / ^ ( [ 0 - 9 ] { 3 } ) _ r e s p o n s e $ / )
236- if ( ! match )
235+ if ( match == null ) {
237236 throw new Error ( `Unexpected response folder: ${ rspSubfolder } ` )
237+ }
238238 const statusCode = match [ 1 ]
239239 const responseSubfolder = path . join ( examplesSubfolder , rspSubfolder )
240- responseTypeMap . set ( statusCode , responseSubfolder )
240+ responseTypeMap . set ( statusCode , responseSubfolder )
241241 }
242242 return responseTypeMap
243243 }
244244
245245 // Find all the response examples for this request and add them to the model.
246246 addExamples ( response : model . TypeName ) : void {
247247 const responseDefinition = this . getResponseDefinition ( this . model , response )
248- const examplesFolder = this . getExamplesFolder ( responseDefinition . specLocation )
249- if ( ! examplesFolder )
248+ const examplesFolder = this . getExamplesFolder ( responseDefinition . specLocation )
249+ if ( examplesFolder === undefined ) {
250250 return
251- // Get a map of status code to response example subfolder.
251+ }
252+ // Get a map of status code to response example subfolder.
252253 const examplesResponseSubfolderMap = this . getExamplesResponseSubfolderMap ( examplesFolder )
253- const examples200ResponseSubfolder = examplesResponseSubfolderMap ?. get ( " 200" )
254- // If there is an examples/response or examples/200_response folder,
254+ const examples200ResponseSubfolder = examplesResponseSubfolderMap ?. get ( ' 200' )
255+ // If there is an examples/response or examples/200_response folder,
255256 // add the response examples to the model.
256- if ( examples200ResponseSubfolder )
257+ if ( examples200ResponseSubfolder !== undefined ) {
257258 responseDefinition . examples = this . getExampleMap ( examples200ResponseSubfolder )
259+ }
258260 }
259261}
0 commit comments