bc-node-sdk
Version:
BetterCommerce's NodeJS SDK encapsulates the base framework for all the Next.js applications.
94 lines (93 loc) • 3.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("../domain/constants");
/**
* Class {@link UriUtil} encapsulates the utility methods related to URIs.
*/
class UriUtil {
/**
* Sanitizes the given URI by extracting the query parameters and re-assembling
* the URI with the query parameters sorted alphabetically by key.
* @param {string} uri - The URI to be sanitized
* @returns {string} - The sanitized URI
*/
static sanitizeUri(uri) {
const params = UriUtil.uriParams(uri);
if (params) {
var qs;
for (let key in params) {
if (!qs) {
qs = `?${key}=${params[key]}`;
}
else {
qs = qs + `&${key}=${params[key]}`;
}
}
return `${uri.substring(0, uri.indexOf("?"))}${qs}`;
}
return uri;
}
/**
* Generates a URI with the given query string appended to the end.
* If the given URI does not already contain a query string, a new query string is added.
* Otherwise, the given query string is appended to the existing query string.
* @param {string} uri - The base URI
* @param {string} qs - The query string to append
* @returns {string} - The URI with the query string appended
*/
static generateUri(uri, qs) {
if (uri) {
return uri.indexOf("?") == -1 ? UriUtil.sanitizeUri(`${uri}?${qs}`) : UriUtil.sanitizeUri(`${uri}&${qs}`);
}
return null;
}
/**
* Given a URI, returns the query parameters as a JSON object, or null if no query parameters are present
* @param {string} uri - The URI with query parameters
* @returns {Object} - The query parameters as a JSON object, or null if no query parameters are present
*/
static uriParams(uri) {
if (uri) {
//let url = decodeURIComponent(uri)
const url = uri.substring(uri.indexOf('?') + 1);
const params = url
.replace('?', constants_1.Defaults.String.Value)
.split('&')
.map((param) => param.split('='))
.reduce((values, [key, value]) => {
values[key] = decodeURIComponent(value);
return values;
}, {});
return params;
}
return null;
}
/**
* Removes the query string from the given URI path.
*
* @param {any} path - The URI path from which the query string should be removed.
* @returns {string} - The path without the query string, or the original path if no query string is present.
*/
static removeQueryString(path) {
const queryStringIndex = path.indexOf('?');
if (queryStringIndex !== -1) {
return path.slice(0, queryStringIndex);
}
return path;
}
/**
* Returns true if the given URI has a base URL, false otherwise.
*
* A URI is considered to have a base URL if it starts with 'http://' or 'https://'.
*
* @param {string} uri - The URI to check for a base URL.
* @returns {boolean} - True if the URI has a base URL, false otherwise.
*/
static hasBaseUrl(uri) {
if (!uri) {
return false;
}
return ((uri === null || uri === void 0 ? void 0 : uri.startsWith('http://')) || (uri === null || uri === void 0 ? void 0 : uri.startsWith('https://')));
}
}
exports.default = UriUtil;