@itwin/property-validation-client
Version:
Property Validation client for the iTwin platform
68 lines • 3.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AxiosRestClient = void 0;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
const axios_1 = require("axios");
const ValidationErrorParser_1 = require("../ValidationErrorParser");
/** Default implementation for {@link RestClient} interface that uses `axios` library for sending the requests. */
class AxiosRestClient {
constructor(parseErrorFunc = ValidationErrorParser_1.ValidationErrorParser.parse) {
this._parseErrorFunc = parseErrorFunc;
}
async sendGetRequest(params) {
const requestConfig = {
headers: params.headers,
};
return axios_1.default.get(params.url, requestConfig)
.then((successResponse) => this.handleSuccess(successResponse))
.catch((errorResponse) => this.handleError(errorResponse));
}
async sendPostRequest(params) {
var _a;
const requestConfig = {
headers: params.headers,
};
return axios_1.default.post(params.url, (_a = params.body) !== null && _a !== void 0 ? _a : {}, requestConfig)
.then((successResponse) => this.handleSuccess(successResponse))
.catch((errorResponse) => this.handleError(errorResponse));
}
async sendPutRequest(params) {
var _a;
const requestConfig = {
headers: params.headers,
};
return axios_1.default.put(params.url, (_a = params.body) !== null && _a !== void 0 ? _a : {}, requestConfig)
.then((successResponse) => this.handleSuccess(successResponse))
.catch((errorResponse) => this.handleError(errorResponse));
}
async sendPatchRequest(params) {
var _a;
const requestConfig = {
headers: params.headers,
};
return axios_1.default.patch(params.url, (_a = params.body) !== null && _a !== void 0 ? _a : {}, requestConfig)
.then((successResponse) => this.handleSuccess(successResponse))
.catch((errorResponse) => this.handleError(errorResponse));
}
async sendDeleteRequest(params) {
const requestConfig = {
headers: params.headers,
};
return axios_1.default.delete(params.url, requestConfig)
.then((successResponse) => this.handleSuccess(successResponse))
.catch((errorResponse) => this.handleError(errorResponse));
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
handleError(errorResponse) {
var _a, _b;
return Promise.reject(this._parseErrorFunc({ statusCode: (_a = errorResponse.response) === null || _a === void 0 ? void 0 : _a.status, body: (_b = errorResponse.response) === null || _b === void 0 ? void 0 : _b.data }));
}
handleSuccess(response) {
return response.data;
}
}
exports.AxiosRestClient = AxiosRestClient;
//# sourceMappingURL=AxiosRestClient.js.map