UNPKG

@softvision/webpdf-wsclient-typescript

Version:

A simplified and optimized API client library for the webPDF server

169 lines 7.86 kB
"use strict"; 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.HttpRestRequest = void 0; const DataFormat_1 = require("../../DataFormat"); const HttpMethod_1 = require("./HttpMethod"); const exception_1 = require("../../../exception"); const httpStatusCode_1 = __importDefault(require("./httpStatusCode")); const HttpHeader_1 = require("./HttpHeader"); class HttpRestRequest { constructor(session) { this.session = session; this.acceptHeader = DataFormat_1.DataFormats.JSON.getMimeType(); this.requestConfig = { beforeRedirect: (options) => { var _a; let requestHeaders = ((_a = this.requestConfig) === null || _a === void 0 ? void 0 : _a.headers) || {}; if (typeof options.headers[HttpHeader_1.HttpHeaders.AUTHORIZATION] === "undefined") { options.headers[HttpHeader_1.HttpHeaders.AUTHORIZATION] = requestHeaders[HttpHeader_1.HttpHeaders.AUTHORIZATION]; } } }; } static createRequest(session) { return new HttpRestRequest(session); } setAcceptHeader(mimeType) { this.acceptHeader = mimeType; return this; } setAdditionalHeader(key, value) { if (typeof this.requestConfig.headers === "undefined") { this.requestConfig.headers = {}; } this.requestConfig.headers[key] = value; return this; } buildRequest(httpMethod, url, httpEntity, contentType, authMaterial) { return __awaiter(this, void 0, void 0, function* () { switch (httpMethod) { case HttpMethod_1.HttpMethod.GET: case HttpMethod_1.HttpMethod.POST: case HttpMethod_1.HttpMethod.DELETE: case HttpMethod_1.HttpMethod.PUT: case HttpMethod_1.HttpMethod.HEAD: this.requestConfig.method = httpMethod; break; default: throw new exception_1.ClientResultException(exception_1.WsclientErrors.UNKNOWN_HTTP_METHOD); } this.requestConfig.url = url.toString(); if (typeof this.requestConfig.responseEncoding === "undefined") { this.requestConfig.responseEncoding = "utf8"; } if (typeof this.requestConfig.maxContentLength === "undefined") { this.requestConfig.maxContentLength = Infinity; } if (typeof this.requestConfig.maxBodyLength === "undefined") { this.requestConfig.maxBodyLength = Infinity; } if (typeof this.requestConfig.headers === "undefined") { this.requestConfig.headers = {}; } if (typeof this.requestConfig.headers[HttpHeader_1.HttpHeaders.CONTENT_TYPE] === "undefined" && typeof contentType !== "undefined") { this.requestConfig.headers[HttpHeader_1.HttpHeaders.CONTENT_TYPE] = contentType; } this.requestConfig.headers[HttpHeader_1.HttpHeaders.ACCEPT] = this.acceptHeader; if (this.acceptHeader === DataFormat_1.DataFormats.JSON.getMimeType()) { this.requestConfig.responseType = "json"; } else if (this.acceptHeader === DataFormat_1.DataFormats.OCTET_STREAM.getMimeType()) { this.requestConfig.responseType = "arraybuffer"; } let authorizationHeader; if (typeof authMaterial !== "undefined") { authorizationHeader = authMaterial.getAuthHeader(); } else { authorizationHeader = (yield this.session.getAuthProvider().provide(this.session)).getAuthHeader(); } if (typeof authorizationHeader !== "undefined") { this.requestConfig.headers = Object.assign(Object.assign({}, this.requestConfig.headers), authorizationHeader); } if (typeof httpEntity !== "undefined") { this.requestConfig.data = httpEntity; } return this; }); } setAbortSignal(signal) { this.requestConfig.signal = signal; return this; } setOnUploadProgress(callback) { this.requestConfig.onUploadProgress = callback; return this; } setOnDownloadProgress(callback) { this.requestConfig.onDownloadProgress = callback; return this; } checkResponse(httpResponse) { return __awaiter(this, void 0, void 0, function* () { let code = httpResponse.status; if (typeof code !== "undefined" && code >= httpStatusCode_1.default.OK && code < httpStatusCode_1.default.MULTIPLE_CHOICES) { return; } let data = httpResponse.data; if (typeof data === "undefined") { throw new exception_1.ClientResultException(exception_1.WsclientErrors.HTTP_EMPTY_ENTITY); } let exceptionMessage = httpResponse.statusText; let contentType = httpResponse.headers[HttpHeader_1.HttpHeaders.CONTENT_TYPE.toLowerCase()]; if (DataFormat_1.DataFormats.JSON.matches(contentType)) { let wsException = exception_1.ServerResultException.createWebserviceException(data.errorMessage, data.errorCode, data.stackTrace); if (wsException.getErrorCode() !== 0) { throw wsException; } } if (typeof data === "string") { exceptionMessage = data; } throw new exception_1.ClientResultException(exception_1.WsclientErrors.HTTP_CUSTOM_ERROR) .appendMessage(exceptionMessage) .setHttpErrorCode(code); }); } executeRequest() { return __awaiter(this, void 0, void 0, function* () { let response = yield this.execute(); return response.data; }); } execute() { return __awaiter(this, void 0, void 0, function* () { var _a; if (typeof this.requestConfig.url === "undefined") { throw new exception_1.ClientResultException(exception_1.WsclientErrors.INVALID_URL); } let response; try { response = yield this.session.getHttpClient().request(this.requestConfig); } catch (e) { let error = e; if (typeof error.response !== "undefined") { yield this.checkResponse(error.response); } throw exception_1.ServerResultException.createWebserviceException(error.message, error.status || ((_a = error.response) === null || _a === void 0 ? void 0 : _a.status), error.stack); } yield this.checkResponse(response); return response; }); } } exports.HttpRestRequest = HttpRestRequest; //# sourceMappingURL=HttpRestRequest.js.map