@odata2ts/http-client-fetch
Version:
HTTP client based on fetch and consumable by odata2ts
96 lines • 4.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FetchClient = exports.DEFAULT_ERROR_MESSAGE = void 0;
const tslib_1 = require("tslib");
const http_client_base_1 = require("@odata2ts/http-client-base");
const FetchClientError_1 = require("./FetchClientError");
const FetchRequestConfig_1 = require("./FetchRequestConfig");
exports.DEFAULT_ERROR_MESSAGE = "No error message!";
const FETCH_FAILURE_MESSAGE = "OData request failed entirely: ";
const JSON_RETRIEVAL_FAILURE_MESSAGE = "Retrieving JSON body from OData response failed: ";
const BLOB_RETRIEVAL_FAILURE_MESSAGE = "Retrieving blob from OData response failed: ";
const RESPONSE_FAILURE_MESSAGE = "OData server responded with error: ";
function buildErrorMessage(prefix, error) {
const msg = typeof error === "string" ? error : error === null || error === void 0 ? void 0 : error.message;
return prefix + (msg || exports.DEFAULT_ERROR_MESSAGE);
}
class FetchClient extends http_client_base_1.BaseHttpClient {
constructor(config, clientOptions) {
super(clientOptions);
this.config = (0, FetchRequestConfig_1.getDefaultConfig)(config);
}
executeRequest(method, url, data, requestConfig = {}, internalConfig = {}) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const { headers, noBodyEvaluation } = internalConfig;
const _a = (0, FetchRequestConfig_1.mergeFetchConfig)(this.config, { headers }, requestConfig), { params } = _a, config = tslib_1.__rest(_a, ["params"]);
config.method = method;
if (typeof data !== "undefined") {
config.body = internalConfig.dataType === "json" ? JSON.stringify(data) : data;
}
let finalUrl = url;
if (params && Object.values(params).length) {
finalUrl +=
(url.match(/\?/) ? "&" : "?") +
// @ts-ignore
new URLSearchParams(params).toString();
}
// the actual request
let response;
try {
response = yield fetch(finalUrl, config);
}
catch (fetchError) {
throw new FetchClientError_1.FetchClientError(buildErrorMessage(FETCH_FAILURE_MESSAGE, fetchError), undefined, undefined, fetchError);
}
// error response
if (!response.ok) {
let responseData;
try {
responseData = yield this.getResponseBody(response, internalConfig);
}
catch (e) {
responseData = undefined;
}
const errMsg = this.retrieveErrorMessage(responseData);
throw new FetchClientError_1.FetchClientError(buildErrorMessage(RESPONSE_FAILURE_MESSAGE, errMsg), response.status, this.mapHeaders(response.headers), new Error(errMsg || exports.DEFAULT_ERROR_MESSAGE), responseData);
}
let responseData;
try {
responseData = noBodyEvaluation ? undefined : yield this.getResponseBody(response, internalConfig);
}
catch (error) {
const msg = internalConfig.dataType === "blob" ? BLOB_RETRIEVAL_FAILURE_MESSAGE : JSON_RETRIEVAL_FAILURE_MESSAGE;
throw new FetchClientError_1.FetchClientError(buildErrorMessage(msg, error), response.status, this.mapHeaders(response.headers), error);
}
return {
status: response.status,
statusText: response.statusText,
headers: this.mapHeaders(response.headers),
data: responseData,
};
});
}
getResponseBody(response, options) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (response.status === 204) {
return undefined;
}
switch (options.dataType) {
case "json":
return response.json();
case "blob":
return response.blob();
case "stream":
return response.body;
}
return undefined;
});
}
mapHeaders(headers) {
const result = {};
headers.forEach((value, key) => (result[key] = value));
return result;
}
}
exports.FetchClient = FetchClient;
//# sourceMappingURL=FetchClient.js.map