cogsbox-state 0.5.444 → 0.5.446

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.444",
3
+ "version": "0.5.446",
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
@@ -37,7 +37,7 @@ import { useCogsConfig } from './CogsStateClient.js';
37
37
  import { Operation } from 'fast-json-patch';
38
38
  import { useInView } from 'react-intersection-observer';
39
39
  import * as z3 from 'zod/v3';
40
- import z, * as z4 from 'zod/v4';
40
+ import * as z4 from 'zod/v4';
41
41
 
42
42
  type Prettify<T> = T extends any ? { [K in keyof T]: T[K] } : never;
43
43
 
@@ -534,36 +534,15 @@ export function addStateOptions<T extends unknown>(
534
534
  ) {
535
535
  return { initialState: initialState, formElements, validation } as T;
536
536
  }
537
- type CogsSyncSchema = {
538
- schemas: Record<
539
- string,
540
- {
541
- schemas: { defaultValues: any };
542
- apiParamsSchema?: z.ZodObject<any, any>;
543
- [key: string]: any;
544
- }
545
- >;
546
- notifications: Record<string, any>;
547
- };
548
-
549
- type UseCogsStateHook<TSchema extends CogsSyncSchema> = <
550
- // TStateKey is now DIRECTLY a key of the schemas object. No ambiguity.
551
- TStateKey extends keyof TSchema['schemas'],
552
- >(
553
- stateKey: TStateKey,
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,
554
542
  options?: Prettify<
555
- OptionsType<
556
- // The state slice type is derived directly from the schema.
557
- TSchema['schemas'][TStateKey]['schemas']['defaultValues'],
558
- // The API params type is also derived directly and safely.
559
- TSchema['schemas'][TStateKey] extends {
560
- apiParamsSchema: z.ZodObject<any, any>;
561
- }
562
- ? z.infer<TSchema['schemas'][TStateKey]['apiParamsSchema']>
563
- : never
564
- >
543
+ OptionsType<TransformedStateType<T>[StateKey]> & { apiParams?: apiParams }
565
544
  >
566
- ) => StateObject<TSchema['schemas'][TStateKey]['schemas']['defaultValues']>;
545
+ ) => StateObject<TransformedStateType<T>[StateKey]>;
567
546
 
568
547
  // Define the type for the options setter using the Transformed state
569
548
  type SetCogsOptionsFunc<T extends Record<string, any>> = <
@@ -574,9 +553,12 @@ type SetCogsOptionsFunc<T extends Record<string, any>> = <
574
553
  ) => void;
575
554
 
576
555
  // Define the final API object shape
577
- type CogsApi<TSchema extends CogsSyncSchema> = {
578
- useCogsState: UseCogsStateHook<TSchema>;
579
- setCogsOptions: SetCogsOptionsFunc<TSchema>;
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>;
580
562
  };
581
563
 
582
564
  // Minimal change - just add a second parameter to detect sync schema
@@ -699,34 +681,41 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
699
681
  notifyComponents(stateKey as string);
700
682
  }
701
683
 
702
- return { useCogsState, setCogsOptions };
684
+ return { useCogsState, setCogsOptions } as CogsApi<State>;
703
685
  };
704
- export function createCogsStateFromSync<TSchema extends CogsSyncSchema>(
705
- syncSchema: TSchema
706
- ): CogsApi<TSchema> {
707
- // The internal implementation doesn't need complex types.
708
- // It just needs to provide the functions.
709
- if (syncSchema.notifications) {
710
- getGlobalStore
711
- .getState()
712
- .setInitialStateOptions('__notifications', syncSchema.notifications);
713
- }
714
-
686
+ export function createCogsStateFromSync<
687
+ TSyncSchema extends {
688
+ schemas: Record<
689
+ string,
690
+ {
691
+ schemas: { defaultValues: any };
692
+ [key: string]: any;
693
+ }
694
+ >;
695
+ notifications: Record<string, any>;
696
+ },
697
+ >(
698
+ syncSchema: TSyncSchema
699
+ ): CogsApi<
700
+ {
701
+ [K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['schemas']['defaultValues'];
702
+ },
703
+ {
704
+ [K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['apiParams'];
705
+ }[keyof {
706
+ [K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['apiParams'];
707
+ }]
708
+ > {
715
709
  const schemas = syncSchema.schemas;
716
710
  const initialState: any = {};
711
+
712
+ // Extract defaultValues from each entry
717
713
  for (const key in schemas) {
718
- initialState[key] = schemas[key]?.schemas?.defaultValues || {};
714
+ const entry = schemas[key];
715
+ initialState[key] = entry?.schemas?.defaultValues || {};
719
716
  }
720
717
 
721
- // Get the real hook and options functions from your library
722
- const { useCogsState, setCogsOptions } = createCogsState(initialState);
723
-
724
- // Return the functions, casting the result to our perfect, explicit CogsApi type.
725
- // This is the bridge between the untyped internals and the strongly-typed public API.
726
- return {
727
- useCogsState,
728
- setCogsOptions,
729
- } as CogsApi<TSchema>;
718
+ return createCogsState(initialState);
730
719
  }
731
720
 
732
721
  const {