cogsbox-state 0.5.434 → 0.5.436
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 +40 -42
- package/dist/CogsState.jsx +1358 -1155
- package/dist/CogsState.jsx.map +1 -1
- package/dist/CogsStateClient.jsx.map +1 -1
- package/dist/Functions.d.ts +1 -5
- package/dist/Functions.jsx +17 -49
- package/dist/Functions.jsx.map +1 -1
- package/dist/TRPCValidationLink.js.map +1 -1
- package/dist/store.d.ts +11 -0
- package/dist/store.js +247 -185
- package/dist/store.js.map +1 -1
- package/dist/utility.js.map +1 -1
- package/package.json +6 -5
- package/src/CogsState.tsx +816 -211
- package/src/Functions.tsx +8 -241
- package/src/TRPCValidationLink.ts +12 -12
- package/src/store.ts +209 -43
- package/dist/useValidateZodPath.d.ts +0 -34
- package/src/useValidateZodPath.ts +0 -231
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { ZodObject, ZodRawShape, ZodTypeAny } from 'zod';
|
|
2
|
-
|
|
3
|
-
export type ResultItem = {
|
|
4
|
-
status: "loading" | "success" | "failure" | "error";
|
|
5
|
-
message?: string;
|
|
6
|
-
};
|
|
7
|
-
type RequestType = Array<{
|
|
8
|
-
path: string[];
|
|
9
|
-
data: any;
|
|
10
|
-
key: string;
|
|
11
|
-
}>;
|
|
12
|
-
type ResultsState = {
|
|
13
|
-
results: Record<string, Record<string, ResultItem>>;
|
|
14
|
-
request: Record<string, RequestType>;
|
|
15
|
-
getResultsByKey: (key: string) => Record<string, ResultItem> | undefined;
|
|
16
|
-
setResults: (key: string) => (result: Record<string, ResultItem> | ((prevState: Record<string, ResultItem>) => Record<string, ResultItem>)) => void;
|
|
17
|
-
setRequest: (key: string) => (request: RequestType | ((prevState: RequestType) => RequestType)) => void;
|
|
18
|
-
getRequestsByKey: (key: string) => Array<{
|
|
19
|
-
path: string[];
|
|
20
|
-
data: any;
|
|
21
|
-
key: string;
|
|
22
|
-
}> | undefined;
|
|
23
|
-
};
|
|
24
|
-
export declare const useResultsStore: import('zustand').UseBoundStore<import('zustand').StoreApi<ResultsState>>;
|
|
25
|
-
export default function useValidateZodPath<T extends ZodRawShape>(validationKey: string, schema: ZodObject<T>, stateKey?: string): {
|
|
26
|
-
validateZodPath: (path: string[], data: any, results?: Record<string, ResultItem> | undefined) => "loading" | "success" | "error" | "failure";
|
|
27
|
-
getZodPathResults: (path: string[]) => string[] | ResultItem;
|
|
28
|
-
zodPathResults: Record<string, ResultItem> | undefined;
|
|
29
|
-
};
|
|
30
|
-
export declare function validateZodPathFunc<T extends ZodRawShape, U>(validationKey: string, schema: ZodTypeAny, path: string[], data: U): Promise<{
|
|
31
|
-
success: boolean;
|
|
32
|
-
message?: string;
|
|
33
|
-
}>;
|
|
34
|
-
export {};
|
|
@@ -1,231 +0,0 @@
|
|
|
1
|
-
import { useEffect, useState } from "react";
|
|
2
|
-
import {
|
|
3
|
-
z,
|
|
4
|
-
ZodArray,
|
|
5
|
-
ZodEffects,
|
|
6
|
-
ZodError,
|
|
7
|
-
ZodNullable,
|
|
8
|
-
ZodObject,
|
|
9
|
-
ZodOptional,
|
|
10
|
-
type SafeParseReturnType,
|
|
11
|
-
type ZodRawShape,
|
|
12
|
-
type ZodTypeAny,
|
|
13
|
-
} from "zod";
|
|
14
|
-
import { create } from "zustand";
|
|
15
|
-
import { v4 as uuidv4 } from "uuid";
|
|
16
|
-
import { getGlobalStore } from "./store";
|
|
17
|
-
|
|
18
|
-
// Your existing types and store
|
|
19
|
-
export type ResultItem = {
|
|
20
|
-
status: "loading" | "success" | "failure" | "error";
|
|
21
|
-
message?: string;
|
|
22
|
-
};
|
|
23
|
-
type RequestType = Array<{ path: string[]; data: any; key: string }>;
|
|
24
|
-
|
|
25
|
-
type ResultsState = {
|
|
26
|
-
results: Record<string, Record<string, ResultItem>>;
|
|
27
|
-
request: Record<string, RequestType>;
|
|
28
|
-
getResultsByKey: (key: string) => Record<string, ResultItem> | undefined;
|
|
29
|
-
setResults: (
|
|
30
|
-
key: string
|
|
31
|
-
) => (
|
|
32
|
-
result:
|
|
33
|
-
| Record<string, ResultItem>
|
|
34
|
-
| ((prevState: Record<string, ResultItem>) => Record<string, ResultItem>)
|
|
35
|
-
) => void;
|
|
36
|
-
setRequest: (
|
|
37
|
-
key: string
|
|
38
|
-
) => (
|
|
39
|
-
request: RequestType | ((prevState: RequestType) => RequestType)
|
|
40
|
-
) => void;
|
|
41
|
-
getRequestsByKey: (
|
|
42
|
-
key: string
|
|
43
|
-
) => Array<{ path: string[]; data: any; key: string }> | undefined;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
export const useResultsStore = create<ResultsState>((set, get) => ({
|
|
47
|
-
results: {},
|
|
48
|
-
request: {},
|
|
49
|
-
getResultsByKey: (key) => get().results[key],
|
|
50
|
-
setResults: (key) => (result) => {
|
|
51
|
-
set((state) => {
|
|
52
|
-
return {
|
|
53
|
-
results: {
|
|
54
|
-
...state.results,
|
|
55
|
-
[key]:
|
|
56
|
-
typeof result === "function" ? result(state.results[key]!) : result,
|
|
57
|
-
},
|
|
58
|
-
};
|
|
59
|
-
});
|
|
60
|
-
},
|
|
61
|
-
setRequest: (key) => (request) =>
|
|
62
|
-
set((state) => ({
|
|
63
|
-
request: {
|
|
64
|
-
...state.request,
|
|
65
|
-
[key]:
|
|
66
|
-
typeof request === "function"
|
|
67
|
-
? request(state.request[key]!)
|
|
68
|
-
: request,
|
|
69
|
-
},
|
|
70
|
-
})),
|
|
71
|
-
getRequestsByKey: (key) => get().request[key],
|
|
72
|
-
}));
|
|
73
|
-
|
|
74
|
-
export default function useValidateZodPath<T extends ZodRawShape>(
|
|
75
|
-
validationKey: string,
|
|
76
|
-
schema: ZodObject<T>,
|
|
77
|
-
stateKey?: string
|
|
78
|
-
) {
|
|
79
|
-
const [thisKey, setThisKey] = useState(stateKey ?? uuidv4());
|
|
80
|
-
const [requests, setRequests] = useState<RequestType | undefined>(undefined);
|
|
81
|
-
|
|
82
|
-
const [localResults, setLocalResults] = useState<Record<string, ResultItem>>(
|
|
83
|
-
{}
|
|
84
|
-
);
|
|
85
|
-
const results = stateKey
|
|
86
|
-
? useResultsStore((state) => state.getResultsByKey(thisKey))
|
|
87
|
-
: localResults;
|
|
88
|
-
const setResults = stateKey
|
|
89
|
-
? useResultsStore.getState().setResults(thisKey)
|
|
90
|
-
: setLocalResults;
|
|
91
|
-
|
|
92
|
-
useEffect(() => {
|
|
93
|
-
requests?.forEach(async ({ path, data, key }) => {
|
|
94
|
-
setResults((prevResults) => ({
|
|
95
|
-
...prevResults,
|
|
96
|
-
[key]: { status: "loading" },
|
|
97
|
-
})); // prevResults is saying its any
|
|
98
|
-
|
|
99
|
-
try {
|
|
100
|
-
const response = await validateZodPathFunc(
|
|
101
|
-
validationKey,
|
|
102
|
-
schema,
|
|
103
|
-
path,
|
|
104
|
-
data
|
|
105
|
-
);
|
|
106
|
-
setResults((prevResults) => ({
|
|
107
|
-
...prevResults,
|
|
108
|
-
[key]: {
|
|
109
|
-
status: response.success ? "success" : "failure",
|
|
110
|
-
message: response.success ? undefined : response.message, // Now just a string
|
|
111
|
-
},
|
|
112
|
-
}));
|
|
113
|
-
} catch (error) {
|
|
114
|
-
console.error(error);
|
|
115
|
-
setResults((prevResults) => ({
|
|
116
|
-
...prevResults,
|
|
117
|
-
[key]: {
|
|
118
|
-
status: "error",
|
|
119
|
-
message:
|
|
120
|
-
error instanceof Error
|
|
121
|
-
? error.message
|
|
122
|
-
: "An unknown error occurred",
|
|
123
|
-
},
|
|
124
|
-
}));
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
// Clear requests after processing
|
|
129
|
-
if (requests && requests.length > 0) {
|
|
130
|
-
setRequests([]);
|
|
131
|
-
}
|
|
132
|
-
}, [requests, schema]);
|
|
133
|
-
|
|
134
|
-
const validateZodPath = (
|
|
135
|
-
path: string[],
|
|
136
|
-
data: any,
|
|
137
|
-
results?: Record<string, ResultItem> | undefined
|
|
138
|
-
) => {
|
|
139
|
-
const pathKey = path.join(".");
|
|
140
|
-
|
|
141
|
-
setResults((prevResults) => ({
|
|
142
|
-
...prevResults,
|
|
143
|
-
[pathKey]: { status: "loading" },
|
|
144
|
-
}));
|
|
145
|
-
setRequests((prevRequests) => [
|
|
146
|
-
...(prevRequests ?? []),
|
|
147
|
-
{ path, data, key: pathKey },
|
|
148
|
-
]);
|
|
149
|
-
|
|
150
|
-
return results?.[pathKey]?.status ?? "loading";
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
const getZodPathResults = (path: string[]) => {
|
|
154
|
-
const pathKey = path.join(".");
|
|
155
|
-
let endsWith = Object.keys(results ?? {}).filter(
|
|
156
|
-
(key) =>
|
|
157
|
-
key.endsWith(pathKey) && key.split(".").length === path.length + 1
|
|
158
|
-
);
|
|
159
|
-
|
|
160
|
-
return results?.[pathKey] ?? endsWith ?? null;
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
return { validateZodPath, getZodPathResults, zodPathResults: results };
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
export async function validateZodPathFunc<T extends ZodRawShape, U>(
|
|
167
|
-
validationKey: string,
|
|
168
|
-
schema: ZodTypeAny,
|
|
169
|
-
path: string[],
|
|
170
|
-
data: U
|
|
171
|
-
): Promise<{ success: boolean; message?: string }> {
|
|
172
|
-
let currentSchema: ZodTypeAny = schema;
|
|
173
|
-
const addValidationError = getGlobalStore.getState().addValidationError;
|
|
174
|
-
|
|
175
|
-
for (const key of path) {
|
|
176
|
-
currentSchema = unwrapSchema(currentSchema);
|
|
177
|
-
|
|
178
|
-
if (currentSchema instanceof ZodArray) {
|
|
179
|
-
const index = Number(key);
|
|
180
|
-
if (!isNaN(index)) {
|
|
181
|
-
currentSchema = currentSchema.element;
|
|
182
|
-
} else {
|
|
183
|
-
throw new Error(`Invalid path: array index expected but got '${key}'.`);
|
|
184
|
-
}
|
|
185
|
-
} else if (currentSchema instanceof ZodObject) {
|
|
186
|
-
if (key in currentSchema.shape) {
|
|
187
|
-
currentSchema = currentSchema.shape[key];
|
|
188
|
-
} else {
|
|
189
|
-
throw new Error(`Invalid path: key '${key}' not found in schema.`);
|
|
190
|
-
}
|
|
191
|
-
} else {
|
|
192
|
-
throw new Error(`Invalid path: key '${key}' not found in schema.`);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// Ensure the final schema is fully unwrapped
|
|
197
|
-
currentSchema = unwrapSchema(currentSchema, true);
|
|
198
|
-
// Now currentSchema should be the schema at the end of the path, and we can validate.
|
|
199
|
-
const result: SafeParseReturnType<any, any> =
|
|
200
|
-
await currentSchema.safeParseAsync(data);
|
|
201
|
-
|
|
202
|
-
if (!result.success) {
|
|
203
|
-
const messages = result.error.issues
|
|
204
|
-
.map((issue) => issue.message)
|
|
205
|
-
.join(", ");
|
|
206
|
-
|
|
207
|
-
const fullErrorPath = [validationKey, ...path].join(".");
|
|
208
|
-
addValidationError(fullErrorPath, messages);
|
|
209
|
-
return { success: false, message: messages };
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
return { success: true, message: undefined };
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
function unwrapSchema(
|
|
216
|
-
schema: ZodTypeAny,
|
|
217
|
-
notEffects: boolean = false
|
|
218
|
-
): ZodTypeAny {
|
|
219
|
-
while (
|
|
220
|
-
schema instanceof ZodOptional ||
|
|
221
|
-
schema instanceof ZodNullable ||
|
|
222
|
-
(!notEffects && schema instanceof ZodEffects)
|
|
223
|
-
) {
|
|
224
|
-
if (schema instanceof ZodOptional || schema instanceof ZodNullable) {
|
|
225
|
-
schema = schema.unwrap();
|
|
226
|
-
} else if (schema instanceof ZodEffects) {
|
|
227
|
-
schema = schema._def.schema;
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
return schema;
|
|
231
|
-
}
|