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/dist/CogsState.d.ts +9 -18
- package/dist/CogsState.d.ts.map +1 -1
- package/dist/CogsState.jsx +305 -308
- package/dist/CogsState.jsx.map +1 -1
- package/package.json +1 -1
- package/src/CogsState.tsx +31 -83
package/package.json
CHANGED
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 {
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
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
|
-
|
|
762
|
-
|
|
763
|
-
|
|
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,
|