url-sync
Version:
Easily bind filter and sorter state to URL query parameters.
95 lines (72 loc) β’ 2.03 kB
Markdown
# π url-sync
A lightweight utility to **synchronize filters, sorters, and search states** via **URL query parameters**.
Supports both **frontend (like React)** and **backend (like Node.js)** usage with a clean, declarative API.
## π Why url-sync?
Most frontend apps store filters and sorters in local state, which:
- β Doesnβt persist on page refresh
- β Canβt be shared or bookmarked
- β Breaks in SSR or backend filtering
`url-sync` solves this by syncing your filters and sorters with the URL, enabling:
- π State persistence
- π Sharable, bookmarkable URLs
- π» Server-side compatibility
## π¦ Installation
```bash
npm install url-sync
# or
yarn add url-sync
````
## π§ Core Functions
1. URLSync(config, locationSearch?)
Parses filters and sorters from the current URL.
````
import URLSync from "url-sync";
const { filters, sorters, sorterState, parsedQuery } = URLSync({
filters: {
status: { type: "string", queryString: "status", defaultValue: "active" },
price: { type: "number", queryString: "price" },
},
sorters: {
fieldToQueryString: { name: "sort_name" },
sortOrdersValues: {
ASCENDING: "asc",
DESCENDING: "desc",
},
defaultSortValues: { sortBy: "asc", sortOn: "name" },
},
});
# Output
{
filters: {
status: "active",
price: 500
},
sorters: [
{ sortOn: "name", sortBy: "asc" }
],
sorterState: {
name: "ascend"
},
parsedQuery: {
status: "active",
price: "500",
sort_name: "asc"
}
}
````
2. URLSyncNavigate(updatedObj, locationSearch?, locationPathname?)
Creates a new URL with updated query parameters.
````
import { URLSyncNavigate } from "url-sync";
const newUrl = URLSyncNavigate({
status: "completed",
search: "shoes",
page: null, // null removes it from the URL
});
# Output
/current-pathname?status=completed&search=shoes
````
## π· License
MIT Β© Denish Puthawala