@@ -9,14 +9,13 @@ export class AtelierAPI {
9
9
private cookies : string [ ] = [ ] ;
10
10
private _config : any ;
11
11
private _namespace : string ;
12
- private static _apiVersion : number ;
13
12
14
13
private get ns ( ) : string {
15
14
return this . _namespace || this . _config . ns ;
16
15
}
17
16
18
17
private get apiVersion ( ) : number {
19
- return AtelierAPI . _apiVersion || DEFAULT_API_VERSION ;
18
+ return this . _config . version || DEFAULT_API_VERSION ;
20
19
}
21
20
22
21
constructor ( ) {
@@ -27,10 +26,6 @@ export class AtelierAPI {
27
26
this . _namespace = namespace ;
28
27
}
29
28
30
- setApiVersion ( apiVersion : number ) {
31
- AtelierAPI . _apiVersion = apiVersion ;
32
- }
33
-
34
29
updateCookies ( cookies : string [ ] ) {
35
30
cookies . forEach ( cookie => {
36
31
let [ cookieName ] = cookie . split ( '=' ) ;
@@ -48,7 +43,20 @@ export class AtelierAPI {
48
43
this . _config = conn ;
49
44
}
50
45
51
- async request ( method : string , path ?: string , body ?: any , params ?: any , headers ?: any ) : Promise < any > {
46
+ async request (
47
+ minVersion : number ,
48
+ method : string ,
49
+ path ?: string ,
50
+ body ?: any ,
51
+ params ?: any ,
52
+ headers ?: any
53
+ ) : Promise < any > {
54
+ if ( minVersion > this . apiVersion ) {
55
+ return Promise . reject ( `${ path } not supported by API version ${ this . apiVersion } ` ) ;
56
+ }
57
+ if ( minVersion && minVersion > 0 ) {
58
+ path = `v${ this . apiVersion } /${ path } ` ;
59
+ }
52
60
if ( ! this . _config . active ) {
53
61
return Promise . reject ( ) ;
54
62
}
@@ -83,7 +91,6 @@ export class AtelierAPI {
83
91
const http : any = this . _config . https ? httpsModule : httpModule ;
84
92
const agent = new http . Agent ( { keepAlive : true , maxSockets : 10 } ) ;
85
93
path = encodeURI ( `/api/atelier/${ path || '' } ${ buildParams ( ) } ` ) ;
86
- console . log ( `API request: ${ method } ${ path } ` ) ;
87
94
88
95
if ( headers [ 'Content-Type' ] && headers [ 'Content-Type' ] . includes ( 'json' ) ) {
89
96
body = JSON . stringify ( body ) ;
@@ -143,7 +150,7 @@ export class AtelierAPI {
143
150
}
144
151
145
152
serverInfo ( ) : Promise < any > {
146
- return this . request ( 'GET' ) ;
153
+ return this . request ( 0 , 'GET' ) ;
147
154
}
148
155
// api v1+
149
156
getDocNames ( {
@@ -157,7 +164,7 @@ export class AtelierAPI {
157
164
type ?: string ;
158
165
filter ?: string ;
159
166
} ) : Promise < any > {
160
- return this . request ( 'GET' , `v ${ this . apiVersion } / ${ this . ns } /docnames/${ category } /${ type } ` , null , {
167
+ return this . request ( 1 , 'GET' , `${ this . ns } /docnames/${ category } /${ type } ` , null , {
161
168
filter,
162
169
generated
163
170
} ) ;
@@ -170,56 +177,50 @@ export class AtelierAPI {
170
177
format
171
178
} ;
172
179
}
173
- return this . request ( 'GET' , `v ${ this . apiVersion } / ${ this . ns } /doc/${ name } ` , params ) ;
180
+ return this . request ( 1 , 'GET' , `${ this . ns } /doc/${ name } ` , params ) ;
174
181
}
175
182
// v1+
176
183
putDoc ( name : string , data : { enc : boolean ; content : string [ ] } , ignoreConflict ?: boolean ) : Promise < any > {
177
184
let params = { ignoreConflict } ;
178
- return this . request ( 'PUT' , `v ${ this . apiVersion } / ${ this . ns } /doc/${ name } ` , data , params ) ;
185
+ return this . request ( 1 , 'PUT' , `${ this . ns } /doc/${ name } ` , data , params ) ;
179
186
}
180
187
// v1+
181
188
actionIndex ( docs : string [ ] ) : Promise < any > {
182
- return this . request ( 'POST' , `v ${ this . apiVersion } / ${ this . ns } /action/index` , docs ) ;
189
+ return this . request ( 1 , 'POST' , `${ this . ns } /action/index` , docs ) ;
183
190
}
184
191
// v2+
185
192
actionSearch ( params : { query : string ; files ?: string ; sys ?: boolean ; gen ?: boolean ; max ?: number } ) : Promise < any > {
186
- return this . apiVersion >= 2 ?
187
- this . request ( 'GET' , `v${ this . apiVersion } /${ this . ns } /action/search` , null , params ) :
188
- Promise . reject ( `Method 'search' not supported by API version ${ this . apiVersion } ` ) ;
193
+ return this . request ( 2 , 'GET' , `${ this . ns } /action/search` , null , params ) ;
189
194
}
190
195
// v1+
191
196
actionQuery ( query : string , parameters : string [ ] ) : Promise < any > {
192
- return this . request ( 'POST' , `v ${ this . apiVersion } / ${ this . ns } /action/query` , {
197
+ return this . request ( 1 , 'POST' , `${ this . ns } /action/query` , {
193
198
query,
194
199
parameters
195
200
} ) ;
196
201
}
197
202
// v1+
198
203
actionCompile ( docs : string [ ] , flags ?: string , source = false ) : Promise < any > {
199
- return this . request ( 'POST' , `v ${ this . apiVersion } / ${ this . ns } /action/compile` , docs , { flags, source } ) ;
204
+ return this . request ( 1 , 'POST' , `${ this . ns } /action/compile` , docs , { flags, source } ) ;
200
205
}
201
206
202
207
cvtXmlUdl ( source : string ) : Promise < any > {
203
- return this . request ( 'POST' , `v ${ this . apiVersion } / ${ this . ns } /cvt/xml/doc ` , source , { } , { 'Content-Type' : 'application/xml' } ) ;
208
+ return this . request ( 1 , 'POST' , `${ this . ns } /` , source , { } , { 'Content-Type' : 'application/xml' } ) ;
204
209
}
205
210
// v2+
206
211
getmacrodefinition ( docname : string , macroname : string , includes : string [ ] ) {
207
- return this . apiVersion >= 2 ?
208
- this . request ( 'POST' , `v${ this . apiVersion } /${ this . ns } /action/getmacrodefinition` , {
209
- docname,
210
- macroname,
211
- includes
212
- } ) :
213
- Promise . reject ( `Method 'getmacrodefinition' not supported by API version ${ this . apiVersion } ` ) ;
212
+ return this . request ( 2 , 'POST' , `${ this . ns } /action/getmacrodefinition` , {
213
+ docname,
214
+ macroname,
215
+ includes
216
+ } ) ;
214
217
}
215
218
// v2+
216
219
getmacrolocation ( docname : string , macroname : string , includes : string [ ] ) {
217
- return this . apiVersion >= 2 ?
218
- this . request ( 'POST' , `v${ this . apiVersion } /${ this . ns } /action/getmacrolocation` , {
219
- docname,
220
- macroname,
221
- includes
222
- } ) :
223
- Promise . reject ( `Method 'getmacrolocation' not supported by API version ${ this . apiVersion } ` ) ;
220
+ return this . request ( 2 , 'POST' , `${ this . ns } /action/getmacrolocation` , {
221
+ docname,
222
+ macroname,
223
+ includes
224
+ } ) ;
224
225
}
225
226
}
0 commit comments