UNPKG

@sap-cloud-sdk/odata-common

Version:

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

107 lines 4.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ODataRequestConfig = void 0; const util_1 = require("@sap-cloud-sdk/util"); const internal_1 = require("@sap-cloud-sdk/http-client/internal"); /** * Parent class for all OData request configs like `getAll`, `delete` or `count`. */ class ODataRequestConfig { /** * Creates an instance of ODataRequest. * @param method - HTTP method of the request. * @param defaultBasePath - Default path of the according service. * @param defaultHeaders - The default headers of the given request as an object. */ constructor(method, defaultBasePath, defaultHeaders) { this.method = method; this.defaultBasePath = defaultBasePath; this.defaultHeaders = { 'content-type': 'application/json', accept: 'application/json' }; this.parameterEncoder = internal_1.oDataTypedClientParameterEncoder; this._customHeaders = {}; this._customQueryParameters = {}; this._customRequestConfiguration = {}; this._appendedPaths = []; this._fetchCsrfToken = true; this._middlewares = []; this.defaultHeaders = (0, util_1.mergeIgnoreCase)(this.defaultHeaders, defaultHeaders); } set middlewares(middlewares) { this._middlewares = middlewares; } get middlewares() { return this._middlewares; } set customHeaders(headers) { this._customHeaders = {}; this.addCustomHeaders(headers); } get customHeaders() { return this._customHeaders; } set customQueryParameters(queryParameters) { this._customQueryParameters = {}; this.addCustomQueryParameters(queryParameters); } get customQueryParameters() { return this._customQueryParameters; } set customRequestConfiguration(requestConfiguration) { this._customRequestConfiguration = {}; this.addCustomRequestConfiguration(requestConfiguration); } get customRequestConfiguration() { return this._customRequestConfiguration; } get appendedPaths() { return this._appendedPaths; } set fetchCsrfToken(fetchCsrfToken) { this._fetchCsrfToken = fetchCsrfToken; } get fetchCsrfToken() { return this._fetchCsrfToken; } /** * Add custom headers to the request. This is useful in case you want to provide your own authorization headers for example. * @param headers - Key-value pairs where the key is the name of a header property and the value is the respective value. */ addCustomHeaders(headers) { Object.entries(headers).forEach(([key, value]) => { // Enforce lower case as HTTP headers are case-insensitive this.customHeaders[key.toLowerCase()] = value; }); } /** * Add custom query parameters to the request. This is useful in case your OData service allows non-standard query parameters. * @param queryParameters - Key-value pairs where the key is the name of a query parameter and the value is the respective value. */ addCustomQueryParameters(queryParameters) { Object.entries(queryParameters).forEach(([key, value]) => { this.customQueryParameters[key] = value; }); } /** * Add custom request configuration to the request. * @param requestConfiguration - Key-value pairs where the key is the name of a request configuration and the value is the respective value. */ addCustomRequestConfiguration(requestConfiguration) { Object.entries(requestConfiguration).forEach(([key, value]) => { this.customRequestConfiguration[key] = value; }); } appendPath(...path) { this.appendedPaths.push(...path); } prependDollarToQueryParameters(params) { return Object.entries(params).reduce((newParams, [key, value]) => { newParams[`$${key}`] = value; return newParams; }, {}); } } exports.ODataRequestConfig = ODataRequestConfig; //# sourceMappingURL=odata-request-config.js.map