universal-common
Version:
Library that provides useful missing base class library functionality.
110 lines (109 loc) • 3.79 kB
TypeScript
/**
* A builder class for constructing relative URIs.
*
* Relative URIs can be one of three types:
* 1. Scheme-relative (starts with "//") - inherits protocol from current page
* 2. Root-relative (starts with "/") - relative to domain root
* 3. Current-path-relative (no prefix) - relative to current path
*
* This class follows the builder pattern to allow fluent method chaining.
*/
export default class RelativeUriBuilder {
/**
* Type constant for scheme-relative URIs (e.g., "//example.com/path")
* These URIs inherit the protocol (http/https) from the current page.
* @type {string}
*/
static get TYPE_SCHEME(): string;
/**
* Type constant for root-relative URIs (e.g., "/path/to/resource")
* These URIs are relative to the domain root.
* @type {string}
*/
static get TYPE_ROOT(): string;
/**
* Type constant for current-path-relative URIs (e.g., "resource" or "subdir/resource")
* These URIs are relative to the current path.
* @type {string}
*/
static get TYPE_CURRENT(): string;
/**
* Creates a new RelativeUriBuilder instance.
*
* @param {string} type - The type of relative URI to build.
* Use the static TYPE_* constants for valid values.
* @example
* // Create a root-relative URI builder
* const builder = new RelativeUriBuilder(RelativeUriBuilder.TYPE_ROOT);
*/
constructor(type: string);
/**
* Gets the path part of the URI.
*
* @returns {string} The path as a string with trailing slash
*/
get path(): string;
/**
* Gets the query string part of the URI.
*
* @returns {string} The query string without the leading '?'
*/
get query(): string;
/**
* Gets a copy of the path segments array.
*
* @returns {Array<string>} Array of path segments
*/
get segments(): string[];
/**
* Sets the type of relative URI being built.
*
* @param {string} value - The URI type (should be one of the TYPE_* constants)
*/
set type(arg: string);
/**
* Gets the type of relative URI being built.
*
* @returns {string} The URI type (one of the TYPE_* constants)
*/
get type(): string;
/**
* Adds a single path segment.
*
* @param {string} value - The path segment to add
* @returns {RelativeUriBuilder} This builder instance for method chaining
*/
addSegment(value: string): RelativeUriBuilder;
/**
* Adds multiple path segments.
*
* @param {...string} arguments - Path segments to add
* @returns {RelativeUriBuilder} This builder instance for method chaining
*
* DIFFERENCE FROM UriBuilder: This implementation doesn't handle array arguments,
* unlike the similar method in UriBuilder.
*/
addSegments(...args: string[]): RelativeUriBuilder;
/**
* Adds a query parameter.
*
* @param {string} name - The parameter name
* @param {string} value - The parameter value
* @returns {RelativeUriBuilder} This builder instance for method chaining
*/
addQuery(name: string, value: string): RelativeUriBuilder;
/**
* Adds multiple query parameters from an object.
*
* @param {Object|Map} queries - Object containing name-value pairs
* @returns {RelativeUriBuilder} This builder instance for method chaining
*/
addQueries(queries: any | Map<any, any>): RelativeUriBuilder;
/**
* Builds and returns the complete relative URI string.
*
* @returns {string} The complete relative URI
*/
get uri(): string;
#private;
}