cogsbox-state 0.5.452 → 0.5.453
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 +4 -2
- package/dist/CogsState.d.ts.map +1 -1
- package/dist/CogsState.jsx +473 -463
- package/dist/CogsState.jsx.map +1 -1
- package/package.json +1 -1
- package/src/CogsState.tsx +38 -7
package/package.json
CHANGED
package/src/CogsState.tsx
CHANGED
|
@@ -38,6 +38,7 @@ import { Operation } from 'fast-json-patch';
|
|
|
38
38
|
import { useInView } from 'react-intersection-observer';
|
|
39
39
|
import * as z3 from 'zod/v3';
|
|
40
40
|
import z, * as z4 from 'zod/v4';
|
|
41
|
+
import { keyof } from 'zod';
|
|
41
42
|
|
|
42
43
|
type Prettify<T> = T extends any ? { [K in keyof T]: T[K] } : never;
|
|
43
44
|
|
|
@@ -683,7 +684,7 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
|
|
|
683
684
|
|
|
684
685
|
return { useCogsState, setCogsOptions } as CogsApi<State>;
|
|
685
686
|
};
|
|
686
|
-
|
|
687
|
+
// Fixed version that properly handles the apiParams type without complex inference
|
|
687
688
|
export function createCogsStateFromSync<
|
|
688
689
|
TSyncSchema extends {
|
|
689
690
|
schemas: Record<
|
|
@@ -696,7 +697,20 @@ export function createCogsStateFromSync<
|
|
|
696
697
|
>;
|
|
697
698
|
notifications: Record<string, any>;
|
|
698
699
|
},
|
|
699
|
-
>(
|
|
700
|
+
>(
|
|
701
|
+
syncSchema: TSyncSchema
|
|
702
|
+
): {
|
|
703
|
+
useCogsState: <K extends keyof TSyncSchema['schemas']>(
|
|
704
|
+
stateKey: K,
|
|
705
|
+
options?: OptionsType<
|
|
706
|
+
TSyncSchema['schemas'][K]['schemas']['defaultValues'],
|
|
707
|
+
any
|
|
708
|
+
> & {
|
|
709
|
+
apiParams?: any;
|
|
710
|
+
}
|
|
711
|
+
) => StateObject<TSyncSchema['schemas'][K]['schemas']['defaultValues']>;
|
|
712
|
+
setCogsOptions: any;
|
|
713
|
+
} {
|
|
700
714
|
const schemas = syncSchema.schemas;
|
|
701
715
|
const initialState: any = {};
|
|
702
716
|
|
|
@@ -712,12 +726,30 @@ export function createCogsStateFromSync<
|
|
|
712
726
|
__syncNotifications: syncSchema.notifications,
|
|
713
727
|
});
|
|
714
728
|
|
|
715
|
-
//
|
|
729
|
+
// Create the properly typed useCogsState function
|
|
716
730
|
const useCogsState = <K extends keyof TSyncSchema['schemas']>(
|
|
717
731
|
stateKey: K,
|
|
718
|
-
options?:
|
|
719
|
-
|
|
720
|
-
|
|
732
|
+
options?: OptionsType<
|
|
733
|
+
TSyncSchema['schemas'][K]['schemas']['defaultValues'],
|
|
734
|
+
any
|
|
735
|
+
> & {
|
|
736
|
+
apiParams?: any;
|
|
737
|
+
}
|
|
738
|
+
): StateObject<TSyncSchema['schemas'][K]['schemas']['defaultValues']> => {
|
|
739
|
+
// Runtime validation of API params
|
|
740
|
+
const schemaEntry = schemas[stateKey as keyof typeof schemas];
|
|
741
|
+
if (schemaEntry?.apiParamsSchema && options?.apiParams) {
|
|
742
|
+
const result = schemaEntry.apiParamsSchema.safeParse(options.apiParams);
|
|
743
|
+
if (!result.success) {
|
|
744
|
+
throw new Error(
|
|
745
|
+
`Invalid API params for ${String(stateKey)}: ${result.error.message}`
|
|
746
|
+
);
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
return baseApi.useCogsState(stateKey as any, options as any) as StateObject<
|
|
751
|
+
TSyncSchema['schemas'][K]['schemas']['defaultValues']
|
|
752
|
+
>;
|
|
721
753
|
};
|
|
722
754
|
|
|
723
755
|
return {
|
|
@@ -725,7 +757,6 @@ export function createCogsStateFromSync<
|
|
|
725
757
|
setCogsOptions: baseApi.setCogsOptions,
|
|
726
758
|
};
|
|
727
759
|
}
|
|
728
|
-
|
|
729
760
|
const {
|
|
730
761
|
getInitialOptions,
|
|
731
762
|
getValidationErrors,
|