cogsbox-state 0.5.437 → 0.5.439

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.437",
3
+ "version": "0.5.439",
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
@@ -713,31 +713,41 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
713
713
  return { useCogsState, setCogsOptions } as CogsApi<State>;
714
714
  };
715
715
 
716
+ type ExtractStateFromSync<T> = T extends { schemas: infer S }
717
+ ? S extends Record<string, any>
718
+ ? {
719
+ [K in keyof S]: S[K] extends { rawSchema: infer R }
720
+ ? R
721
+ : S[K] extends { schemas: { defaults: infer D } }
722
+ ? D
723
+ : any;
724
+ }
725
+ : never
726
+ : never;
727
+
716
728
  // Then create a simple helper that extracts state from sync schema
717
729
  export function createCogsStateFromSync<
718
- T extends Record<string, any>,
719
- >(syncSchema: {
720
- schemas: any;
721
- notifications: any;
722
- }): ReturnType<typeof createCogsState<T>> {
730
+ TSyncSchema extends { schemas: Record<string, any>; notifications: any },
731
+ >(syncSchema: TSyncSchema): CogsApi<ExtractStateFromSync<TSyncSchema>> {
723
732
  // Extract initial state
724
- const initialState = {} as T;
733
+ type StateType = ExtractStateFromSync<TSyncSchema>;
734
+ const initialState = {} as StateType;
725
735
 
726
736
  for (const key in syncSchema.schemas) {
727
737
  const entry = syncSchema.schemas[key];
728
738
  if (entry.rawSchema) {
729
- initialState[key as keyof T] = entry.rawSchema;
739
+ initialState[key as keyof StateType] = entry.rawSchema;
730
740
  } else if (entry.schemas?.defaults) {
731
- initialState[key as keyof T] = entry.schemas.defaults;
741
+ initialState[key as keyof StateType] = entry.schemas.defaults;
732
742
  } else {
733
- initialState[key as keyof T] = {} as any;
743
+ initialState[key as keyof StateType] = {} as any;
734
744
  }
735
745
  }
736
746
 
737
747
  return createCogsState(initialState, {
738
748
  __fromSyncSchema: true,
739
749
  __syncNotifications: syncSchema.notifications,
740
- }) as any;
750
+ });
741
751
  }
742
752
 
743
753
  const {