UNPKG

@sap-cloud-sdk/core

Version:
201 lines • 10.5 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 __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MethodRequestBuilderBase = exports.MethodRequestBuilder = void 0; var util_1 = require("@sap-cloud-sdk/util"); var scp_cf_1 = require("../../connectivity/scp-cf"); var odata_request_1 = require("../request/odata-request"); /** * Base class for all request builders. * @typeparam EntityT - Type of the entity to create a request for. */ var MethodRequestBuilder = /** @class */ (function () { /** * Creates an instance of MethodRequestBuilder. * @param requestConfig - Request configuration to initialize with. */ function MethodRequestBuilder(requestConfig) { this.requestConfig = requestConfig; } /** * Create the URL based on configuration of the given builder. * @param destination - Destination to execute the request against. * @param options - Options to employ when fetching destinations. * @returns Promise resolving to the URL for the request */ MethodRequestBuilder.prototype.url = function (destination, options) { return __awaiter(this, void 0, void 0, function () { var request; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.build(destination, options)]; case 1: request = _a.sent(); return [2 /*return*/, request.url()]; } }); }); }; /** * Create the relative URL based on configuration of the given builder. * @returns The relative URL for the request */ MethodRequestBuilder.prototype.relativeUrl = function () { return this.build().relativeUrl(); }; /** * @deprecated Since version 1.34.0 Use [[addCustomHeaders]] instead. * Add custom headers to the request. * @param headers - Key-value pairs denoting additional custom headers. * @returns The request builder itself, to facilitate method chaining. */ MethodRequestBuilder.prototype.withCustomHeaders = function (headers) { this.requestConfig.addCustomHeaders(headers); return this; }; /** * Add custom headers to the request. Existing headers will be overwritten. * @param headers - Key-value pairs denoting additional custom headers. * @returns The request builder itself, to facilitate method chaining. */ MethodRequestBuilder.prototype.addCustomHeaders = function (headers) { this.requestConfig.addCustomHeaders(headers); return this; }; /** * @deprecated Since version 1.34.0 Use [[addCustomQueryParameters]] instead. * Add custom query parameters to the request. * @param queryParameters - Key-value pairs denoting additional custom query parameters to be set in the request. * @returns The request builder itself, to facilitate method chaining. */ MethodRequestBuilder.prototype.withCustomQueryParameters = function (queryParameters) { this.requestConfig.addCustomQueryParameters(queryParameters); return this; }; /** * Add custom query parameters to the request. If a query parameter with the given name already exists it is overwritten. * @param queryParameters - Key-value pairs denoting additional custom query parameters to be set in the request. * @returns The request builder itself, to facilitate method chaining */ MethodRequestBuilder.prototype.addCustomQueryParameters = function (queryParameters) { this.requestConfig.addCustomQueryParameters(queryParameters); return this; }; /** * @deprecated Since version 1.34.0 Use [[setCustomServicePath]] instead. * Replace the default service path with the given custom path. * In case of the SAP S/4HANA APIs the servicePath defaults to `/sap/opu/odata/sap/<SERVICE_NAME>` and can be overwritten here. * @param servicePath - Path to override the default with. * @returns The request builder itself, to facilitate method chaining. */ MethodRequestBuilder.prototype.withCustomServicePath = function (servicePath) { this.requestConfig.customServicePath = servicePath; return this; }; /** * Replace the default service path with the given custom path. * In case of the SAP S/4HANA APIs the servicePath defaults to `/sap/opu/odata/sap/<SERVICE_NAME>` and can be overwritten here. * @param servicePath - Path to override the default with * @returns The request builder itself, to facilitate method chaining */ MethodRequestBuilder.prototype.setCustomServicePath = function (servicePath) { this.requestConfig.customServicePath = servicePath; return this; }; /** * Add a custom request configuration to the request. Typically, this is used when specifying a response type for downloading files. * If the custom request configuration contains [[defaultDisallowedKeys | disallowed keys]], those will be ignored. * @param requestConfiguration - Key-value pairs denoting additional custom request configuration options to be set in the request. * @returns The request builder itself, to facilitate method chaining. */ MethodRequestBuilder.prototype.addCustomRequestConfiguration = function (requestConfiguration) { this.requestConfig.addCustomRequestConfiguration(requestConfiguration); return this; }; /** * Append the given path to the URL. * This can be used for querying navigation properties of an entity. * To execute a request with an appended path use `executeRaw` to avoid errors during deserialization. When using this, the `execute` method is omitted from the return type. * @param path - Path to be appended. * @returns The request builder itself without "execute" function, to facilitate method chaining. */ MethodRequestBuilder.prototype.appendPath = function () { var _a; var path = []; for (var _i = 0; _i < arguments.length; _i++) { path[_i] = arguments[_i]; } (_a = this.requestConfig).appendPath.apply(_a, path); return this; }; /** * Skip fetching csrf token for this request, which is typically useful when the csrf token is not required. * @returns The request builder itself, to facilitate method chaining. */ MethodRequestBuilder.prototype.skipCsrfTokenFetching = function () { this.requestConfig.fetchCsrfToken = false; return this; }; /** * Build an ODataRequest that holds essential configuration for the service request and executes it. * @deprecated Since v1.30.0. This method will be protected and should not be used externally. * @param destination - Targeted destination on which the request is performed. * @param options - Options to employ when fetching destinations. * @returns The OData request executor including the destination configuration, if one was given. */ MethodRequestBuilder.prototype.build = function (destination, options) { var _this = this; if (destination) { return (0, scp_cf_1.useOrFetchDestination)(destination, options) .then(function (dest) { if (!dest) { throw Error((0, scp_cf_1.noDestinationErrorMessage)(destination)); } return new odata_request_1.ODataRequest(_this.requestConfig, dest); }) .catch(function (error) { throw new util_1.ErrorWithCause((0, scp_cf_1.noDestinationErrorMessage)(destination), error); }); } return new odata_request_1.ODataRequest(this.requestConfig); }; return MethodRequestBuilder; }()); exports.MethodRequestBuilder = MethodRequestBuilder; exports.MethodRequestBuilderBase = MethodRequestBuilder; //# sourceMappingURL=request-builder-base.js.map