next-action-handler 1.0.0 → 1.0.1
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/README.md +8 -0
- package/package.json +1 -1
- package/src/safe-action.ts +28 -0
package/README.md
CHANGED
|
@@ -2,6 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
Install a ready-to-use `next-safe-action` handler setup into your Next.js project.
|
|
4
4
|
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install the package (optional if you prefer `npx`):
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install -D next-action-handler
|
|
11
|
+
```
|
|
12
|
+
|
|
5
13
|
## Usage
|
|
6
14
|
|
|
7
15
|
Run the installer from the root of your project:
|
package/package.json
CHANGED
package/src/safe-action.ts
CHANGED
|
@@ -10,8 +10,20 @@ import {
|
|
|
10
10
|
import { logActionError, logActionExecution } from "./log/logger";
|
|
11
11
|
|
|
12
12
|
import { normalizeError } from "./error/normalize-error";
|
|
13
|
+
import { InternalServerError } from "./error/errors";
|
|
13
14
|
import { requireUser } from "../auth-helpers";
|
|
14
15
|
|
|
16
|
+
const OUTPUT_VALIDATION_SERVER_ERROR_MESSAGE =
|
|
17
|
+
"Unexpected response. Please try again.";
|
|
18
|
+
|
|
19
|
+
function isActionOutputDataValidationError(error: unknown): error is Error {
|
|
20
|
+
return (
|
|
21
|
+
error instanceof Error &&
|
|
22
|
+
(error.name === "ActionOutputDataValidationError" ||
|
|
23
|
+
error.constructor?.name === "ActionOutputDataValidationError")
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
15
27
|
export const actionClient = createSafeActionClient({
|
|
16
28
|
defineMetadataSchema: () =>
|
|
17
29
|
z.object({
|
|
@@ -19,6 +31,22 @@ export const actionClient = createSafeActionClient({
|
|
|
19
31
|
}),
|
|
20
32
|
|
|
21
33
|
handleServerError(error, ctx) {
|
|
34
|
+
if (isActionOutputDataValidationError(error)) {
|
|
35
|
+
const normalized = normalizeError(
|
|
36
|
+
new InternalServerError("Action output validation failed", error),
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
logActionError({
|
|
40
|
+
action: ctx.metadata.actionName,
|
|
41
|
+
error: normalized,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
code: normalized.code,
|
|
46
|
+
message: OUTPUT_VALIDATION_SERVER_ERROR_MESSAGE,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
22
50
|
const normalized = normalizeError(error);
|
|
23
51
|
|
|
24
52
|
logActionError({
|