Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | 4x 4x 4x 4x 7x 7x 4x 14x 37x 37x 37x 37x 37x 37x 7x 7x 30x 7x 7x 7x 37x 4x 14x 36x 12x 14x 16x 4x 34x 37x | import axios, { AxiosResponse, Method } from "axios";
import { Collection, collect, ReadonlyRecord } from "./apis/shared";
import { OptionsWrapperWithSetter } from "./options-wrapper";
export type Headers = ReadonlyRecord<string, string>;
export interface HttpClientOptions<Rq = any, Rs = any> {
headers?: Headers;
agent?: any;
okStatusCodes?: Collection<number>;
baseURL?: string;
timeout?: number;
stringifyBody?: (body: Rq) => string | Buffer;
parseResponse?: (body: Buffer) => Rs;
mapHttpErrors?: (error: HttpError) => Error;
}
export interface HttpRequestOptions<Rq = any, Rs = any>
extends Partial<HttpClientOptions<Rq, Rs>> {
url: string;
method: string;
body?: Rq;
}
export interface HttpResponse<Rq = any, Rs = any> {
request: HttpRequestOptions<Rq, Rs>;
statusCode: number;
headers: Headers;
body?: Rs;
}
/**
*
*
* @export
* @class HttpError
* @extends {Error}
*/
export class HttpError<Rq = any, Rs = any> extends Error {
/**
* Creates an instance of HttpError.
* @param {string} message
* @param {HttpResponse} response
* @memberof HttpError
*/
constructor(message: string, public response: HttpResponse<Rq, Rs>) {
super(message);
}
}
export interface HttpClient<Rq = any, Rs = any> {
options: HttpClientOptions<Rq, Rs>;
request(options: HttpRequestOptions<Rq, Rs>): Promise<HttpResponse<Rq, Rs>>;
}
/**
* Identity function
*
* @template T
* @param {T} argument
* @return {T}
*/
function _<T>(argument: T): T {
/* istanbul ignore next */
return argument;
}
/**
*
*
* @export
* @class DefaultHttpClient
* @implements {HttpClient}
*/
export class DefaultHttpClient
extends OptionsWrapperWithSetter<HttpClientOptions>
implements HttpClient
{
/**
*
*
* @protected
* @memberof DefaultHttpClient
*/
protected readonly _axios = axios.create();
/**
*
*
* @param {HttpRequestOptions} options
* @return {Promise<HttpResponse>}
* @memberof DefaultHttpClient
*/
async request(options: HttpRequestOptions): Promise<HttpResponse> {
const request = {
...this.options,
...options,
headers: { ...this.options.headers, ...options.headers }
};
const okStatusCodes = new Set(
request.okStatusCodes ? collect(request.okStatusCodes) : []
);
const validateStatus = (status: number) =>
okStatusCodes.size === 0 ? true : okStatusCodes.has(status);
try {
const axiosResponse = await this._axios
.request({
httpAgent: request.agent,
httpsAgent: request.agent,
baseURL: request.baseURL,
timeout: request.timeout,
url: request.url,
method: request.method as Method,
headers: { ...request.headers },
responseType: "arraybuffer",
data: request.body
? (request.stringifyBody ?? _)(request.body)
: undefined,
validateStatus
})
.catch((error) => {
if (axios.isAxiosError(error) && error.response) {
throw new HttpError(
error.message,
this._mapResponse(error.response, request)
);
}
// Тесты не покрывают кривые использования API
/* istanbul ignore next */
throw error;
});
return this._mapResponse(axiosResponse, request);
} catch (error: unknown) {
Iif (!(error instanceof HttpError)) throw error;
Iif (typeof request.mapHttpErrors !== "function") throw error;
throw request.mapHttpErrors(error);
}
}
/**
*
*
* @protected
* @param {AxiosResponse} axiosResponse
* @param {HttpRequestOptions} request
* @return {HttpResponse} {HttpResponse}
* @memberof DefaultHttpClient
*/
protected _mapResponse(
axiosResponse: AxiosResponse,
request: HttpRequestOptions
): HttpResponse {
return {
headers: axiosResponse.headers,
statusCode: axiosResponse.status,
body:
axiosResponse.data !== undefined
? (request.parseResponse ?? _)(axiosResponse.data)
: undefined,
request
};
}
}
/**
*
*
* @export
* @class SimpleJsonHttp
*/
export class SimpleJsonHttp {
/**
* Creates an instance of SimpleJsonHttp.
* @param {HttpClient} client
* @memberof SimpleJsonHttp
*/
constructor(
public client: HttpClient = new DefaultHttpClient({
parseResponse: (body) => JSON.parse(body.toString()),
stringifyBody: (body) => JSON.stringify(body)
})
) {}
/**
*
*
* @template T
* @param {string} url
* @return {Promise<T>}
* @memberof SimpleJsonHttp
*/
async get<T>(url: string): Promise<T> {
return await this.simpleRequest("GET", url);
}
/**
*
*
* @template T
* @param {string} url
* @param {*} [data]
* @return {Promise<T>}
* @memberof SimpleJsonHttp
*/
async post<T>(url: string, data?: any): Promise<T> {
return await this.simpleRequest("POST", url, data);
}
/**
*
*
* @template T
* @param {string} url
* @param {*} [data]
* @return {Promise<T>}
* @memberof SimpleJsonHttp
*/
async put<T>(url: string, data?: any): Promise<T> {
return await this.simpleRequest("PUT", url, data);
}
/**
*
*
* @template T
* @param {string} url
* @param {*} [data]
* @return {Promise<T>}
* @memberof SimpleJsonHttp
*/
async patch<T>(url: string, data?: any): Promise<T> {
// Метод PATCH не используется в тестах
/* istanbul ignore next */
return await this.simpleRequest("PATCH", url, data);
}
/**
*
*
* @template T
* @param {string} url
* @param {*} [data]
* @return {Promise<T>}
* @memberof SimpleJsonHttp
*/
async delete<T>(url: string, data?: any): Promise<T> {
// Метод DELETE не используется в тестах
/* istanbul ignore next */
return await this.simpleRequest("DELETE", url, data);
}
/**
*
* @template T
* @param {string} method
* @param {string} url
* @param {*} [body]
* @return {Promise<T>} {Promise<T>}
* @memberof SimpleJsonHttp
*/
async simpleRequest<T>(method: string, url: string, body?: any): Promise<T> {
return await this.request<T>({ method, url, body });
}
/**
*
* @template T
* @param {HttpRequestOptions} option
* @return {Promise<T>} {Promise<T>}
* @memberof SimpleJsonHttp
*/
async request<T>(option: HttpRequestOptions): Promise<T> {
return await this.client.request(option).then((response) => response.body);
}
}
|