cogsbox-state 0.5.449 → 0.5.451
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 +3 -1
- package/dist/CogsState.d.ts.map +1 -1
- package/dist/CogsState.jsx +477 -463
- package/dist/CogsState.jsx.map +1 -1
- package/package.json +1 -1
- package/src/CogsState.tsx +69 -5
package/package.json
CHANGED
package/src/CogsState.tsx
CHANGED
|
@@ -683,6 +683,48 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
|
|
|
683
683
|
|
|
684
684
|
return { useCogsState, setCogsOptions } as CogsApi<State>;
|
|
685
685
|
};
|
|
686
|
+
|
|
687
|
+
// First, let's create a proper type to extract API params from the sync schema
|
|
688
|
+
type ExtractApiParamsFromSchema<T> = T extends {
|
|
689
|
+
apiParamsSchema: z.ZodObject<infer U>;
|
|
690
|
+
}
|
|
691
|
+
? { [K in keyof U]: z.infer<U[K]> }
|
|
692
|
+
: never;
|
|
693
|
+
|
|
694
|
+
// Type to check if a schema has API params
|
|
695
|
+
type HasApiParams<T> = T extends { apiParamsSchema: z.ZodObject<any> }
|
|
696
|
+
? true
|
|
697
|
+
: false;
|
|
698
|
+
|
|
699
|
+
// Create the proper return type for useCogsState
|
|
700
|
+
type UseCogsStateFromSync<
|
|
701
|
+
TSyncSchema extends {
|
|
702
|
+
schemas: Record<
|
|
703
|
+
string,
|
|
704
|
+
{ schemas: { defaultValues: any }; apiParamsSchema?: z.ZodObject<any> }
|
|
705
|
+
>;
|
|
706
|
+
},
|
|
707
|
+
> = {
|
|
708
|
+
// For schemas WITH API params - require apiParams
|
|
709
|
+
<K extends keyof TSyncSchema['schemas']>(
|
|
710
|
+
stateKey: K,
|
|
711
|
+
options: HasApiParams<TSyncSchema['schemas'][K]> extends true
|
|
712
|
+
? OptionsType<TSyncSchema['schemas'][K]['schemas']['defaultValues']> & {
|
|
713
|
+
apiParams: ExtractApiParamsFromSchema<TSyncSchema['schemas'][K]>;
|
|
714
|
+
}
|
|
715
|
+
: never
|
|
716
|
+
): StateObject<TSyncSchema['schemas'][K]['schemas']['defaultValues']>;
|
|
717
|
+
|
|
718
|
+
// For schemas WITHOUT API params - apiParams is optional
|
|
719
|
+
<K extends keyof TSyncSchema['schemas']>(
|
|
720
|
+
stateKey: K,
|
|
721
|
+
options?: HasApiParams<TSyncSchema['schemas'][K]> extends true
|
|
722
|
+
? never
|
|
723
|
+
: OptionsType<TSyncSchema['schemas'][K]['schemas']['defaultValues']>
|
|
724
|
+
): StateObject<TSyncSchema['schemas'][K]['schemas']['defaultValues']>;
|
|
725
|
+
};
|
|
726
|
+
// Simplified approach that actually works
|
|
727
|
+
// Fixed version that properly handles the apiParams type
|
|
686
728
|
export function createCogsStateFromSync<
|
|
687
729
|
TSyncSchema extends {
|
|
688
730
|
schemas: Record<
|
|
@@ -711,12 +753,35 @@ export function createCogsStateFromSync<
|
|
|
711
753
|
__syncNotifications: syncSchema.notifications,
|
|
712
754
|
});
|
|
713
755
|
|
|
714
|
-
// Override the useCogsState function to handle apiParams properly
|
|
715
756
|
const useCogsState = <K extends keyof TSyncSchema['schemas']>(
|
|
716
757
|
stateKey: K,
|
|
717
|
-
options?:
|
|
718
|
-
|
|
719
|
-
|
|
758
|
+
options?: Omit<
|
|
759
|
+
OptionsType<TSyncSchema['schemas'][K]['schemas']['defaultValues']>,
|
|
760
|
+
'apiParams'
|
|
761
|
+
> & {
|
|
762
|
+
apiParams?: any; // Allow any apiParams, validate at runtime
|
|
763
|
+
}
|
|
764
|
+
): StateObject<TSyncSchema['schemas'][K]['schemas']['defaultValues']> => {
|
|
765
|
+
// Runtime validation of API params
|
|
766
|
+
const schemaEntry = schemas[stateKey as keyof typeof schemas];
|
|
767
|
+
if (schemaEntry?.apiParamsSchema && options?.apiParams) {
|
|
768
|
+
// Validate the API params at runtime
|
|
769
|
+
const result = schemaEntry.apiParamsSchema.safeParse(options.apiParams);
|
|
770
|
+
if (!result.success) {
|
|
771
|
+
throw new Error(
|
|
772
|
+
`Invalid API params for ${String(stateKey)}: ${result.error.message}`
|
|
773
|
+
);
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
// Create options without apiParams for the base call
|
|
778
|
+
const { apiParams, ...baseOptions } = options || {};
|
|
779
|
+
|
|
780
|
+
// Cast to the correct type since we know it's properly typed
|
|
781
|
+
return baseApi.useCogsState(
|
|
782
|
+
stateKey as any,
|
|
783
|
+
baseOptions as any
|
|
784
|
+
) as StateObject<TSyncSchema['schemas'][K]['schemas']['defaultValues']>;
|
|
720
785
|
};
|
|
721
786
|
|
|
722
787
|
return {
|
|
@@ -724,7 +789,6 @@ export function createCogsStateFromSync<
|
|
|
724
789
|
setCogsOptions: baseApi.setCogsOptions,
|
|
725
790
|
};
|
|
726
791
|
}
|
|
727
|
-
|
|
728
792
|
const {
|
|
729
793
|
getInitialOptions,
|
|
730
794
|
getValidationErrors,
|