@sap-cloud-sdk/core
Version:
SAP Cloud SDK for JavaScript core
153 lines • 6.16 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ODataRequestConfig = void 0;
var util_1 = require("@sap-cloud-sdk/util");
var logger = (0, util_1.createLogger)({
package: '@sap-cloud-sdk/core',
messageContext: 'odata-request-config'
});
/**
* @hidden
*/
var ODataRequestConfig = /** @class */ (function () {
/**
* Creates an instance of ODataRequest.
* @param method - HTTP method of the request
* @param defaultServicePath - default path of the according service
* @param defaultHeadersOrContentType - The default headers of the given request as an object. When passing a string only set the content type header will be set. Setting the content type only is deprecated since v1.30.0.
*/
function ODataRequestConfig(method, defaultServicePath, defaultHeadersOrContentType) {
this.method = method;
this.defaultServicePath = defaultServicePath;
this.defaultHeaders = {
'content-type': 'application/json',
accept: 'application/json'
};
this._customHeaders = {};
this._customQueryParameters = {};
this._customRequestConfiguration = {};
this._appendedPaths = [];
this._fetchCsrfToken = true;
if (defaultServicePath === util_1.VALUE_IS_UNDEFINED) {
logger.warn('The service path is undefined in "_defaultServicePath".');
}
if (typeof defaultHeadersOrContentType === 'string') {
this.defaultHeaders['content-type'] = defaultHeadersOrContentType;
}
else {
this.defaultHeaders = (0, util_1.mergeIgnoreCase)(this.defaultHeaders, defaultHeadersOrContentType);
}
}
Object.defineProperty(ODataRequestConfig.prototype, "contentType", {
/**
* @deprecated Since v1.30.0. Use [[defaultHeaders]] instead.
*/
get: function () {
return this.defaultHeaders['content-type'];
},
enumerable: false,
configurable: true
});
Object.defineProperty(ODataRequestConfig.prototype, "customHeaders", {
get: function () {
return this._customHeaders;
},
set: function (headers) {
this._customHeaders = {};
this.addCustomHeaders(headers);
},
enumerable: false,
configurable: true
});
Object.defineProperty(ODataRequestConfig.prototype, "customQueryParameters", {
get: function () {
return this._customQueryParameters;
},
set: function (queryParameters) {
this._customQueryParameters = {};
this.addCustomQueryParameters(queryParameters);
},
enumerable: false,
configurable: true
});
Object.defineProperty(ODataRequestConfig.prototype, "customRequestConfiguration", {
get: function () {
return this._customRequestConfiguration;
},
set: function (requestConfiguration) {
this._customRequestConfiguration = {};
this.addCustomRequestConfiguration(requestConfiguration);
},
enumerable: false,
configurable: true
});
Object.defineProperty(ODataRequestConfig.prototype, "appendedPaths", {
get: function () {
return this._appendedPaths;
},
enumerable: false,
configurable: true
});
Object.defineProperty(ODataRequestConfig.prototype, "fetchCsrfToken", {
get: function () {
return this._fetchCsrfToken;
},
set: function (fetchCsrfToken) {
this._fetchCsrfToken = fetchCsrfToken;
},
enumerable: false,
configurable: true
});
/**
* 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
*/
ODataRequestConfig.prototype.addCustomHeaders = function (headers) {
var _this = this;
Object.entries(headers).forEach(function (_a) {
var key = _a[0], value = _a[1];
// 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
*/
ODataRequestConfig.prototype.addCustomQueryParameters = function (queryParameters) {
var _this = this;
Object.entries(queryParameters).forEach(function (_a) {
var key = _a[0], value = _a[1];
_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
*/
ODataRequestConfig.prototype.addCustomRequestConfiguration = function (requestConfiguration) {
var _this = this;
Object.entries(requestConfiguration).forEach(function (_a) {
var key = _a[0], value = _a[1];
_this.customRequestConfiguration[key] = value;
});
};
ODataRequestConfig.prototype.appendPath = function () {
var _a;
var path = [];
for (var _i = 0; _i < arguments.length; _i++) {
path[_i] = arguments[_i];
}
(_a = this.appendedPaths).push.apply(_a, path);
};
ODataRequestConfig.prototype.prependDollarToQueryParameters = function (params) {
return Object.entries(params).reduce(function (newParams, _a) {
var key = _a[0], value = _a[1];
newParams["$".concat(key)] = value;
return newParams;
}, {});
};
return ODataRequestConfig;
}());
exports.ODataRequestConfig = ODataRequestConfig;
//# sourceMappingURL=odata-request-config.js.map
;