Skip to content

Commit

Permalink
refactor: remove clone method
Browse files Browse the repository at this point in the history
`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`.
  • Loading branch information
TheEdoRan committed Apr 19, 2024
1 parent e8932bb commit 8dc2138
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 28 deletions.
2 changes: 0 additions & 2 deletions packages/example-app/src/lib/safe-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ async function getSessionId() {
}

export const authAction = action
// Clone the base client to extend this one with additional middleware functions.
.clone()
// In this case, context is used for (fake) auth purposes.
.use(async ({ next }) => {
const userId = randomUUID();
Expand Down
15 changes: 1 addition & 14 deletions packages/next-safe-action/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,6 @@ class SafeActionClient<const ServerError, const Ctx = null, const Metadata = nul
this.#handleReturnedServerError = opts.handleReturnedServerError;
}

/**
* Clone the safe action client keeping the same middleware and initialization functions.
* This is used to extend the base client with additional middleware functions.
* @returns {SafeActionClient}
*/
public clone() {
return new SafeActionClient<ServerError, Ctx, Metadata>({
handleReturnedServerError: this.#handleReturnedServerError,
handleServerErrorLog: this.#handleServerErrorLog,
middlewareFns: [...this.#middlewareFns], // copy the middleware stack so we don't mutate it
});
}

/**
* Use a middleware function.
* @param middlewareFn Middleware function
Expand All @@ -71,7 +58,7 @@ class SafeActionClient<const ServerError, const Ctx = null, const Metadata = nul
this.#middlewareFns.push(middlewareFn);

return new SafeActionClient<ServerError, NextCtx, Metadata>({
middlewareFns: this.#middlewareFns,
middlewareFns: [...this.#middlewareFns, middlewareFn],
handleReturnedServerError: this.#handleReturnedServerError,
handleServerErrorLog: this.#handleServerErrorLog,
});
Expand Down
2 changes: 0 additions & 2 deletions website/docs/recipes/extending-base-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export const actionClient = createSafeActionClient().use(async ({ next }) => {
// action server code function. Note that by extending the base client, you don't need to
// redeclare the logging middleware, is will simply be inherited by the new client.
export const authActionClient = actionClient
// Clone the base client to extend this one with additional middleware functions.
.clone()
// In this case, context is used for (fake) auth purposes.
.use(async ({ next }) => {
const session = cookies().get("session")?.value;
Expand Down
8 changes: 0 additions & 8 deletions website/docs/safe-action-client/instance-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ description: List of methods of the safe action client.

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

## `clone`

```typescript
actionClient.clone() => new SafeActionClient()
```

`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.

## `use`

```typescript
Expand Down
2 changes: 0 additions & 2 deletions website/docs/usage/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ const actionClient = createSafeActionClient({
// Note that the same initialization options and middleware functions of the base client
// will also be used for this one.
const authActionClient = actionClient
// Clone the base client so it doesn't get mutated.
.clone()
// Define authorization middleware.
.use(async ({ next }) => {
const session = cookies().get("session")?.value;
Expand Down

0 comments on commit 8dc2138

Please sign in to comment.