@@ -221,83 +221,52 @@ export class Tailscale {
221221 }
222222
223223 async serveStatus ( ) : Promise < ServeStatus > {
224- if ( ! this . url ) {
225- throw new Error ( 'uninitialized client' ) ;
226- }
227- try {
228- const resp = await fetch ( `${ this . url } /serve` , {
229- headers : {
230- Authorization : 'Basic ' + this . authkey ,
231- } ,
232- } ) ;
233-
234- const status = ( await resp . json ( ) ) as ServeStatus ;
235- return status ;
236- } catch ( e ) {
237- Logger . error ( `error calling status: ${ JSON . stringify ( e , null , 2 ) } ` ) ;
238- throw e ;
239- }
224+ return ( await this . performFetch ( '/serve' ) ) as ServeStatus ;
240225 }
241226
242227 async serveAdd ( p : ServeParams ) {
243- if ( ! this . url ) {
244- throw new Error ( 'uninitialized client' ) ;
245- }
246- try {
247- const resp = await fetch ( `${ this . url } /serve` , {
248- method : 'POST' ,
249- headers : {
250- Authorization : 'Basic ' + this . authkey ,
251- } ,
252- body : JSON . stringify ( p ) ,
253- } ) ;
254- if ( ! resp . ok ) {
255- throw new Error ( '/serve failed' ) ;
256- }
257- } catch ( e ) {
258- Logger . info ( `error adding serve: ${ e } ` ) ;
259- throw e ;
260- }
228+ await this . performFetch ( '/serve' , 'POST' , p ) ;
261229 }
262230
263231 async serveDelete ( p ?: ServeParams ) {
264- if ( ! this . url ) {
265- throw new Error ( 'uninitialized client' ) ;
266- }
267- try {
268- const resp = await fetch ( `${ this . url } /serve` , {
269- method : 'DELETE' ,
270- headers : {
271- Authorization : 'Basic ' + this . authkey ,
272- } ,
273- body : JSON . stringify ( p ) ,
274- } ) ;
275- if ( ! resp . ok ) {
276- throw new Error ( '/serve failed' ) ;
277- }
278- } catch ( e ) {
279- Logger . info ( `error deleting serve: ${ e } ` ) ;
280- throw e ;
281- }
232+ await this . performFetch ( '/serve' , 'DELETE' , p ) ;
282233 }
283234
284235 async setFunnel ( port : number , on : boolean ) {
236+ await this . performFetch ( '/funnel' , 'POST' , { port, on } ) ;
237+ }
238+
239+ async performFetch ( endpoint : string , method = 'GET' , body ?: unknown ) {
285240 if ( ! this . url ) {
286241 throw new Error ( 'uninitialized client' ) ;
287242 }
243+
288244 try {
289- const resp = await fetch ( `${ this . url } /funnel ` , {
290- method : 'POST' ,
245+ const resp = await fetch ( `${ this . url } ${ endpoint } ` , {
246+ method,
291247 headers : {
292248 Authorization : 'Basic ' + this . authkey ,
293249 } ,
294- body : JSON . stringify ( { port , on } ) ,
250+ body : body !== undefined && typeof body === 'object' ? JSON . stringify ( body ) : undefined ,
295251 } ) ;
252+
296253 if ( ! resp . ok ) {
297- throw new Error ( '/serve failed' ) ;
254+ Logger . error ( `${ endpoint } failed: ${ JSON . stringify ( resp ) } ` ) ;
255+ throw new Error ( `${ endpoint } failed` ) ;
298256 }
257+
258+ const text = await resp . text ( ) ;
259+
260+ try {
261+ return JSON . parse ( text ) ;
262+ // eslint-disable-next-line no-empty
263+ } catch {
264+ Logger . error ( `failed to parse json: ${ text } ` ) ;
265+ }
266+
267+ return text ;
299268 } catch ( e ) {
300- Logger . info ( `error deleting serve : ${ e } ` ) ;
269+ Logger . info ( `error in ${ method } ${ endpoint } : ${ e } ` ) ;
301270 throw e ;
302271 }
303272 }
0 commit comments