tsbase
Version:
Base class libraries for TypeScript
129 lines • 6.19 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpClient = void 0;
var tslib_1 = require("tslib");
var HttpMethod_1 = require("./HttpMethod");
var HttpClient = /** @class */ (function () {
function HttpClient(DefaultRequestHeaders, fetchRef) {
if (DefaultRequestHeaders === void 0) { DefaultRequestHeaders = {}; }
if (fetchRef === void 0) { fetchRef = (_a = globalThis.fetch) === null || _a === void 0 ? void 0 : _a.bind(globalThis); }
var _a;
this.DefaultRequestHeaders = DefaultRequestHeaders;
this.fetchRef = fetchRef;
}
HttpClient.prototype.Request = function (uri, method, body, additionalHeaders) {
var _a, _b;
return tslib_1.__awaiter(this, void 0, void 0, function () {
var request, requestOrResponse, response, _c;
return tslib_1.__generator(this, function (_d) {
switch (_d.label) {
case 0:
request = new Request(uri, {
method: method,
body: body,
headers: tslib_1.__assign(tslib_1.__assign({}, this.DefaultRequestHeaders), additionalHeaders)
});
return [4 /*yield*/, ((_a = this.OnRequestReceived) === null || _a === void 0 ? void 0 : _a.call(this, request))];
case 1:
requestOrResponse = _d.sent();
if (!(requestOrResponse && requestOrResponse instanceof Response)) return [3 /*break*/, 2];
_c = requestOrResponse;
return [3 /*break*/, 4];
case 2: return [4 /*yield*/, this.fetchRef(request)];
case 3:
_c = _d.sent();
_d.label = 4;
case 4:
response = _c;
(_b = this.OnResponseResolved) === null || _b === void 0 ? void 0 : _b.call(this, response);
return [2 /*return*/, response];
}
});
});
};
HttpClient.prototype.Get = function (uri, additionalHeaders) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getRestResponse(uri, HttpMethod_1.HttpMethod.Get, undefined, additionalHeaders)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
HttpClient.prototype.Patch = function (uri, body, additionalHeaders) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getRestResponse(uri, HttpMethod_1.HttpMethod.Patch, body, additionalHeaders)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
HttpClient.prototype.Post = function (uri, body, additionalHeaders) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getRestResponse(uri, HttpMethod_1.HttpMethod.Post, body, additionalHeaders)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
HttpClient.prototype.Put = function (uri, body, additionalHeaders) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getRestResponse(uri, HttpMethod_1.HttpMethod.Put, body, additionalHeaders)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
HttpClient.prototype.Delete = function (uri, additionalHeaders) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getRestResponse(uri, HttpMethod_1.HttpMethod.Delete, undefined, additionalHeaders)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
HttpClient.prototype.getRestResponse = function (uri, method, body, additionalHeaders) {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function () {
var response, isJson, _b;
var _c;
return tslib_1.__generator(this, function (_d) {
switch (_d.label) {
case 0: return [4 /*yield*/, this.Request(uri, method, body, additionalHeaders)];
case 1:
response = _d.sent();
isJson = (_a = response.headers.get('content-type')) === null || _a === void 0 ? void 0 : _a.includes('application/json');
_c = {
ok: response.ok,
status: response.status,
statusText: response.statusText,
headers: response.headers
};
if (!isJson) return [3 /*break*/, 3];
return [4 /*yield*/, response.json()];
case 2:
_b = _d.sent();
return [3 /*break*/, 5];
case 3: return [4 /*yield*/, response.text()];
case 4:
_b = _d.sent();
_d.label = 5;
case 5: return [2 /*return*/, (_c.body = _b,
_c)];
}
});
});
};
return HttpClient;
}());
exports.HttpClient = HttpClient;
//# sourceMappingURL=HttpClient.js.map