Params & Queries APIs

These functions allow you to read and write parameters or history states programmatically.


getPageParams

Retrieves an object containing all parameters parsed from the current URL pathname.

typescript
1function getPageParams<T extends Record<string, string>>(): T

Example

javascript
1// Browser location: /projects/marketing/dashboard2// Route pattern: /projects/:category/:tab3 4import { getPageParams } from '@beforesemicolon/router'5 6const params = getPageParams()7// Returns: { category: 'marketing', tab: 'dashboard' }

getSearchParams

Retrieves parsed URL query parameters as a key-value object. Supports automatic JSON parsing for complex query objects.

typescript
1function getSearchParams<T extends Record<string, string>>(): T

Example

javascript
1// Browser location: /search?query=hello&tags=%5B%22js%22%2C%22web%22%5D2 3import { getSearchParams } from '@beforesemicolon/router'4 5const query = getSearchParams()6// Returns: { query: 'hello', tags: ['js', 'web'] }

updateSearchQuery

Updates or clears URL search parameters. Updates are made in-place using history replacement (does not add a new entry to the browser back stack).

typescript
1function updateSearchQuery(searchObject: Record<string, unknown> | null): void

Example

javascript
1import { updateSearchQuery } from '@beforesemicolon/router'2 3// Add page parameters to current URL4updateSearchQuery({ page: 2, filter: 'active' })5// Browser updates to: ?page=2&filter=active6 7// Clear queries8updateSearchQuery(null)9// Browser updates to: (query parameters removed)

getPageData

Retrieves the current history state payload object.

typescript
1function getPageData<T extends Record<string, unknown>>(): T

Example

javascript
1import { getPageData } from '@beforesemicolon/router'2 3const data = getPageData()4console.log('User Role:', data.role)
edit this doc