create-ej-app 10.0.0 → 11.0.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-ej-app",
3
- "version": "10.0.0",
3
+ "version": "11.0.0",
4
4
  "description": "Bootstrap a next.js app with my preferred setup",
5
5
  "main": "cli.js",
6
6
  "scripts": {
@@ -14,17 +14,34 @@ type AnyAction = (
14
14
  type ActionInput<T extends AnyAction> = Parameters<T>[0]
15
15
  type ActionData<T extends AnyAction> = Awaited<ReturnType<T>>['data']
16
16
 
17
+ class SafeActionError extends Error {
18
+ constructor(
19
+ public message: string,
20
+ public validationErrors?: Record<string, string[] | undefined>
21
+ ) {
22
+ super(message)
23
+ }
24
+ }
25
+
17
26
  export function useSafeActionQuery<TAction extends AnyAction>(
27
+ key: string,
18
28
  action: TAction,
19
29
  input: ActionInput<TAction>,
20
30
  options?: Omit<UseQueryOptions<ActionData<TAction>>, 'queryKey' | 'queryFn'>
21
31
  ) {
22
32
  return useQuery({
23
- queryKey: [`${action.name}`, input],
33
+ queryKey: [key, input],
24
34
  queryFn: async () => {
25
35
  const result = await action(input)
26
- if (result?.serverError) throw new Error(result.serverError)
27
- if (result?.validationErrors) throw new Error('Validation failed')
36
+
37
+ if (result?.serverError) {
38
+ throw new SafeActionError(result.serverError)
39
+ }
40
+
41
+ if (result?.validationErrors) {
42
+ throw new SafeActionError('Validation failed', result.validationErrors)
43
+ }
44
+
28
45
  return result?.data
29
46
  },
30
47
  ...options
@@ -33,13 +50,24 @@ export function useSafeActionQuery<TAction extends AnyAction>(
33
50
 
34
51
  export function useSafeActionMutation<TAction extends AnyAction>(
35
52
  action: TAction,
36
- options?: UseMutationOptions<ActionData<TAction>, Error, ActionInput<TAction>>
53
+ options?: UseMutationOptions<
54
+ ActionData<TAction>,
55
+ SafeActionError,
56
+ ActionInput<TAction>
57
+ >
37
58
  ) {
38
59
  return useMutation({
39
60
  mutationFn: async (input: ActionInput<TAction>) => {
40
61
  const result = await action(input)
41
- if (result?.serverError) throw new Error(result.serverError)
42
- if (result?.validationErrors) throw new Error('Validation failed')
62
+
63
+ if (result?.serverError) {
64
+ throw new SafeActionError(result.serverError)
65
+ }
66
+
67
+ if (result?.validationErrors) {
68
+ throw new SafeActionError('Validation failed', result.validationErrors)
69
+ }
70
+
43
71
  return result?.data
44
72
  },
45
73
  ...options