1 | import { AxiosInstance, AxiosRequestConfig } from 'axios';
|
2 | import { IHttpRequestHandler, IRequestData, IRequestResponse, InterceptorCallback, ErrorHandlingStrategy, RequestHandlerConfig } from './types/http-request-handler';
|
3 | /**
|
4 | * Generic Request Handler
|
5 | * It creates an Axios instance and handles requests within that instance
|
6 | * It handles errors depending on a chosen error handling strategy
|
7 | */
|
8 | export declare class HttpRequestHandler implements IHttpRequestHandler {
|
9 | /**
|
10 | * @var requestInstance Provider's instance
|
11 | *
|
12 | * @memberof HttpRequestHandler
|
13 | */
|
14 | requestInstance: AxiosInstance;
|
15 | /**
|
16 | * @var timeout Request timeout
|
17 | *
|
18 | * @memberof HttpRequestHandler
|
19 | */
|
20 | timeout: number;
|
21 | /**
|
22 | * @var strategy Request timeout
|
23 | *
|
24 | * @memberof HttpRequestHandler
|
25 | */
|
26 | strategy: ErrorHandlingStrategy;
|
27 | /**
|
28 | * @var flattenResponse Response flattening
|
29 | *
|
30 | * @memberof HttpRequestHandler
|
31 | */
|
32 | flattenResponse: boolean;
|
33 | /**
|
34 | * @var logger Logger
|
35 | *
|
36 | * @memberof HttpRequestHandler
|
37 | */
|
38 | protected logger: any;
|
39 | /**
|
40 | * @var httpRequestErrorService HTTP error service
|
41 | *
|
42 | * @memberof HttpRequestHandler
|
43 | */
|
44 | protected httpRequestErrorService: any;
|
45 | /**
|
46 | * Creates an instance of HttpRequestHandler
|
47 | *
|
48 | * @param {string} baseURL Base URL for all API calls
|
49 | * @param {number} timeout Request timeout
|
50 | * @param {string} strategy Error Handling Strategy
|
51 | * @param {string} flattenResponse Whether to flatten response "data" object within "data" one
|
52 | * @param {*} logger Instance of Logger Class
|
53 | * @param {*} httpRequestErrorService Instance of Error Service Class
|
54 | *
|
55 | * @memberof HttpRequestHandler
|
56 | */
|
57 | constructor({ baseURL, timeout, strategy, flattenResponse, logger, httpRequestErrorService, ...config }: RequestHandlerConfig);
|
58 | /**
|
59 | * Get Provider Instance
|
60 | *
|
61 | * @returns {AxiosInstance} Provider's instance
|
62 | * HttpRequestHandler
|
63 | */
|
64 | getInstance(): AxiosInstance;
|
65 | /**
|
66 | * Intercept Request
|
67 | *
|
68 | * @param {*} callback callback to use before request
|
69 | * @returns {void}
|
70 | * @memberof HttpRequestHandler
|
71 | */
|
72 | interceptRequest(callback: InterceptorCallback): void;
|
73 | /**
|
74 | * POST Request
|
75 | *
|
76 | * @param {string} url Url
|
77 | * @param {*} data Payload
|
78 | * @param {AxiosRequestConfig} config Config
|
79 | * @throws {Error} If request fails
|
80 | * @returns {Promise} Request response or error info
|
81 | * @memberof HttpRequestHandler
|
82 | */
|
83 | post(url: string, data?: any, config?: AxiosRequestConfig): Promise<IRequestResponse>;
|
84 | /**
|
85 | * GET Request
|
86 | *
|
87 | * @param {string} url Url
|
88 | * @param {*} data Payload
|
89 | * @param {AxiosRequestConfig} config Config
|
90 | * @throws {Error} If request fails
|
91 | * @returns {Promise} Request response or error info
|
92 | * @memberof HttpRequestHandler
|
93 | */
|
94 | get(url: string, data?: any, config?: AxiosRequestConfig): Promise<IRequestResponse>;
|
95 | /**
|
96 | * PUT Request
|
97 | *
|
98 | * @param {string} url Url
|
99 | * @param {*} data Payload
|
100 | * @param {AxiosRequestConfig} config Config
|
101 | * @throws {Error} If request fails
|
102 | * @returns {Promise} Request response or error info
|
103 | * @memberof HttpRequestHandler
|
104 | */
|
105 | put(url: string, data?: any, config?: AxiosRequestConfig): Promise<IRequestResponse>;
|
106 | /**
|
107 | * DELETE Request
|
108 | *
|
109 | * @param {string} url Url
|
110 | * @param {*} data Payload
|
111 | * @param {AxiosRequestConfig} config Config
|
112 | * @throws {Error} If request fails
|
113 | * @returns {Promise} Request response or error info
|
114 | * @memberof HttpRequestHandler
|
115 | */
|
116 | delete(url: string, data?: any, config?: AxiosRequestConfig): Promise<IRequestResponse>;
|
117 | /**
|
118 | * PATCH Request
|
119 | *
|
120 | * @param {string} url Url
|
121 | * @param {*} data Payload
|
122 | * @param {AxiosRequestConfig} config Config
|
123 | * @throws {Error} If request fails
|
124 | * @returns {Promise} Request response or error info
|
125 | * @memberof HttpRequestHandler
|
126 | */
|
127 | patch(url: string, data?: any, config?: AxiosRequestConfig): Promise<IRequestResponse>;
|
128 | /**
|
129 | * HEAD Request
|
130 | *
|
131 | * @param {string} url Url
|
132 | * @param {*} data Payload
|
133 | * @param {AxiosRequestConfig} config Config
|
134 | * @throws {Error} If request fails
|
135 | * @returns {Promise} Request response or error info
|
136 | * @memberof HttpRequestHandler
|
137 | */
|
138 | head(url: string, data?: any, config?: AxiosRequestConfig): Promise<IRequestResponse>;
|
139 | /**
|
140 | * Get Provider Instance
|
141 | *
|
142 | * @param {Error} err Error instance
|
143 | * @returns {AxiosInstance} Provider's instance
|
144 | * @memberof HttpRequestHandler
|
145 | */
|
146 | protected processRequestError(err: Error): void;
|
147 | /**
|
148 | * Handle Request depending on used strategy
|
149 | *
|
150 | * @param {object} payload Payload
|
151 | * @param {string} payload.type Request type
|
152 | * @param {string} payload.url Request url
|
153 | * @param {*} payload.data Request data
|
154 | * @param {AxiosRequestConfig} payload.config Config to modify request
|
155 | * @throws {Error}
|
156 | * @returns {Promise} Response Data
|
157 | * @memberof HttpRequestHandler
|
158 | */
|
159 | protected handleRequest({ type, url, data, config, }: IRequestData): Promise<IRequestResponse>;
|
160 | protected processResponseData(response: any): any;
|
161 | }
|