cogsbox-state 0.5.456 → 0.5.457
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 -9
- package/dist/CogsState.d.ts.map +1 -1
- package/dist/CogsState.jsx +0 -1
- package/dist/CogsState.jsx.map +1 -1
- package/package.json +1 -1
- package/src/CogsState.tsx +31 -22
package/package.json
CHANGED
package/src/CogsState.tsx
CHANGED
|
@@ -535,15 +535,6 @@ export function addStateOptions<T extends unknown>(
|
|
|
535
535
|
) {
|
|
536
536
|
return { initialState: initialState, formElements, validation } as T;
|
|
537
537
|
}
|
|
538
|
-
type UseCogsStateHook<
|
|
539
|
-
T extends Record<string, any>,
|
|
540
|
-
apiParams extends Record<string, any> = never,
|
|
541
|
-
> = <StateKey extends keyof TransformedStateType<T>>(
|
|
542
|
-
stateKey: StateKey,
|
|
543
|
-
options?: Prettify<
|
|
544
|
-
OptionsType<TransformedStateType<T>[StateKey]> & { apiParams?: apiParams }
|
|
545
|
-
>
|
|
546
|
-
) => StateObject<TransformedStateType<T>[StateKey]>;
|
|
547
538
|
|
|
548
539
|
// Define the type for the options setter using the Transformed state
|
|
549
540
|
type SetCogsOptionsFunc<T extends Record<string, any>> = <
|
|
@@ -553,14 +544,6 @@ type SetCogsOptionsFunc<T extends Record<string, any>> = <
|
|
|
553
544
|
options: OptionsType<TransformedStateType<T>[StateKey]>
|
|
554
545
|
) => void;
|
|
555
546
|
|
|
556
|
-
// Define the final API object shape
|
|
557
|
-
type CogsApi<
|
|
558
|
-
T extends Record<string, any>,
|
|
559
|
-
apiParams extends Record<string, any> = never,
|
|
560
|
-
> = {
|
|
561
|
-
useCogsState: UseCogsStateHook<T, apiParams>;
|
|
562
|
-
setCogsOptions: SetCogsOptionsFunc<T>;
|
|
563
|
-
};
|
|
564
547
|
export const createCogsState = <State extends Record<StateKeys, unknown>>(
|
|
565
548
|
initialState: State,
|
|
566
549
|
opt?: {
|
|
@@ -694,14 +677,38 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
|
|
|
694
677
|
|
|
695
678
|
return { useCogsState, setCogsOptions } as CogsApi<State>;
|
|
696
679
|
};
|
|
680
|
+
// Fix for UseCogsStateHook to support per-key apiParams
|
|
681
|
+
type UseCogsStateHook<
|
|
682
|
+
T extends Record<string, any>,
|
|
683
|
+
TApiParamsMap extends Record<string, any> = Record<string, never>,
|
|
684
|
+
> = <StateKey extends keyof TransformedStateType<T>>(
|
|
685
|
+
stateKey: StateKey,
|
|
686
|
+
options?: Prettify<
|
|
687
|
+
OptionsType<TransformedStateType<T>[StateKey]> & {
|
|
688
|
+
apiParams?: StateKey extends keyof TApiParamsMap
|
|
689
|
+
? TApiParamsMap[StateKey]
|
|
690
|
+
: never;
|
|
691
|
+
}
|
|
692
|
+
>
|
|
693
|
+
) => StateObject<TransformedStateType<T>[StateKey]>;
|
|
697
694
|
|
|
695
|
+
// Updated CogsApi type
|
|
696
|
+
type CogsApi<
|
|
697
|
+
T extends Record<string, any>,
|
|
698
|
+
TApiParamsMap extends Record<string, any> = Record<string, never>,
|
|
699
|
+
> = {
|
|
700
|
+
useCogsState: UseCogsStateHook<T, TApiParamsMap>;
|
|
701
|
+
setCogsOptions: SetCogsOptionsFunc<T>;
|
|
702
|
+
};
|
|
703
|
+
|
|
704
|
+
// Fixed createCogsStateFromSync return type
|
|
698
705
|
export function createCogsStateFromSync<
|
|
699
706
|
TSyncSchema extends {
|
|
700
707
|
schemas: Record<
|
|
701
708
|
string,
|
|
702
709
|
{
|
|
703
710
|
schemas: { defaultValues: any };
|
|
704
|
-
apiParamsSchema?: any;
|
|
711
|
+
apiParamsSchema?: any;
|
|
705
712
|
[key: string]: any;
|
|
706
713
|
}
|
|
707
714
|
>;
|
|
@@ -714,10 +721,12 @@ export function createCogsStateFromSync<
|
|
|
714
721
|
[K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['schemas']['defaultValues'];
|
|
715
722
|
},
|
|
716
723
|
{
|
|
717
|
-
[K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['apiParamsSchema'] extends z.ZodObject<
|
|
718
|
-
|
|
724
|
+
[K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['apiParamsSchema'] extends z.ZodObject<
|
|
725
|
+
infer P
|
|
726
|
+
>
|
|
727
|
+
? P
|
|
719
728
|
: never;
|
|
720
|
-
}
|
|
729
|
+
}
|
|
721
730
|
> {
|
|
722
731
|
const schemas = syncSchema.schemas;
|
|
723
732
|
const initialState: any = {};
|
|
@@ -738,7 +747,7 @@ export function createCogsStateFromSync<
|
|
|
738
747
|
return createCogsState(initialState, {
|
|
739
748
|
__fromSyncSchema: true,
|
|
740
749
|
__syncNotifications: syncSchema.notifications,
|
|
741
|
-
__apiParamsMap: apiParamsMap,
|
|
750
|
+
__apiParamsMap: apiParamsMap,
|
|
742
751
|
}) as any;
|
|
743
752
|
}
|
|
744
753
|
|