@@ -3,15 +3,22 @@ import * as httpsModule from 'https';
3
3
import { outputConsole , currentWorkspaceFolder } from '../utils' ;
4
4
import { config } from '../extension' ;
5
5
6
+ const DEFAULT_API_VERSION : number = 3 ;
7
+
6
8
export class AtelierAPI {
7
9
private cookies : string [ ] = [ ] ;
8
10
private _config : any ;
9
11
private _namespace : string ;
12
+ private static _apiVersion : number ;
10
13
11
14
private get ns ( ) : string {
12
15
return this . _namespace || this . _config . ns ;
13
16
}
14
17
18
+ private get apiVersion ( ) : number {
19
+ return AtelierAPI . _apiVersion || DEFAULT_API_VERSION ;
20
+ }
21
+
15
22
constructor ( ) {
16
23
this . setConnection ( currentWorkspaceFolder ( ) ) ;
17
24
}
@@ -20,6 +27,10 @@ export class AtelierAPI {
20
27
this . _namespace = namespace ;
21
28
}
22
29
30
+ setApiVersion ( apiVersion : number ) {
31
+ AtelierAPI . _apiVersion = apiVersion ;
32
+ }
33
+
23
34
updateCookies ( cookies : string [ ] ) {
24
35
cookies . forEach ( cookie => {
25
36
let [ cookieName ] = cookie . split ( '=' ) ;
@@ -134,7 +145,7 @@ export class AtelierAPI {
134
145
serverInfo ( ) : Promise < any > {
135
146
return this . request ( 'GET' ) ;
136
147
}
137
-
148
+ // api v1+
138
149
getDocNames ( {
139
150
generated = false ,
140
151
category = '*' ,
@@ -146,63 +157,69 @@ export class AtelierAPI {
146
157
type ?: string ;
147
158
filter ?: string ;
148
159
} ) : Promise < any > {
149
- return this . request ( 'GET' , `v3 /${ this . ns } /docnames/${ category } /${ type } ` , null , {
160
+ return this . request ( 'GET' , `v ${ this . apiVersion } /${ this . ns } /docnames/${ category } /${ type } ` , null , {
150
161
filter,
151
162
generated
152
163
} ) ;
153
164
}
154
-
165
+ // api v1+
155
166
getDoc ( name : string , format ?: string ) : Promise < any > {
156
167
let params = { } ;
157
168
if ( format ) {
158
169
params = {
159
170
format
160
171
} ;
161
172
}
162
- return this . request ( 'GET' , `v3 /${ this . ns } /doc/${ name } ` , params ) ;
173
+ return this . request ( 'GET' , `v ${ this . apiVersion } /${ this . ns } /doc/${ name } ` , params ) ;
163
174
}
164
-
175
+ // v1+
165
176
putDoc ( name : string , data : { enc : boolean ; content : string [ ] } , ignoreConflict ?: boolean ) : Promise < any > {
166
177
let params = { ignoreConflict } ;
167
- return this . request ( 'PUT' , `v3 /${ this . ns } /doc/${ name } ` , data , params ) ;
178
+ return this . request ( 'PUT' , `v ${ this . apiVersion } /${ this . ns } /doc/${ name } ` , data , params ) ;
168
179
}
169
-
180
+ // v1+
170
181
actionIndex ( docs : string [ ] ) : Promise < any > {
171
- return this . request ( 'POST' , `v3 /${ this . ns } /action/index` , docs ) ;
182
+ return this . request ( 'POST' , `v ${ this . apiVersion } /${ this . ns } /action/index` , docs ) ;
172
183
}
173
-
184
+ // v2+
174
185
actionSearch ( params : { query : string ; files ?: string ; sys ?: boolean ; gen ?: boolean ; max ?: number } ) : Promise < any > {
175
- return this . request ( 'GET' , `v3/${ this . ns } /action/search` , null , params ) ;
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 } ` ) ;
176
189
}
177
-
190
+ // v1+
178
191
actionQuery ( query : string , parameters : string [ ] ) : Promise < any > {
179
- return this . request ( 'POST' , `v3 /${ this . ns } /action/query` , {
192
+ return this . request ( 'POST' , `v ${ this . apiVersion } /${ this . ns } /action/query` , {
180
193
query,
181
194
parameters
182
195
} ) ;
183
196
}
184
-
197
+ // v1+
185
198
actionCompile ( docs : string [ ] , flags ?: string , source = false ) : Promise < any > {
186
- return this . request ( 'POST' , `v3 /${ this . ns } /action/compile` , docs , { flags, source } ) ;
199
+ return this . request ( 'POST' , `v ${ this . apiVersion } /${ this . ns } /action/compile` , docs , { flags, source } ) ;
187
200
}
188
201
189
202
cvtXmlUdl ( source : string ) : Promise < any > {
190
- return this . request ( 'POST' , `v3 /${ this . ns } /cvt/xml/doc` , source , { } , { 'Content-Type' : 'application/xml' } ) ;
203
+ return this . request ( 'POST' , `v ${ this . apiVersion } /${ this . ns } /cvt/xml/doc` , source , { } , { 'Content-Type' : 'application/xml' } ) ;
191
204
}
192
-
205
+ // v2+
193
206
getmacrodefinition ( docname : string , macroname : string , includes : string [ ] ) {
194
- return this . request ( 'POST' , `v3/${ this . ns } /action/getmacrodefinition` , {
195
- docname,
196
- macroname,
197
- includes
198
- } ) ;
199
- }
200
-
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 } ` ) ;
214
+ }
215
+ // v2+
201
216
getmacrolocation ( docname : string , macroname : string , includes : string [ ] ) {
202
- return this . request ( 'POST' , `v3/${ this . ns } /action/getmacrolocation` , {
203
- docname,
204
- macroname,
205
- includes
206
- } ) ;
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 } ` ) ;
207
224
}
208
225
}
0 commit comments