cogsbox-state 0.5.456 → 0.5.458

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.456",
3
+ "version": "0.5.458",
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
@@ -426,7 +426,7 @@ export type OptionsType<T extends unknown = unknown, TApiParams = never> = {
426
426
  syncUpdate?: Partial<UpdateTypeDetail>;
427
427
 
428
428
  defaultState?: T;
429
- apiParams?: TApiParams;
429
+
430
430
  dependencies?: any[];
431
431
  };
432
432
 
@@ -535,15 +535,6 @@ export function addStateOptions<T extends unknown>(
535
535
  ) {
536
536
  return { initialState: initialState, formElements, validation } as T;
537
537
  }
538
- type UseCogsStateHook<
539
- T extends Record<string, any>,
540
- apiParams extends Record<string, any> = never,
541
- > = <StateKey extends keyof TransformedStateType<T>>(
542
- stateKey: StateKey,
543
- options?: Prettify<
544
- OptionsType<TransformedStateType<T>[StateKey]> & { apiParams?: apiParams }
545
- >
546
- ) => StateObject<TransformedStateType<T>[StateKey]>;
547
538
 
548
539
  // Define the type for the options setter using the Transformed state
549
540
  type SetCogsOptionsFunc<T extends Record<string, any>> = <
@@ -553,14 +544,6 @@ type SetCogsOptionsFunc<T extends Record<string, any>> = <
553
544
  options: OptionsType<TransformedStateType<T>[StateKey]>
554
545
  ) => void;
555
546
 
556
- // Define the final API object shape
557
- type CogsApi<
558
- T extends Record<string, any>,
559
- apiParams extends Record<string, any> = never,
560
- > = {
561
- useCogsState: UseCogsStateHook<T, apiParams>;
562
- setCogsOptions: SetCogsOptionsFunc<T>;
563
- };
564
547
  export const createCogsState = <State extends Record<StateKeys, unknown>>(
565
548
  initialState: State,
566
549
  opt?: {
@@ -694,14 +677,40 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
694
677
 
695
678
  return { useCogsState, setCogsOptions } as CogsApi<State>;
696
679
  };
680
+ // Fix for UseCogsStateHook to support per-key apiParams
681
+ type UseCogsStateHook<
682
+ T extends Record<string, any>,
683
+ TApiParamsMap extends Record<string, any> = Record<string, never>,
684
+ > = <StateKey extends keyof TransformedStateType<T>>(
685
+ stateKey: StateKey,
686
+ options?: Prettify<
687
+ OptionsType<TransformedStateType<T>[StateKey]> & {
688
+ apiParams?: StateKey extends keyof TApiParamsMap
689
+ ? TApiParamsMap[StateKey]
690
+ : never;
691
+ }
692
+ >
693
+ ) => StateObject<TransformedStateType<T>[StateKey]>;
697
694
 
695
+ // Updated CogsApi type
696
+ type CogsApi<
697
+ T extends Record<string, any>,
698
+ TApiParamsMap extends Record<string, any> = Record<string, never>,
699
+ > = {
700
+ useCogsState: UseCogsStateHook<T, TApiParamsMap>;
701
+ setCogsOptions: SetCogsOptionsFunc<T>;
702
+ };
703
+
704
+ // Fixed createCogsStateFromSync return type
698
705
  export function createCogsStateFromSync<
699
706
  TSyncSchema extends {
700
707
  schemas: Record<
701
708
  string,
702
709
  {
703
710
  schemas: { defaultValues: any };
704
- apiParamsSchema?: any; // This contains the zod schema for params
711
+ api?: {
712
+ queryData?: any;
713
+ };
705
714
  [key: string]: any;
706
715
  }
707
716
  >;
@@ -714,10 +723,14 @@ export function createCogsStateFromSync<
714
723
  [K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['schemas']['defaultValues'];
715
724
  },
716
725
  {
717
- [K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['apiParamsSchema'] extends z.ZodObject<any>
718
- ? z.infer<TSyncSchema['schemas'][K]['apiParamsSchema']>
726
+ [K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['api'] extends {
727
+ queryData: infer Q;
728
+ }
729
+ ? Q extends { _paramType: infer P }
730
+ ? P
731
+ : never
719
732
  : never;
720
- }[keyof TSyncSchema['schemas']]
733
+ }
721
734
  > {
722
735
  const schemas = syncSchema.schemas;
723
736
  const initialState: any = {};
@@ -728,9 +741,9 @@ export function createCogsStateFromSync<
728
741
  const entry = schemas[key];
729
742
  initialState[key] = entry?.schemas?.defaultValues || {};
730
743
 
731
- // Store the apiParamsSchema for each key
732
- if (entry?.apiParamsSchema) {
733
- apiParamsMap[key] = entry.apiParamsSchema;
744
+ // Extract apiParams from the api.queryData._paramType
745
+ if (entry?.api?.queryData?._paramType) {
746
+ apiParamsMap[key] = entry.api.queryData._paramType;
734
747
  }
735
748
  }
736
749
 
@@ -738,10 +751,9 @@ export function createCogsStateFromSync<
738
751
  return createCogsState(initialState, {
739
752
  __fromSyncSchema: true,
740
753
  __syncNotifications: syncSchema.notifications,
741
- __apiParamsMap: apiParamsMap, // Pass the apiParams schemas
754
+ __apiParamsMap: apiParamsMap,
742
755
  }) as any;
743
756
  }
744
-
745
757
  const {
746
758
  getInitialOptions,
747
759
  getValidationErrors,