cogsbox-state 0.5.460 → 0.5.461
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 +6 -5
- package/dist/CogsState.d.ts.map +1 -1
- package/dist/CogsState.jsx +512 -509
- package/dist/CogsState.jsx.map +1 -1
- package/dist/TRPCValidationLink.d.ts.map +1 -1
- package/dist/TRPCValidationLink.js.map +1 -1
- package/package.json +1 -1
- package/src/CogsState.tsx +27 -26
- package/src/TRPCValidationLink.ts +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TRPCValidationLink.d.ts","sourceRoot":"","sources":["../src/TRPCValidationLink.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAK7C,eAAO,MAAM,yBAAyB,GACpC,OAAO,SAAS,SAAS,EACzB,aAAa;IACb,GAAG,CAAC,EAAE,OAAO,CAAC;CACf,
|
|
1
|
+
{"version":3,"file":"TRPCValidationLink.d.ts","sourceRoot":"","sources":["../src/TRPCValidationLink.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAK7C,eAAO,MAAM,yBAAyB,GACpC,OAAO,SAAS,SAAS,EACzB,aAAa;IACb,GAAG,CAAC,EAAE,OAAO,CAAC;CACf,WAEgC,QAAQ,CAAC,OAAO,CAuDhD,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TRPCValidationLink.js","sources":["../src/TRPCValidationLink.ts"],"sourcesContent":["//@ts-nocheck\r\nimport { observable } from '@trpc/server/observable';\r\nimport type { AnyRouter } from '@trpc/server';\r\nimport type { TRPCLink } from '@trpc/client';\r\nimport type { Operation } from '@trpc/client';\r\nimport type { TRPCClientError } from '@trpc/client';\r\nimport { getGlobalStore } from './store';\r\nimport type { Observer } from '@trpc/server/observable';\r\nexport const useCogsTrpcValidationLink = <\r\n TRouter extends AnyRouter,\r\n>(passedOpts?: {\r\n log?: boolean;\r\n}) => {\r\n const addValidationError = getGlobalStore.getState().addValidationError;\r\n
|
|
1
|
+
{"version":3,"file":"TRPCValidationLink.js","sources":["../src/TRPCValidationLink.ts"],"sourcesContent":["//@ts-nocheck\r\nimport { observable } from '@trpc/server/observable';\r\nimport type { AnyRouter } from '@trpc/server';\r\nimport type { TRPCLink } from '@trpc/client';\r\nimport type { Operation } from '@trpc/client';\r\nimport type { TRPCClientError } from '@trpc/client';\r\nimport { getGlobalStore } from './store';\r\nimport type { Observer } from '@trpc/server/observable';\r\nexport const useCogsTrpcValidationLink = <\r\n TRouter extends AnyRouter,\r\n>(passedOpts?: {\r\n log?: boolean;\r\n}) => {\r\n const addValidationError = getGlobalStore.getState().addValidationError;\r\n const TrpcValidationLink = (): TRPCLink<TRouter> => {\r\n return (opts) => {\r\n return ({ next, op }: { next: any; op: Operation }) => {\r\n return observable(\r\n (observer: Observer<any, TRPCClientError<TRouter>>) => {\r\n const unsubscribe = next(op).subscribe({\r\n next(value: unknown) {\r\n observer.next(value);\r\n },\r\n error(err: TRPCClientError<TRouter>) {\r\n try {\r\n const errorObject = JSON.parse(err.message);\r\n if (passedOpts?.log) {\r\n console.log('errorObject', errorObject);\r\n }\r\n if (Array.isArray(errorObject)) {\r\n errorObject.forEach(\r\n (error: { path: string[]; message: string }) => {\r\n const fullpath = `${op.path}.${error.path.join('.')}`;\r\n // In your TRPC link\r\n if (passedOpts?.log) {\r\n console.log('fullpath 1', fullpath);\r\n }\r\n addValidationError(fullpath, error.message);\r\n }\r\n );\r\n } else if (\r\n typeof errorObject === 'object' &&\r\n errorObject !== null\r\n ) {\r\n Object.entries(errorObject).forEach(([key, value]) => {\r\n const fullpath = `${op.path}.${key}`;\r\n if (passedOpts?.log) {\r\n console.log('fullpath 2', fullpath);\r\n }\r\n addValidationError(fullpath, value as string);\r\n });\r\n }\r\n } catch (e) {\r\n // Silently handle parse errors\r\n }\r\n\r\n observer.error(err);\r\n },\r\n complete() {\r\n observer.complete();\r\n },\r\n });\r\n return unsubscribe;\r\n }\r\n );\r\n };\r\n };\r\n };\r\n return TrpcValidationLink;\r\n};\r\n"],"names":["useCogsTrpcValidationLink","passedOpts","addValidationError","getGlobalStore","opts","next","op","observable","observer","value","err","errorObject","error","fullpath","key"],"mappings":";;AAQO,MAAMA,IAA4B,CAEvCC,MAEI;AACJ,QAAMC,IAAqBC,EAAe,SAAA,EAAW;AAuDrD,SAtD2B,MAClB,CAACC,MACC,CAAC,EAAE,MAAAC,GAAM,IAAAC,QACPC;AAAA,IACL,CAACC,MACqBH,EAAKC,CAAE,EAAE,UAAU;AAAA,MACrC,KAAKG,GAAgB;AACnB,QAAAD,EAAS,KAAKC,CAAK;AAAA,MACrB;AAAA,MACA,MAAMC,GAA+B;AACnC,YAAI;AACF,gBAAMC,IAAc,KAAK,MAAMD,EAAI,OAAO;AAC1C,UAAIT,GAAY,OACd,QAAQ,IAAI,eAAeU,CAAW,GAEpC,MAAM,QAAQA,CAAW,IAC3BA,EAAY;AAAA,YACV,CAACC,MAA+C;AAC9C,oBAAMC,IAAW,GAAGP,EAAG,IAAI,IAAIM,EAAM,KAAK,KAAK,GAAG,CAAC;AAEnD,cAAIX,GAAY,OACd,QAAQ,IAAI,cAAcY,CAAQ,GAEpCX,EAAmBW,GAAUD,EAAM,OAAO;AAAA,YAC5C;AAAA,UAAA,IAGF,OAAOD,KAAgB,YACvBA,MAAgB,QAEhB,OAAO,QAAQA,CAAW,EAAE,QAAQ,CAAC,CAACG,GAAKL,CAAK,MAAM;AACpD,kBAAMI,IAAW,GAAGP,EAAG,IAAI,IAAIQ,CAAG;AAClC,YAAIb,GAAY,OACd,QAAQ,IAAI,cAAcY,CAAQ,GAEpCX,EAAmBW,GAAUJ,CAAe;AAAA,UAC9C,CAAC;AAAA,QAEL,QAAY;AAAA,QAEZ;AAEA,QAAAD,EAAS,MAAME,CAAG;AAAA,MACpB;AAAA,MACA,WAAW;AACT,QAAAF,EAAS,SAAA;AAAA,MACX;AAAA,IAAA,CACD;AAAA,EAEH;AAMV;"}
|
package/package.json
CHANGED
package/src/CogsState.tsx
CHANGED
|
@@ -574,6 +574,7 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
|
|
|
574
574
|
__syncNotifications?: Record<string, Function>;
|
|
575
575
|
__apiParamsMap?: Record<string, any>;
|
|
576
576
|
__useSync?: UseSyncType<State>;
|
|
577
|
+
__syncSchemas?: Record<string, any>;
|
|
577
578
|
}
|
|
578
579
|
) => {
|
|
579
580
|
let newInitialState = initialState;
|
|
@@ -621,7 +622,12 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
|
|
|
621
622
|
mergedOptions.validation.key = `${opt.validation.key}.${key}`;
|
|
622
623
|
}
|
|
623
624
|
}
|
|
624
|
-
|
|
625
|
+
if (opt?.__syncSchemas?.[key]?.schemas?.validation) {
|
|
626
|
+
mergedOptions.validation = {
|
|
627
|
+
zodSchemaV4: opt.__syncSchemas[key].schemas.validation,
|
|
628
|
+
...existingOptions.validation,
|
|
629
|
+
};
|
|
630
|
+
}
|
|
625
631
|
if (Object.keys(mergedOptions).length > 0) {
|
|
626
632
|
const existingGlobalOptions = getInitialOptions(key);
|
|
627
633
|
|
|
@@ -692,38 +698,33 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
|
|
|
692
698
|
notifyComponents(stateKey as string);
|
|
693
699
|
}
|
|
694
700
|
|
|
695
|
-
return { useCogsState, setCogsOptions } as CogsApi<State>;
|
|
701
|
+
return { useCogsState, setCogsOptions } as CogsApi<State, never>;
|
|
696
702
|
};
|
|
697
|
-
|
|
698
|
-
// Fix for UseCogsStateHook to support per-key apiParams
|
|
699
703
|
type UseCogsStateHook<
|
|
700
704
|
T extends Record<string, any>,
|
|
701
|
-
TApiParamsMap extends Record<string, any> =
|
|
705
|
+
TApiParamsMap extends Record<string, any> = never,
|
|
702
706
|
> = <StateKey extends keyof TransformedStateType<T> & string>(
|
|
703
707
|
stateKey: StateKey,
|
|
704
|
-
options?:
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
>
|
|
719
|
-
>;
|
|
720
|
-
})
|
|
721
|
-
>
|
|
708
|
+
options?: [TApiParamsMap] extends [never]
|
|
709
|
+
? // When TApiParamsMap is never (no sync)
|
|
710
|
+
Prettify<OptionsType<TransformedStateType<T>[StateKey]>>
|
|
711
|
+
: // When TApiParamsMap exists (sync enabled)
|
|
712
|
+
StateKey extends keyof TApiParamsMap
|
|
713
|
+
? Prettify<
|
|
714
|
+
OptionsType<
|
|
715
|
+
TransformedStateType<T>[StateKey],
|
|
716
|
+
TApiParamsMap[StateKey]
|
|
717
|
+
> & {
|
|
718
|
+
syncOptions: Prettify<SyncOptionsType<TApiParamsMap[StateKey]>>;
|
|
719
|
+
}
|
|
720
|
+
>
|
|
721
|
+
: Prettify<OptionsType<TransformedStateType<T>[StateKey]>>
|
|
722
722
|
) => StateObject<TransformedStateType<T>[StateKey]>;
|
|
723
|
-
|
|
723
|
+
|
|
724
|
+
// Update CogsApi to default to never instead of Record<string, never>
|
|
724
725
|
type CogsApi<
|
|
725
726
|
T extends Record<string, any>,
|
|
726
|
-
TApiParamsMap extends Record<string, any> =
|
|
727
|
+
TApiParamsMap extends Record<string, any> = never,
|
|
727
728
|
> = {
|
|
728
729
|
useCogsState: UseCogsStateHook<T, TApiParamsMap>;
|
|
729
730
|
setCogsOptions: SetCogsOptionsFunc<T>;
|
|
@@ -781,6 +782,7 @@ export function createCogsStateFromSync<
|
|
|
781
782
|
__syncNotifications: syncSchema.notifications,
|
|
782
783
|
__apiParamsMap: apiParamsMap,
|
|
783
784
|
__useSync: useSync,
|
|
785
|
+
__syncSchemas: schemas,
|
|
784
786
|
}) as any;
|
|
785
787
|
}
|
|
786
788
|
const {
|
|
@@ -3483,7 +3485,6 @@ function createProxyHandler<T>(
|
|
|
3483
3485
|
const init = getGlobalStore
|
|
3484
3486
|
.getState()
|
|
3485
3487
|
.getInitialOptions(stateKey)?.validation;
|
|
3486
|
-
if (!init?.key) throw new Error('Validation key not found');
|
|
3487
3488
|
|
|
3488
3489
|
// For each error, set shadow metadata
|
|
3489
3490
|
zodErrors.forEach((error) => {
|
|
@@ -12,7 +12,6 @@ export const useCogsTrpcValidationLink = <
|
|
|
12
12
|
log?: boolean;
|
|
13
13
|
}) => {
|
|
14
14
|
const addValidationError = getGlobalStore.getState().addValidationError;
|
|
15
|
-
|
|
16
15
|
const TrpcValidationLink = (): TRPCLink<TRouter> => {
|
|
17
16
|
return (opts) => {
|
|
18
17
|
return ({ next, op }: { next: any; op: Operation }) => {
|