@skyway-sdk/common
Version:
The official Next Generation JavaScript SDK for SkyWay
139 lines • 5.81 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpClient = void 0;
const axios_1 = __importDefault(require("axios"));
const logger_1 = require("./logger");
const log = new logger_1.Logger('packages/common/src/http.ts');
/**@internal */
class HttpClient {
constructor(baseURL) {
this.api = axios_1.default.create({ baseURL });
}
/**@throws {@link HttpResponse} */
get(url, config) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.api
.get(url, config)
.catch((err) => err);
if (axios_1.default.isAxiosError(res)) {
const error = Object.assign(Object.assign({}, res.response), { message: res.message });
if (config === null || config === void 0 ? void 0 : config.retry) {
const retry = yield config.retry(error);
if (retry) {
log.warn('retry get', { url });
return this.get(url, config);
}
else {
log.warn('retry get failed', { url });
throw error;
}
}
log.warn('response error', { error });
throw error;
}
else {
return res.data;
}
});
}
/**@throws {@link HttpResponse} */
post(url, data, config) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.api
.post(url, data, config)
.catch((err) => err);
if (axios_1.default.isAxiosError(res)) {
const error = {
data: (_a = res.response) === null || _a === void 0 ? void 0 : _a.data,
status: (_b = res.response) === null || _b === void 0 ? void 0 : _b.status,
statusText: (_c = res.response) === null || _c === void 0 ? void 0 : _c.statusText,
message: res.message,
};
log.warn('error received', error);
if (config === null || config === void 0 ? void 0 : config.retry) {
const needRetry = yield config.retry(error);
if (needRetry) {
log.warn('retry post', url, { data, error, needRetry });
return this.post(url, data, config);
}
else {
throw error;
}
}
throw error;
}
else {
return res.data;
}
});
}
/**@throws {@link HttpResponse} */
put(url, data, config) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.api
.put(url, data, config)
.catch((err) => err);
if (axios_1.default.isAxiosError(res)) {
const error = Object.assign(Object.assign({}, res.response), { message: res.message });
if (config === null || config === void 0 ? void 0 : config.retry) {
const retry = yield config.retry(error);
if (retry) {
log.warn('retry put', { url, data });
return this.put(url, data, config);
}
else {
log.warn('retry put failed', { url, data });
throw error;
}
}
log.warn('response error', { error });
throw error;
}
else {
return res.data;
}
});
}
/**@throws {@link HttpResponse} */
delete(url, config) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.api
.delete(url, config)
.catch((err) => err);
if (axios_1.default.isAxiosError(res)) {
const error = Object.assign(Object.assign({}, res.response), { message: res.message });
if (config === null || config === void 0 ? void 0 : config.retry) {
const retry = yield config.retry(error);
if (retry) {
log.warn('retry delete', { url });
return this.delete(url, config);
}
else {
log.warn('retry delete failed', { url });
throw error;
}
}
log.warn('response error', { error });
throw error;
}
else {
return res.data;
}
});
}
}
exports.HttpClient = HttpClient;
//# sourceMappingURL=http.js.map