UNPKG

@sap-cloud-sdk/odata-common

Version:

SAP Cloud SDK for JavaScript common functions of OData client generator and OpenAPI clint generator.

121 lines 5.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MethodRequestBuilder = void 0; const util_1 = require("@sap-cloud-sdk/util"); const connectivity_1 = require("@sap-cloud-sdk/connectivity"); const internal_1 = require("@sap-cloud-sdk/connectivity/internal"); const request_1 = require("../request"); /** * Base class for all request builders. * @typeParam EntityT - Type of the entity to create a request for. * @internal */ class MethodRequestBuilder { /** * Creates an instance of MethodRequestBuilder. * @param requestConfig - Request configuration to initialize with. */ constructor(requestConfig) { this.requestConfig = requestConfig; } /** * Create the URL based on configuration of the given builder. * @param destination - Destination or DestinationFetchOptions to execute the request against. * @returns Promise resolving to the URL for the request. */ async url(destination) { const request = await this.build(destination); return request.url(); } /** * Create the relative URL based on configuration of the given builder. * @returns The relative URL for the request. */ relativeUrl() { return this.build().relativeUrl(); } /** * 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. */ addCustomHeaders(headers) { this.requestConfig.addCustomHeaders(headers); return this; } middleware(first, ...rest) { this.requestConfig.middlewares = (0, util_1.transformVariadicArgumentToArray)(first, rest); 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. */ addCustomQueryParameters(queryParameters) { this.requestConfig.addCustomQueryParameters(queryParameters); return this; } /** * Replace the default service path with the given custom path. * In case of the SAP S/4HANA APIs the basePath defaults to `/sap/opu/odata/sap/<SERVICE_NAME>` and can be overwritten here. * @param basePath - Path to override the default with. * @returns The request builder itself, to facilitate method chaining. */ setBasePath(basePath) { this.requestConfig.basePath = basePath; 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 {@link @sap-cloud-sdk/http-client!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. */ addCustomRequestConfiguration(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. */ appendPath(...path) { this.requestConfig.appendPath(...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. */ skipCsrfTokenFetching() { this.requestConfig.fetchCsrfToken = false; return this; } /** * Build an ODataRequest that holds essential configuration for the service request and executes it. * @param destination - Targeted destination or DestinationFetchOptions on which the request is performed. * @returns The OData request executor including the destination configuration, if one was given. */ build(destination) { if (destination) { return (0, connectivity_1.useOrFetchDestination)(destination) .then(dest => { if (!dest) { throw Error((0, internal_1.noDestinationErrorMessage)(destination)); } (0, internal_1.assertHttpDestination)(dest); return new request_1.ODataRequest(this.requestConfig, dest); }) .catch(error => { throw new util_1.ErrorWithCause((0, internal_1.noDestinationErrorMessage)(destination), error); }); } return new request_1.ODataRequest(this.requestConfig); } } exports.MethodRequestBuilder = MethodRequestBuilder; //# sourceMappingURL=request-builder-base.js.map