server-act 1.5.2 → 1.6.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 +1 -1
- package/dist/index.d.mts +52 -52
- package/dist/index.d.ts +52 -52
- package/dist/index.js +109 -180
- package/dist/index.mjs +87 -179
- package/package.json +1 -14
package/README.md
CHANGED
|
@@ -111,7 +111,7 @@ export const sayHelloAction = serverAct
|
|
|
111
111
|
),
|
|
112
112
|
}),
|
|
113
113
|
)
|
|
114
|
-
.
|
|
114
|
+
.stateAction(async ({ formData, input, formErrors, ctx }) => {
|
|
115
115
|
if (formErrors) {
|
|
116
116
|
return { formData, formErrors: formErrors.fieldErrors };
|
|
117
117
|
}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,70 +1,70 @@
|
|
|
1
|
-
import { StandardSchemaV1 } from
|
|
1
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
2
|
|
|
3
|
+
//#region src/utils.d.ts
|
|
3
4
|
declare function getFormErrors(issues: ReadonlyArray<StandardSchemaV1.Issue>): {
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
messages: string[];
|
|
6
|
+
fieldErrors: Record<string, string[]>;
|
|
6
7
|
};
|
|
7
|
-
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/index.d.ts
|
|
8
10
|
declare const unsetMarker: unique symbol;
|
|
9
11
|
type UnsetMarker = typeof unsetMarker;
|
|
10
12
|
type RemoveUnsetMarker<T> = T extends UnsetMarker ? undefined : T;
|
|
11
|
-
type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
|
|
12
|
-
type Prettify<T> = {
|
|
13
|
-
|
|
14
|
-
} & {};
|
|
15
|
-
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;
|
|
13
|
+
type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? true : false;
|
|
14
|
+
type Prettify<T> = { [P in keyof T]: T[P] } & {};
|
|
15
|
+
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
16
|
type InferParserType<T, TType extends "in" | "out"> = T extends StandardSchemaV1 ? TType extends "in" ? StandardSchemaV1.InferInput<T> : StandardSchemaV1.InferOutput<T> : never;
|
|
17
17
|
type InferInputType<T, TType extends "in" | "out"> = T extends UnsetMarker ? undefined : InferParserType<T, TType>;
|
|
18
18
|
type InferContextType<T> = RemoveUnsetMarker<T>;
|
|
19
19
|
interface ActionParams<TInput = unknown, TContext = unknown> {
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
_input: TInput;
|
|
21
|
+
_context: TContext;
|
|
22
22
|
}
|
|
23
23
|
interface ActionBuilder<TParams extends ActionParams> {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Middleware allows you to run code before the action, its return value will pass as context to the action.
|
|
26
|
+
*/
|
|
27
|
+
middleware: <TContext>(middleware: () => Promise<TContext> | TContext) => Omit<ActionBuilder<{
|
|
28
|
+
_input: TParams["_input"];
|
|
29
|
+
_context: TContext;
|
|
30
|
+
}>, "middleware">;
|
|
31
|
+
/**
|
|
32
|
+
* Input validation for the action.
|
|
33
|
+
*/
|
|
34
|
+
input: <TParser extends StandardSchemaV1>(input: ((params: {
|
|
35
|
+
ctx: InferContextType<TParams["_context"]>;
|
|
36
|
+
}) => Promise<TParser> | TParser) | TParser) => Omit<ActionBuilder<{
|
|
37
|
+
_input: TParser;
|
|
38
|
+
_context: TParams["_context"];
|
|
39
|
+
}>, "input">;
|
|
40
|
+
/**
|
|
41
|
+
* Create an action.
|
|
42
|
+
*/
|
|
43
|
+
action: <TOutput>(action: (params: {
|
|
44
|
+
ctx: InferContextType<TParams["_context"]>;
|
|
45
|
+
input: InferInputType<TParams["_input"], "out">;
|
|
46
|
+
}) => Promise<TOutput>) => SanitizeFunctionParam<(input: InferInputType<TParams["_input"], "in">) => Promise<TOutput>>;
|
|
47
|
+
/**
|
|
48
|
+
* Create an action for React `useActionState`
|
|
49
|
+
*/
|
|
50
|
+
stateAction: <TState, TPrevState = UnsetMarker>(action: (params: Prettify<{
|
|
51
|
+
ctx: InferContextType<TParams["_context"]>;
|
|
52
|
+
prevState: RemoveUnsetMarker<TPrevState>;
|
|
53
|
+
formData: FormData;
|
|
54
|
+
} & ({
|
|
55
|
+
input: InferInputType<TParams["_input"], "out">;
|
|
56
|
+
formErrors?: undefined;
|
|
57
|
+
} | {
|
|
58
|
+
input?: undefined;
|
|
59
|
+
formErrors: ReturnType<typeof getFormErrors>;
|
|
60
|
+
})>) => Promise<TState>) => (prevState: TState | RemoveUnsetMarker<TPrevState>, formData: InferInputType<TParams["_input"], "in">) => Promise<TState | RemoveUnsetMarker<TPrevState>>;
|
|
61
61
|
}
|
|
62
62
|
/**
|
|
63
63
|
* Server action builder
|
|
64
64
|
*/
|
|
65
65
|
declare const serverAct: ActionBuilder<{
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
_input: UnsetMarker;
|
|
67
|
+
_context: UnsetMarker;
|
|
68
68
|
}>;
|
|
69
|
-
|
|
70
|
-
export { serverAct };
|
|
69
|
+
//#endregion
|
|
70
|
+
export { serverAct };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,70 +1,70 @@
|
|
|
1
|
-
import { StandardSchemaV1 } from
|
|
1
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
2
|
|
|
3
|
+
//#region src/utils.d.ts
|
|
3
4
|
declare function getFormErrors(issues: ReadonlyArray<StandardSchemaV1.Issue>): {
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
messages: string[];
|
|
6
|
+
fieldErrors: Record<string, string[]>;
|
|
6
7
|
};
|
|
7
|
-
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/index.d.ts
|
|
8
10
|
declare const unsetMarker: unique symbol;
|
|
9
11
|
type UnsetMarker = typeof unsetMarker;
|
|
10
12
|
type RemoveUnsetMarker<T> = T extends UnsetMarker ? undefined : T;
|
|
11
|
-
type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
|
|
12
|
-
type Prettify<T> = {
|
|
13
|
-
|
|
14
|
-
} & {};
|
|
15
|
-
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;
|
|
13
|
+
type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? true : false;
|
|
14
|
+
type Prettify<T> = { [P in keyof T]: T[P] } & {};
|
|
15
|
+
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
16
|
type InferParserType<T, TType extends "in" | "out"> = T extends StandardSchemaV1 ? TType extends "in" ? StandardSchemaV1.InferInput<T> : StandardSchemaV1.InferOutput<T> : never;
|
|
17
17
|
type InferInputType<T, TType extends "in" | "out"> = T extends UnsetMarker ? undefined : InferParserType<T, TType>;
|
|
18
18
|
type InferContextType<T> = RemoveUnsetMarker<T>;
|
|
19
19
|
interface ActionParams<TInput = unknown, TContext = unknown> {
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
_input: TInput;
|
|
21
|
+
_context: TContext;
|
|
22
22
|
}
|
|
23
23
|
interface ActionBuilder<TParams extends ActionParams> {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Middleware allows you to run code before the action, its return value will pass as context to the action.
|
|
26
|
+
*/
|
|
27
|
+
middleware: <TContext>(middleware: () => Promise<TContext> | TContext) => Omit<ActionBuilder<{
|
|
28
|
+
_input: TParams["_input"];
|
|
29
|
+
_context: TContext;
|
|
30
|
+
}>, "middleware">;
|
|
31
|
+
/**
|
|
32
|
+
* Input validation for the action.
|
|
33
|
+
*/
|
|
34
|
+
input: <TParser extends StandardSchemaV1>(input: ((params: {
|
|
35
|
+
ctx: InferContextType<TParams["_context"]>;
|
|
36
|
+
}) => Promise<TParser> | TParser) | TParser) => Omit<ActionBuilder<{
|
|
37
|
+
_input: TParser;
|
|
38
|
+
_context: TParams["_context"];
|
|
39
|
+
}>, "input">;
|
|
40
|
+
/**
|
|
41
|
+
* Create an action.
|
|
42
|
+
*/
|
|
43
|
+
action: <TOutput>(action: (params: {
|
|
44
|
+
ctx: InferContextType<TParams["_context"]>;
|
|
45
|
+
input: InferInputType<TParams["_input"], "out">;
|
|
46
|
+
}) => Promise<TOutput>) => SanitizeFunctionParam<(input: InferInputType<TParams["_input"], "in">) => Promise<TOutput>>;
|
|
47
|
+
/**
|
|
48
|
+
* Create an action for React `useActionState`
|
|
49
|
+
*/
|
|
50
|
+
stateAction: <TState, TPrevState = UnsetMarker>(action: (params: Prettify<{
|
|
51
|
+
ctx: InferContextType<TParams["_context"]>;
|
|
52
|
+
prevState: RemoveUnsetMarker<TPrevState>;
|
|
53
|
+
formData: FormData;
|
|
54
|
+
} & ({
|
|
55
|
+
input: InferInputType<TParams["_input"], "out">;
|
|
56
|
+
formErrors?: undefined;
|
|
57
|
+
} | {
|
|
58
|
+
input?: undefined;
|
|
59
|
+
formErrors: ReturnType<typeof getFormErrors>;
|
|
60
|
+
})>) => Promise<TState>) => (prevState: TState | RemoveUnsetMarker<TPrevState>, formData: InferInputType<TParams["_input"], "in">) => Promise<TState | RemoveUnsetMarker<TPrevState>>;
|
|
61
61
|
}
|
|
62
62
|
/**
|
|
63
63
|
* Server action builder
|
|
64
64
|
*/
|
|
65
65
|
declare const serverAct: ActionBuilder<{
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
_input: UnsetMarker;
|
|
67
|
+
_context: UnsetMarker;
|
|
68
68
|
}>;
|
|
69
|
-
|
|
70
|
-
export { serverAct };
|
|
69
|
+
//#endregion
|
|
70
|
+
export { serverAct };
|
package/dist/index.js
CHANGED
|
@@ -1,192 +1,121 @@
|
|
|
1
|
-
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
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));
|
|
2
22
|
|
|
3
|
-
|
|
23
|
+
//#endregion
|
|
24
|
+
const __standard_schema_utils = __toESM(require("@standard-schema/utils"));
|
|
4
25
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
reject(error);
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
if (info.done) {
|
|
14
|
-
resolve(value);
|
|
15
|
-
} else {
|
|
16
|
-
Promise.resolve(value).then(_next, _throw);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
function _async_to_generator$1(fn) {
|
|
20
|
-
return function() {
|
|
21
|
-
var self = this, args = arguments;
|
|
22
|
-
return new Promise(function(resolve, reject) {
|
|
23
|
-
var gen = fn.apply(self, args);
|
|
24
|
-
function _next(value) {
|
|
25
|
-
asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "next", value);
|
|
26
|
-
}
|
|
27
|
-
function _throw(err) {
|
|
28
|
-
asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "throw", err);
|
|
29
|
-
}
|
|
30
|
-
_next(undefined);
|
|
31
|
-
});
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
function standardValidate(schema, input) {
|
|
35
|
-
return _standardValidate.apply(this, arguments);
|
|
36
|
-
}
|
|
37
|
-
function _standardValidate() {
|
|
38
|
-
_standardValidate = _async_to_generator$1(function*(schema, input) {
|
|
39
|
-
let result = schema["~standard"].validate(input);
|
|
40
|
-
if (result instanceof Promise) result = yield result;
|
|
41
|
-
return result;
|
|
42
|
-
});
|
|
43
|
-
return _standardValidate.apply(this, arguments);
|
|
26
|
+
//#region src/utils.ts
|
|
27
|
+
async function standardValidate(schema, input) {
|
|
28
|
+
let result = schema["~standard"].validate(input);
|
|
29
|
+
if (result instanceof Promise) result = await result;
|
|
30
|
+
return result;
|
|
44
31
|
}
|
|
45
32
|
function getFormErrors(issues) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
} else {
|
|
59
|
-
messages.push(issue.message);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
return {
|
|
63
|
-
messages,
|
|
64
|
-
fieldErrors
|
|
65
|
-
};
|
|
33
|
+
const messages = [];
|
|
34
|
+
const fieldErrors = {};
|
|
35
|
+
for (const issue of issues) {
|
|
36
|
+
const dotPath = (0, __standard_schema_utils.getDotPath)(issue);
|
|
37
|
+
if (dotPath) if (fieldErrors[dotPath]) fieldErrors[dotPath].push(issue.message);
|
|
38
|
+
else fieldErrors[dotPath] = [issue.message];
|
|
39
|
+
else messages.push(issue.message);
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
messages,
|
|
43
|
+
fieldErrors
|
|
44
|
+
};
|
|
66
45
|
}
|
|
67
46
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
var value = info.value;
|
|
72
|
-
} catch (error) {
|
|
73
|
-
reject(error);
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
if (info.done) {
|
|
77
|
-
resolve(value);
|
|
78
|
-
} else {
|
|
79
|
-
Promise.resolve(value).then(_next, _throw);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
function _async_to_generator(fn) {
|
|
83
|
-
return function() {
|
|
84
|
-
var self = this, args = arguments;
|
|
85
|
-
return new Promise(function(resolve, reject) {
|
|
86
|
-
var gen = fn.apply(self, args);
|
|
87
|
-
function _next(value) {
|
|
88
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
89
|
-
}
|
|
90
|
-
function _throw(err) {
|
|
91
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
92
|
-
}
|
|
93
|
-
_next(undefined);
|
|
94
|
-
});
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
function _extends() {
|
|
98
|
-
_extends = Object.assign || function(target) {
|
|
99
|
-
for(var i = 1; i < arguments.length; i++){
|
|
100
|
-
var source = arguments[i];
|
|
101
|
-
for(var key in source){
|
|
102
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
103
|
-
target[key] = source[key];
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
return target;
|
|
108
|
-
};
|
|
109
|
-
return _extends.apply(this, arguments);
|
|
110
|
-
}
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/index.ts
|
|
49
|
+
const unsetMarker = Symbol("unsetMarker");
|
|
111
50
|
function createNewServerActionBuilder(def) {
|
|
112
|
-
|
|
51
|
+
return createServerActionBuilder(def);
|
|
113
52
|
}
|
|
114
53
|
function createServerActionBuilder(initDef = {}) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
return yield action({
|
|
178
|
-
ctx,
|
|
179
|
-
// biome-ignore lint/suspicious/noExplicitAny: It's fine
|
|
180
|
-
prevState: prevState,
|
|
181
|
-
formData,
|
|
182
|
-
input: undefined
|
|
183
|
-
});
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
};
|
|
54
|
+
const _def = {
|
|
55
|
+
input: void 0,
|
|
56
|
+
middleware: void 0,
|
|
57
|
+
...initDef
|
|
58
|
+
};
|
|
59
|
+
return {
|
|
60
|
+
middleware: (middleware) => createNewServerActionBuilder({
|
|
61
|
+
..._def,
|
|
62
|
+
middleware
|
|
63
|
+
}),
|
|
64
|
+
input: (input) => createNewServerActionBuilder({
|
|
65
|
+
..._def,
|
|
66
|
+
input
|
|
67
|
+
}),
|
|
68
|
+
action: (action) => {
|
|
69
|
+
return async (input) => {
|
|
70
|
+
const ctx = await _def.middleware?.();
|
|
71
|
+
if (_def.input) {
|
|
72
|
+
const inputSchema = typeof _def.input === "function" ? await _def.input({ ctx }) : _def.input;
|
|
73
|
+
const result = await standardValidate(inputSchema, input);
|
|
74
|
+
if (result.issues) throw new __standard_schema_utils.SchemaError(result.issues);
|
|
75
|
+
return await action({
|
|
76
|
+
ctx,
|
|
77
|
+
input: result.value
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return await action({
|
|
81
|
+
ctx,
|
|
82
|
+
input: void 0
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
},
|
|
86
|
+
stateAction: (action) => {
|
|
87
|
+
return async (prevState, formData) => {
|
|
88
|
+
const ctx = await _def.middleware?.();
|
|
89
|
+
if (_def.input) {
|
|
90
|
+
const inputSchema = typeof _def.input === "function" ? await _def.input({ ctx }) : _def.input;
|
|
91
|
+
const result = await standardValidate(inputSchema, formData);
|
|
92
|
+
if (result.issues) return await action({
|
|
93
|
+
ctx,
|
|
94
|
+
prevState,
|
|
95
|
+
formData,
|
|
96
|
+
formErrors: getFormErrors(result.issues)
|
|
97
|
+
});
|
|
98
|
+
return await action({
|
|
99
|
+
ctx,
|
|
100
|
+
prevState,
|
|
101
|
+
formData,
|
|
102
|
+
input: result.value
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
return await action({
|
|
106
|
+
ctx,
|
|
107
|
+
prevState,
|
|
108
|
+
formData,
|
|
109
|
+
input: void 0
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
};
|
|
187
114
|
}
|
|
188
115
|
/**
|
|
189
|
-
|
|
190
|
-
|
|
116
|
+
* Server action builder
|
|
117
|
+
*/
|
|
118
|
+
const serverAct = createServerActionBuilder();
|
|
191
119
|
|
|
192
|
-
|
|
120
|
+
//#endregion
|
|
121
|
+
exports.serverAct = serverAct;
|
package/dist/index.mjs
CHANGED
|
@@ -1,190 +1,98 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SchemaError, getDotPath } from "@standard-schema/utils";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
reject(error);
|
|
9
|
-
return;
|
|
10
|
-
}
|
|
11
|
-
if (info.done) {
|
|
12
|
-
resolve(value);
|
|
13
|
-
} else {
|
|
14
|
-
Promise.resolve(value).then(_next, _throw);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
function _async_to_generator$1(fn) {
|
|
18
|
-
return function() {
|
|
19
|
-
var self = this, args = arguments;
|
|
20
|
-
return new Promise(function(resolve, reject) {
|
|
21
|
-
var gen = fn.apply(self, args);
|
|
22
|
-
function _next(value) {
|
|
23
|
-
asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "next", value);
|
|
24
|
-
}
|
|
25
|
-
function _throw(err) {
|
|
26
|
-
asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "throw", err);
|
|
27
|
-
}
|
|
28
|
-
_next(undefined);
|
|
29
|
-
});
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
function standardValidate(schema, input) {
|
|
33
|
-
return _standardValidate.apply(this, arguments);
|
|
34
|
-
}
|
|
35
|
-
function _standardValidate() {
|
|
36
|
-
_standardValidate = _async_to_generator$1(function*(schema, input) {
|
|
37
|
-
let result = schema["~standard"].validate(input);
|
|
38
|
-
if (result instanceof Promise) result = yield result;
|
|
39
|
-
return result;
|
|
40
|
-
});
|
|
41
|
-
return _standardValidate.apply(this, arguments);
|
|
3
|
+
//#region src/utils.ts
|
|
4
|
+
async function standardValidate(schema, input) {
|
|
5
|
+
let result = schema["~standard"].validate(input);
|
|
6
|
+
if (result instanceof Promise) result = await result;
|
|
7
|
+
return result;
|
|
42
8
|
}
|
|
43
9
|
function getFormErrors(issues) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
} else {
|
|
57
|
-
messages.push(issue.message);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return {
|
|
61
|
-
messages,
|
|
62
|
-
fieldErrors
|
|
63
|
-
};
|
|
10
|
+
const messages = [];
|
|
11
|
+
const fieldErrors = {};
|
|
12
|
+
for (const issue of issues) {
|
|
13
|
+
const dotPath = getDotPath(issue);
|
|
14
|
+
if (dotPath) if (fieldErrors[dotPath]) fieldErrors[dotPath].push(issue.message);
|
|
15
|
+
else fieldErrors[dotPath] = [issue.message];
|
|
16
|
+
else messages.push(issue.message);
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
messages,
|
|
20
|
+
fieldErrors
|
|
21
|
+
};
|
|
64
22
|
}
|
|
65
23
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
var value = info.value;
|
|
70
|
-
} catch (error) {
|
|
71
|
-
reject(error);
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
if (info.done) {
|
|
75
|
-
resolve(value);
|
|
76
|
-
} else {
|
|
77
|
-
Promise.resolve(value).then(_next, _throw);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
function _async_to_generator(fn) {
|
|
81
|
-
return function() {
|
|
82
|
-
var self = this, args = arguments;
|
|
83
|
-
return new Promise(function(resolve, reject) {
|
|
84
|
-
var gen = fn.apply(self, args);
|
|
85
|
-
function _next(value) {
|
|
86
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
87
|
-
}
|
|
88
|
-
function _throw(err) {
|
|
89
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
90
|
-
}
|
|
91
|
-
_next(undefined);
|
|
92
|
-
});
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
function _extends() {
|
|
96
|
-
_extends = Object.assign || function(target) {
|
|
97
|
-
for(var i = 1; i < arguments.length; i++){
|
|
98
|
-
var source = arguments[i];
|
|
99
|
-
for(var key in source){
|
|
100
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
101
|
-
target[key] = source[key];
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return target;
|
|
106
|
-
};
|
|
107
|
-
return _extends.apply(this, arguments);
|
|
108
|
-
}
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/index.ts
|
|
26
|
+
const unsetMarker = Symbol("unsetMarker");
|
|
109
27
|
function createNewServerActionBuilder(def) {
|
|
110
|
-
|
|
28
|
+
return createServerActionBuilder(def);
|
|
111
29
|
}
|
|
112
30
|
function createServerActionBuilder(initDef = {}) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
return yield action({
|
|
176
|
-
ctx,
|
|
177
|
-
// biome-ignore lint/suspicious/noExplicitAny: It's fine
|
|
178
|
-
prevState: prevState,
|
|
179
|
-
formData,
|
|
180
|
-
input: undefined
|
|
181
|
-
});
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
};
|
|
31
|
+
const _def = {
|
|
32
|
+
input: void 0,
|
|
33
|
+
middleware: void 0,
|
|
34
|
+
...initDef
|
|
35
|
+
};
|
|
36
|
+
return {
|
|
37
|
+
middleware: (middleware) => createNewServerActionBuilder({
|
|
38
|
+
..._def,
|
|
39
|
+
middleware
|
|
40
|
+
}),
|
|
41
|
+
input: (input) => createNewServerActionBuilder({
|
|
42
|
+
..._def,
|
|
43
|
+
input
|
|
44
|
+
}),
|
|
45
|
+
action: (action) => {
|
|
46
|
+
return async (input) => {
|
|
47
|
+
const ctx = await _def.middleware?.();
|
|
48
|
+
if (_def.input) {
|
|
49
|
+
const inputSchema = typeof _def.input === "function" ? await _def.input({ ctx }) : _def.input;
|
|
50
|
+
const result = await standardValidate(inputSchema, input);
|
|
51
|
+
if (result.issues) throw new SchemaError(result.issues);
|
|
52
|
+
return await action({
|
|
53
|
+
ctx,
|
|
54
|
+
input: result.value
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return await action({
|
|
58
|
+
ctx,
|
|
59
|
+
input: void 0
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
},
|
|
63
|
+
stateAction: (action) => {
|
|
64
|
+
return async (prevState, formData) => {
|
|
65
|
+
const ctx = await _def.middleware?.();
|
|
66
|
+
if (_def.input) {
|
|
67
|
+
const inputSchema = typeof _def.input === "function" ? await _def.input({ ctx }) : _def.input;
|
|
68
|
+
const result = await standardValidate(inputSchema, formData);
|
|
69
|
+
if (result.issues) return await action({
|
|
70
|
+
ctx,
|
|
71
|
+
prevState,
|
|
72
|
+
formData,
|
|
73
|
+
formErrors: getFormErrors(result.issues)
|
|
74
|
+
});
|
|
75
|
+
return await action({
|
|
76
|
+
ctx,
|
|
77
|
+
prevState,
|
|
78
|
+
formData,
|
|
79
|
+
input: result.value
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
return await action({
|
|
83
|
+
ctx,
|
|
84
|
+
prevState,
|
|
85
|
+
formData,
|
|
86
|
+
input: void 0
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
};
|
|
185
91
|
}
|
|
186
92
|
/**
|
|
187
|
-
|
|
188
|
-
|
|
93
|
+
* Server action builder
|
|
94
|
+
*/
|
|
95
|
+
const serverAct = createServerActionBuilder();
|
|
189
96
|
|
|
190
|
-
|
|
97
|
+
//#endregion
|
|
98
|
+
export { serverAct };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "server-act",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"homepage": "https://github.com/chungweileong94/server-act#readme",
|
|
5
5
|
"author": "chungweileong94",
|
|
6
6
|
"license": "MIT",
|
|
@@ -62,18 +62,5 @@
|
|
|
62
62
|
"valibot": {
|
|
63
63
|
"optional": true
|
|
64
64
|
}
|
|
65
|
-
},
|
|
66
|
-
"devDependencies": {
|
|
67
|
-
"bunchee": "^6.2.0",
|
|
68
|
-
"typescript": "^5.6.3",
|
|
69
|
-
"valibot": "1.0.0-rc.0",
|
|
70
|
-
"zod": "^3.24.2",
|
|
71
|
-
"zod-form-data": "^2.0.6"
|
|
72
|
-
},
|
|
73
|
-
"scripts": {
|
|
74
|
-
"build": "bunchee",
|
|
75
|
-
"dev": "bunchee -w",
|
|
76
|
-
"typecheck": "tsc --noEmit",
|
|
77
|
-
"test": "vitest run"
|
|
78
65
|
}
|
|
79
66
|
}
|