kit-query-params
Advanced tools
Comparing version 0.0.23 to 0.0.24
import { SvelteURLSearchParams } from 'svelte/reactivity'; | ||
import type { Default, Opts, Schema, SchemaOutput } from './types.js'; | ||
export declare const stateParams: <T extends Schema, D extends Default<T> | undefined, Enforce extends boolean = false>({ schema, default: _defaultValue, enforceDefault, debounce: debounceTime, preserveUnknownParams, pushHistory, invalidateAll, twoWayBinding, invalidate: invalidations, shallow }: Opts<T, D, Enforce>) => import("./types.js").ConditionalSimplifyDeep<SchemaOutput<T, D, Enforce>, import("./types.js").NonRecursiveType | Set<unknown> | Map<unknown, unknown>, object> & { | ||
export declare const queryParamsState: <T extends Schema, D extends Default<T> | undefined, Enforce extends boolean = false>({ schema, default: _defaultValue, enforceDefault, debounce: debounceTime, preserveUnknownParams, pushHistory, invalidateAll, twoWayBinding, invalidate: invalidations, shallow }: Opts<T, D, Enforce>) => import("./types.js").ConditionalSimplifyDeep<SchemaOutput<T, D, Enforce>, import("./types.js").NonRecursiveType | Set<unknown> | Map<unknown, unknown>, object> & { | ||
$searchParams: SvelteURLSearchParams; | ||
$reset: (enforceDefault?: boolean) => void; | ||
}; |
@@ -9,3 +9,3 @@ import { SvelteURLSearchParams } from 'svelte/reactivity'; | ||
import { coerceObject } from './coerce.js'; | ||
export const stateParams = ({ schema, default: _defaultValue, enforceDefault, debounce: debounceTime = 200, preserveUnknownParams = true, pushHistory = false, invalidateAll = false, twoWayBinding = true, invalidate: invalidations = [], shallow = false }) => { | ||
export const queryParamsState = ({ schema, default: _defaultValue, enforceDefault, debounce: debounceTime = 200, preserveUnknownParams = true, pushHistory = false, invalidateAll = false, twoWayBinding = true, invalidate: invalidations = [], shallow = false }) => { | ||
const defaultValue = _defaultValue ? coerceObject(schema, _defaultValue) : undefined; | ||
@@ -12,0 +12,0 @@ const url = building ? new URL('https://github.com/beynar/kit-state-params') : get(page).url; |
{ | ||
"name": "kit-query-params", | ||
"version": "0.0.23", | ||
"version": "0.0.24", | ||
"scripts": { | ||
@@ -5,0 +5,0 @@ "dev": "vite dev", |
@@ -1,6 +0,6 @@ | ||
# kit-state-params | ||
# kit-query-params | ||
## 1. Introduction | ||
kit-state-params is a lightweight query params state management library for SvelteKit that simplifies state management and URL synchronization. It provides a seamless way to keep your application state in sync with URL search parameters, enhancing user experience and enabling easy sharing of application states. | ||
kit-query-params is a lightweight query params state management library for SvelteKit that simplifies state management and URL synchronization. It provides a seamless way to keep your application state in sync with URL search parameters, enhancing user experience and enabling easy sharing of application states. | ||
@@ -22,11 +22,11 @@ Key features: | ||
```bash | ||
npm install kit-state-params | ||
npm install kit-query-params | ||
``` | ||
```bash | ||
pnpm install kit-state-params | ||
pnpm install kit-query-params | ||
``` | ||
```bash | ||
bun install kit-state-params | ||
bun install kit-query-params | ||
``` | ||
@@ -36,3 +36,3 @@ | ||
Create a state with the searchParams function. You need to pass a schema of the parameters you want to use. The type of the state will be infered from the schema. | ||
Create a state with the queryParams function. You need to pass a schema of the parameters you want to use. The type of the state will be infered from the schema. | ||
The state is a like a normal state in svelte 5. It is reactive and you can mutate or update its properties. | ||
@@ -53,5 +53,5 @@ | ||
<script lang="ts"> | ||
import { stateParams } from 'kit-state-params'; | ||
import { queryParamsState } from 'kit-query-params'; | ||
const searchParams = stateParams({ | ||
const queryParams = queryParamsState({ | ||
schema: { | ||
@@ -66,3 +66,3 @@ search: 'string', | ||
onclick={() => { | ||
searchParams.tags.push(searchParams.tags.length + 1); | ||
queryParams.tags.push(queryParams.tags.length + 1); | ||
}} | ||
@@ -73,5 +73,5 @@ > | ||
<input bind:value={searchParams.search} /> | ||
<input bind:value={queryParams.search} /> | ||
{JSON.stringify(searchParams)} | ||
{JSON.stringify(queryParams)} | ||
``` | ||
@@ -81,3 +81,3 @@ | ||
The `stateParams` function accepts an options object. Here's a table describing the available options: | ||
The `queryParamsState` function accepts an options object. Here's a table describing the available options: | ||
@@ -158,15 +158,15 @@ | Name | Type | Description | Default Value | Required | Example | | ||
the `stateParams` function returns a proxy reflecting the defined schema that also contains: | ||
the `queryParamsState` function returns a proxy reflecting the defined schema that also contains: | ||
- a `$reset` function to clear the search params (it will also clear the unknown params in the url if you set `preserveUnknownParams` to `false`) | ||
- a `$searchParams` property that is the underlying `SvelteURLSearchParams` reactive URLSearchParams | ||
- a `$queryParams` property that is the underlying `SvelteURLqueryParams` reactive URLqueryParams | ||
## 4. Server Side | ||
kit-state-params also exports a `parseURL` function. This function can be used to parse the URL parameters into a typed object. It can be usefull inside the `load` function of a route. | ||
kit-query-params also exports a `parseURL` function. This function can be used to parse the URL parameters into a typed object. It can be usefull inside the `load` function of a route. | ||
```ts | ||
import { parseURL } from 'kit-state-params'; | ||
import { parseURL } from 'kit-query-params'; | ||
export const load = ({ url }) => { | ||
const searchParams = parseURL(url, { | ||
const queryParams = parseURL(url, { | ||
search: 'string', | ||
@@ -176,5 +176,5 @@ tags: ['number'] | ||
const result = await api.getCustomers({ | ||
search: searchParams.search, | ||
search: queryParams.search, | ||
// search is of type string | null | ||
tags: searchParams.tags | ||
tags: queryParams.tags | ||
// tags is of type number[] | ||
@@ -181,0 +181,0 @@ }); |
39386