1- import * as vscode from "vscode" ;
21import * as httpModule from "http" ;
32import * 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" ;
67import {
78 config ,
89 extensionContext ,
10+ FILESYSTEM_SCHEMA ,
911 workspaceState ,
10- FILESYSTEM_SCHEMA
1112} from "../extension" ;
12- import * as url from "url" ;
13- import * as request from "request-promise" ;
13+ import { currentWorkspaceFolder , outputConsole } from "../utils" ;
1414
1515const DEFAULT_API_VERSION : number = 1 ;
1616// require("request-promise").debug = true;
1717
1818export 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 ;
2323
2424 public get ns ( ) : string {
25- return this . _namespace || this . _config . ns ;
25+ return this . namespace || this . config . ns ;
2626 }
2727
2828 private get apiVersion ( ) : number {
2929 return workspaceState . get (
30- this . _workspaceFolder + ":apiVersion" ,
31- DEFAULT_API_VERSION
30+ this . workspaceFolder + ":apiVersion" ,
31+ DEFAULT_API_VERSION ,
3232 ) ;
3333 }
3434
@@ -38,11 +38,11 @@ export class AtelierAPI {
3838 if ( wsOrFile instanceof vscode . Uri ) {
3939 if ( wsOrFile . scheme === FILESYSTEM_SCHEMA ) {
4040 workspaceFolderName = wsOrFile . authority ;
41- let query = url . parse ( decodeURIComponent ( wsOrFile . toString ( ) ) , true )
41+ const query = url . parse ( decodeURIComponent ( wsOrFile . toString ( ) ) , true )
4242 . query ;
4343 if ( query ) {
4444 if ( query . ns && query . ns !== "" ) {
45- let namespace = query . ns . toString ( ) ;
45+ const namespace = query . ns . toString ( ) ;
4646 this . setNamespace ( namespace ) ;
4747 }
4848 }
@@ -54,65 +54,65 @@ export class AtelierAPI {
5454 this . setConnection ( workspaceFolderName || currentWorkspaceFolder ( ) ) ;
5555 }
5656
57- setNamespace ( namespace : string ) {
58- this . _namespace = namespace ;
57+ public setNamespace ( namespace : string ) {
58+ this . namespace = namespace ;
5959 }
6060
6161 get cookies ( ) : string [ ] {
62- return this . _cache . get ( "cookies" , [ ] ) ;
62+ return this . cache . get ( "cookies" , [ ] ) ;
6363 }
6464
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 ) ) ;
7070 if ( index >= 0 ) {
7171 cookies [ index ] = cookie ;
7272 } else {
7373 cookies . push ( cookie ) ;
7474 }
7575 } ) ;
76- return this . _cache . put ( "cookies" , cookies ) ;
76+ return this . cache . put ( "cookies" , cookies ) ;
7777 }
7878
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 } ` ) ;
8585 }
8686
87- async request (
87+ public async request (
8888 minVersion : number ,
8989 method : string ,
9090 path ?: string ,
9191 body ?: any ,
9292 params ?: any ,
93- headers ?: any
93+ headers ?: any ,
9494 ) : Promise < any > {
9595 if ( minVersion > this . apiVersion ) {
9696 return Promise . reject (
97- `${ path } not supported by API version ${ this . apiVersion } `
97+ `${ path } not supported by API version ${ this . apiVersion } ` ,
9898 ) ;
9999 }
100100 if ( minVersion && minVersion > 0 ) {
101101 path = `v${ this . apiVersion } /${ path } ` ;
102102 }
103- if ( ! this . _config . active ) {
103+ if ( ! this . config . active ) {
104104 return Promise . reject ( ) ;
105105 }
106106 headers = {
107107 ...headers ,
108- Accept : "application/json"
108+ Accept : "application/json" ,
109109 } ;
110110 const buildParams = ( ) : string => {
111111 if ( ! params ) {
112112 return "" ;
113113 }
114- let result = [ ] ;
115- Object . keys ( params ) . forEach ( key => {
114+ const result = [ ] ;
115+ Object . keys ( params ) . forEach ( ( key ) => {
116116 let value = params [ key ] ;
117117 if ( value && value !== "" ) {
118118 if ( typeof value === "boolean" ) {
@@ -129,54 +129,46 @@ export class AtelierAPI {
129129 }
130130 headers [ "Cache-Control" ] = "no-cache" ;
131131
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 ;
135135 const agent = new http . Agent ( {
136136 keepAlive : true ,
137137 maxSockets : 10 ,
138- rejectUnauthorized : https && config ( "http.proxyStrictSSL" )
138+ rejectUnauthorized : https && config ( "http.proxyStrictSSL" ) ,
139139 } ) ;
140140 path = encodeURI ( `/api/atelier/${ path || "" } ${ buildParams ( ) } ` ) ;
141141
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 ;
149143 let auth ;
150- if ( cookies . length || method === ' HEAD' ) {
144+ if ( cookies . length || method === " HEAD" ) {
151145 auth = Promise . resolve ( cookies ) ;
152146 } else if ( ! cookies . length ) {
153- auth = this . request ( 0 , ' HEAD' )
147+ auth = this . request ( 0 , " HEAD" ) ;
154148 }
155- return auth . then ( ( cookies ) => {
156- // console.log('cookies', cookies);
149+ return auth . then ( ( cookie ) => {
157150 return request ( {
158- // jar: cookieJar,
159- uri : `${ proto } ://${ host } :${ port } ${ path } ` ,
160- method,
161151 agent,
162152 auth : { username, password, sendImmediately : false } ,
153+ body : [ "PUT" , "POST" ] . includes ( method ) ? body : null ,
163154 headers : {
164155 ...headers ,
165- Cookie : cookies
156+ Cookie : cookie ,
166157 } ,
167- body : [ "PUT" , "POST" ] . includes ( method ) ? body : null ,
168158 json : true ,
159+ method,
169160 resolveWithFullResponse : true ,
170- simple : true
161+ simple : true ,
162+ uri : `${ proto } ://${ host } :${ port } ${ path } ` ,
171163 } )
172164 // .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 ) => {
175167 // console.log(`APIResponse: ${method} ${proto}://${host}:${port}${path}`)
176- if ( method === ' HEAD' ) {
177- return this . cookies
168+ if ( method === " HEAD" ) {
169+ return this . cookies ;
178170 }
179- let data = response . body ;
171+ const data = response . body ;
180172 if ( data . console ) {
181173 outputConsole ( data . console ) ;
182174 }
@@ -185,27 +177,27 @@ export class AtelierAPI {
185177 } else if ( data . result . status ) {
186178 throw new Error ( data . result . status ) ;
187179 } else {
188- return data
180+ return data ;
189181 }
190- } )
182+ } ) ;
191183 } ) ;
192184 }
193185
194- serverInfo ( ) : Promise < any > {
186+ public serverInfo ( ) : Promise < any > {
195187 return this . request ( 0 , "GET" )
196- . then ( info => {
188+ . then ( ( info ) => {
197189 if (
198190 info &&
199191 info . result &&
200192 info . result . content &&
201193 info . result . content . api > 0
202194 ) {
203- let data = info . result . content ;
204- let apiVersion = data . api ;
195+ const data = info . result . content ;
196+ const apiVersion = data . api ;
205197 if ( ! data . namespaces . includes ( this . ns ) ) {
206198 throw {
207199 code : "WrongNamespace" ,
208- message : "This server does not have specified namespace."
200+ message : "This server does not have specified namespace." ,
209201 } ;
210202 }
211203 return workspaceState
@@ -215,11 +207,11 @@ export class AtelierAPI {
215207 } ) ;
216208 }
217209 // api v1+
218- getDocNames ( {
210+ public getDocNames ( {
219211 generated = false ,
220212 category = "*" ,
221213 type = "*" ,
222- filter = ""
214+ filter = "" ,
223215 } : {
224216 generated ?: boolean ;
225217 category ?: string ;
@@ -233,39 +225,39 @@ export class AtelierAPI {
233225 null ,
234226 {
235227 filter,
236- generated
237- }
228+ generated,
229+ } ,
238230 ) ;
239231 }
240232 // api v1+
241- getDoc ( name : string , format ?: string ) : Promise < any > {
233+ public getDoc ( name : string , format ?: string ) : Promise < any > {
242234 let params = { } ;
243235 if ( format ) {
244236 params = {
245- format
237+ format,
246238 } ;
247239 }
248240 return this . request ( 1 , "GET" , `${ this . ns } /doc/${ name } ` , params ) ;
249241 }
250242 // api v1+
251- deleteDoc ( name : string ) : Promise < any > {
243+ public deleteDoc ( name : string ) : Promise < any > {
252244 return this . request ( 1 , "DELETE" , `${ this . ns } /doc/${ name } ` ) ;
253245 }
254246 // v1+
255- putDoc (
247+ public putDoc (
256248 name : string ,
257249 data : { enc : boolean ; content : string [ ] } ,
258- ignoreConflict ?: boolean
250+ ignoreConflict ?: boolean ,
259251 ) : Promise < any > {
260- let params = { ignoreConflict } ;
252+ const params = { ignoreConflict } ;
261253 return this . request ( 1 , "PUT" , `${ this . ns } /doc/${ name } ` , data , params ) ;
262254 }
263255 // v1+
264- actionIndex ( docs : string [ ] ) : Promise < any > {
256+ public actionIndex ( docs : string [ ] ) : Promise < any > {
265257 return this . request ( 1 , "POST" , `${ this . ns } /action/index` , docs ) ;
266258 }
267259 // v2+
268- actionSearch ( params : {
260+ public actionSearch ( params : {
269261 query : string ;
270262 files ?: string ;
271263 sys ?: boolean ;
@@ -275,53 +267,53 @@ export class AtelierAPI {
275267 return this . request ( 2 , "GET" , `${ this . ns } /action/search` , null , params ) ;
276268 }
277269 // v1+
278- actionQuery ( query : string , parameters : string [ ] ) : Promise < any > {
270+ public actionQuery ( query : string , parameters : string [ ] ) : Promise < any > {
279271 // outputChannel.appendLine('SQL: ' + query);
280272 // outputChannel.appendLine('SQLPARAMS: ' + JSON.stringify(parameters));
281273 return this . request ( 1 , "POST" , `${ this . ns } /action/query` , {
274+ parameters,
282275 query,
283- parameters
284276 } ) ;
285277 }
286278 // v1+
287- actionCompile ( docs : string [ ] , flags ?: string , source = false ) : Promise < any > {
279+ public actionCompile ( docs : string [ ] , flags ?: string , source = false ) : Promise < any > {
288280 return this . request ( 1 , "POST" , `${ this . ns } /action/compile` , docs , {
289281 flags,
290- source
282+ source,
291283 } ) ;
292284 }
293285
294- cvtXmlUdl ( source : string ) : Promise < any > {
286+ public cvtXmlUdl ( source : string ) : Promise < any > {
295287 return this . request (
296288 1 ,
297289 "POST" ,
298290 `${ this . ns } /` ,
299291 source ,
300292 { } ,
301- { "Content-Type" : "application/xml" }
293+ { "Content-Type" : "application/xml" } ,
302294 ) ;
303295 }
304296 // v2+
305- getmacrodefinition ( docname : string , macroname : string , includes : string [ ] ) {
297+ public getmacrodefinition ( docname : string , macroname : string , includes : string [ ] ) {
306298 return this . request ( 2 , "POST" , `${ this . ns } /action/getmacrodefinition` , {
307299 docname,
300+ includes,
308301 macroname,
309- includes
310302 } ) ;
311303 }
312304 // v2+
313- getmacrolocation ( docname : string , macroname : string , includes : string [ ] ) {
305+ public getmacrolocation ( docname : string , macroname : string , includes : string [ ] ) {
314306 return this . request ( 2 , "POST" , `${ this . ns } /action/getmacrolocation` , {
315307 docname,
308+ includes,
316309 macroname,
317- includes
318310 } ) ;
319311 }
320312 // v2+
321- getmacrollist ( docname : string , includes : string [ ] ) {
313+ public getmacrollist ( docname : string , includes : string [ ] ) {
322314 return this . request ( 2 , "POST" , `${ this . ns } /action/getmacrolist` , {
323315 docname,
324- includes
316+ includes,
325317 } ) ;
326318 }
327319}
0 commit comments