@jay-js/system
Version:
A powerful and flexible TypeScript library for UI, state management, lazy loading, routing and managing draggable elements in modern web applications.
44 lines • 1.62 kB
JavaScript
import { getPotentialMatch } from "../core/matching/get-potential-match";
import { createMatcher } from "./helpers";
/**
* Retrieves all URL parameters from the current route
*
* This function extracts both route parameters (defined with :paramName in routes)
* and query string parameters from the current URL.
*
* @returns {Record<string, string | string[]>} An object containing all URL parameters with parameter names as keys
*
* @example
* // For a URL like '/users/123?filter=active'
* // With a route defined as '/users/:id'
* const params = getParams();
* // Result: { id: '123', filter: 'active' }
*/
export function getParams() {
var _a;
const params = {};
const pathName = window.location.pathname;
const match = getPotentialMatch(pathName);
if ((_a = match.route) === null || _a === void 0 ? void 0 : _a.path) {
const matcher = createMatcher(match.route.path);
const matchResult = matcher(window.location.pathname);
if (!matchResult) {
return params;
}
if (matchResult.params) {
// Process params to ensure they are all string or string[]
Object.entries(matchResult.params).forEach(([key, value]) => {
if (value !== undefined && value !== null) {
params[key] = value;
}
});
}
}
// Also include query parameters
const searchParams = new URLSearchParams(window.location.search);
for (const [key, value] of searchParams) {
params[key] = value;
}
return params;
}
//# sourceMappingURL=get-params.js.map