cogsbox-state 0.5.440 → 0.5.441

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.440",
3
+ "version": "0.5.441",
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
@@ -554,44 +554,6 @@ type CogsApi<T extends Record<string, any>> = {
554
554
  setCogsOptions: SetCogsOptionsFunc<T>;
555
555
  };
556
556
 
557
- type ExtractStateFromSyncSchema<T> = T extends {
558
- schemas: infer S;
559
- notifications: any;
560
- }
561
- ? S extends Record<string, any>
562
- ? {
563
- [K in keyof S]: S[K] extends { rawSchema: infer R }
564
- ? R
565
- : S[K] extends { schemas: { defaults: infer D } }
566
- ? D
567
- : never;
568
- }
569
- : never
570
- : never;
571
-
572
- // Type to extract just the sync schema structure
573
- type SyncSchemaStructure<T = any> = {
574
- schemas: Record<
575
- string,
576
- {
577
- rawSchema?: any;
578
- schemas?: {
579
- sql?: any;
580
- client?: any;
581
- validation?: any;
582
- defaults?: any;
583
- };
584
- api?: {
585
- initialData?: string;
586
- update?: string;
587
- };
588
- validate?: (data: unknown, ctx: any) => any;
589
- validateClient?: (data: unknown) => any;
590
- serializable?: any;
591
- }
592
- >;
593
- notifications: Record<string, (state: any, context: any) => any>;
594
- };
595
557
  // Minimal change - just add a second parameter to detect sync schema
596
558
  export const createCogsState = <State extends Record<StateKeys, unknown>>(
597
559
  initialState: State,
@@ -714,55 +676,41 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
714
676
 
715
677
  return { useCogsState, setCogsOptions } as CogsApi<State>;
716
678
  };
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
679
+ // or wherever your shape types are
740
680
  export function createCogsStateFromSync<
741
- TSyncSchema extends { schemas: Record<string, any>; notifications: any },
742
- >(syncSchema: TSyncSchema): CogsApi<ExtractStateFromSync<TSyncSchema>> {
743
- // Extract initial state
744
- type StateType = ExtractStateFromSync<TSyncSchema>;
745
- const initialState = {} as StateType;
746
-
747
- for (const key in syncSchema.schemas) {
748
- const entry = syncSchema.schemas[key];
749
- if (entry.rawSchema) {
750
- initialState[key as keyof StateType] = entry.rawSchema;
751
- } else if (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;
756
- } else {
757
- initialState[key as keyof StateType] = {} as any;
758
- }
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
+ }> {
696
+ const schemas = syncSchema.schemas;
697
+ const initialState: any = {};
698
+
699
+ // Extract defaults from each entry
700
+ for (const key in schemas) {
701
+ const entry = schemas[key]!;
702
+ initialState[key] = entry.schemas?.defaults || {};
759
703
  }
760
704
 
761
- return createCogsState(initialState, {
762
- __fromSyncSchema: true,
763
- __syncNotifications: syncSchema.notifications,
764
- });
705
+ // Store sync metadata
706
+ // getGlobalStore.getState().setInitialStateOptions('__syncSchema', syncSchema);
707
+ getGlobalStore
708
+ .getState()
709
+ .setInitialStateOptions('__notifications', syncSchema.notifications);
710
+
711
+ return createCogsState(initialState);
765
712
  }
713
+
766
714
  const {
767
715
  getInitialOptions,
768
716
  getValidationErrors,