Skip to content

Commit

Permalink
improve RestClient typings
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-marechal committed Oct 1, 2021
1 parent 1496d5e commit c873d59
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions src/protocol/rest-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ export interface HttpResponse {
*/
export class RestClient {

public static get<T>(url: string, parameters?: Map<string, string>): Promise<TspClientResponse<BigintOrNumber<T>>>;
public static get<T>(url: string, parameters?: Map<string, string>, normalizer?: Normalizer<T>): Promise<TspClientResponse<T>>;
static get<T>(url: string, parameters?: Map<string, string>): Promise<TspClientResponse<BigintOrNumber<T>>>;
static get<T>(url: string, parameters: Map<string, string> | undefined, normalizer: Normalizer<T>): Promise<TspClientResponse<T>>;
/**
* Perform GET
* @template T is the expected type of the json object returned by this request
* @param url URL to query without query parameters
* @param parameters Query parameters. Mapped keys and values are used to build the final URL
*/
public static async get<T>(url: string, parameters?: Map<string, string>, normalizer?: Normalizer<T>) {
static async get<T>(url: string, parameters?: Map<string, string>, normalizer?: Normalizer<T>) {
let getUrl = url;
if (parameters) {
const urlParameters = this.encodeURLParameters(parameters);
Expand All @@ -44,39 +44,39 @@ export class RestClient {
return this.performRequest('get', getUrl, undefined, normalizer);
}

public static post<T>(url: string, body?: any): Promise<TspClientResponse<BigintOrNumber<T>>>;
public static post<T>(url: string, body?: any, normalizer?: Normalizer<T>): Promise<TspClientResponse<T>>;
static post<T>(url: string, body?: any): Promise<TspClientResponse<BigintOrNumber<T>>>;
static post<T>(url: string, body: any, normalizer: Normalizer<T>): Promise<TspClientResponse<T>>;
/**
* Perform POST
* @template T is the expected type of the json object returned by this request
* @param url URL to query
* @param body Query object as defined by the Query interface
*/
public static async post<T>(url: string, body?: any, normalizer?: Normalizer<T>) {
static async post<T>(url: string, body?: any, normalizer?: Normalizer<T>) {
return this.performRequest<T>('post', url, body, normalizer);
}

public static put<T>(url: string, body?: any): Promise<TspClientResponse<BigintOrNumber<T>>>;
public static put<T>(url: string, body?: any, normalizer?: Normalizer<T>): Promise<TspClientResponse<T>>;
static put<T>(url: string, body?: any): Promise<TspClientResponse<BigintOrNumber<T>>>;
static put<T>(url: string, body: any, normalizer: Normalizer<T>): Promise<TspClientResponse<T>>;
/**
* Perform PUT
* @template T is the expected type of the json object returned by this request
* @param url URL to query
* @param body Query object as defined by the Query interface
*/
public static async put<T>(url: string, body?: any, normalizer?: Normalizer<T>) {
static async put<T>(url: string, body?: any, normalizer?: Normalizer<T>) {
return this.performRequest<T>('put', url, body, normalizer);
}

public static delete<T>(url: string, parameters?: Map<string, string>): Promise<TspClientResponse<BigintOrNumber<T>>>;
public static delete<T>(url: string, parameters?: Map<string, string>, normalizer?: Normalizer<T>): Promise<TspClientResponse<T>>;
static delete<T>(url: string, parameters?: Map<string, string>): Promise<TspClientResponse<BigintOrNumber<T>>>;
static delete<T>(url: string, parameters: Map<string, string> | undefined, normalizer: Normalizer<T>): Promise<TspClientResponse<T>>;
/**
* Perform DELETE
* @template T is the expected type of the json object returned by this request
* @param url URL to query without query parameters
* @param parameters Query parameters. Mapped keys and values are used to build the final URL
*/
public static async delete<T>(url: string, parameters?: Map<string, string>, normalizer?: Normalizer<T>) {
static async delete<T>(url: string, parameters?: Map<string, string>, normalizer?: Normalizer<T>) {
let deleteUrl = url;
if (parameters) {
const urlParameters = this.encodeURLParameters(parameters);
Expand All @@ -85,9 +85,7 @@ export class RestClient {
return this.performRequest<T>('delete', deleteUrl, undefined, normalizer);
}

protected static performRequest<T>(method: string, url: string, body?: any): Promise<TspClientResponse<BigintOrNumber<T>>>;
protected static performRequest<T>(method: string, url: string, body: any, normalizer?: Normalizer<T>): Promise<TspClientResponse<T>>;
protected static async performRequest(method, url, body?, normalizer?) {
protected static async performRequest<T>(method: string, url: string, body?: any, normalizer?: Normalizer<T>) {
const response = await this.httpRequest({
url,
method,
Expand Down Expand Up @@ -126,15 +124,15 @@ export class RestClient {
}

/**
* Stringify JS objects. Can stringify `BigInt`s.
* Stringify JS objects. Can stringify `BigInt` values.
*/
protected static jsonStringify(data: any): string {
return JSONBig.stringify(data);
}

/**
* Parse JSON-encoded data. If a number is too large to fit
* into a regular `number` then it will be deserialized as `BigInt`.
* Parse JSON-encoded data. If a number is too large to fit into a regular
* `number` then it will be deserialized as `BigInt`.
*/
protected static jsonParse(text: string): any {
return JSONBig.parse(text);
Expand Down

0 comments on commit c873d59

Please sign in to comment.