1
- import * as vscode from "vscode" ;
2
1
import * as httpModule from "http" ;
3
2
import * as httpsModule from "https" ;
4
- import { outputConsole , currentWorkspaceFolder } from "../utils" ;
5
- const Cache = require ( "vscode-cache" ) ;
3
+ import * as request from "request-promise" ;
4
+ import * as url from "url" ;
5
+ import * as vscode from "vscode" ;
6
+ import * as Cache from "vscode-cache" ;
6
7
import {
7
8
config ,
8
9
extensionContext ,
10
+ FILESYSTEM_SCHEMA ,
9
11
workspaceState ,
10
- FILESYSTEM_SCHEMA
11
12
} from "../extension" ;
12
- import * as url from "url" ;
13
- import * as request from "request-promise" ;
13
+ import { currentWorkspaceFolder , outputConsole } from "../utils" ;
14
14
15
15
const DEFAULT_API_VERSION : number = 1 ;
16
16
// require("request-promise").debug = true;
17
17
18
18
export class AtelierAPI {
19
- private _config : any ;
20
- private _namespace : string ;
21
- private _cache ;
22
- private _workspaceFolder ;
19
+ private config : any ;
20
+ private namespace : string ;
21
+ private cache ;
22
+ private workspaceFolder ;
23
23
24
24
public get ns ( ) : string {
25
- return this . _namespace || this . _config . ns ;
25
+ return this . namespace || this . config . ns ;
26
26
}
27
27
28
28
private get apiVersion ( ) : number {
29
29
return workspaceState . get (
30
- this . _workspaceFolder + ":apiVersion" ,
31
- DEFAULT_API_VERSION
30
+ this . workspaceFolder + ":apiVersion" ,
31
+ DEFAULT_API_VERSION ,
32
32
) ;
33
33
}
34
34
@@ -38,11 +38,11 @@ export class AtelierAPI {
38
38
if ( wsOrFile instanceof vscode . Uri ) {
39
39
if ( wsOrFile . scheme === FILESYSTEM_SCHEMA ) {
40
40
workspaceFolderName = wsOrFile . authority ;
41
- let query = url . parse ( decodeURIComponent ( wsOrFile . toString ( ) ) , true )
41
+ const query = url . parse ( decodeURIComponent ( wsOrFile . toString ( ) ) , true )
42
42
. query ;
43
43
if ( query ) {
44
44
if ( query . ns && query . ns !== "" ) {
45
- let namespace = query . ns . toString ( ) ;
45
+ const namespace = query . ns . toString ( ) ;
46
46
this . setNamespace ( namespace ) ;
47
47
}
48
48
}
@@ -54,65 +54,65 @@ export class AtelierAPI {
54
54
this . setConnection ( workspaceFolderName || currentWorkspaceFolder ( ) ) ;
55
55
}
56
56
57
- setNamespace ( namespace : string ) {
58
- this . _namespace = namespace ;
57
+ public setNamespace ( namespace : string ) {
58
+ this . namespace = namespace ;
59
59
}
60
60
61
61
get cookies ( ) : string [ ] {
62
- return this . _cache . get ( "cookies" , [ ] ) ;
62
+ return this . cache . get ( "cookies" , [ ] ) ;
63
63
}
64
64
65
- updateCookies ( newCookies : string [ ] ) : Promise < any > {
66
- let cookies = this . _cache . get ( "cookies" , [ ] ) ;
67
- newCookies . forEach ( cookie => {
68
- let [ cookieName ] = cookie . split ( "=" ) ;
69
- let index = cookies . findIndex ( el => el . startsWith ( cookieName ) ) ;
65
+ public updateCookies ( newCookies : string [ ] ) : Promise < any > {
66
+ const cookies = this . cache . get ( "cookies" , [ ] ) ;
67
+ newCookies . forEach ( ( cookie ) => {
68
+ const [ cookieName ] = cookie . split ( "=" ) ;
69
+ const index = cookies . findIndex ( ( el ) => el . startsWith ( cookieName ) ) ;
70
70
if ( index >= 0 ) {
71
71
cookies [ index ] = cookie ;
72
72
} else {
73
73
cookies . push ( cookie ) ;
74
74
}
75
75
} ) ;
76
- return this . _cache . put ( "cookies" , cookies ) ;
76
+ return this . cache . put ( "cookies" , cookies ) ;
77
77
}
78
78
79
- setConnection ( workspaceFolderName : string ) {
80
- this . _workspaceFolder = workspaceFolderName ;
81
- let conn = config ( "conn" , workspaceFolderName ) ;
82
- this . _config = conn ;
83
- const { name, host, port } = this . _config ;
84
- this . _cache = new Cache ( extensionContext , `API:${ name } :${ host } :${ port } ` ) ;
79
+ public setConnection ( workspaceFolderName : string ) {
80
+ this . workspaceFolder = workspaceFolderName ;
81
+ const conn = config ( "conn" , workspaceFolderName ) ;
82
+ this . config = conn ;
83
+ const { name, host, port } = this . config ;
84
+ this . cache = new Cache ( extensionContext , `API:${ name } :${ host } :${ port } ` ) ;
85
85
}
86
86
87
- async request (
87
+ public async request (
88
88
minVersion : number ,
89
89
method : string ,
90
90
path ?: string ,
91
91
body ?: any ,
92
92
params ?: any ,
93
- headers ?: any
93
+ headers ?: any ,
94
94
) : Promise < any > {
95
95
if ( minVersion > this . apiVersion ) {
96
96
return Promise . reject (
97
- `${ path } not supported by API version ${ this . apiVersion } `
97
+ `${ path } not supported by API version ${ this . apiVersion } ` ,
98
98
) ;
99
99
}
100
100
if ( minVersion && minVersion > 0 ) {
101
101
path = `v${ this . apiVersion } /${ path } ` ;
102
102
}
103
- if ( ! this . _config . active ) {
103
+ if ( ! this . config . active ) {
104
104
return Promise . reject ( ) ;
105
105
}
106
106
headers = {
107
107
...headers ,
108
- Accept : "application/json"
108
+ Accept : "application/json" ,
109
109
} ;
110
110
const buildParams = ( ) : string => {
111
111
if ( ! params ) {
112
112
return "" ;
113
113
}
114
- let result = [ ] ;
115
- Object . keys ( params ) . forEach ( key => {
114
+ const result = [ ] ;
115
+ Object . keys ( params ) . forEach ( ( key ) => {
116
116
let value = params [ key ] ;
117
117
if ( value && value !== "" ) {
118
118
if ( typeof value === "boolean" ) {
@@ -129,54 +129,46 @@ export class AtelierAPI {
129
129
}
130
130
headers [ "Cache-Control" ] = "no-cache" ;
131
131
132
- const { host, port, username, password, https } = this . _config ;
133
- const proto = this . _config . https ? "https" : "http" ;
134
- const http : any = this . _config . https ? httpsModule : httpModule ;
132
+ const { host, port, username, password, https } = this . config ;
133
+ const proto = this . config . https ? "https" : "http" ;
134
+ const http : any = this . config . https ? httpsModule : httpModule ;
135
135
const agent = new http . Agent ( {
136
136
keepAlive : true ,
137
137
maxSockets : 10 ,
138
- rejectUnauthorized : https && config ( "http.proxyStrictSSL" )
138
+ rejectUnauthorized : https && config ( "http.proxyStrictSSL" ) ,
139
139
} ) ;
140
140
path = encodeURI ( `/api/atelier/${ path || "" } ${ buildParams ( ) } ` ) ;
141
141
142
- // if (headers["Content-Type"] && headers["Content-Type"].includes("json")) {
143
- // body = JSON.stringify(body);
144
- // }
145
-
146
- // console.log(`APIRequest: ${method} ${proto}://${host}:${port}${path}`)
147
-
148
- let cookies = this . cookies ;
142
+ const cookies = this . cookies ;
149
143
let auth ;
150
- if ( cookies . length || method === ' HEAD' ) {
144
+ if ( cookies . length || method === " HEAD" ) {
151
145
auth = Promise . resolve ( cookies ) ;
152
146
} else if ( ! cookies . length ) {
153
- auth = this . request ( 0 , ' HEAD' )
147
+ auth = this . request ( 0 , " HEAD" ) ;
154
148
}
155
- return auth . then ( ( cookies ) => {
156
- // console.log('cookies', cookies);
149
+ return auth . then ( ( cookie ) => {
157
150
return request ( {
158
- // jar: cookieJar,
159
- uri : `${ proto } ://${ host } :${ port } ${ path } ` ,
160
- method,
161
151
agent,
162
152
auth : { username, password, sendImmediately : false } ,
153
+ body : [ "PUT" , "POST" ] . includes ( method ) ? body : null ,
163
154
headers : {
164
155
...headers ,
165
- Cookie : cookies
156
+ Cookie : cookie ,
166
157
} ,
167
- body : [ "PUT" , "POST" ] . includes ( method ) ? body : null ,
168
158
json : true ,
159
+ method,
169
160
resolveWithFullResponse : true ,
170
- simple : true
161
+ simple : true ,
162
+ uri : `${ proto } ://${ host } :${ port } ${ path } ` ,
171
163
} )
172
164
// .catch(error => error.error)
173
- . then ( response => this . updateCookies ( response . headers [ "set-cookie" ] ) . then ( ( ) => response ) )
174
- . then ( response => {
165
+ . then ( ( response ) => this . updateCookies ( response . headers [ "set-cookie" ] ) . then ( ( ) => response ) )
166
+ . then ( ( response ) => {
175
167
// console.log(`APIResponse: ${method} ${proto}://${host}:${port}${path}`)
176
- if ( method === ' HEAD' ) {
177
- return this . cookies
168
+ if ( method === " HEAD" ) {
169
+ return this . cookies ;
178
170
}
179
- let data = response . body ;
171
+ const data = response . body ;
180
172
if ( data . console ) {
181
173
outputConsole ( data . console ) ;
182
174
}
@@ -185,27 +177,27 @@ export class AtelierAPI {
185
177
} else if ( data . result . status ) {
186
178
throw new Error ( data . result . status ) ;
187
179
} else {
188
- return data
180
+ return data ;
189
181
}
190
- } )
182
+ } ) ;
191
183
} ) ;
192
184
}
193
185
194
- serverInfo ( ) : Promise < any > {
186
+ public serverInfo ( ) : Promise < any > {
195
187
return this . request ( 0 , "GET" )
196
- . then ( info => {
188
+ . then ( ( info ) => {
197
189
if (
198
190
info &&
199
191
info . result &&
200
192
info . result . content &&
201
193
info . result . content . api > 0
202
194
) {
203
- let data = info . result . content ;
204
- let apiVersion = data . api ;
195
+ const data = info . result . content ;
196
+ const apiVersion = data . api ;
205
197
if ( ! data . namespaces . includes ( this . ns ) ) {
206
198
throw {
207
199
code : "WrongNamespace" ,
208
- message : "This server does not have specified namespace."
200
+ message : "This server does not have specified namespace." ,
209
201
} ;
210
202
}
211
203
return workspaceState
@@ -215,11 +207,11 @@ export class AtelierAPI {
215
207
} ) ;
216
208
}
217
209
// api v1+
218
- getDocNames ( {
210
+ public getDocNames ( {
219
211
generated = false ,
220
212
category = "*" ,
221
213
type = "*" ,
222
- filter = ""
214
+ filter = "" ,
223
215
} : {
224
216
generated ?: boolean ;
225
217
category ?: string ;
@@ -233,39 +225,39 @@ export class AtelierAPI {
233
225
null ,
234
226
{
235
227
filter,
236
- generated
237
- }
228
+ generated,
229
+ } ,
238
230
) ;
239
231
}
240
232
// api v1+
241
- getDoc ( name : string , format ?: string ) : Promise < any > {
233
+ public getDoc ( name : string , format ?: string ) : Promise < any > {
242
234
let params = { } ;
243
235
if ( format ) {
244
236
params = {
245
- format
237
+ format,
246
238
} ;
247
239
}
248
240
return this . request ( 1 , "GET" , `${ this . ns } /doc/${ name } ` , params ) ;
249
241
}
250
242
// api v1+
251
- deleteDoc ( name : string ) : Promise < any > {
243
+ public deleteDoc ( name : string ) : Promise < any > {
252
244
return this . request ( 1 , "DELETE" , `${ this . ns } /doc/${ name } ` ) ;
253
245
}
254
246
// v1+
255
- putDoc (
247
+ public putDoc (
256
248
name : string ,
257
249
data : { enc : boolean ; content : string [ ] } ,
258
- ignoreConflict ?: boolean
250
+ ignoreConflict ?: boolean ,
259
251
) : Promise < any > {
260
- let params = { ignoreConflict } ;
252
+ const params = { ignoreConflict } ;
261
253
return this . request ( 1 , "PUT" , `${ this . ns } /doc/${ name } ` , data , params ) ;
262
254
}
263
255
// v1+
264
- actionIndex ( docs : string [ ] ) : Promise < any > {
256
+ public actionIndex ( docs : string [ ] ) : Promise < any > {
265
257
return this . request ( 1 , "POST" , `${ this . ns } /action/index` , docs ) ;
266
258
}
267
259
// v2+
268
- actionSearch ( params : {
260
+ public actionSearch ( params : {
269
261
query : string ;
270
262
files ?: string ;
271
263
sys ?: boolean ;
@@ -275,53 +267,53 @@ export class AtelierAPI {
275
267
return this . request ( 2 , "GET" , `${ this . ns } /action/search` , null , params ) ;
276
268
}
277
269
// v1+
278
- actionQuery ( query : string , parameters : string [ ] ) : Promise < any > {
270
+ public actionQuery ( query : string , parameters : string [ ] ) : Promise < any > {
279
271
// outputChannel.appendLine('SQL: ' + query);
280
272
// outputChannel.appendLine('SQLPARAMS: ' + JSON.stringify(parameters));
281
273
return this . request ( 1 , "POST" , `${ this . ns } /action/query` , {
274
+ parameters,
282
275
query,
283
- parameters
284
276
} ) ;
285
277
}
286
278
// v1+
287
- actionCompile ( docs : string [ ] , flags ?: string , source = false ) : Promise < any > {
279
+ public actionCompile ( docs : string [ ] , flags ?: string , source = false ) : Promise < any > {
288
280
return this . request ( 1 , "POST" , `${ this . ns } /action/compile` , docs , {
289
281
flags,
290
- source
282
+ source,
291
283
} ) ;
292
284
}
293
285
294
- cvtXmlUdl ( source : string ) : Promise < any > {
286
+ public cvtXmlUdl ( source : string ) : Promise < any > {
295
287
return this . request (
296
288
1 ,
297
289
"POST" ,
298
290
`${ this . ns } /` ,
299
291
source ,
300
292
{ } ,
301
- { "Content-Type" : "application/xml" }
293
+ { "Content-Type" : "application/xml" } ,
302
294
) ;
303
295
}
304
296
// v2+
305
- getmacrodefinition ( docname : string , macroname : string , includes : string [ ] ) {
297
+ public getmacrodefinition ( docname : string , macroname : string , includes : string [ ] ) {
306
298
return this . request ( 2 , "POST" , `${ this . ns } /action/getmacrodefinition` , {
307
299
docname,
300
+ includes,
308
301
macroname,
309
- includes
310
302
} ) ;
311
303
}
312
304
// v2+
313
- getmacrolocation ( docname : string , macroname : string , includes : string [ ] ) {
305
+ public getmacrolocation ( docname : string , macroname : string , includes : string [ ] ) {
314
306
return this . request ( 2 , "POST" , `${ this . ns } /action/getmacrolocation` , {
315
307
docname,
308
+ includes,
316
309
macroname,
317
- includes
318
310
} ) ;
319
311
}
320
312
// v2+
321
- getmacrollist ( docname : string , includes : string [ ] ) {
313
+ public getmacrollist ( docname : string , includes : string [ ] ) {
322
314
return this . request ( 2 , "POST" , `${ this . ns } /action/getmacrolist` , {
323
315
docname,
324
- includes
316
+ includes,
325
317
} ) ;
326
318
}
327
319
}
0 commit comments