Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
fix createWithServerProps()
Browse files Browse the repository at this point in the history
  • Loading branch information
mkloubert committed Aug 4, 2023
1 parent 5bc0a79 commit 6ae2ccc
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Change Log (@egomobile/next-utils)

## 0.9.1
## 0.9.2

- **BREAKING CHANGE**: improve use of execution contextes of `EnhanceApiContext`

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@egomobile/next-utils",
"version": "0.9.1",
"version": "0.9.2",
"description": "Handy utils and extensions for Next.js",
"main": "lib/index.js",
"engines": {
Expand Down
10 changes: 6 additions & 4 deletions src/middlewares/withServerProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import type { GetServerSideProps, GetServerSidePropsContext, GetServerSidePropsR
import type { Nilable, Nullable } from "../types/internal";
import type { IServerErrorHandlerContext, ServerErrorHandler, ServerMiddleware } from "../types";
import { wrapServerHandler } from "../utils/internal/wrapServerHandler";
import { deepMerge } from "../utils/internal/deepMerge";
import { cloneObj } from "../utils/internal/cloneObj";


/**
Expand Down Expand Up @@ -167,10 +169,10 @@ export function createWithServerProps<TContext = IWithServerPropsActionContext>(

const resultProps = await action(enhanceExecCtx.context as unknown as TContext);

return {
...enhanceExecCtx.result,
...resultProps
};
return deepMerge(
cloneObj(enhanceExecCtx.result),
cloneObj(resultProps)
);
}
catch (ex: any) {
let error = ex;
Expand Down
24 changes: 24 additions & 0 deletions src/utils/internal/cloneObj.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// This file is part of the @egomobile/next-utils distribution.
// Copyright (c) Next.e.GO Mobile SE, Aachen, Germany (https://e-go-mobile.com/)
//
// @egomobile/next-utils is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, version 3.
//
// @egomobile/next-utils is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

export function cloneObj(val: unknown): any {
if (!val) {
return val;
}

return JSON.parse(
JSON.stringify(val)
);
}
32 changes: 32 additions & 0 deletions src/utils/internal/deepMerge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// This file is part of the @egomobile/next-utils distribution.
// Copyright (c) Next.e.GO Mobile SE, Aachen, Germany (https://e-go-mobile.com/)
//
// @egomobile/next-utils is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, version 3.
//
// @egomobile/next-utils is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

export function deepMerge(source: any, target: any): any {
for (const key in source) {
if (typeof source[key] === "object") {
if (typeof target[key] === "object") {
deepMerge(target[key], source[key]);
}
else {
target[key] = source[key];
}
}
else {
target[key] = source[key];
}
}

return target;
}

0 comments on commit 6ae2ccc

Please sign in to comment.