cogsbox-state 0.5.440 → 0.5.442

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.442",
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,45 @@ 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
740
679
  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
- }
680
+ TSyncSchema extends {
681
+ schemas: Record<
682
+ string,
683
+ {
684
+ schemas: { defaultValues: any };
685
+ [key: string]: any;
686
+ }
687
+ >;
688
+ notifications: Record<string, any>;
689
+ },
690
+ >(
691
+ syncSchema: TSyncSchema
692
+ ): CogsApi<{
693
+ [K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['schemas']['defaultValues'];
694
+ }> {
695
+ const schemas = syncSchema.schemas;
696
+ const initialState: any = {};
697
+
698
+ // Extract defaultValues from each entry
699
+ for (const key in schemas) {
700
+ const entry = schemas[key];
701
+ initialState[key] = entry?.schemas?.defaultValues || {};
759
702
  }
760
703
 
761
- return createCogsState(initialState, {
762
- __fromSyncSchema: true,
763
- __syncNotifications: syncSchema.notifications,
764
- });
704
+ // Store sync metadata in each state key's options
705
+ // for (const key in schemas) {
706
+ // const currentOptions =
707
+ // getGlobalStore.getState().getInitialOptions(key) || {};
708
+ // getGlobalStore.getState().setInitialStateOptions(key, {
709
+ // ...currentOptions,
710
+ // syncSchema: schemas[key],
711
+ // notificationChannels: syncSchema.notifications,
712
+ // });
713
+ // }
714
+
715
+ return createCogsState(initialState);
765
716
  }
717
+
766
718
  const {
767
719
  getInitialOptions,
768
720
  getValidationErrors,