1- type RequestArgumentTransport = 'path' | 'header' | 'body' | 'query' | 'PATH' | 'HEADER' | 'BODY' | 'QUERY'
1+ type RequestArgumentTransport = 'path' | 'header' | 'body' | 'query' | 'PATH' | 'HEADER' | 'BODY' | 'QUERY' ;
22type RequestMethod =
33 | 'GET'
44 | 'POST'
@@ -27,13 +27,11 @@ export class RestError extends Error {
2727 public readonly response : Response ;
2828 public readonly statusCode : number ;
2929
30- constructor ( error :string , response : Response ) {
30+ constructor ( error : string , response : Response ) {
3131 super ( error ) ;
3232 this . response = response ;
3333 this . statusCode = response . status ;
34-
3534 }
36-
3735}
3836
3937export class RestClient {
@@ -57,13 +55,18 @@ export class RestClient {
5755 }
5856
5957 /**
58+ * Executes a request to the specified path using the specified method.
6059 *
61- * @param {string } method
62- * @param {string } path
63- * @param {RequestArgument[] } requestArguments
64- * @return {Promise<Object> }
60+ * @param {RequestMethod } method The HTTP method to use for the request.
61+ * @param {string } path The path of the resource to request.
62+ * @param {RequestArgument[] } requestArguments An array of request arguments.
63+ * @return {Promise<ReturnData | null> } The result of the request, or null if the response status is 404.
6564 */
66- async execute ( method : RequestMethod , path : string , requestArguments : RequestArgument [ ] = [ ] ) {
65+ async execute < ReturnData extends Record < string , unknown > = any > (
66+ method : RequestMethod ,
67+ path : string ,
68+ requestArguments : RequestArgument [ ] = [ ]
69+ ) {
6770 while ( path . startsWith ( '/' ) ) {
6871 path = path . substring ( 1 ) ;
6972 }
@@ -113,15 +116,15 @@ export class RestClient {
113116 return null ;
114117 }
115118
116- let output = null ;
119+ let output : ReturnData | null = null ;
117120 if ( result . headers . get ( 'content-type' ) ?. startsWith ( 'application/json' ) ) {
118121 //Only parse json if content-type is application/json
119122 const text = await result . text ( ) ;
120- output = text ? JSON . parse ( text ) : null ;
123+ output = text ? ( JSON . parse ( text ) as ReturnData ) : null ;
121124 }
122125
123126 if ( result . status >= 400 ) {
124- let error = output ?. error ?? 'Unknown error' ;
127+ const error = ( output ?. error ?? 'Unknown error' ) as string ;
125128 throw new RestError ( error , result ) ;
126129 }
127130
0 commit comments