Skip to content

Commit d9d326e

Browse files
committedAug 18, 2024··
feat(middleware): make createMiddleware function stable
Rename exported `experimental_createMiddleware` function to `createMiddleware`, making it stable API.
1 parent 4876d2d commit d9d326e

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed
 

‎packages/next-safe-action/src/__tests__/middleware.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import assert from "node:assert";
44
import { test } from "node:test";
55
import { z } from "zod";
66
import {
7+
createMiddleware,
78
createSafeActionClient,
8-
experimental_createMiddleware,
99
formatBindArgsValidationErrors,
1010
formatValidationErrors,
1111
returnValidationErrors,
@@ -397,7 +397,7 @@ test("overridden formatted validation errors in execution result from middleware
397397
});
398398

399399
test("standalone middleware extends context", async () => {
400-
const myMiddleware = experimental_createMiddleware<{ ctx: { foo: string } }>().define(async ({ next }) => {
400+
const myMiddleware = createMiddleware<{ ctx: { foo: string } }>().define(async ({ next }) => {
401401
return next({ ctx: { baz: "qux" } });
402402
});
403403

‎packages/next-safe-action/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
formatValidationErrors,
1111
} from "./validation-errors";
1212

13-
export { createMiddleware as experimental_createMiddleware } from "./middleware";
13+
export { createMiddleware } from "./middleware";
1414
export { ActionMetadataError, DEFAULT_SERVER_ERROR_MESSAGE } from "./utils";
1515
export {
1616
ActionValidationError,

‎website/docs/safe-action-client/middleware.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,17 @@ export const testAction = actionClient
243243
## Create standalone middleware
244244

245245
:::info
246-
Experimental feature
246+
Since version 7.7.0, this API is stable, so it was renamed from `experimental_createMiddleware` to `createMiddleware`.
247247
:::
248248

249-
Starting from version 7.6.0, you can create standalone middleware functions using the built-in `experimental_createMiddleware()` function. It's labeled as experimental because the API could change in the future, but it's perfectly fine to use it, as it's a pretty simple function that just wraps the creation of middleware.
249+
Starting from version 7.6.0, you can create standalone middleware functions using the built-in `createMiddleware()` function.
250250

251251
Thanks to this feature, and the previously mentioned [context extension](#extend-context), you can now define standalone middleware functions and even publish them as packages, if you want to.
252252

253-
Here's how to use `experimental_createMiddleware()`:
253+
Here's how to use `createMiddleware()`:
254254

255255
```typescript title="src/lib/safe-action.ts"
256-
import { createSafeActionClient, experimental_createMiddleware } from "next-safe-action";
256+
import { createSafeActionClient, createMiddleware } from "next-safe-action";
257257
import { z } from "zod";
258258

259259
export const actionClient = createSafeActionClient({
@@ -268,14 +268,14 @@ export const actionClient = createSafeActionClient({
268268
});
269269

270270
// This middleware works with any client.
271-
const myMiddleware1 = experimental_createMiddleware().define(async ({ next }) => {
271+
const myMiddleware1 = createMiddleware().define(async ({ next }) => {
272272
// Do something useful here...
273273
return next({ ctx: { baz: "qux" } });
274274
});
275275

276276
// This middleware works with clients that at minimum have `ctx.foo`, `metadata.actionName`
277277
// and `serverError.message` properties. More information below. *
278-
const myMiddleware2 = experimental_createMiddleware<{
278+
const myMiddleware2 = createMiddleware<{
279279
ctx: { foo: string }; // [1]
280280
metadata: { actionName: string }; // [2]
281281
serverError: { message: string } // [3]
@@ -290,4 +290,4 @@ export const actionClientWithMyMiddleware = actionClient.use(myMiddleware1).use(
290290

291291
An action defined using the `actionClientWithMyMiddleware` will contain `foo`, `baz` and `john` in its context.
292292

293-
\* Note that you can pass, **but not required to**, an object with two generic properties to the `experimental_createMiddleware()` function: `ctx` \[1\], `metadata` \[2\] and `serverError` \[3\]. Those keys are optional, and you should only provide them if you want your middleware to require **at minimum** the shape you passed in as generic. By doing that, following the above example, you can then access `ctx.foo` and `metadata.actionName` in the middleware you're defining, and by awaiting the `next` function you'll see that `serverError` is an object with the `message` property. If you pass a middleware that requires those properties to a client that doesn't have them, you'll get an error in `use()` method.
293+
\* Note that you can pass, **but not required to**, an object with two generic properties to the `createMiddleware()` function: `ctx` \[1\], `metadata` \[2\] and `serverError` \[3\]. Those keys are optional, and you should only provide them if you want your middleware to require **at minimum** the shape you passed in as generic. By doing that, following the above example, you can then access `ctx.foo` and `metadata.actionName` in the middleware you're defining, and by awaiting the `next` function you'll see that `serverError` is an object with the `message` property. If you pass a middleware that requires those properties to a client that doesn't have them, you'll get an error in `use()` method.

‎website/docs/types/infer-types.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ Suppose we have declared this safe action client:
1111

1212
```typescript title="src/lib/safe-action.ts"
1313
import { z } from "zod";
14-
import { createSafeActionClient, experimental_createMiddleware } from "next-safe-action";
14+
import { createSafeActionClient, createMiddleware } from "next-safe-action";
1515
import { getSessionData } from "@/services/auth"
1616

1717
// Here we declare a standalone auth middleware.
18-
export const authMiddleware = experimental_createMiddleware<{
18+
export const authMiddleware = createMiddleware<{
1919
ctx: { sessionToken: string };
2020
metadata: { actionName: string };
2121
}>().define(async ({ ctx, next }) => {

0 commit comments

Comments
 (0)
Please sign in to comment.