cogsbox-state 0.5.454 → 0.5.456
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 +11 -7
- package/dist/CogsState.d.ts.map +1 -1
- package/dist/CogsState.jsx +1226 -1223
- package/dist/CogsState.jsx.map +1 -1
- package/package.json +1 -1
- package/src/CogsState.tsx +46 -28
package/package.json
CHANGED
package/src/CogsState.tsx
CHANGED
|
@@ -37,7 +37,8 @@ import { useCogsConfig } from './CogsStateClient.js';
|
|
|
37
37
|
import { Operation } from 'fast-json-patch';
|
|
38
38
|
import { useInView } from 'react-intersection-observer';
|
|
39
39
|
import * as z3 from 'zod/v3';
|
|
40
|
-
import
|
|
40
|
+
import * as z4 from 'zod/v4';
|
|
41
|
+
import z from 'zod';
|
|
41
42
|
|
|
42
43
|
type Prettify<T> = T extends any ? { [K in keyof T]: T[K] } : never;
|
|
43
44
|
|
|
@@ -560,31 +561,35 @@ type CogsApi<
|
|
|
560
561
|
useCogsState: UseCogsStateHook<T, apiParams>;
|
|
561
562
|
setCogsOptions: SetCogsOptionsFunc<T>;
|
|
562
563
|
};
|
|
563
|
-
|
|
564
|
-
// Minimal change - just add a second parameter to detect sync schema
|
|
565
564
|
export const createCogsState = <State extends Record<StateKeys, unknown>>(
|
|
566
565
|
initialState: State,
|
|
567
566
|
opt?: {
|
|
568
567
|
formElements?: FormsElementsType<State>;
|
|
569
568
|
validation?: ValidationOptionsType;
|
|
570
|
-
// Add this flag to indicate it's from sync schema
|
|
571
569
|
__fromSyncSchema?: boolean;
|
|
572
570
|
__syncNotifications?: Record<string, Function>;
|
|
571
|
+
__apiParamsMap?: Record<string, any>; // Add this
|
|
573
572
|
}
|
|
574
573
|
) => {
|
|
575
|
-
// Keep ALL your existing code exactly the same
|
|
576
574
|
let newInitialState = initialState;
|
|
577
575
|
|
|
578
576
|
const [statePart, initialOptionsPart] =
|
|
579
577
|
transformStateFunc<State>(newInitialState);
|
|
580
578
|
|
|
581
|
-
//
|
|
579
|
+
// Store notifications if provided
|
|
582
580
|
if (opt?.__fromSyncSchema && opt?.__syncNotifications) {
|
|
583
581
|
getGlobalStore
|
|
584
582
|
.getState()
|
|
585
583
|
.setInitialStateOptions('__notifications', opt.__syncNotifications);
|
|
586
584
|
}
|
|
587
585
|
|
|
586
|
+
// Store apiParams map if provided
|
|
587
|
+
if (opt?.__fromSyncSchema && opt?.__apiParamsMap) {
|
|
588
|
+
getGlobalStore
|
|
589
|
+
.getState()
|
|
590
|
+
.setInitialStateOptions('__apiParamsMap', opt.__apiParamsMap);
|
|
591
|
+
}
|
|
592
|
+
|
|
588
593
|
// ... rest of your existing createCogsState code unchanged ...
|
|
589
594
|
|
|
590
595
|
Object.keys(statePart).forEach((key) => {
|
|
@@ -637,14 +642,20 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
|
|
|
637
642
|
stateKey: StateKey,
|
|
638
643
|
options?: Prettify<OptionsType<(typeof statePart)[StateKey]>>
|
|
639
644
|
) => {
|
|
640
|
-
// ... your existing useCogsState implementation ...
|
|
641
645
|
const [componentId] = useState(options?.componentId ?? uuidv4());
|
|
646
|
+
const apiParamsSchema = opt?.__apiParamsMap?.[stateKey as string];
|
|
647
|
+
|
|
648
|
+
// Merge apiParams into options
|
|
649
|
+
const enhancedOptions = {
|
|
650
|
+
...options,
|
|
651
|
+
apiParamsSchema, // Add the schema here
|
|
652
|
+
} as any;
|
|
653
|
+
|
|
642
654
|
setOptions({
|
|
643
655
|
stateKey,
|
|
644
|
-
options:
|
|
656
|
+
options: enhancedOptions,
|
|
645
657
|
initialOptionsPart,
|
|
646
658
|
});
|
|
647
|
-
|
|
648
659
|
const thiState =
|
|
649
660
|
getGlobalStore.getState().getShadowValue(stateKey as string) ||
|
|
650
661
|
statePart[stateKey as string];
|
|
@@ -690,40 +701,45 @@ export function createCogsStateFromSync<
|
|
|
690
701
|
string,
|
|
691
702
|
{
|
|
692
703
|
schemas: { defaultValues: any };
|
|
693
|
-
apiParamsSchema?:
|
|
704
|
+
apiParamsSchema?: any; // This contains the zod schema for params
|
|
694
705
|
[key: string]: any;
|
|
695
706
|
}
|
|
696
707
|
>;
|
|
697
708
|
notifications: Record<string, any>;
|
|
698
709
|
},
|
|
699
|
-
>(
|
|
710
|
+
>(
|
|
711
|
+
syncSchema: TSyncSchema
|
|
712
|
+
): CogsApi<
|
|
713
|
+
{
|
|
714
|
+
[K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['schemas']['defaultValues'];
|
|
715
|
+
},
|
|
716
|
+
{
|
|
717
|
+
[K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['apiParamsSchema'] extends z.ZodObject<any>
|
|
718
|
+
? z.infer<TSyncSchema['schemas'][K]['apiParamsSchema']>
|
|
719
|
+
: never;
|
|
720
|
+
}[keyof TSyncSchema['schemas']]
|
|
721
|
+
> {
|
|
700
722
|
const schemas = syncSchema.schemas;
|
|
701
723
|
const initialState: any = {};
|
|
724
|
+
const apiParamsMap: any = {};
|
|
702
725
|
|
|
703
|
-
// Extract defaultValues from each entry
|
|
726
|
+
// Extract defaultValues AND apiParams from each entry
|
|
704
727
|
for (const key in schemas) {
|
|
705
728
|
const entry = schemas[key];
|
|
706
729
|
initialState[key] = entry?.schemas?.defaultValues || {};
|
|
730
|
+
|
|
731
|
+
// Store the apiParamsSchema for each key
|
|
732
|
+
if (entry?.apiParamsSchema) {
|
|
733
|
+
apiParamsMap[key] = entry.apiParamsSchema;
|
|
734
|
+
}
|
|
707
735
|
}
|
|
708
736
|
|
|
709
|
-
//
|
|
710
|
-
|
|
737
|
+
// Pass the sync schema metadata to createCogsState
|
|
738
|
+
return createCogsState(initialState, {
|
|
711
739
|
__fromSyncSchema: true,
|
|
712
740
|
__syncNotifications: syncSchema.notifications,
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
// Override the useCogsState function to handle apiParams properly
|
|
716
|
-
const useCogsState = <K extends keyof TSyncSchema['schemas']>(
|
|
717
|
-
stateKey: K,
|
|
718
|
-
options?: any // Just make this flexible
|
|
719
|
-
) => {
|
|
720
|
-
return baseApi.useCogsState(stateKey as any, options);
|
|
721
|
-
};
|
|
722
|
-
|
|
723
|
-
return {
|
|
724
|
-
useCogsState,
|
|
725
|
-
setCogsOptions: baseApi.setCogsOptions,
|
|
726
|
-
};
|
|
741
|
+
__apiParamsMap: apiParamsMap, // Pass the apiParams schemas
|
|
742
|
+
}) as any;
|
|
727
743
|
}
|
|
728
744
|
|
|
729
745
|
const {
|
|
@@ -1052,10 +1068,12 @@ export function useCogsStateFn<TStateObject extends unknown>(
|
|
|
1052
1068
|
syncUpdate,
|
|
1053
1069
|
dependencies,
|
|
1054
1070
|
serverState,
|
|
1071
|
+
apiParamsSchema,
|
|
1055
1072
|
}: {
|
|
1056
1073
|
stateKey?: string;
|
|
1057
1074
|
componentId?: string;
|
|
1058
1075
|
defaultState?: TStateObject;
|
|
1076
|
+
apiParamsSchema?: z.ZodObject<any>; // Add this type
|
|
1059
1077
|
} & OptionsType<TStateObject> = {}
|
|
1060
1078
|
) {
|
|
1061
1079
|
const [reactiveForce, forceUpdate] = useState({}); //this is the key to reactivity
|