Skip to content

Commit 8dc2138

Browse files
committedApr 19, 2024··
refactor: remove clone method
`clone` was used to extend an action client with new middleware functions. Now you can directly use `use` method to extend your action client(s), since it will return a new instance of the action client itself, with previous cloned middleware functions + the one you just passed to `use`.
1 parent e8932bb commit 8dc2138

File tree

5 files changed

+1
-28
lines changed

5 files changed

+1
-28
lines changed
 

‎packages/example-app/src/lib/safe-action.ts

-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ async function getSessionId() {
6262
}
6363

6464
export const authAction = action
65-
// Clone the base client to extend this one with additional middleware functions.
66-
.clone()
6765
// In this case, context is used for (fake) auth purposes.
6866
.use(async ({ next }) => {
6967
const userId = randomUUID();

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

+1-14
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,6 @@ class SafeActionClient<const ServerError, const Ctx = null, const Metadata = nul
4949
this.#handleReturnedServerError = opts.handleReturnedServerError;
5050
}
5151

52-
/**
53-
* Clone the safe action client keeping the same middleware and initialization functions.
54-
* This is used to extend the base client with additional middleware functions.
55-
* @returns {SafeActionClient}
56-
*/
57-
public clone() {
58-
return new SafeActionClient<ServerError, Ctx, Metadata>({
59-
handleReturnedServerError: this.#handleReturnedServerError,
60-
handleServerErrorLog: this.#handleServerErrorLog,
61-
middlewareFns: [...this.#middlewareFns], // copy the middleware stack so we don't mutate it
62-
});
63-
}
64-
6552
/**
6653
* Use a middleware function.
6754
* @param middlewareFn Middleware function
@@ -71,7 +58,7 @@ class SafeActionClient<const ServerError, const Ctx = null, const Metadata = nul
7158
this.#middlewareFns.push(middlewareFn);
7259

7360
return new SafeActionClient<ServerError, NextCtx, Metadata>({
74-
middlewareFns: this.#middlewareFns,
61+
middlewareFns: [...this.#middlewareFns, middlewareFn],
7562
handleReturnedServerError: this.#handleReturnedServerError,
7663
handleServerErrorLog: this.#handleServerErrorLog,
7764
});

‎website/docs/recipes/extending-base-client.md

-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ export const actionClient = createSafeActionClient().use(async ({ next }) => {
2727
// action server code function. Note that by extending the base client, you don't need to
2828
// redeclare the logging middleware, is will simply be inherited by the new client.
2929
export const authActionClient = actionClient
30-
// Clone the base client to extend this one with additional middleware functions.
31-
.clone()
3230
// In this case, context is used for (fake) auth purposes.
3331
.use(async ({ next }) => {
3432
const session = cookies().get("session")?.value;

‎website/docs/safe-action-client/instance-methods.md

-8
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ description: List of methods of the safe action client.
77

88
`createSafeActionClient` creates an instance of the safe action client, which has the following methods:
99

10-
## `clone`
11-
12-
```typescript
13-
actionClient.clone() => new SafeActionClient()
14-
```
15-
16-
`clone` returns a new instance of the safe action client with the same initialization options and middleware functions as the original one. It is used to extend a base client with additional middleware functions. If you don't use `clone` when creating a new client, the middleware function list of the original one will be mutated and extended with the new ones, which is not desirable.
17-
1810
## `use`
1911

2012
```typescript

‎website/docs/usage/middleware.md

-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ const actionClient = createSafeActionClient({
6060
// Note that the same initialization options and middleware functions of the base client
6161
// will also be used for this one.
6262
const authActionClient = actionClient
63-
// Clone the base client so it doesn't get mutated.
64-
.clone()
6563
// Define authorization middleware.
6664
.use(async ({ next }) => {
6765
const session = cookies().get("session")?.value;

0 commit comments

Comments
 (0)
Please sign in to comment.