Skip to content

Commit

Permalink
encode the key
Browse files Browse the repository at this point in the history
  • Loading branch information
lyleschemmerling committed Jan 4, 2024
1 parent f0f369c commit e7239f0
Showing 1 changed file with 41 additions and 41 deletions.
82 changes: 41 additions & 41 deletions src/DefaultRESTClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* language governing permissions and limitations under the License.
*/

import IRESTClient, {ErrorResponseHandler, ResponseHandler} from "./IRESTClient";
import IRESTClient, { ErrorResponseHandler, ResponseHandler } from "./IRESTClient";
import ClientResponse from "./ClientResponse";
import fetch, {BodyInit, RequestCredentials, Response} from 'node-fetch';
import {URLSearchParams} from "url";
import fetch, { BodyInit, RequestCredentials, Response } from 'node-fetch';
import { URLSearchParams } from "url";

/**
* @author Brett P
Expand All @@ -37,6 +37,42 @@ export default class DefaultRESTClient<RT, ERT> implements IRESTClient<RT, ERT>
constructor(public host: string) {
}

/**
* A function that returns the JSON form of the response text.
*
* @param response
* @constructor
*/
static async JSONResponseHandler<RT>(response: Response): Promise<ClientResponse<RT>> {
let clientResponse = new ClientResponse<RT>();

clientResponse.statusCode = response.status;
let type = response.headers.get("content-type");
if (type && type.startsWith("application/json")) {
clientResponse.response = await response.json();
}

return clientResponse;
}

/**
* A function that returns the JSON form of the response text.
*
* @param response
* @constructor
*/
static async ErrorJSONResponseHandler<ERT>(response: Response): Promise<ClientResponse<ERT>> {
let clientResponse = new ClientResponse<ERT>();

clientResponse.statusCode = response.status;
let type = response.headers.get("content-type");
if (type && type.startsWith("application/json")) {
clientResponse.exception = await response.json();
}

return clientResponse;
}

/**
* Sets the authorization header using a key
*
Expand Down Expand Up @@ -86,7 +122,7 @@ export default class DefaultRESTClient<RT, ERT> implements IRESTClient<RT, ERT>
if (body) {
body.forEach((value, name, searchParams) => {
if (value && value.length > 0 && value != "null" && value != "undefined") {
body2.set(name,value);
body2.set(name, value);
}
});
body = body2;
Expand Down Expand Up @@ -209,7 +245,7 @@ export default class DefaultRESTClient<RT, ERT> implements IRESTClient<RT, ERT>
let queryString = '';
const appendParam = (key: string, param: string) => {
queryString += (queryString.length === 0) ? '?' : '&';
queryString += key + '=' + encodeURIComponent(param);
queryString += encodeURIComponent(key) + '=' + encodeURIComponent(param);
}
for (let key in this.parameters) {
const value = this.parameters[key];
Expand All @@ -221,40 +257,4 @@ export default class DefaultRESTClient<RT, ERT> implements IRESTClient<RT, ERT>
}
return queryString;
}

/**
* A function that returns the JSON form of the response text.
*
* @param response
* @constructor
*/
static async JSONResponseHandler<RT>(response: Response): Promise<ClientResponse<RT>> {
let clientResponse = new ClientResponse<RT>();

clientResponse.statusCode = response.status;
let type = response.headers.get("content-type");
if (type && type.startsWith("application/json")) {
clientResponse.response = await response.json();
}

return clientResponse;
}

/**
* A function that returns the JSON form of the response text.
*
* @param response
* @constructor
*/
static async ErrorJSONResponseHandler<ERT>(response: Response): Promise<ClientResponse<ERT>> {
let clientResponse = new ClientResponse<ERT>();

clientResponse.statusCode = response.status;
let type = response.headers.get("content-type");
if (type && type.startsWith("application/json")) {
clientResponse.exception = await response.json();
}

return clientResponse;
}
}

0 comments on commit e7239f0

Please sign in to comment.