cogsbox-state 0.5.447 → 0.5.448

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.447",
3
+ "version": "0.5.448",
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
@@ -534,46 +534,33 @@ export function addStateOptions<T extends unknown>(
534
534
  ) {
535
535
  return { initialState: initialState, formElements, validation } as T;
536
536
  }
537
- type BasicUseCogsStateHook<T extends Record<string, any>> = <
538
- StateKey extends keyof TransformedStateType<T>,
539
- >(
537
+ type UseCogsStateHook<
538
+ T extends Record<string, any>,
539
+ apiParams extends Record<string, any> = never,
540
+ > = <StateKey extends keyof TransformedStateType<T>>(
540
541
  stateKey: StateKey,
541
- // This hook DOES NOT know about apiParams.
542
- options?: Prettify<OptionsType<TransformedStateType<T>[StateKey]>>
542
+ options?: Prettify<
543
+ OptionsType<TransformedStateType<T>[StateKey]> & { apiParams?: apiParams }
544
+ >
543
545
  ) => StateObject<TransformedStateType<T>[StateKey]>;
546
+
547
+ // Define the type for the options setter using the Transformed state
544
548
  type SetCogsOptionsFunc<T extends Record<string, any>> = <
545
549
  StateKey extends keyof TransformedStateType<T>,
546
550
  >(
547
551
  stateKey: StateKey,
548
552
  options: OptionsType<TransformedStateType<T>[StateKey]>
549
553
  ) => void;
550
- type BasicCogsApi<T extends Record<string, any>> = {
551
- useCogsState: BasicUseCogsStateHook<T>;
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>;
552
561
  setCogsOptions: SetCogsOptionsFunc<T>;
553
562
  };
554
563
 
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
- };
577
564
  // Minimal change - just add a second parameter to detect sync schema
578
565
  export const createCogsState = <State extends Record<StateKeys, unknown>>(
579
566
  initialState: State,
@@ -694,11 +681,34 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
694
681
  notifyComponents(stateKey as string);
695
682
  }
696
683
 
697
- return { useCogsState, setCogsOptions } as BasicCogsApi<State>;
684
+ return { useCogsState, setCogsOptions } as CogsApi<State>;
698
685
  };
699
686
  export function createCogsStateFromSync<
700
- TSchema extends { schemas: Record<string, any> }, // Use a simpler generic
701
- >(syncSchema: TSchema): CogsApi<TSchema> {
687
+ TSyncSchema extends {
688
+ schemas: Record<
689
+ string,
690
+ {
691
+ schemas: { defaultValues: any };
692
+ apiParamsSchema?: z.ZodObject<any>; // Add this type
693
+ [key: string]: any;
694
+ }
695
+ >;
696
+ notifications: Record<string, any>;
697
+ },
698
+ >(
699
+ syncSchema: TSyncSchema
700
+ ): CogsApi<
701
+ {
702
+ [K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['schemas']['defaultValues'];
703
+ },
704
+ {
705
+ [K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['apiParamsSchema'] extends z.ZodObject<
706
+ infer U
707
+ >
708
+ ? { [P in keyof U]: z.infer<U[P]> }
709
+ : never;
710
+ }[keyof TSyncSchema['schemas']]
711
+ > {
702
712
  const schemas = syncSchema.schemas;
703
713
  const initialState: any = {};
704
714
 
@@ -708,7 +718,11 @@ export function createCogsStateFromSync<
708
718
  initialState[key] = entry?.schemas?.defaultValues || {};
709
719
  }
710
720
 
711
- return createCogsState(initialState) as any;
721
+ // Create the cogs state with proper API params type inference
722
+ return createCogsState(initialState, {
723
+ __fromSyncSchema: true,
724
+ __syncNotifications: syncSchema.notifications,
725
+ });
712
726
  }
713
727
 
714
728
  const {