@jimjam.dev/url-state
Version:
A url state manager for nextjs
73 lines • 2.58 kB
TypeScript
import { type SearchParams } from './serializer';
import { ReadonlyURLSearchParams } from 'next/navigation';
export interface QueryBuilderConfig {
/** Default values for parameters */
defaults?: Record<string, any>;
/** Properties to ignore during processing */
ignored?: string[];
/** Custom property mappings */
mappings?: Record<string, (value: any) => any>;
/** Post-processing function to transform the final result */
postProcess?: (result: Record<string, any>) => Record<string, any>;
}
/**
* Configurable QueryBuilder class for processing URL state
*/
export declare class QueryBuilder<T extends Record<string, any> = Record<string, any>> {
private config;
constructor(config?: QueryBuilderConfig);
/**
* Add default values (rebuilds ignoredSet for consistency)
*/
setDefaults(defaults: Record<string, any>): this;
/**
* Add properties to ignore (rebuilds ignoredSet)
*/
ignore(...properties: string[]): this;
/**
* Add custom property mappings
*/
addMapping(property: string, mapper: (value: any) => any): this;
/**
* Build query from searchParams with optional unique key filtering
*/
fromUrl(searchParams: ReadonlyURLSearchParams | SearchParams, uniqueKey?: string): T;
/**
* Build query from already-filtered parameters
*/
build(params: Record<string, any>): T;
}
/**
* Create a configured query builder function
*
* @example
* ```ts
* // In your lib/utils/qb.ts file:
* import { createQueryBuilder } from '@jimjam.dev/url-state';
*
* export const qb = createQueryBuilder({
* defaults: { page: 1, pageSize: 10 },
* ignored: ['debug'],
* mappings: {
* orderBy: (value) => value,
* orderDir: (value) => value || '+'
* },
* postProcess: (result) => {
* if (result.orderBy) {
* result.sort = `${result.orderDir}${result.orderBy}`;
* }
* return result;
* }
* });
*
* // Use it:
* const query = qb(searchParams, 'users_');
* ```
*/
export declare function createQueryBuilder<T extends Record<string, any> = Record<string, any>>(config?: QueryBuilderConfig): (searchParams: ReadonlyURLSearchParams | SearchParams, uniqueKey?: string) => T;
/**
* Direct queryBuilder function (unconfigured)
* Takes searchParams and uniqueKey, returns filtered and processed query
*/
export declare function queryBuilder<T extends Record<string, any> = Record<string, any>>(searchParams: ReadonlyURLSearchParams | SearchParams, uniqueKey?: string): T;
//# sourceMappingURL=query-builder.d.ts.map