1414 * language governing permissions and limitations under the License.
1515 */
1616
17- import IRESTClient , { ErrorResponseHandler , ResponseHandler } from "./IRESTClient" ;
17+ import IRESTClient , { ErrorResponseHandler , ResponseHandler } from "./IRESTClient" ;
1818import ClientResponse from "./ClientResponse" ;
19- import fetch , { BodyInit , RequestCredentials , Response } from 'node-fetch' ;
20- import { URLSearchParams } from "url" ;
19+ import fetch , { BodyInit , RequestCredentials , Response } from 'node-fetch' ;
20+ import { URLSearchParams } from "url" ;
2121
2222/**
2323 * @author Brett P
@@ -37,6 +37,42 @@ export default class DefaultRESTClient<RT, ERT> implements IRESTClient<RT, ERT>
3737 constructor ( public host : string ) {
3838 }
3939
40+ /**
41+ * A function that returns the JSON form of the response text.
42+ *
43+ * @param response
44+ * @constructor
45+ */
46+ static async JSONResponseHandler < RT > ( response : Response ) : Promise < ClientResponse < RT > > {
47+ let clientResponse = new ClientResponse < RT > ( ) ;
48+
49+ clientResponse . statusCode = response . status ;
50+ let type = response . headers . get ( "content-type" ) ;
51+ if ( type && type . startsWith ( "application/json" ) ) {
52+ clientResponse . response = await response . json ( ) ;
53+ }
54+
55+ return clientResponse ;
56+ }
57+
58+ /**
59+ * A function that returns the JSON form of the response text.
60+ *
61+ * @param response
62+ * @constructor
63+ */
64+ static async ErrorJSONResponseHandler < ERT > ( response : Response ) : Promise < ClientResponse < ERT > > {
65+ let clientResponse = new ClientResponse < ERT > ( ) ;
66+
67+ clientResponse . statusCode = response . status ;
68+ let type = response . headers . get ( "content-type" ) ;
69+ if ( type && type . startsWith ( "application/json" ) ) {
70+ clientResponse . exception = await response . json ( ) ;
71+ }
72+
73+ return clientResponse ;
74+ }
75+
4076 /**
4177 * Sets the authorization header using a key
4278 *
@@ -86,7 +122,7 @@ export default class DefaultRESTClient<RT, ERT> implements IRESTClient<RT, ERT>
86122 if ( body ) {
87123 body . forEach ( ( value , name , searchParams ) => {
88124 if ( value && value . length > 0 && value != "null" && value != "undefined" ) {
89- body2 . set ( name , value ) ;
125+ body2 . set ( name , value ) ;
90126 }
91127 } ) ;
92128 body = body2 ;
@@ -209,7 +245,7 @@ export default class DefaultRESTClient<RT, ERT> implements IRESTClient<RT, ERT>
209245 let queryString = '' ;
210246 const appendParam = ( key : string , param : string ) => {
211247 queryString += ( queryString . length === 0 ) ? '?' : '&' ;
212- queryString += key + '=' + encodeURIComponent ( param ) ;
248+ queryString += encodeURIComponent ( key ) + '=' + encodeURIComponent ( param ) ;
213249 }
214250 for ( let key in this . parameters ) {
215251 const value = this . parameters [ key ] ;
@@ -221,40 +257,4 @@ export default class DefaultRESTClient<RT, ERT> implements IRESTClient<RT, ERT>
221257 }
222258 return queryString ;
223259 }
224-
225- /**
226- * A function that returns the JSON form of the response text.
227- *
228- * @param response
229- * @constructor
230- */
231- static async JSONResponseHandler < RT > ( response : Response ) : Promise < ClientResponse < RT > > {
232- let clientResponse = new ClientResponse < RT > ( ) ;
233-
234- clientResponse . statusCode = response . status ;
235- let type = response . headers . get ( "content-type" ) ;
236- if ( type && type . startsWith ( "application/json" ) ) {
237- clientResponse . response = await response . json ( ) ;
238- }
239-
240- return clientResponse ;
241- }
242-
243- /**
244- * A function that returns the JSON form of the response text.
245- *
246- * @param response
247- * @constructor
248- */
249- static async ErrorJSONResponseHandler < ERT > ( response : Response ) : Promise < ClientResponse < ERT > > {
250- let clientResponse = new ClientResponse < ERT > ( ) ;
251-
252- clientResponse . statusCode = response . status ;
253- let type = response . headers . get ( "content-type" ) ;
254- if ( type && type . startsWith ( "application/json" ) ) {
255- clientResponse . exception = await response . json ( ) ;
256- }
257-
258- return clientResponse ;
259- }
260260}
0 commit comments