Skip to content

Commit

Permalink
refactor(middleware): use deepmerge-ts to merge context object
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Aug 13, 2024
1 parent 0a13fa9 commit b18bf3c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 27 deletions.
1 change: 1 addition & 0 deletions packages/next-safe-action/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"@types/node": "^20.14.11",
"@types/react": "^18.3.1",
"@types/react-dom": "18.3.0",
"deepmerge-ts": "^7.1.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-define-config": "^2.1.0",
Expand Down
5 changes: 3 additions & 2 deletions packages/next-safe-action/src/action-builder.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { deepmerge } from "deepmerge-ts";
import { isNotFoundError } from "next/dist/client/components/not-found.js";
import { isRedirectError } from "next/dist/client/components/redirect.js";
import type {} from "zod";
Expand All @@ -13,7 +14,7 @@ import type {
ServerCodeFn,
StateServerCodeFn,
} from "./index.types";
import { ActionMetadataError, deepMerge, DEFAULT_SERVER_ERROR_MESSAGE, isError } from "./utils";
import { ActionMetadataError, DEFAULT_SERVER_ERROR_MESSAGE, isError } from "./utils";
import { ActionServerValidationError, ActionValidationError, buildValidationErrors } from "./validation-errors";
import type {
BindArgsValidationErrors,
Expand Down Expand Up @@ -121,7 +122,7 @@ export function actionBuilder<
ctx: currentCtx,
metadata: args.metadata,
next: async (nextOpts) => {
currentCtx = deepMerge(currentCtx, nextOpts?.ctx ?? {});
currentCtx = deepmerge(currentCtx, nextOpts?.ctx ?? {});
// currentCtx = { ...cloneDeep(currentCtx), ...(nextOpts?.ctx ?? {}) };
await executeMiddlewareStack(idx + 1);
return middlewareResult;
Expand Down
16 changes: 0 additions & 16 deletions packages/next-safe-action/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,6 @@ export const DEFAULT_SERVER_ERROR_MESSAGE = "Something went wrong while executin

export const isError = (error: unknown): error is Error => error instanceof Error;

export const deepMerge = (obj1: object, obj2: object) => {
for (const key of Object.keys(obj2)) {
const k = key as keyof typeof obj2;
// eslint-disable-next-line
if (typeof obj2[k] === "object" && Object.hasOwn(obj1, k)) {
// @ts-expect-error
if (!obj1[k]) obj1[k] = {};
deepMerge(obj1[k], obj2[k]);
} else {
obj1[k] = obj2[k];
}
}

return obj1;
};

/**
* This error is thrown when an action's metadata input is invalid, i.e. when there's a mismatch between the
* type of the metadata schema returned from `defineMetadataSchema` and the actual input.
Expand Down
27 changes: 18 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b18bf3c

Please sign in to comment.