cogsbox-state 0.5.448 → 0.5.450

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.448",
3
+ "version": "0.5.450",
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
@@ -683,32 +683,60 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
683
683
 
684
684
  return { useCogsState, setCogsOptions } as CogsApi<State>;
685
685
  };
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
686
727
  export function createCogsStateFromSync<
687
728
  TSyncSchema extends {
688
729
  schemas: Record<
689
730
  string,
690
731
  {
691
732
  schemas: { defaultValues: any };
692
- apiParamsSchema?: z.ZodObject<any>; // Add this type
733
+ apiParamsSchema?: z.ZodObject<any>;
693
734
  [key: string]: any;
694
735
  }
695
736
  >;
696
737
  notifications: Record<string, any>;
697
738
  },
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
- > {
739
+ >(syncSchema: TSyncSchema) {
712
740
  const schemas = syncSchema.schemas;
713
741
  const initialState: any = {};
714
742
 
@@ -718,13 +746,44 @@ export function createCogsStateFromSync<
718
746
  initialState[key] = entry?.schemas?.defaultValues || {};
719
747
  }
720
748
 
721
- // Create the cogs state with proper API params type inference
722
- return createCogsState(initialState, {
749
+ // Create the base CogsApi
750
+ const baseApi = createCogsState(initialState, {
723
751
  __fromSyncSchema: true,
724
752
  __syncNotifications: syncSchema.notifications,
725
753
  });
726
- }
727
754
 
755
+ // Create a wrapper that validates API params at runtime
756
+ const useCogsState = <K extends keyof TSyncSchema['schemas']>(
757
+ stateKey: K,
758
+ options?: OptionsType<
759
+ TSyncSchema['schemas'][K]['schemas']['defaultValues']
760
+ > & {
761
+ apiParams?: any; // We'll validate this at runtime
762
+ }
763
+ ): StateObject<TSyncSchema['schemas'][K]['schemas']['defaultValues']> => {
764
+ // Runtime validation of API params
765
+ const schemaEntry = schemas[stateKey as keyof typeof schemas];
766
+ if (schemaEntry?.apiParamsSchema && options?.apiParams) {
767
+ // Validate the API params at runtime
768
+ const result = schemaEntry.apiParamsSchema.safeParse(options.apiParams);
769
+ if (!result.success) {
770
+ throw new Error(
771
+ `Invalid API params for ${String(stateKey)}: ${result.error.message}`
772
+ );
773
+ }
774
+ }
775
+
776
+ // Cast to the correct type since we know it's properly typed
777
+ return baseApi.useCogsState(stateKey as any, options as any) as StateObject<
778
+ TSyncSchema['schemas'][K]['schemas']['defaultValues']
779
+ >;
780
+ };
781
+
782
+ return {
783
+ useCogsState,
784
+ setCogsOptions: baseApi.setCogsOptions,
785
+ };
786
+ }
728
787
  const {
729
788
  getInitialOptions,
730
789
  getValidationErrors,