cogsbox-state 0.5.446 → 0.5.447

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-state",
3
- "version": "0.5.446",
3
+ "version": "0.5.447",
4
4
  "description": "React state management library with form controls and server sync",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
package/src/CogsState.tsx CHANGED
@@ -37,7 +37,7 @@ import { useCogsConfig } from './CogsStateClient.js';
37
37
  import { Operation } from 'fast-json-patch';
38
38
  import { useInView } from 'react-intersection-observer';
39
39
  import * as z3 from 'zod/v3';
40
- import * as z4 from 'zod/v4';
40
+ import z, * as z4 from 'zod/v4';
41
41
 
42
42
  type Prettify<T> = T extends any ? { [K in keyof T]: T[K] } : never;
43
43
 
@@ -534,33 +534,46 @@ export function addStateOptions<T extends unknown>(
534
534
  ) {
535
535
  return { initialState: initialState, formElements, validation } as T;
536
536
  }
537
- type UseCogsStateHook<
538
- T extends Record<string, any>,
539
- apiParams extends Record<string, any> = never,
540
- > = <StateKey extends keyof TransformedStateType<T>>(
537
+ type BasicUseCogsStateHook<T extends Record<string, any>> = <
538
+ StateKey extends keyof TransformedStateType<T>,
539
+ >(
541
540
  stateKey: StateKey,
542
- options?: Prettify<
543
- OptionsType<TransformedStateType<T>[StateKey]> & { apiParams?: apiParams }
544
- >
541
+ // This hook DOES NOT know about apiParams.
542
+ options?: Prettify<OptionsType<TransformedStateType<T>[StateKey]>>
545
543
  ) => StateObject<TransformedStateType<T>[StateKey]>;
546
-
547
- // Define the type for the options setter using the Transformed state
548
544
  type SetCogsOptionsFunc<T extends Record<string, any>> = <
549
545
  StateKey extends keyof TransformedStateType<T>,
550
546
  >(
551
547
  stateKey: StateKey,
552
548
  options: OptionsType<TransformedStateType<T>[StateKey]>
553
549
  ) => void;
554
-
555
- // Define the final API object shape
556
- type CogsApi<
557
- T extends Record<string, any>,
558
- apiParams extends Record<string, any> = never,
559
- > = {
560
- useCogsState: UseCogsStateHook<T, apiParams>;
550
+ type BasicCogsApi<T extends Record<string, any>> = {
551
+ useCogsState: BasicUseCogsStateHook<T>;
561
552
  setCogsOptions: SetCogsOptionsFunc<T>;
562
553
  };
563
554
 
555
+ export type CogsApi<TSchema extends { schemas: Record<string, any> }> = {
556
+ useCogsState: <TStateKey extends keyof TSchema['schemas']>(
557
+ stateKey: TStateKey,
558
+ options?: OptionsType<
559
+ TSchema['schemas'][TStateKey]['schemas']['defaultValues'],
560
+ TSchema['schemas'][TStateKey] extends {
561
+ // This checks for the params schema we create in `createSyncSchema`
562
+ apiParamsSchema: z.ZodObject<any, any>;
563
+ }
564
+ ? z.infer<TSchema['schemas'][TStateKey]['apiParamsSchema']>
565
+ : never
566
+ >
567
+ ) => StateObject<TSchema['schemas'][TStateKey]['schemas']['defaultValues']>;
568
+
569
+ // You also need the type for the other function
570
+ setCogsOptions: <TStateKey extends keyof TSchema['schemas']>(
571
+ stateKey: TStateKey,
572
+ options: OptionsType<
573
+ TSchema['schemas'][TStateKey]['schemas']['defaultValues']
574
+ >
575
+ ) => void;
576
+ };
564
577
  // Minimal change - just add a second parameter to detect sync schema
565
578
  export const createCogsState = <State extends Record<StateKeys, unknown>>(
566
579
  initialState: State,
@@ -681,31 +694,11 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
681
694
  notifyComponents(stateKey as string);
682
695
  }
683
696
 
684
- return { useCogsState, setCogsOptions } as CogsApi<State>;
697
+ return { useCogsState, setCogsOptions } as BasicCogsApi<State>;
685
698
  };
686
699
  export function createCogsStateFromSync<
687
- TSyncSchema extends {
688
- schemas: Record<
689
- string,
690
- {
691
- schemas: { defaultValues: any };
692
- [key: string]: any;
693
- }
694
- >;
695
- notifications: Record<string, any>;
696
- },
697
- >(
698
- syncSchema: TSyncSchema
699
- ): CogsApi<
700
- {
701
- [K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['schemas']['defaultValues'];
702
- },
703
- {
704
- [K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['apiParams'];
705
- }[keyof {
706
- [K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['apiParams'];
707
- }]
708
- > {
700
+ TSchema extends { schemas: Record<string, any> }, // Use a simpler generic
701
+ >(syncSchema: TSchema): CogsApi<TSchema> {
709
702
  const schemas = syncSchema.schemas;
710
703
  const initialState: any = {};
711
704
 
@@ -715,7 +708,7 @@ export function createCogsStateFromSync<
715
708
  initialState[key] = entry?.schemas?.defaultValues || {};
716
709
  }
717
710
 
718
- return createCogsState(initialState);
711
+ return createCogsState(initialState) as any;
719
712
  }
720
713
 
721
714
  const {