2
2
import fsp from 'fs/promises' ;
3
3
import path from 'path' ;
4
4
5
+ import SwaggerParser from '@apidevtools/swagger-parser' ;
5
6
import Mustache from 'mustache' ;
6
7
import type { OpenAPIV3 } from 'openapi-types' ;
7
- import SwaggerParser from 'swagger-parser' ;
8
8
9
9
import openapitools from '../openapitools.json' ;
10
10
11
11
const availableLanguages = [ 'javascript' ] as const ;
12
12
type Language = typeof availableLanguages [ number ] ;
13
13
14
- type CTSBlock = {
14
+ type Tests = {
15
15
testName ?: string ;
16
16
method : string ;
17
17
parameters : any [ ] ;
@@ -22,6 +22,11 @@ type CTSBlock = {
22
22
} ;
23
23
} ;
24
24
25
+ type CTSBlock = {
26
+ operationId : string ;
27
+ tests : Tests [ ] ;
28
+ } ;
29
+
25
30
// Array of test per client
26
31
type CTS = Record < string , CTSBlock [ ] > ;
27
32
@@ -68,6 +73,10 @@ function capitalize(str: string): string {
68
73
async function loadCTSForClient ( client : string ) : Promise < CTSBlock [ ] > {
69
74
// load the list of operations from the spec
70
75
const spec = await SwaggerParser . validate ( `../specs/${ client } /spec.yml` ) ;
76
+ if ( ! spec . paths ) {
77
+ throw new Error ( `No paths found for spec ${ client } /spec.yml` ) ;
78
+ }
79
+
71
80
const operations = Object . values ( spec . paths )
72
81
. flatMap < OpenAPIV3 . OperationObject > ( ( p ) => Object . values ( p ) )
73
82
. map ( ( obj ) => obj . operationId ) ;
@@ -78,22 +87,18 @@ async function loadCTSForClient(client: string): Promise<CTSBlock[]> {
78
87
if ( ! file . name . endsWith ( 'json' ) ) {
79
88
continue ;
80
89
}
81
- const operationId = file . name . replace ( '.json' , '' ) ;
90
+ const fileName = file . name . replace ( '.json' , '' ) ;
82
91
const fileContent = ( await fsp . readFile ( file . path ) ) . toString ( ) ;
83
92
84
93
if ( ! fileContent ) {
85
- throw new Error (
86
- `cannot read empty file for operationId ${ operationId } - ${ client } client`
87
- ) ;
94
+ throw new Error ( `cannot read empty file ${ fileName } - ${ client } client` ) ;
88
95
}
89
96
90
- const tests : CTSBlock [ ] = JSON . parse ( fileContent ) ;
97
+ const tests : Tests [ ] = JSON . parse ( fileContent ) ;
91
98
92
99
// check test validity against spec
93
- if ( ! operations . includes ( operationId ) ) {
94
- throw new Error (
95
- `cannot find operationId ${ operationId } for the ${ client } client`
96
- ) ;
100
+ if ( ! operations . includes ( fileName ) ) {
101
+ throw new Error ( `cannot find ${ fileName } for the ${ client } client` ) ;
97
102
}
98
103
99
104
for ( const test of tests ) {
@@ -116,8 +121,13 @@ async function loadCTSForClient(client: string): Promise<CTSBlock[]> {
116
121
// stringify request.data too
117
122
test . request . data = JSON . stringify ( test . request . data ) ;
118
123
}
119
- ctsClient . push ( ...tests ) ;
124
+
125
+ ctsClient . push ( {
126
+ operationId : fileName ,
127
+ tests,
128
+ } ) ;
120
129
}
130
+
121
131
return ctsClient ;
122
132
}
123
133
@@ -142,7 +152,7 @@ async function generateCode(language: Language): Promise<void> {
142
152
const code = Mustache . render ( template , {
143
153
import : packageNames [ language ] [ client ] ,
144
154
client : `${ capitalize ( client ) } Api` ,
145
- tests : cts [ client ] ,
155
+ blocks : cts [ client ] ,
146
156
} ) ;
147
157
await fsp . writeFile (
148
158
`output/${ language } /${ client } ${ extensionForLanguage [ language ] } ` ,
0 commit comments