UNPKG

trade360-nodejs-sdk

Version:
98 lines (97 loc) 4.71 kB
import { HttpRequestDto, IHttpServiceConfig } from '../common'; import { BaseEntity } from '../../entities/core-entities/index.js'; import { ILogger } from '../../logger'; import { IHttpService, IRequestArgs } from './interfaces'; /** * BaseHttpClient class is responsible for sending requests * to the REST API. It is a base class for all HTTP clients. * It contains the basic logic for sending requests. * @param apiBaseUrl The base URL of the REST API * @param packageCredentials The package credentials for the API * @param logger The logger instance to be used for logging */ export declare abstract class BaseHttpClient { protected readonly httpService: IHttpService<HttpRequestDto>; protected readonly baseUrl: string; protected requestSettings: HttpRequestDto; protected readonly logger: ILogger; constructor({ restApiBaseUrl, packageCredentials, logger }: IHttpServiceConfig); /** * This method is responsible for sending POST requests to * the customers API. basic POST request, with body contains * packageId, userName and password. * The request expect getting generic type R which declare * the expected response structure. * @param route string that represent the route expected to * be sent to the API endpoint. * @param responseBodyType new instance of the expected response * structure. * @param requestBody optional parameter that represent the body * of the request. * @returns promise with the TResponse type response type of * the API. */ protected postRequest<TResponse extends BaseEntity>({ route, responseBodyType, requestBody, }: IRequestArgs<TResponse>): Promise<TResponse | undefined>; /** * This method is responsible for sending GET requests to the * customers API. The request expect getting generic type R which * declare the expected response structure. * @param route string that represent the route expected to be * sent to the API endpoint. * @param responseBodyType new instance of the expected response * structure. * @param params optional parameter that represent the query * parameters of the request. * @returns promise with the TResponse type response type of the * API. */ protected getRequest<TResponse extends BaseEntity>({ route, responseBodyType, requestBody: params, }: IRequestArgs<TResponse>): Promise<TResponse | undefined>; /** * This method is responsible for handling the valid response. * @param httpResponse The response received from the API call * @param responsePayloadDto The response payload DTO to be used * for transforming the response * @returns The response payload DTO with the required properties * serialized to PascalCase format (API format) * @throws HttpResponseError if the response does not contain the * required properties */ private handleValidResponse; /** * This method is responsible for validating the structure of the * response payload. It checks if the response payload contains the * required properties. * @param responsePayload The response payload to be validated * @returns void if the response payload contains the required * properties * @throws HttpResponseError if the response payload does not contain * the required properties */ private validateResponsePayloadStructure; /** * This method is responsible for handling the error response. * It throws an error with the appropriate message based on the error * response received. If the error response is not an AxiosError, it * throws the error as is. * @param errorResponse The error response received from the API call * @param responsePayloadDto The response payload DTO to be used for * transforming the error response * @returns void if the error response is handled successfully * @throws HttpResponseError if the error response contains errors in * the response body, it throws an error with the error messages as * the context of the error response * @throws HttpResponseError if the error response is not an AxiosError * or if the error response is an AxiosError with a status code that is * not handled by the HttpResponseError class */ private handleErrorResponse; /** * This method is responsible for building the query string from the * request parameters. * @param requestParams The request parameters to be used for building * the query string * @returns The query string built from the request parameters or an * */ private buildQueryString; }