server-act 1.6.1 → 1.7.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/README.md +38 -9
- package/dist/{index.js → index.cjs} +29 -36
- package/dist/{index.d.ts → index.d.cts} +15 -8
- package/dist/index.d.mts +15 -8
- package/dist/index.mjs +28 -12
- package/dist/{utils.js → utils.cjs} +1 -4
- package/dist/utils.mjs +1 -4
- package/package.json +22 -22
- /package/dist/{utils.d.ts → utils.d.cts} +0 -0
package/README.md
CHANGED
|
@@ -71,7 +71,7 @@ import { z } from "zod";
|
|
|
71
71
|
export const sayHelloAction = serverAct
|
|
72
72
|
.middleware(() => {
|
|
73
73
|
const t = i18n();
|
|
74
|
-
const userId = "..."
|
|
74
|
+
const userId = "...";
|
|
75
75
|
return { t, userId };
|
|
76
76
|
})
|
|
77
77
|
.input((ctx) => {
|
|
@@ -85,6 +85,39 @@ export const sayHelloAction = serverAct
|
|
|
85
85
|
});
|
|
86
86
|
```
|
|
87
87
|
|
|
88
|
+
#### Chaining Middlewares
|
|
89
|
+
|
|
90
|
+
You can chain multiple middlewares by calling `.middleware(...)` repeatedly.
|
|
91
|
+
|
|
92
|
+
- Middlewares run in registration order.
|
|
93
|
+
- Each middleware receives the current `ctx` and can return additional context.
|
|
94
|
+
- Returned objects are shallow-merged into `ctx`.
|
|
95
|
+
- Later middleware values override earlier values for the same key.
|
|
96
|
+
- Errors thrown in middleware propagate and stop later middleware from running.
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
// action.ts
|
|
100
|
+
"use server";
|
|
101
|
+
|
|
102
|
+
import { serverAct } from "server-act";
|
|
103
|
+
|
|
104
|
+
export const createGreetingAction = serverAct
|
|
105
|
+
.middleware(() => ({
|
|
106
|
+
requestId: crypto.randomUUID(),
|
|
107
|
+
role: "user",
|
|
108
|
+
}))
|
|
109
|
+
.middleware(({ ctx }) => ({
|
|
110
|
+
role: "admin", // overrides previous role
|
|
111
|
+
actorLabel: `${ctx.role}-actor`,
|
|
112
|
+
}))
|
|
113
|
+
.middleware(({ ctx }) => ({
|
|
114
|
+
trace: `${ctx.requestId}:${ctx.actorLabel}`,
|
|
115
|
+
}))
|
|
116
|
+
.action(async ({ ctx }) => {
|
|
117
|
+
return `${ctx.role} -> ${ctx.trace}`;
|
|
118
|
+
});
|
|
119
|
+
```
|
|
120
|
+
|
|
88
121
|
### `useActionState` Support
|
|
89
122
|
|
|
90
123
|
> `useActionState` Documentation:
|
|
@@ -99,9 +132,7 @@ import { serverAct } from "server-act";
|
|
|
99
132
|
import { formDataToObject } from "server-act/utils";
|
|
100
133
|
import { z } from "zod";
|
|
101
134
|
|
|
102
|
-
function zodFormData<T extends z.ZodType>(
|
|
103
|
-
schema: T,
|
|
104
|
-
): z.ZodPipe<z.ZodTransform<Record<string, unknown>, FormData>, T> {
|
|
135
|
+
function zodFormData<T extends z.ZodType>(schema: T) {
|
|
105
136
|
return z.preprocess<Record<string, unknown>, T, FormData>(
|
|
106
137
|
(v) => formDataToObject(v),
|
|
107
138
|
schema,
|
|
@@ -141,7 +172,7 @@ import { formDataToObject } from "server-act/utils";
|
|
|
141
172
|
|
|
142
173
|
```ts
|
|
143
174
|
const formData = new FormData();
|
|
144
|
-
formData.append(
|
|
175
|
+
formData.append("name", "John");
|
|
145
176
|
|
|
146
177
|
const result = formDataToObject(formData);
|
|
147
178
|
// Result: { name: 'John' }
|
|
@@ -151,7 +182,7 @@ const result = formDataToObject(formData);
|
|
|
151
182
|
|
|
152
183
|
```ts
|
|
153
184
|
const formData = new FormData();
|
|
154
|
-
formData.append(
|
|
185
|
+
formData.append("user.name", "John");
|
|
155
186
|
|
|
156
187
|
const result = formDataToObject(formData);
|
|
157
188
|
// Result: { user: { name: 'John' } }
|
|
@@ -166,9 +197,7 @@ import { serverAct } from "server-act";
|
|
|
166
197
|
import { formDataToObject } from "server-act/utils";
|
|
167
198
|
import { z } from "zod";
|
|
168
199
|
|
|
169
|
-
function zodFormData<T extends z.ZodType>(
|
|
170
|
-
schema: T,
|
|
171
|
-
): z.ZodPipe<z.ZodTransform<Record<string, unknown>, FormData>, T> {
|
|
200
|
+
function zodFormData<T extends z.ZodType>(schema: T) {
|
|
172
201
|
return z.preprocess<Record<string, unknown>, T, FormData>(
|
|
173
202
|
(v) => formDataToObject(v),
|
|
174
203
|
schema,
|
|
@@ -1,28 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
if (
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
1
|
+
let _standard_schema_utils = require("@standard-schema/utils");
|
|
2
|
+
|
|
3
|
+
//#region src/internal/middleware.ts
|
|
4
|
+
/**
|
|
5
|
+
* Executes an array of middleware functions with the given initial context.
|
|
6
|
+
*/
|
|
7
|
+
async function executeMiddlewares(middlewares, initialCtx) {
|
|
8
|
+
let ctx = initialCtx && typeof initialCtx === "object" ? { ...initialCtx } : {};
|
|
9
|
+
for (const middleware of middlewares) {
|
|
10
|
+
const result = await middleware({ ctx });
|
|
11
|
+
if (result && typeof result === "object") ctx = {
|
|
12
|
+
...ctx,
|
|
13
|
+
...result
|
|
14
|
+
};
|
|
15
15
|
}
|
|
16
|
-
return
|
|
17
|
-
}
|
|
18
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
-
value: mod,
|
|
20
|
-
enumerable: true
|
|
21
|
-
}) : target, mod));
|
|
16
|
+
return ctx;
|
|
17
|
+
}
|
|
22
18
|
|
|
23
19
|
//#endregion
|
|
24
|
-
const __standard_schema_utils = __toESM(require("@standard-schema/utils"));
|
|
25
|
-
|
|
26
20
|
//#region src/internal/schema.ts
|
|
27
21
|
async function standardValidate(schema, input) {
|
|
28
22
|
let result = schema["~standard"].validate(input);
|
|
@@ -33,7 +27,7 @@ function getInputErrors(issues) {
|
|
|
33
27
|
const messages = [];
|
|
34
28
|
const fieldErrors = {};
|
|
35
29
|
for (const issue of issues) {
|
|
36
|
-
const dotPath = (0,
|
|
30
|
+
const dotPath = (0, _standard_schema_utils.getDotPath)(issue);
|
|
37
31
|
if (dotPath) if (fieldErrors[dotPath]) fieldErrors[dotPath].push(issue.message);
|
|
38
32
|
else fieldErrors[dotPath] = [issue.message];
|
|
39
33
|
else messages.push(issue.message);
|
|
@@ -46,20 +40,19 @@ function getInputErrors(issues) {
|
|
|
46
40
|
|
|
47
41
|
//#endregion
|
|
48
42
|
//#region src/index.ts
|
|
49
|
-
const unsetMarker = Symbol("unsetMarker");
|
|
50
43
|
function createNewServerActionBuilder(def) {
|
|
51
44
|
return createServerActionBuilder(def);
|
|
52
45
|
}
|
|
53
46
|
function createServerActionBuilder(initDef = {}) {
|
|
54
47
|
const _def = {
|
|
55
48
|
input: void 0,
|
|
56
|
-
middleware:
|
|
49
|
+
middleware: [],
|
|
57
50
|
...initDef
|
|
58
51
|
};
|
|
59
52
|
return {
|
|
60
53
|
middleware: (middleware) => createNewServerActionBuilder({
|
|
61
54
|
..._def,
|
|
62
|
-
middleware
|
|
55
|
+
middleware: [..._def.middleware, middleware]
|
|
63
56
|
}),
|
|
64
57
|
input: (input) => createNewServerActionBuilder({
|
|
65
58
|
..._def,
|
|
@@ -67,11 +60,11 @@ function createServerActionBuilder(initDef = {}) {
|
|
|
67
60
|
}),
|
|
68
61
|
action: (action) => {
|
|
69
62
|
return async (input) => {
|
|
70
|
-
|
|
63
|
+
let ctx = {};
|
|
64
|
+
if (_def.middleware.length > 0) ctx = await executeMiddlewares(_def.middleware, ctx);
|
|
71
65
|
if (_def.input) {
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
if (result.issues) throw new __standard_schema_utils.SchemaError(result.issues);
|
|
66
|
+
const result = await standardValidate(typeof _def.input === "function" ? await _def.input({ ctx }) : _def.input, input);
|
|
67
|
+
if (result.issues) throw new _standard_schema_utils.SchemaError(result.issues);
|
|
75
68
|
return await action({
|
|
76
69
|
ctx,
|
|
77
70
|
input: result.value
|
|
@@ -85,10 +78,10 @@ function createServerActionBuilder(initDef = {}) {
|
|
|
85
78
|
},
|
|
86
79
|
stateAction: (action) => {
|
|
87
80
|
return async (prevState, rawInput) => {
|
|
88
|
-
|
|
81
|
+
let ctx = {};
|
|
82
|
+
if (_def.middleware.length > 0) ctx = await executeMiddlewares(_def.middleware, ctx);
|
|
89
83
|
if (_def.input) {
|
|
90
|
-
const
|
|
91
|
-
const result = await standardValidate(inputSchema, rawInput);
|
|
84
|
+
const result = await standardValidate(typeof _def.input === "function" ? await _def.input({ ctx }) : _def.input, rawInput);
|
|
92
85
|
if (result.issues) return await action({
|
|
93
86
|
ctx,
|
|
94
87
|
prevState,
|
|
@@ -112,10 +105,10 @@ function createServerActionBuilder(initDef = {}) {
|
|
|
112
105
|
},
|
|
113
106
|
formAction: (action) => {
|
|
114
107
|
return async (prevState, formData) => {
|
|
115
|
-
|
|
108
|
+
let ctx = {};
|
|
109
|
+
if (_def.middleware.length > 0) ctx = await executeMiddlewares(_def.middleware, ctx);
|
|
116
110
|
if (_def.input) {
|
|
117
|
-
const
|
|
118
|
-
const result = await standardValidate(inputSchema, formData);
|
|
111
|
+
const result = await standardValidate(typeof _def.input === "function" ? await _def.input({ ctx }) : _def.input, formData);
|
|
119
112
|
if (result.issues) return await action({
|
|
120
113
|
ctx,
|
|
121
114
|
prevState,
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
2
|
|
|
3
|
+
//#region src/internal/middleware.d.ts
|
|
4
|
+
type MiddlewareFunction<TContext, TReturn> = (params: {
|
|
5
|
+
ctx: TContext;
|
|
6
|
+
}) => Promise<TReturn> | TReturn;
|
|
7
|
+
//#endregion
|
|
3
8
|
//#region src/internal/schema.d.ts
|
|
4
9
|
declare function getInputErrors(issues: ReadonlyArray<StandardSchemaV1.Issue>): {
|
|
5
10
|
messages: string[];
|
|
@@ -15,7 +20,6 @@ type Prettify<T> = { [P in keyof T]: T[P] } & {};
|
|
|
15
20
|
type SanitizeFunctionParam<T extends (param: any) => any> = T extends ((param: infer P) => infer R) ? Equals<P, undefined> extends true ? () => R : Equals<P, P | undefined> extends true ? (param?: P) => R : (param: P) => R : never;
|
|
16
21
|
type InferParserType<T, TType extends "in" | "out"> = T extends StandardSchemaV1 ? TType extends "in" ? StandardSchemaV1.InferInput<T> : StandardSchemaV1.InferOutput<T> : never;
|
|
17
22
|
type InferInputType<T, TType extends "in" | "out"> = T extends UnsetMarker ? undefined : InferParserType<T, TType>;
|
|
18
|
-
type InferContextType<T> = RemoveUnsetMarker<T>;
|
|
19
23
|
interface ActionParams<TInput = unknown, TContext = unknown> {
|
|
20
24
|
_input: TInput;
|
|
21
25
|
_context: TContext;
|
|
@@ -23,16 +27,19 @@ interface ActionParams<TInput = unknown, TContext = unknown> {
|
|
|
23
27
|
interface ActionBuilder<TParams extends ActionParams> {
|
|
24
28
|
/**
|
|
25
29
|
* Middleware allows you to run code before the action, its return value will pass as context to the action.
|
|
30
|
+
*
|
|
31
|
+
* Chaining multiple middlewares is possible, each middleware receives context from previous middlewares
|
|
32
|
+
* and returns additional context that gets merged.
|
|
26
33
|
*/
|
|
27
|
-
middleware: <
|
|
34
|
+
middleware: <TNewContext>(middleware: MiddlewareFunction<RemoveUnsetMarker<TParams["_context"]>, TNewContext>) => ActionBuilder<{
|
|
28
35
|
_input: TParams["_input"];
|
|
29
|
-
_context:
|
|
30
|
-
}
|
|
36
|
+
_context: TParams["_context"] extends UnsetMarker ? TNewContext : Prettify<TParams["_context"] & TNewContext>;
|
|
37
|
+
}>;
|
|
31
38
|
/**
|
|
32
39
|
* Input validation for the action.
|
|
33
40
|
*/
|
|
34
41
|
input: <TParser extends StandardSchemaV1>(input: ((params: {
|
|
35
|
-
ctx:
|
|
42
|
+
ctx: RemoveUnsetMarker<TParams["_context"]>;
|
|
36
43
|
}) => Promise<TParser> | TParser) | TParser) => Omit<ActionBuilder<{
|
|
37
44
|
_input: TParser;
|
|
38
45
|
_context: TParams["_context"];
|
|
@@ -41,14 +48,14 @@ interface ActionBuilder<TParams extends ActionParams> {
|
|
|
41
48
|
* Create an action.
|
|
42
49
|
*/
|
|
43
50
|
action: <TOutput>(action: (params: {
|
|
44
|
-
ctx:
|
|
51
|
+
ctx: RemoveUnsetMarker<TParams["_context"]>;
|
|
45
52
|
input: InferInputType<TParams["_input"], "out">;
|
|
46
53
|
}) => Promise<TOutput>) => SanitizeFunctionParam<(input: InferInputType<TParams["_input"], "in">) => Promise<TOutput>>;
|
|
47
54
|
/**
|
|
48
55
|
* Create an action for React `useActionState`
|
|
49
56
|
*/
|
|
50
57
|
stateAction: <TState, TPrevState = UnsetMarker>(action: (params: Prettify<{
|
|
51
|
-
ctx:
|
|
58
|
+
ctx: RemoveUnsetMarker<TParams["_context"]>;
|
|
52
59
|
prevState: RemoveUnsetMarker<TPrevState>;
|
|
53
60
|
rawInput: InferInputType<TParams["_input"], "in">;
|
|
54
61
|
} & ({
|
|
@@ -64,7 +71,7 @@ interface ActionBuilder<TParams extends ActionParams> {
|
|
|
64
71
|
* @deprecated Use `stateAction` instead.
|
|
65
72
|
*/
|
|
66
73
|
formAction: <TState, TPrevState = UnsetMarker>(action: (params: Prettify<{
|
|
67
|
-
ctx:
|
|
74
|
+
ctx: RemoveUnsetMarker<TParams["_context"]>;
|
|
68
75
|
prevState: RemoveUnsetMarker<TPrevState>;
|
|
69
76
|
formData: FormData;
|
|
70
77
|
} & ({
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
2
|
|
|
3
|
+
//#region src/internal/middleware.d.ts
|
|
4
|
+
type MiddlewareFunction<TContext, TReturn> = (params: {
|
|
5
|
+
ctx: TContext;
|
|
6
|
+
}) => Promise<TReturn> | TReturn;
|
|
7
|
+
//#endregion
|
|
3
8
|
//#region src/internal/schema.d.ts
|
|
4
9
|
declare function getInputErrors(issues: ReadonlyArray<StandardSchemaV1.Issue>): {
|
|
5
10
|
messages: string[];
|
|
@@ -15,7 +20,6 @@ type Prettify<T> = { [P in keyof T]: T[P] } & {};
|
|
|
15
20
|
type SanitizeFunctionParam<T extends (param: any) => any> = T extends ((param: infer P) => infer R) ? Equals<P, undefined> extends true ? () => R : Equals<P, P | undefined> extends true ? (param?: P) => R : (param: P) => R : never;
|
|
16
21
|
type InferParserType<T, TType extends "in" | "out"> = T extends StandardSchemaV1 ? TType extends "in" ? StandardSchemaV1.InferInput<T> : StandardSchemaV1.InferOutput<T> : never;
|
|
17
22
|
type InferInputType<T, TType extends "in" | "out"> = T extends UnsetMarker ? undefined : InferParserType<T, TType>;
|
|
18
|
-
type InferContextType<T> = RemoveUnsetMarker<T>;
|
|
19
23
|
interface ActionParams<TInput = unknown, TContext = unknown> {
|
|
20
24
|
_input: TInput;
|
|
21
25
|
_context: TContext;
|
|
@@ -23,16 +27,19 @@ interface ActionParams<TInput = unknown, TContext = unknown> {
|
|
|
23
27
|
interface ActionBuilder<TParams extends ActionParams> {
|
|
24
28
|
/**
|
|
25
29
|
* Middleware allows you to run code before the action, its return value will pass as context to the action.
|
|
30
|
+
*
|
|
31
|
+
* Chaining multiple middlewares is possible, each middleware receives context from previous middlewares
|
|
32
|
+
* and returns additional context that gets merged.
|
|
26
33
|
*/
|
|
27
|
-
middleware: <
|
|
34
|
+
middleware: <TNewContext>(middleware: MiddlewareFunction<RemoveUnsetMarker<TParams["_context"]>, TNewContext>) => ActionBuilder<{
|
|
28
35
|
_input: TParams["_input"];
|
|
29
|
-
_context:
|
|
30
|
-
}
|
|
36
|
+
_context: TParams["_context"] extends UnsetMarker ? TNewContext : Prettify<TParams["_context"] & TNewContext>;
|
|
37
|
+
}>;
|
|
31
38
|
/**
|
|
32
39
|
* Input validation for the action.
|
|
33
40
|
*/
|
|
34
41
|
input: <TParser extends StandardSchemaV1>(input: ((params: {
|
|
35
|
-
ctx:
|
|
42
|
+
ctx: RemoveUnsetMarker<TParams["_context"]>;
|
|
36
43
|
}) => Promise<TParser> | TParser) | TParser) => Omit<ActionBuilder<{
|
|
37
44
|
_input: TParser;
|
|
38
45
|
_context: TParams["_context"];
|
|
@@ -41,14 +48,14 @@ interface ActionBuilder<TParams extends ActionParams> {
|
|
|
41
48
|
* Create an action.
|
|
42
49
|
*/
|
|
43
50
|
action: <TOutput>(action: (params: {
|
|
44
|
-
ctx:
|
|
51
|
+
ctx: RemoveUnsetMarker<TParams["_context"]>;
|
|
45
52
|
input: InferInputType<TParams["_input"], "out">;
|
|
46
53
|
}) => Promise<TOutput>) => SanitizeFunctionParam<(input: InferInputType<TParams["_input"], "in">) => Promise<TOutput>>;
|
|
47
54
|
/**
|
|
48
55
|
* Create an action for React `useActionState`
|
|
49
56
|
*/
|
|
50
57
|
stateAction: <TState, TPrevState = UnsetMarker>(action: (params: Prettify<{
|
|
51
|
-
ctx:
|
|
58
|
+
ctx: RemoveUnsetMarker<TParams["_context"]>;
|
|
52
59
|
prevState: RemoveUnsetMarker<TPrevState>;
|
|
53
60
|
rawInput: InferInputType<TParams["_input"], "in">;
|
|
54
61
|
} & ({
|
|
@@ -64,7 +71,7 @@ interface ActionBuilder<TParams extends ActionParams> {
|
|
|
64
71
|
* @deprecated Use `stateAction` instead.
|
|
65
72
|
*/
|
|
66
73
|
formAction: <TState, TPrevState = UnsetMarker>(action: (params: Prettify<{
|
|
67
|
-
ctx:
|
|
74
|
+
ctx: RemoveUnsetMarker<TParams["_context"]>;
|
|
68
75
|
prevState: RemoveUnsetMarker<TPrevState>;
|
|
69
76
|
formData: FormData;
|
|
70
77
|
} & ({
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
import { SchemaError, getDotPath } from "@standard-schema/utils";
|
|
2
2
|
|
|
3
|
+
//#region src/internal/middleware.ts
|
|
4
|
+
/**
|
|
5
|
+
* Executes an array of middleware functions with the given initial context.
|
|
6
|
+
*/
|
|
7
|
+
async function executeMiddlewares(middlewares, initialCtx) {
|
|
8
|
+
let ctx = initialCtx && typeof initialCtx === "object" ? { ...initialCtx } : {};
|
|
9
|
+
for (const middleware of middlewares) {
|
|
10
|
+
const result = await middleware({ ctx });
|
|
11
|
+
if (result && typeof result === "object") ctx = {
|
|
12
|
+
...ctx,
|
|
13
|
+
...result
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
return ctx;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
3
20
|
//#region src/internal/schema.ts
|
|
4
21
|
async function standardValidate(schema, input) {
|
|
5
22
|
let result = schema["~standard"].validate(input);
|
|
@@ -23,20 +40,19 @@ function getInputErrors(issues) {
|
|
|
23
40
|
|
|
24
41
|
//#endregion
|
|
25
42
|
//#region src/index.ts
|
|
26
|
-
const unsetMarker = Symbol("unsetMarker");
|
|
27
43
|
function createNewServerActionBuilder(def) {
|
|
28
44
|
return createServerActionBuilder(def);
|
|
29
45
|
}
|
|
30
46
|
function createServerActionBuilder(initDef = {}) {
|
|
31
47
|
const _def = {
|
|
32
48
|
input: void 0,
|
|
33
|
-
middleware:
|
|
49
|
+
middleware: [],
|
|
34
50
|
...initDef
|
|
35
51
|
};
|
|
36
52
|
return {
|
|
37
53
|
middleware: (middleware) => createNewServerActionBuilder({
|
|
38
54
|
..._def,
|
|
39
|
-
middleware
|
|
55
|
+
middleware: [..._def.middleware, middleware]
|
|
40
56
|
}),
|
|
41
57
|
input: (input) => createNewServerActionBuilder({
|
|
42
58
|
..._def,
|
|
@@ -44,10 +60,10 @@ function createServerActionBuilder(initDef = {}) {
|
|
|
44
60
|
}),
|
|
45
61
|
action: (action) => {
|
|
46
62
|
return async (input) => {
|
|
47
|
-
|
|
63
|
+
let ctx = {};
|
|
64
|
+
if (_def.middleware.length > 0) ctx = await executeMiddlewares(_def.middleware, ctx);
|
|
48
65
|
if (_def.input) {
|
|
49
|
-
const
|
|
50
|
-
const result = await standardValidate(inputSchema, input);
|
|
66
|
+
const result = await standardValidate(typeof _def.input === "function" ? await _def.input({ ctx }) : _def.input, input);
|
|
51
67
|
if (result.issues) throw new SchemaError(result.issues);
|
|
52
68
|
return await action({
|
|
53
69
|
ctx,
|
|
@@ -62,10 +78,10 @@ function createServerActionBuilder(initDef = {}) {
|
|
|
62
78
|
},
|
|
63
79
|
stateAction: (action) => {
|
|
64
80
|
return async (prevState, rawInput) => {
|
|
65
|
-
|
|
81
|
+
let ctx = {};
|
|
82
|
+
if (_def.middleware.length > 0) ctx = await executeMiddlewares(_def.middleware, ctx);
|
|
66
83
|
if (_def.input) {
|
|
67
|
-
const
|
|
68
|
-
const result = await standardValidate(inputSchema, rawInput);
|
|
84
|
+
const result = await standardValidate(typeof _def.input === "function" ? await _def.input({ ctx }) : _def.input, rawInput);
|
|
69
85
|
if (result.issues) return await action({
|
|
70
86
|
ctx,
|
|
71
87
|
prevState,
|
|
@@ -89,10 +105,10 @@ function createServerActionBuilder(initDef = {}) {
|
|
|
89
105
|
},
|
|
90
106
|
formAction: (action) => {
|
|
91
107
|
return async (prevState, formData) => {
|
|
92
|
-
|
|
108
|
+
let ctx = {};
|
|
109
|
+
if (_def.middleware.length > 0) ctx = await executeMiddlewares(_def.middleware, ctx);
|
|
93
110
|
if (_def.input) {
|
|
94
|
-
const
|
|
95
|
-
const result = await standardValidate(inputSchema, formData);
|
|
111
|
+
const result = await standardValidate(typeof _def.input === "function" ? await _def.input({ ctx }) : _def.input, formData);
|
|
96
112
|
if (result.issues) return await action({
|
|
97
113
|
ctx,
|
|
98
114
|
prevState,
|
|
@@ -99,10 +99,7 @@ function set(obj, path, value) {
|
|
|
99
99
|
*/
|
|
100
100
|
function formDataToObject(formData) {
|
|
101
101
|
const obj = {};
|
|
102
|
-
for (const [key, value] of formData.entries())
|
|
103
|
-
const parts = key.split(/[\.\[\]]/).filter(Boolean);
|
|
104
|
-
set(obj, parts, value);
|
|
105
|
-
}
|
|
102
|
+
for (const [key, value] of formData.entries()) set(obj, key.split(/[.[\]]/).filter(Boolean), value);
|
|
106
103
|
return obj;
|
|
107
104
|
}
|
|
108
105
|
|
package/dist/utils.mjs
CHANGED
|
@@ -98,10 +98,7 @@ function set(obj, path, value) {
|
|
|
98
98
|
*/
|
|
99
99
|
function formDataToObject(formData) {
|
|
100
100
|
const obj = {};
|
|
101
|
-
for (const [key, value] of formData.entries())
|
|
102
|
-
const parts = key.split(/[\.\[\]]/).filter(Boolean);
|
|
103
|
-
set(obj, parts, value);
|
|
104
|
-
}
|
|
101
|
+
for (const [key, value] of formData.entries()) set(obj, key.split(/[.[\]]/).filter(Boolean), value);
|
|
105
102
|
return obj;
|
|
106
103
|
}
|
|
107
104
|
|
package/package.json
CHANGED
|
@@ -1,16 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "server-act",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"action",
|
|
6
|
+
"next",
|
|
7
|
+
"nextjs",
|
|
8
|
+
"react",
|
|
9
|
+
"react server action",
|
|
10
|
+
"react server component",
|
|
11
|
+
"rsc",
|
|
12
|
+
"server action",
|
|
13
|
+
"server component"
|
|
14
|
+
],
|
|
4
15
|
"homepage": "https://github.com/chungweileong94/server-act#readme",
|
|
5
|
-
"
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/chungweileong94/server-act/issues"
|
|
18
|
+
},
|
|
6
19
|
"license": "MIT",
|
|
20
|
+
"author": "chungweileong94",
|
|
7
21
|
"repository": {
|
|
8
22
|
"type": "git",
|
|
9
23
|
"url": "git+https://github.com/chungweileong94/server-act.git"
|
|
10
24
|
},
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"LICENSE",
|
|
28
|
+
"package.json",
|
|
29
|
+
"README.md"
|
|
30
|
+
],
|
|
14
31
|
"exports": {
|
|
15
32
|
".": {
|
|
16
33
|
"types": "./dist/index.d.ts",
|
|
@@ -24,23 +41,6 @@
|
|
|
24
41
|
},
|
|
25
42
|
"./package.json": "./package.json"
|
|
26
43
|
},
|
|
27
|
-
"files": [
|
|
28
|
-
"dist",
|
|
29
|
-
"package.json",
|
|
30
|
-
"LICENSE",
|
|
31
|
-
"README.md"
|
|
32
|
-
],
|
|
33
|
-
"keywords": [
|
|
34
|
-
"next",
|
|
35
|
-
"nextjs",
|
|
36
|
-
"react",
|
|
37
|
-
"react server component",
|
|
38
|
-
"react server action",
|
|
39
|
-
"rsc",
|
|
40
|
-
"server component",
|
|
41
|
-
"server action",
|
|
42
|
-
"action"
|
|
43
|
-
],
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"@standard-schema/spec": "^1.0.0",
|
|
46
46
|
"@standard-schema/utils": "^0.3.0"
|
|
File without changes
|