/** * useApiParams Hook - REST API Integration for URL State Management * * Manual schema definition for REST APIs. Provides same URL sync functionality * as useQueryParams but without GraphQL dependency. * * @example * const { params, setParam } = useApiParams({ * search: { type: 'string', default: '' }, * page: { type: 'number', default: 1 }, * tags: { type: 'array', default: [] } * }) * * fetch(`/api/items?${new URLSearchParams(params)}`) * * // URL: /items?search=laptop&page=2&tags=electronics&tags=sale * // params: { search: 'laptop', page: 2, tags: ['electronics', 'sale'] } */ import { type ParamSchema, type JSType } from '../../utils/search-params'; /** * Type mapping from JSType to TypeScript types for OUTPUT (reading params) */ type OutputTypeMap = { string: string; number: number; boolean: boolean; array: string[]; object: Record; int: number; }; /** * Type mapping from JSType to TypeScript types for INPUT (setting params) * More permissive to allow null/undefined in arrays which get filtered */ type InputTypeMap = { string: string | null | undefined; number: number | null | undefined; boolean: boolean | null | undefined; array: (string | null | undefined)[]; object: Record | null | undefined; int: number | null | undefined; }; /** * Get the TypeScript type for OUTPUT (reading from params) */ type OutputTypeForJSType = OutputTypeMap[T]; /** * Get the TypeScript type for INPUT (setting params) */ type InputTypeForJSType = InputTypeMap[T]; export type { ParamConfig, ParamSchema } from '../../utils/search-params'; export { defineParamSchema } from '../../utils/search-params'; /** * Options for useApiParams hook */ export interface UseApiParamsOptions { /** Enable debug logging */ debug?: boolean; /** * What an ABSENT scalar with no declared `default` reads as. Passed straight * through to `parseSchemaParams`, so the hook's `params` ARE the same parsed * contract the server produces for the same URL. */ absent?: 'undefined' | 'null'; } /** * Infer the OUTPUT params type from a ParamSchema (for reading) * Maps each key in the schema to its corresponding TypeScript type * * @example * const schema = defineParamSchema({ * search: { type: 'string', default: '' }, * page: { type: 'number', default: 1 }, * tags: { type: 'array', default: [] } * }) * type Params = InferParamsFromSchema * // { search: string; page: number; tags: string[] } */ export type InferParamsFromSchema = { [K in keyof TSchema]: TSchema[K]['type'] extends infer T ? T extends JSType ? OutputTypeForJSType : never : never; }; /** * Infer the INPUT params type from a ParamSchema (for setting) * More permissive to allow null/undefined values */ export type InferInputParamsFromSchema = { [K in keyof TSchema]: TSchema[K]['type'] extends infer T ? (T extends JSType ? InputTypeForJSType : never) : never; }; /** * Type for parameter values that can be set * Allows setting values that match the schema types or can be coerced to them */ export type ParamValue = string | number | boolean | string[] | (string | null | undefined)[] | Record | null | undefined; /** * Return type for useApiParams hook with strict typing */ export interface UseApiParamsReturn> { /** Parsed parameters object with strict typing */ params: TParams; /** * `params` of the last write this hook issued, or `params` itself when no * write is in flight. The URL is authoritative; this is the latest INTENT. */ pendingParams: TParams; /** URLSearchParams for fetch/axios */ urlSearchParams: URLSearchParams; /** Set a single parameter with type-safe key and value */ setParam: (key: K, value: InferInputParamsFromSchema>[K]) => void; /** Set multiple parameters at once */ setParams: (updates: Partial>) => void; /** Clear specific parameters */ clearParams: (keys: (keyof TSchema & string)[]) => void; /** Reset all parameters (clear URL) */ resetParams: () => void; } /** * useApiParams - Manual URL state for REST APIs * * This hook: * 1. Reads URL search parameters * 2. Coerces to correct types based on schema * 3. Provides type-safe parameter updates * 4. Syncs changes to URL automatically * * @param schema - Parameter schema definition * @param options - Configuration options * @returns Hook API for managing URL state */ export declare function useApiParams(schema: TSchema, options?: UseApiParamsOptions): UseApiParamsReturn; export { createSearchParams } from '../../utils/search-params'; //# sourceMappingURL=use-api-params.d.ts.map