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