cogsbox-state 0.5.451 → 0.5.453

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.451",
3
+ "version": "0.5.453",
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
@@ -38,6 +38,7 @@ import { Operation } from 'fast-json-patch';
38
38
  import { useInView } from 'react-intersection-observer';
39
39
  import * as z3 from 'zod/v3';
40
40
  import z, * as z4 from 'zod/v4';
41
+ import { keyof } from 'zod';
41
42
 
42
43
  type Prettify<T> = T extends any ? { [K in keyof T]: T[K] } : never;
43
44
 
@@ -683,48 +684,7 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
683
684
 
684
685
  return { useCogsState, setCogsOptions } as CogsApi<State>;
685
686
  };
686
-
687
- // First, let's create a proper type to extract API params from the sync schema
688
- type ExtractApiParamsFromSchema<T> = T extends {
689
- apiParamsSchema: z.ZodObject<infer U>;
690
- }
691
- ? { [K in keyof U]: z.infer<U[K]> }
692
- : never;
693
-
694
- // Type to check if a schema has API params
695
- type HasApiParams<T> = T extends { apiParamsSchema: z.ZodObject<any> }
696
- ? true
697
- : false;
698
-
699
- // Create the proper return type for useCogsState
700
- type UseCogsStateFromSync<
701
- TSyncSchema extends {
702
- schemas: Record<
703
- string,
704
- { schemas: { defaultValues: any }; apiParamsSchema?: z.ZodObject<any> }
705
- >;
706
- },
707
- > = {
708
- // For schemas WITH API params - require apiParams
709
- <K extends keyof TSyncSchema['schemas']>(
710
- stateKey: K,
711
- options: HasApiParams<TSyncSchema['schemas'][K]> extends true
712
- ? OptionsType<TSyncSchema['schemas'][K]['schemas']['defaultValues']> & {
713
- apiParams: ExtractApiParamsFromSchema<TSyncSchema['schemas'][K]>;
714
- }
715
- : never
716
- ): StateObject<TSyncSchema['schemas'][K]['schemas']['defaultValues']>;
717
-
718
- // For schemas WITHOUT API params - apiParams is optional
719
- <K extends keyof TSyncSchema['schemas']>(
720
- stateKey: K,
721
- options?: HasApiParams<TSyncSchema['schemas'][K]> extends true
722
- ? never
723
- : OptionsType<TSyncSchema['schemas'][K]['schemas']['defaultValues']>
724
- ): StateObject<TSyncSchema['schemas'][K]['schemas']['defaultValues']>;
725
- };
726
- // Simplified approach that actually works
727
- // Fixed version that properly handles the apiParams type
687
+ // Fixed version that properly handles the apiParams type without complex inference
728
688
  export function createCogsStateFromSync<
729
689
  TSyncSchema extends {
730
690
  schemas: Record<
@@ -737,7 +697,20 @@ export function createCogsStateFromSync<
737
697
  >;
738
698
  notifications: Record<string, any>;
739
699
  },
740
- >(syncSchema: TSyncSchema) {
700
+ >(
701
+ syncSchema: TSyncSchema
702
+ ): {
703
+ useCogsState: <K extends keyof TSyncSchema['schemas']>(
704
+ stateKey: K,
705
+ options?: OptionsType<
706
+ TSyncSchema['schemas'][K]['schemas']['defaultValues'],
707
+ any
708
+ > & {
709
+ apiParams?: any;
710
+ }
711
+ ) => StateObject<TSyncSchema['schemas'][K]['schemas']['defaultValues']>;
712
+ setCogsOptions: any;
713
+ } {
741
714
  const schemas = syncSchema.schemas;
742
715
  const initialState: any = {};
743
716
 
@@ -753,19 +726,19 @@ export function createCogsStateFromSync<
753
726
  __syncNotifications: syncSchema.notifications,
754
727
  });
755
728
 
729
+ // Create the properly typed useCogsState function
756
730
  const useCogsState = <K extends keyof TSyncSchema['schemas']>(
757
731
  stateKey: K,
758
- options?: Omit<
759
- OptionsType<TSyncSchema['schemas'][K]['schemas']['defaultValues']>,
760
- 'apiParams'
732
+ options?: OptionsType<
733
+ TSyncSchema['schemas'][K]['schemas']['defaultValues'],
734
+ any
761
735
  > & {
762
- apiParams?: any; // Allow any apiParams, validate at runtime
736
+ apiParams?: any;
763
737
  }
764
738
  ): StateObject<TSyncSchema['schemas'][K]['schemas']['defaultValues']> => {
765
739
  // Runtime validation of API params
766
740
  const schemaEntry = schemas[stateKey as keyof typeof schemas];
767
741
  if (schemaEntry?.apiParamsSchema && options?.apiParams) {
768
- // Validate the API params at runtime
769
742
  const result = schemaEntry.apiParamsSchema.safeParse(options.apiParams);
770
743
  if (!result.success) {
771
744
  throw new Error(
@@ -774,14 +747,9 @@ export function createCogsStateFromSync<
774
747
  }
775
748
  }
776
749
 
777
- // Create options without apiParams for the base call
778
- const { apiParams, ...baseOptions } = options || {};
779
-
780
- // Cast to the correct type since we know it's properly typed
781
- return baseApi.useCogsState(
782
- stateKey as any,
783
- baseOptions as any
784
- ) as StateObject<TSyncSchema['schemas'][K]['schemas']['defaultValues']>;
750
+ return baseApi.useCogsState(stateKey as any, options as any) as StateObject<
751
+ TSyncSchema['schemas'][K]['schemas']['defaultValues']
752
+ >;
785
753
  };
786
754
 
787
755
  return {