server-act 1.8.0 → 1.8.2

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.cjs CHANGED
@@ -7,10 +7,10 @@ function normalizeCtx(ctx) {
7
7
  /**
8
8
  * Executes an array of middleware functions with the given initial context.
9
9
  */
10
- async function executeMiddlewares(middlewares, initialCtx) {
10
+ async function executeMiddlewares(middlewares, initialCtx, terminal) {
11
11
  const executeAt = async (index, ctx) => {
12
12
  const entry = middlewares[index];
13
- if (!entry) return ctx;
13
+ if (!entry) return await terminal(ctx);
14
14
  if (entry.kind === "legacy") {
15
15
  const result = await entry.middleware({ ctx });
16
16
  const nextCtx = result && typeof result === "object" ? {
@@ -32,7 +32,7 @@ async function executeMiddlewares(middlewares, initialCtx) {
32
32
  }
33
33
  });
34
34
  if (!nextCalled) throw new Error(".use() middleware must call next()");
35
- return normalizeCtx(result);
35
+ return result;
36
36
  };
37
37
  return await executeAt(0, normalizeCtx(initialCtx));
38
38
  }
@@ -92,73 +92,73 @@ function createServerActionBuilder(initDef = {}) {
92
92
  }),
93
93
  action: (action) => {
94
94
  return async (input) => {
95
- let ctx = {};
96
- if (_def.middleware.length > 0) ctx = await executeMiddlewares(_def.middleware, ctx);
97
- if (_def.input) {
98
- const result = await standardValidate(typeof _def.input === "function" ? await _def.input({ ctx }) : _def.input, input);
99
- if (result.issues) throw new _standard_schema_utils.SchemaError(result.issues);
95
+ return await executeMiddlewares(_def.middleware, {}, async (ctx) => {
96
+ if (_def.input) {
97
+ const result = await standardValidate(typeof _def.input === "function" ? await _def.input({ ctx }) : _def.input, input);
98
+ if (result.issues) throw new _standard_schema_utils.SchemaError(result.issues);
99
+ return await action({
100
+ ctx,
101
+ input: result.value
102
+ });
103
+ }
100
104
  return await action({
101
105
  ctx,
102
- input: result.value
106
+ input: void 0
103
107
  });
104
- }
105
- return await action({
106
- ctx,
107
- input: void 0
108
108
  });
109
109
  };
110
110
  },
111
111
  stateAction: (action) => {
112
112
  return async (prevState, rawInput) => {
113
- let ctx = {};
114
- if (_def.middleware.length > 0) ctx = await executeMiddlewares(_def.middleware, ctx);
115
- if (_def.input) {
116
- const result = await standardValidate(typeof _def.input === "function" ? await _def.input({ ctx }) : _def.input, rawInput);
117
- if (result.issues) return await action({
118
- ctx,
119
- prevState,
120
- rawInput,
121
- inputErrors: getInputErrors(result.issues)
122
- });
113
+ return await executeMiddlewares(_def.middleware, {}, async (ctx) => {
114
+ if (_def.input) {
115
+ const result = await standardValidate(typeof _def.input === "function" ? await _def.input({ ctx }) : _def.input, rawInput);
116
+ if (result.issues) return await action({
117
+ ctx,
118
+ prevState,
119
+ rawInput,
120
+ inputErrors: getInputErrors(result.issues)
121
+ });
122
+ return await action({
123
+ ctx,
124
+ prevState,
125
+ rawInput,
126
+ input: result.value
127
+ });
128
+ }
123
129
  return await action({
124
130
  ctx,
125
131
  prevState,
126
132
  rawInput,
127
- input: result.value
133
+ input: void 0
128
134
  });
129
- }
130
- return await action({
131
- ctx,
132
- prevState,
133
- rawInput,
134
- input: void 0
135
135
  });
136
136
  };
137
137
  },
138
138
  formAction: (action) => {
139
139
  return async (prevState, formData) => {
140
- let ctx = {};
141
- if (_def.middleware.length > 0) ctx = await executeMiddlewares(_def.middleware, ctx);
142
- if (_def.input) {
143
- const result = await standardValidate(typeof _def.input === "function" ? await _def.input({ ctx }) : _def.input, formData);
144
- if (result.issues) return await action({
145
- ctx,
146
- prevState,
147
- formData,
148
- formErrors: getInputErrors(result.issues)
149
- });
140
+ return await executeMiddlewares(_def.middleware, {}, async (ctx) => {
141
+ if (_def.input) {
142
+ const result = await standardValidate(typeof _def.input === "function" ? await _def.input({ ctx }) : _def.input, formData);
143
+ if (result.issues) return await action({
144
+ ctx,
145
+ prevState,
146
+ formData,
147
+ formErrors: getInputErrors(result.issues)
148
+ });
149
+ return await action({
150
+ ctx,
151
+ prevState,
152
+ formData,
153
+ input: result.value
154
+ });
155
+ }
150
156
  return await action({
151
157
  ctx,
152
158
  prevState,
153
159
  formData,
154
- input: result.value
160
+ input: void 0
155
161
  });
156
- }
157
- return await action({
158
- ctx,
159
- prevState,
160
- formData,
161
- input: void 0
162
162
  });
163
163
  };
164
164
  }
package/dist/index.d.cts CHANGED
@@ -5,14 +5,18 @@ type LegacyMiddlewareFunction<TContext, TReturn> = (params: {
5
5
  ctx: TContext;
6
6
  }) => Promise<TReturn> | TReturn;
7
7
  type MiddlewareContext = Record<string, unknown>;
8
- type Prettify$1<T> = { [P in keyof T]: T[P] } & {};
9
- type MiddlewareNextFunction<TContext extends MiddlewareContext> = <TAddedContext extends MiddlewareContext>(opts?: {
8
+ type Awaitable<T> = T | Promise<T>;
9
+ declare const middlewareResultBrand: unique symbol;
10
+ type MiddlewareResult<TAddedContext extends MiddlewareContext = MiddlewareContext> = {
11
+ readonly [middlewareResultBrand]: TAddedContext;
12
+ };
13
+ type MiddlewareNextFunction = <TAddedContext extends MiddlewareContext = {}>(opts?: {
10
14
  ctx?: TAddedContext;
11
- }) => Promise<Prettify$1<TContext & TAddedContext>>;
12
- type UseMiddlewareFunction<TContext extends MiddlewareContext, TNextContext extends MiddlewareContext> = (params: {
15
+ }) => Promise<MiddlewareResult<TAddedContext>>;
16
+ type UseMiddlewareFunction<TContext extends MiddlewareContext, TAddedContext extends MiddlewareContext> = (params: {
13
17
  ctx: TContext;
14
- next: MiddlewareNextFunction<TContext>;
15
- }) => Promise<TNextContext> | TNextContext;
18
+ next: MiddlewareNextFunction;
19
+ }) => Awaitable<MiddlewareResult<TAddedContext>>;
16
20
  //#endregion
17
21
  //#region src/internal/schema.d.ts
18
22
  declare function getInputErrors(issues: ReadonlyArray<StandardSchemaV1.Issue>): {
@@ -48,11 +52,13 @@ interface ActionBuilder<TParams extends ActionParams> {
48
52
  _context: TParams["_context"] extends UnsetMarker ? TNewContext : Prettify<TParams["_context"] & TNewContext>;
49
53
  }>;
50
54
  /**
51
- * tRPC-style middleware that forwards context via `next()`.
55
+ * Registers middleware in the action pipeline.
56
+ * Call `next()` to continue, optionally passing `ctx` to merge additional
57
+ * context for downstream middleware and the action handler.
52
58
  */
53
59
  use: <TNextContext extends Record<string, unknown>>(middleware: UseMiddlewareFunction<NormalizeContext<TParams["_context"]>, TNextContext>) => ActionBuilder<{
54
60
  _input: TParams["_input"];
55
- _context: TNextContext;
61
+ _context: TParams["_context"] extends UnsetMarker ? TNextContext : Prettify<NormalizeContext<TParams["_context"]> & TNextContext>;
56
62
  }>;
57
63
  /**
58
64
  * Input validation for the action.
@@ -101,7 +107,7 @@ interface ActionBuilder<TParams extends ActionParams> {
101
107
  formErrors: ReturnType<typeof getInputErrors>;
102
108
  })>) => Promise<TState>) => (prevState: TState | RemoveUnsetMarker<TPrevState>, formData: InferInputType<TParams["_input"], "in">) => Promise<TState | RemoveUnsetMarker<TPrevState>>;
103
109
  }
104
- declare function createServerActMiddleware<TAddedContext extends Record<string, unknown>, TContext extends Record<string, unknown> = {}>(middleware: UseMiddlewareFunction<TContext, Prettify<TContext & TAddedContext>>): UseMiddlewareFunction<TContext, Prettify<TContext & TAddedContext>>;
110
+ declare function createServerActMiddleware<TAddedContext extends Record<string, unknown>, TContext extends Record<string, unknown> = {}>(middleware: UseMiddlewareFunction<TContext, TAddedContext>): UseMiddlewareFunction<TContext, TAddedContext>;
105
111
  /**
106
112
  * Server action builder
107
113
  */
package/dist/index.d.mts CHANGED
@@ -5,14 +5,18 @@ type LegacyMiddlewareFunction<TContext, TReturn> = (params: {
5
5
  ctx: TContext;
6
6
  }) => Promise<TReturn> | TReturn;
7
7
  type MiddlewareContext = Record<string, unknown>;
8
- type Prettify$1<T> = { [P in keyof T]: T[P] } & {};
9
- type MiddlewareNextFunction<TContext extends MiddlewareContext> = <TAddedContext extends MiddlewareContext>(opts?: {
8
+ type Awaitable<T> = T | Promise<T>;
9
+ declare const middlewareResultBrand: unique symbol;
10
+ type MiddlewareResult<TAddedContext extends MiddlewareContext = MiddlewareContext> = {
11
+ readonly [middlewareResultBrand]: TAddedContext;
12
+ };
13
+ type MiddlewareNextFunction = <TAddedContext extends MiddlewareContext = {}>(opts?: {
10
14
  ctx?: TAddedContext;
11
- }) => Promise<Prettify$1<TContext & TAddedContext>>;
12
- type UseMiddlewareFunction<TContext extends MiddlewareContext, TNextContext extends MiddlewareContext> = (params: {
15
+ }) => Promise<MiddlewareResult<TAddedContext>>;
16
+ type UseMiddlewareFunction<TContext extends MiddlewareContext, TAddedContext extends MiddlewareContext> = (params: {
13
17
  ctx: TContext;
14
- next: MiddlewareNextFunction<TContext>;
15
- }) => Promise<TNextContext> | TNextContext;
18
+ next: MiddlewareNextFunction;
19
+ }) => Awaitable<MiddlewareResult<TAddedContext>>;
16
20
  //#endregion
17
21
  //#region src/internal/schema.d.ts
18
22
  declare function getInputErrors(issues: ReadonlyArray<StandardSchemaV1.Issue>): {
@@ -48,11 +52,13 @@ interface ActionBuilder<TParams extends ActionParams> {
48
52
  _context: TParams["_context"] extends UnsetMarker ? TNewContext : Prettify<TParams["_context"] & TNewContext>;
49
53
  }>;
50
54
  /**
51
- * tRPC-style middleware that forwards context via `next()`.
55
+ * Registers middleware in the action pipeline.
56
+ * Call `next()` to continue, optionally passing `ctx` to merge additional
57
+ * context for downstream middleware and the action handler.
52
58
  */
53
59
  use: <TNextContext extends Record<string, unknown>>(middleware: UseMiddlewareFunction<NormalizeContext<TParams["_context"]>, TNextContext>) => ActionBuilder<{
54
60
  _input: TParams["_input"];
55
- _context: TNextContext;
61
+ _context: TParams["_context"] extends UnsetMarker ? TNextContext : Prettify<NormalizeContext<TParams["_context"]> & TNextContext>;
56
62
  }>;
57
63
  /**
58
64
  * Input validation for the action.
@@ -101,7 +107,7 @@ interface ActionBuilder<TParams extends ActionParams> {
101
107
  formErrors: ReturnType<typeof getInputErrors>;
102
108
  })>) => Promise<TState>) => (prevState: TState | RemoveUnsetMarker<TPrevState>, formData: InferInputType<TParams["_input"], "in">) => Promise<TState | RemoveUnsetMarker<TPrevState>>;
103
109
  }
104
- declare function createServerActMiddleware<TAddedContext extends Record<string, unknown>, TContext extends Record<string, unknown> = {}>(middleware: UseMiddlewareFunction<TContext, Prettify<TContext & TAddedContext>>): UseMiddlewareFunction<TContext, Prettify<TContext & TAddedContext>>;
110
+ declare function createServerActMiddleware<TAddedContext extends Record<string, unknown>, TContext extends Record<string, unknown> = {}>(middleware: UseMiddlewareFunction<TContext, TAddedContext>): UseMiddlewareFunction<TContext, TAddedContext>;
105
111
  /**
106
112
  * Server action builder
107
113
  */
package/dist/index.mjs CHANGED
@@ -6,10 +6,10 @@ function normalizeCtx(ctx) {
6
6
  /**
7
7
  * Executes an array of middleware functions with the given initial context.
8
8
  */
9
- async function executeMiddlewares(middlewares, initialCtx) {
9
+ async function executeMiddlewares(middlewares, initialCtx, terminal) {
10
10
  const executeAt = async (index, ctx) => {
11
11
  const entry = middlewares[index];
12
- if (!entry) return ctx;
12
+ if (!entry) return await terminal(ctx);
13
13
  if (entry.kind === "legacy") {
14
14
  const result = await entry.middleware({ ctx });
15
15
  const nextCtx = result && typeof result === "object" ? {
@@ -31,7 +31,7 @@ async function executeMiddlewares(middlewares, initialCtx) {
31
31
  }
32
32
  });
33
33
  if (!nextCalled) throw new Error(".use() middleware must call next()");
34
- return normalizeCtx(result);
34
+ return result;
35
35
  };
36
36
  return await executeAt(0, normalizeCtx(initialCtx));
37
37
  }
@@ -91,73 +91,73 @@ function createServerActionBuilder(initDef = {}) {
91
91
  }),
92
92
  action: (action) => {
93
93
  return async (input) => {
94
- let ctx = {};
95
- if (_def.middleware.length > 0) ctx = await executeMiddlewares(_def.middleware, ctx);
96
- if (_def.input) {
97
- const result = await standardValidate(typeof _def.input === "function" ? await _def.input({ ctx }) : _def.input, input);
98
- if (result.issues) throw new SchemaError(result.issues);
94
+ return await executeMiddlewares(_def.middleware, {}, async (ctx) => {
95
+ if (_def.input) {
96
+ const result = await standardValidate(typeof _def.input === "function" ? await _def.input({ ctx }) : _def.input, input);
97
+ if (result.issues) throw new SchemaError(result.issues);
98
+ return await action({
99
+ ctx,
100
+ input: result.value
101
+ });
102
+ }
99
103
  return await action({
100
104
  ctx,
101
- input: result.value
105
+ input: void 0
102
106
  });
103
- }
104
- return await action({
105
- ctx,
106
- input: void 0
107
107
  });
108
108
  };
109
109
  },
110
110
  stateAction: (action) => {
111
111
  return async (prevState, rawInput) => {
112
- let ctx = {};
113
- if (_def.middleware.length > 0) ctx = await executeMiddlewares(_def.middleware, ctx);
114
- if (_def.input) {
115
- const result = await standardValidate(typeof _def.input === "function" ? await _def.input({ ctx }) : _def.input, rawInput);
116
- if (result.issues) return await action({
117
- ctx,
118
- prevState,
119
- rawInput,
120
- inputErrors: getInputErrors(result.issues)
121
- });
112
+ return await executeMiddlewares(_def.middleware, {}, async (ctx) => {
113
+ if (_def.input) {
114
+ const result = await standardValidate(typeof _def.input === "function" ? await _def.input({ ctx }) : _def.input, rawInput);
115
+ if (result.issues) return await action({
116
+ ctx,
117
+ prevState,
118
+ rawInput,
119
+ inputErrors: getInputErrors(result.issues)
120
+ });
121
+ return await action({
122
+ ctx,
123
+ prevState,
124
+ rawInput,
125
+ input: result.value
126
+ });
127
+ }
122
128
  return await action({
123
129
  ctx,
124
130
  prevState,
125
131
  rawInput,
126
- input: result.value
132
+ input: void 0
127
133
  });
128
- }
129
- return await action({
130
- ctx,
131
- prevState,
132
- rawInput,
133
- input: void 0
134
134
  });
135
135
  };
136
136
  },
137
137
  formAction: (action) => {
138
138
  return async (prevState, formData) => {
139
- let ctx = {};
140
- if (_def.middleware.length > 0) ctx = await executeMiddlewares(_def.middleware, ctx);
141
- if (_def.input) {
142
- const result = await standardValidate(typeof _def.input === "function" ? await _def.input({ ctx }) : _def.input, formData);
143
- if (result.issues) return await action({
144
- ctx,
145
- prevState,
146
- formData,
147
- formErrors: getInputErrors(result.issues)
148
- });
139
+ return await executeMiddlewares(_def.middleware, {}, async (ctx) => {
140
+ if (_def.input) {
141
+ const result = await standardValidate(typeof _def.input === "function" ? await _def.input({ ctx }) : _def.input, formData);
142
+ if (result.issues) return await action({
143
+ ctx,
144
+ prevState,
145
+ formData,
146
+ formErrors: getInputErrors(result.issues)
147
+ });
148
+ return await action({
149
+ ctx,
150
+ prevState,
151
+ formData,
152
+ input: result.value
153
+ });
154
+ }
149
155
  return await action({
150
156
  ctx,
151
157
  prevState,
152
158
  formData,
153
- input: result.value
159
+ input: void 0
154
160
  });
155
- }
156
- return await action({
157
- ctx,
158
- prevState,
159
- formData,
160
- input: void 0
161
161
  });
162
162
  };
163
163
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "server-act",
3
- "version": "1.8.0",
3
+ "version": "1.8.2",
4
4
  "keywords": [
5
5
  "action",
6
6
  "next",