@villedemontreal/http-request
Version:
HTTP utilities - send HTTP requests with proper headers, etc.
132 lines • 5.47 kB
TypeScript
import { IOrderBy } from '@villedemontreal/general-utils';
import { Request } from 'express';
import * as superagent from 'superagent';
/**
* HTTP utilities
*/
export declare class HttpUtils {
private readonly REQ_PARAMS_LOWERCASED;
/**
* Remove first and last slash of the string unless the string is the part after protocol (http://)
*/
removeSlashes(text: string): string;
/**
* Join few parts of an url to a final string
*/
urlJoin(...args: string[]): string;
/**
* Sends a HTTP request built with Superagent.
*
* Will add the proper Correlation Id and will write
* useful logs.
*
* IMPORTANT : this method does NOT throw an Error on a
* 4XX-5XX status response! It will return it the same way
* it returns a 200 response and it is up to the calling code
* to validate the actual response's status. For example
* by using :
*
* if(response.ok) {...}
*
* and/or by checking the status :
*
* if(response.status === 404) {...}
*
* An error will be thrown only when a network problem occures or
* if the target server can't be reached.
*
* This is different from SuperAgent's default behavior that DOES
* throw an error on 4XX-5XX status responses.
*
*/
send(request: superagent.SuperAgentRequest): Promise<superagent.Response>;
/**
* Gets all the values of a querystring parameter.
* Manages the fact that we may use insensitive routing.
*
* A querystring parameter may indeed contains multiple values. For
* example : "path?name=aaa&name=bbb" will result in an
* *array* when getting the "name" parameter : ['aaa', 'bbb'].
*
* @returns all the values of the parameters as an array (even if
* only one value is found) or an empty array if none are found.
*/
getQueryParamAll(req: Request, key: string): string[];
/**
* Get the last value of a querystring parameter.
* Manages the fact that we may use insensitive routing.
*
* A querystring parameter may indeed contains multiple values. For
* example : "path?name=aaa&name=bbb" will result in an
* *array* when getting the "name" parameter : ['aaa', 'bbb'].
*
* In many situation, we only want to deal withy a single value.
* This function return the last value of a query param.
*
* @returns the last parameter with that key or `undefined` if
* not found.
*/
getQueryParamOne(req: Request, key: string): string;
/**
* Get the last value of a querystring parameter *as a Date*.
* The parameter must be parsable using `new Date(xxx)`.
* It is recommended to always use ISO-8601 to represent dates
* (ex: "2020-04-21T17:13:33.107Z").
*
* If the parameter is found but can't be parsed to a Date,
* by default an `Error` is thrown. But if `errorHandler`
* is specified, it is called instead. This allows you
* to catch the error and throw a custom error, for
* example by using `throw createInvalidParameterError(xxx)`
* in an API.
*
* Manages the fact that we may use insensitive routing.
*
* @returns the last parameter with that key as a Date
* or `undefined` if not found.
* @throws An Error if the parameter is found but can't be parsed
* to a Date and no `errorHandler` is specified.
*/
getQueryParamOneAsDate: (req: Request, key: string, errorHandler?: (errMsg: string, value?: string) => any) => Date;
/**
* Get the last value of a querystring parameter *as a Number*.
* The parameter must be parsable using `Number(xxx)`.
*
* If the parameter is found but can't be parsed to a Number,
* by default an `Error` is thrown. But if `errorHandler`
* is specified, it is called instead. This allows you
* to catch the error and throw a custom error, for
* example by using `throw createInvalidParameterError(xxx)`
* in an API.
*
* Manages the fact that we may use insensitive routing.
*
* @returns the last parameter with that key as a Number
* or `undefined` if not found.
* @throws An Error if the parameter is found but can't be parsed
* to a Number and no `errorHandler` is specified.
*/
getQueryParamOneAsNumber: (req: Request, key: string, errorHandler?: (errMsg: string, value?: string) => any) => number;
/**
* Get the last value of a querystring parameter *as a boolean*.
* The value must be "true" or "false" (case insensitive) to
* be considered as a valid boolean. For example, the value '1'
* is invalid.
*
* @returns the last parameter with that key as a boolean
* or `undefined` if not found.
* @throws An Error if the parameter is found but can't be parsed
* to a valid boolean and no `errorHandler` is specified.
*/
getQueryParamOneAsBoolean: (req: Request, key: string, errorHandler?: (errMsg: string, value?: string) => any) => boolean;
private getOriginalQueryParamAsArray;
/**
* Gets the "IOrderBy[]" from the querystring parameters
* of a search request.
*
* @see https://confluence.montreal.ca/pages/viewpage.action?spaceKey=AES&title=REST+API#RESTAPI-Tridelarequ%C3%AAte
*/
getOrderBys: (req: Request) => IOrderBy[];
}
export declare const httpUtils: HttpUtils;
//# sourceMappingURL=httpUtils.d.ts.map