server-act 1.2.2 → 1.3.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/dist/index.d.mts CHANGED
@@ -25,7 +25,9 @@ interface ActionBuilder<TParams extends ActionParams> {
25
25
  /**
26
26
  * Input validation for the action.
27
27
  */
28
- input: <TParser extends z.ZodType>(input: TParser) => Omit<ActionBuilder<{
28
+ input: <TParser extends z.ZodType>(input: ((params: {
29
+ ctx: InferContextType<TParams["_context"]>;
30
+ }) => TParser) | TParser) => Omit<ActionBuilder<{
29
31
  _input: TParser;
30
32
  _context: TParams["_context"];
31
33
  }>, "input">;
@@ -37,7 +39,7 @@ interface ActionBuilder<TParams extends ActionParams> {
37
39
  input: InferInputType<TParams["_input"], "out">;
38
40
  }) => Promise<TOutput>) => SanitizeFunctionParam<(input: InferInputType<TParams["_input"], "in">) => Promise<TOutput>>;
39
41
  /**
40
- * Create an action for React `useFormState`
42
+ * Create an action for React `useActionState`
41
43
  */
42
44
  formAction: <TState, TPrevState = undefined>(action: (params: Prettify<{
43
45
  ctx: InferContextType<TParams["_context"]>;
package/dist/index.d.ts CHANGED
@@ -25,7 +25,9 @@ interface ActionBuilder<TParams extends ActionParams> {
25
25
  /**
26
26
  * Input validation for the action.
27
27
  */
28
- input: <TParser extends z.ZodType>(input: TParser) => Omit<ActionBuilder<{
28
+ input: <TParser extends z.ZodType>(input: ((params: {
29
+ ctx: InferContextType<TParams["_context"]>;
30
+ }) => TParser) | TParser) => Omit<ActionBuilder<{
29
31
  _input: TParser;
30
32
  _context: TParams["_context"];
31
33
  }>, "input">;
@@ -37,7 +39,7 @@ interface ActionBuilder<TParams extends ActionParams> {
37
39
  input: InferInputType<TParams["_input"], "out">;
38
40
  }) => Promise<TOutput>) => SanitizeFunctionParam<(input: InferInputType<TParams["_input"], "in">) => Promise<TOutput>>;
39
41
  /**
40
- * Create an action for React `useFormState`
42
+ * Create an action for React `useActionState`
41
43
  */
42
44
  formAction: <TState, TPrevState = undefined>(action: (params: Prettify<{
43
45
  ctx: InferContextType<TParams["_context"]>;
package/dist/index.js CHANGED
@@ -63,15 +63,22 @@ function createServerActionBuilder(initDef = {}) {
63
63
  return /*#__PURE__*/ _async_to_generator(function*(input) {
64
64
  const ctx = yield _def.middleware == null ? void 0 : _def.middleware.call(_def);
65
65
  if (_def.input) {
66
- const result = yield _def.input.safeParseAsync(input);
66
+ const inputSchema = typeof _def.input === "function" ? _def.input({
67
+ ctx
68
+ }) : _def.input;
69
+ const result = yield inputSchema.safeParseAsync(input);
67
70
  if (!result.success) {
68
71
  console.error("❌ Input validation error:", result.error.errors);
69
72
  throw new Error("Input validation error");
70
73
  }
74
+ return yield action({
75
+ ctx,
76
+ input: result.data
77
+ });
71
78
  }
72
79
  return yield action({
73
80
  ctx,
74
- input
81
+ input: undefined
75
82
  });
76
83
  });
77
84
  },
@@ -79,7 +86,10 @@ function createServerActionBuilder(initDef = {}) {
79
86
  return /*#__PURE__*/ _async_to_generator(function*(prevState, formData) {
80
87
  const ctx = yield _def.middleware == null ? void 0 : _def.middleware.call(_def);
81
88
  if (_def.input) {
82
- const result = yield _def.input.safeParseAsync(formData);
89
+ const inputSchema = typeof _def.input === "function" ? _def.input({
90
+ ctx
91
+ }) : _def.input;
92
+ const result = yield inputSchema.safeParseAsync(formData);
83
93
  if (!result.success) {
84
94
  return yield action({
85
95
  ctx,
package/dist/index.mjs CHANGED
@@ -61,15 +61,22 @@ function createServerActionBuilder(initDef = {}) {
61
61
  return /*#__PURE__*/ _async_to_generator(function*(input) {
62
62
  const ctx = yield _def.middleware == null ? void 0 : _def.middleware.call(_def);
63
63
  if (_def.input) {
64
- const result = yield _def.input.safeParseAsync(input);
64
+ const inputSchema = typeof _def.input === "function" ? _def.input({
65
+ ctx
66
+ }) : _def.input;
67
+ const result = yield inputSchema.safeParseAsync(input);
65
68
  if (!result.success) {
66
69
  console.error("❌ Input validation error:", result.error.errors);
67
70
  throw new Error("Input validation error");
68
71
  }
72
+ return yield action({
73
+ ctx,
74
+ input: result.data
75
+ });
69
76
  }
70
77
  return yield action({
71
78
  ctx,
72
- input
79
+ input: undefined
73
80
  });
74
81
  });
75
82
  },
@@ -77,7 +84,10 @@ function createServerActionBuilder(initDef = {}) {
77
84
  return /*#__PURE__*/ _async_to_generator(function*(prevState, formData) {
78
85
  const ctx = yield _def.middleware == null ? void 0 : _def.middleware.call(_def);
79
86
  if (_def.input) {
80
- const result = yield _def.input.safeParseAsync(formData);
87
+ const inputSchema = typeof _def.input === "function" ? _def.input({
88
+ ctx
89
+ }) : _def.input;
90
+ const result = yield inputSchema.safeParseAsync(formData);
81
91
  if (!result.success) {
82
92
  return yield action({
83
93
  ctx,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "server-act",
3
- "version": "1.2.2",
3
+ "version": "1.3.0",
4
4
  "homepage": "https://github.com/chungweileong94/server-act#readme",
5
5
  "author": "chungweileong94",
6
6
  "license": "MIT",