cogsbox-state 0.5.438 → 0.5.440

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.438",
3
+ "version": "0.5.440",
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
@@ -592,6 +592,7 @@ type SyncSchemaStructure<T = any> = {
592
592
  >;
593
593
  notifications: Record<string, (state: any, context: any) => any>;
594
594
  };
595
+ // Minimal change - just add a second parameter to detect sync schema
595
596
  export const createCogsState = <State extends Record<StateKeys, unknown>>(
596
597
  initialState: State,
597
598
  opt?: {
@@ -614,6 +615,7 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
614
615
  .getState()
615
616
  .setInitialStateOptions('__notifications', opt.__syncNotifications);
616
617
  }
618
+
617
619
  // ... rest of your existing createCogsState code unchanged ...
618
620
 
619
621
  Object.keys(statePart).forEach((key) => {
@@ -713,32 +715,54 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
713
715
  return { useCogsState, setCogsOptions } as CogsApi<State>;
714
716
  };
715
717
 
718
+ // Helper to extract just the data shape, not the full Zod types
719
+ type SimplifyType<T> = T extends object
720
+ ? T extends (...args: any[]) => any
721
+ ? T
722
+ : { [K in keyof T]: SimplifyType<T[K]> }
723
+ : T;
724
+
725
+ type ExtractStateFromSync<T> = T extends { schemas: infer S }
726
+ ? S extends Record<string, any>
727
+ ? {
728
+ [K in keyof S]: S[K] extends { rawSchema: infer R }
729
+ ? SimplifyType<R>
730
+ : S[K] extends { schemas: { defaults: infer D } }
731
+ ? SimplifyType<D>
732
+ : S[K] extends { _tableName: string }
733
+ ? SimplifyType<S[K]>
734
+ : never;
735
+ }
736
+ : never
737
+ : never;
738
+
739
+ // Then create a simple helper that extracts state from sync schema
716
740
  export function createCogsStateFromSync<
717
- T extends Record<string, any>,
718
- >(syncSchema: {
719
- schemas: any;
720
- notifications: any;
721
- }): ReturnType<typeof createCogsState<T>> {
741
+ TSyncSchema extends { schemas: Record<string, any>; notifications: any },
742
+ >(syncSchema: TSyncSchema): CogsApi<ExtractStateFromSync<TSyncSchema>> {
722
743
  // Extract initial state
723
- const initialState = {} as T;
744
+ type StateType = ExtractStateFromSync<TSyncSchema>;
745
+ const initialState = {} as StateType;
724
746
 
725
747
  for (const key in syncSchema.schemas) {
726
748
  const entry = syncSchema.schemas[key];
727
749
  if (entry.rawSchema) {
728
- initialState[key as keyof T] = entry.rawSchema;
750
+ initialState[key as keyof StateType] = entry.rawSchema;
729
751
  } else if (entry.schemas?.defaults) {
730
- initialState[key as keyof T] = entry.schemas.defaults;
752
+ initialState[key as keyof StateType] = entry.schemas.defaults;
753
+ } else if (entry._tableName) {
754
+ // It's a raw schema object
755
+ initialState[key as keyof StateType] = entry;
731
756
  } else {
732
- initialState[key as keyof T] = {} as any;
757
+ initialState[key as keyof StateType] = {} as any;
733
758
  }
734
759
  }
735
760
 
736
761
  return createCogsState(initialState, {
737
762
  __fromSyncSchema: true,
738
763
  __syncNotifications: syncSchema.notifications,
739
- }) as any;
764
+ });
740
765
  }
741
-
742
766
  const {
743
767
  getInitialOptions,
744
768
  getValidationErrors,