cogsbox-state 0.5.441 → 0.5.443

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.441",
3
+ "version": "0.5.443",
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
@@ -34,10 +34,10 @@ import {
34
34
  type ComponentsType,
35
35
  } from './store.js';
36
36
  import { useCogsConfig } from './CogsStateClient.js';
37
- import { applyPatch, compare, Operation } from 'fast-json-patch';
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 * as z4 from 'zod/v4';
40
+ import z, * as z4 from 'zod/v4';
41
41
 
42
42
  type Prettify<T> = T extends any ? { [K in keyof T]: T[K] } : never;
43
43
 
@@ -374,7 +374,7 @@ type ValidationOptionsType = {
374
374
 
375
375
  onBlur?: boolean;
376
376
  };
377
- export type OptionsType<T extends unknown = unknown> = {
377
+ export type OptionsType<T extends unknown = unknown, TApiParams = never> = {
378
378
  log?: boolean;
379
379
  componentId?: string;
380
380
  cogsSync?: (stateObject: StateObject<T>) => SyncApi;
@@ -425,6 +425,7 @@ export type OptionsType<T extends unknown = unknown> = {
425
425
  syncUpdate?: Partial<UpdateTypeDetail>;
426
426
 
427
427
  defaultState?: T;
428
+ apiParams?: TApiParams;
428
429
  dependencies?: any[];
429
430
  };
430
431
 
@@ -533,12 +534,36 @@ export function addStateOptions<T extends unknown>(
533
534
  ) {
534
535
  return { initialState: initialState, formElements, validation } as T;
535
536
  }
536
- type UseCogsStateHook<T extends Record<string, any>> = <
537
- StateKey extends keyof TransformedStateType<T>,
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'],
538
552
  >(
539
- stateKey: StateKey,
540
- options?: Prettify<OptionsType<TransformedStateType<T>[StateKey]>>
541
- ) => StateObject<TransformedStateType<T>[StateKey]>;
553
+ stateKey: TStateKey,
554
+ 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
+ >
565
+ >
566
+ ) => StateObject<TSchema['schemas'][TStateKey]['schemas']['defaultValues']>;
542
567
 
543
568
  // Define the type for the options setter using the Transformed state
544
569
  type SetCogsOptionsFunc<T extends Record<string, any>> = <
@@ -549,9 +574,9 @@ type SetCogsOptionsFunc<T extends Record<string, any>> = <
549
574
  ) => void;
550
575
 
551
576
  // Define the final API object shape
552
- type CogsApi<T extends Record<string, any>> = {
553
- useCogsState: UseCogsStateHook<T>;
554
- setCogsOptions: SetCogsOptionsFunc<T>;
577
+ type CogsApi<TSchema extends CogsSyncSchema> = {
578
+ useCogsState: UseCogsStateHook<TSchema>;
579
+ setCogsOptions: SetCogsOptionsFunc<TSchema>;
555
580
  };
556
581
 
557
582
  // Minimal change - just add a second parameter to detect sync schema
@@ -674,41 +699,25 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
674
699
  notifyComponents(stateKey as string);
675
700
  }
676
701
 
677
- return { useCogsState, setCogsOptions } as CogsApi<State>;
702
+ return { useCogsState, setCogsOptions };
678
703
  };
679
- // or wherever your shape types are
680
- export function createCogsStateFromSync<
681
- TSyncSchema extends {
682
- schemas: Record<
683
- string,
684
- {
685
- schemas: { defaults: any };
686
- [key: string]: any;
687
- }
688
- >;
689
- notifications: Record<string, any>;
690
- },
691
- >(
692
- syncSchema: TSyncSchema
693
- ): CogsApi<{
694
- [K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['schemas']['defaults'];
695
- }> {
704
+ export function createCogsStateFromSync<TSchema extends CogsSyncSchema>(
705
+ syncSchema: TSchema
706
+ ): CogsApi<TSchema> {
707
+ if (syncSchema.notifications) {
708
+ getGlobalStore
709
+ .getState()
710
+ .setInitialStateOptions('__notifications', syncSchema.notifications);
711
+ }
712
+
696
713
  const schemas = syncSchema.schemas;
697
714
  const initialState: any = {};
698
-
699
- // Extract defaults from each entry
700
715
  for (const key in schemas) {
701
- const entry = schemas[key]!;
702
- initialState[key] = entry.schemas?.defaults || {};
716
+ initialState[key] = schemas[key]?.schemas?.defaultValues || {};
703
717
  }
704
718
 
705
- // Store sync metadata
706
- // getGlobalStore.getState().setInitialStateOptions('__syncSchema', syncSchema);
707
- getGlobalStore
708
- .getState()
709
- .setInitialStateOptions('__notifications', syncSchema.notifications);
710
-
711
- return createCogsState(initialState);
719
+ // The cast is fine because the exported function signature is what matters.
720
+ return createCogsState(initialState) as any;
712
721
  }
713
722
 
714
723
  const {