cogsbox-state 0.5.455 → 0.5.457

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.455",
3
+ "version": "0.5.457",
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 * as z4 from 'zod/v4';
41
+ import z from 'zod';
41
42
 
42
43
  type Prettify<T> = T extends any ? { [K in keyof T]: T[K] } : never;
43
44
 
@@ -534,15 +535,6 @@ export function addStateOptions<T extends unknown>(
534
535
  ) {
535
536
  return { initialState: initialState, formElements, validation } as T;
536
537
  }
537
- type UseCogsStateHook<
538
- T extends Record<string, any>,
539
- apiParams extends Record<string, any> = never,
540
- > = <StateKey extends keyof TransformedStateType<T>>(
541
- stateKey: StateKey,
542
- options?: Prettify<
543
- OptionsType<TransformedStateType<T>[StateKey]> & { apiParams?: apiParams }
544
- >
545
- ) => StateObject<TransformedStateType<T>[StateKey]>;
546
538
 
547
539
  // Define the type for the options setter using the Transformed state
548
540
  type SetCogsOptionsFunc<T extends Record<string, any>> = <
@@ -552,39 +544,35 @@ type SetCogsOptionsFunc<T extends Record<string, any>> = <
552
544
  options: OptionsType<TransformedStateType<T>[StateKey]>
553
545
  ) => void;
554
546
 
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>;
561
- setCogsOptions: SetCogsOptionsFunc<T>;
562
- };
563
-
564
- // Minimal change - just add a second parameter to detect sync schema
565
547
  export const createCogsState = <State extends Record<StateKeys, unknown>>(
566
548
  initialState: State,
567
549
  opt?: {
568
550
  formElements?: FormsElementsType<State>;
569
551
  validation?: ValidationOptionsType;
570
- // Add this flag to indicate it's from sync schema
571
552
  __fromSyncSchema?: boolean;
572
553
  __syncNotifications?: Record<string, Function>;
554
+ __apiParamsMap?: Record<string, any>; // Add this
573
555
  }
574
556
  ) => {
575
- // Keep ALL your existing code exactly the same
576
557
  let newInitialState = initialState;
577
558
 
578
559
  const [statePart, initialOptionsPart] =
579
560
  transformStateFunc<State>(newInitialState);
580
561
 
581
- // Only addition - store notifications if provided
562
+ // Store notifications if provided
582
563
  if (opt?.__fromSyncSchema && opt?.__syncNotifications) {
583
564
  getGlobalStore
584
565
  .getState()
585
566
  .setInitialStateOptions('__notifications', opt.__syncNotifications);
586
567
  }
587
568
 
569
+ // Store apiParams map if provided
570
+ if (opt?.__fromSyncSchema && opt?.__apiParamsMap) {
571
+ getGlobalStore
572
+ .getState()
573
+ .setInitialStateOptions('__apiParamsMap', opt.__apiParamsMap);
574
+ }
575
+
588
576
  // ... rest of your existing createCogsState code unchanged ...
589
577
 
590
578
  Object.keys(statePart).forEach((key) => {
@@ -637,14 +625,20 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
637
625
  stateKey: StateKey,
638
626
  options?: Prettify<OptionsType<(typeof statePart)[StateKey]>>
639
627
  ) => {
640
- // ... your existing useCogsState implementation ...
641
628
  const [componentId] = useState(options?.componentId ?? uuidv4());
629
+ const apiParamsSchema = opt?.__apiParamsMap?.[stateKey as string];
630
+
631
+ // Merge apiParams into options
632
+ const enhancedOptions = {
633
+ ...options,
634
+ apiParamsSchema, // Add the schema here
635
+ } as any;
636
+
642
637
  setOptions({
643
638
  stateKey,
644
- options: options as any,
639
+ options: enhancedOptions,
645
640
  initialOptionsPart,
646
641
  });
647
-
648
642
  const thiState =
649
643
  getGlobalStore.getState().getShadowValue(stateKey as string) ||
650
644
  statePart[stateKey as string];
@@ -683,12 +677,38 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
683
677
 
684
678
  return { useCogsState, setCogsOptions } as CogsApi<State>;
685
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]>;
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
686
705
  export function createCogsStateFromSync<
687
706
  TSyncSchema extends {
688
707
  schemas: Record<
689
708
  string,
690
709
  {
691
710
  schemas: { defaultValues: any };
711
+ apiParamsSchema?: any;
692
712
  [key: string]: any;
693
713
  }
694
714
  >;
@@ -701,21 +721,34 @@ export function createCogsStateFromSync<
701
721
  [K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['schemas']['defaultValues'];
702
722
  },
703
723
  {
704
- [K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['apiParams'];
705
- }[keyof {
706
- [K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['apiParams'];
707
- }]
724
+ [K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['apiParamsSchema'] extends z.ZodObject<
725
+ infer P
726
+ >
727
+ ? P
728
+ : never;
729
+ }
708
730
  > {
709
731
  const schemas = syncSchema.schemas;
710
732
  const initialState: any = {};
733
+ const apiParamsMap: any = {};
711
734
 
712
- // Extract defaultValues from each entry
735
+ // Extract defaultValues AND apiParams from each entry
713
736
  for (const key in schemas) {
714
737
  const entry = schemas[key];
715
738
  initialState[key] = entry?.schemas?.defaultValues || {};
739
+
740
+ // Store the apiParamsSchema for each key
741
+ if (entry?.apiParamsSchema) {
742
+ apiParamsMap[key] = entry.apiParamsSchema;
743
+ }
716
744
  }
717
745
 
718
- return createCogsState(initialState);
746
+ // Pass the sync schema metadata to createCogsState
747
+ return createCogsState(initialState, {
748
+ __fromSyncSchema: true,
749
+ __syncNotifications: syncSchema.notifications,
750
+ __apiParamsMap: apiParamsMap,
751
+ }) as any;
719
752
  }
720
753
 
721
754
  const {
@@ -1044,10 +1077,12 @@ export function useCogsStateFn<TStateObject extends unknown>(
1044
1077
  syncUpdate,
1045
1078
  dependencies,
1046
1079
  serverState,
1080
+ apiParamsSchema,
1047
1081
  }: {
1048
1082
  stateKey?: string;
1049
1083
  componentId?: string;
1050
1084
  defaultState?: TStateObject;
1085
+ apiParamsSchema?: z.ZodObject<any>; // Add this type
1051
1086
  } & OptionsType<TStateObject> = {}
1052
1087
  ) {
1053
1088
  const [reactiveForce, forceUpdate] = useState({}); //this is the key to reactivity