@sap/odata-v4
Version:
OData V4.0 server library
170 lines (153 loc) • 4.54 kB
JavaScript
'use strict';
/**
* The UriInfo object is the result object of the uri parsing. It holds also relevant
* information about the edm entities found/parsed and the types and kinds or resources.
* @hideconstructor
*/
class UriInfo {
/**
* Creates in instance of UriInfo
*/
constructor() {
this._pathSegments = [];
this._queryOptions = {};
this._aliases = {};
this._finalEdmType = undefined;
}
/**
* Set the current query options object. Expected format is the output like node.js module
* querystring produces.
*
* @param {Object} queryOptions The query options to set.
* @returns {UriInfo} this instance of UriInfo
* @package
*/
setQueryOptions(queryOptions) {
this._queryOptions = queryOptions;
return this;
}
/**
* Set a query option with its value.
* @param {string} name name of the query option
* @param {*} value value of the query option
* @returns {UriInfo} this instance of UriInfo
* @package
*/
setQueryOption(name, value) {
this._queryOptions[name] = value;
return this;
}
/**
* Returns the corresponding query option found by its name. For odata query options
* the value is already parsed and interpreted.
*
* @param {string} optionName Name of the query option.
* @returns {*} the corresponding value for the query option.
*/
getQueryOption(optionName) {
if (!this._queryOptions) {
return null;
}
return this._queryOptions[optionName];
}
/**
* Returns available query options.
*
* @returns {*} Returns all available query options.
*/
getQueryOptions() {
return this._queryOptions;
}
/**
* Set an alias with its value.
* @param {string} name The alias name to set.
* @param {string} value The alias value to set.
* @returns {UriInfo} this instance of UriInfo
* @package
*/
setAlias(name, value) {
this._aliases[name] = value;
return this;
}
/**
* Return available aliases.
* @returns {*} all available aliases
*/
getAliases() {
return this._aliases;
}
/**
* Set the current parsed uri segments.
*
* @param {UriResource[]} segments the segments to set
* @returns {UriInfo} this instance of UriInfo
* @package
*/
setPathSegments(segments) {
this._pathSegments = segments;
return this;
}
/**
* Return the current parsed uri segments.
*
* @returns {UriResource[]} The current uri segments
*/
getPathSegments() {
return this._pathSegments;
}
/**
* Return the last (or a previous, if the parameter offset is provided) path resource segment.
* Example: getLastSegment(-1) returns the segment before the last segment.
* @param {number} offset a negative integer that describes how far to go back from the end
* @returns {?UriResource} The selected resource segment
*/
getLastSegment(offset = 0) {
return this._pathSegments[this._pathSegments.length - 1 + offset];
}
/**
* Return the final EDM type of the resource path or of the $apply option if present.
*
* @returns {?EdmType} The final EDM type or null if it is unknown
*/
getFinalEdmType() {
if (this._finalEdmType === undefined) {
this._finalEdmType = this.getLastSegment().getEdmType()
|| (this._pathSegments.length > 1 ? this.getLastSegment(-1).getEdmType() : null);
}
return this._finalEdmType;
}
/**
* Set the final EDM type in case it is different from the final EDM type of the resource path.
* @param {EdmType} edmType the final EDM type
* @returns {UriInfo} this instance of UriInfo
* @package
*/
setFinalEdmType(edmType) {
this._finalEdmType = edmType;
return this;
}
}
/**
* Query parameter
* System query parameters defined by the OData specification start with an $
*
* @enum {string}
* @readonly
*/
UriInfo.QueryOptions = {
ODATA_DEBUG: 'odata-debug',
FORMAT: '$format',
ID: '$id',
APPLY: '$apply',
SEARCH: '$search',
FILTER: '$filter',
COUNT: '$count',
ORDERBY: '$orderby',
EXPAND: '$expand',
SELECT: '$select',
SKIP: '$skip',
TOP: '$top',
SKIPTOKEN: '$skiptoken',
DELTATOKEN: '$deltatoken'
};
module.exports = UriInfo;