/** * 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 { JSType } from './graphql-parser'; /** * Type mapping from JSType to TypeScript types for OUTPUT (reading params) */ type OutputTypeMap = { string: string; number: number; boolean: boolean; array: string[]; object: Record; }; /** * 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; }; /** * 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]; /** * Get the default value type for a given JSType */ type DefaultValueForType = T extends 'array' ? string[] : T extends 'object' ? Record : OutputTypeMap[T]; /** * Parameter configuration for a single parameter */ export interface ParamConfig { /** JavaScript type for URL parameter */ type: T; /** Default value matching the type */ default?: DefaultValueForType; /** Whether parameter is required */ required?: boolean; } /** * REST API parameter schema definition * Maps parameter names to their configuration */ export type ParamSchema = Record; /** * Helper to create a typed param schema (preserves literal types) */ export declare function defineParamSchema(schema: T): T; /** * Options for useApiParams hook */ export interface UseApiParamsOptions { /** Enable debug logging */ debug?: boolean; } /** * 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; /** 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; /** * Helper: Create URLSearchParams from object * * Handles arrays as repeated parameters. Filters out undefined, and empty values. * * @param params - Parameters object * @returns URLSearchParams */ export declare function createSearchParams(params: Record): URLSearchParams; export {}; //# sourceMappingURL=use-api-params.d.ts.map