From 6ae2ccc95174628485424551e7d12413e590f40f Mon Sep 17 00:00:00 2001 From: Marcel Kloubert Date: Fri, 4 Aug 2023 16:20:00 +0200 Subject: [PATCH] fix createWithServerProps() --- CHANGELOG.md | 2 +- package-lock.json | 4 ++-- package.json | 2 +- src/middlewares/withServerProps.ts | 10 ++++++---- src/utils/internal/cloneObj.ts | 24 ++++++++++++++++++++++ src/utils/internal/deepMerge.ts | 32 ++++++++++++++++++++++++++++++ 6 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 src/utils/internal/cloneObj.ts create mode 100644 src/utils/internal/deepMerge.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bb29b7..b942857 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Change Log (@egomobile/next-utils) -## 0.9.1 +## 0.9.2 - **BREAKING CHANGE**: improve use of execution contextes of `EnhanceApiContext` diff --git a/package-lock.json b/package-lock.json index 1ca7b50..b81c201 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@egomobile/next-utils", - "version": "0.9.1", + "version": "0.9.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@egomobile/next-utils", - "version": "0.9.1", + "version": "0.9.2", "license": "LGPL-3.0", "dependencies": { "@egomobile/api-utils": "^1.2.0", diff --git a/package.json b/package.json index e6b3101..7fa301d 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/middlewares/withServerProps.ts b/src/middlewares/withServerProps.ts index 9b5a613..54a8417 100644 --- a/src/middlewares/withServerProps.ts +++ b/src/middlewares/withServerProps.ts @@ -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"; /** @@ -167,10 +169,10 @@ export function createWithServerProps( 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; diff --git a/src/utils/internal/cloneObj.ts b/src/utils/internal/cloneObj.ts new file mode 100644 index 0000000..878c744 --- /dev/null +++ b/src/utils/internal/cloneObj.ts @@ -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 . + +export function cloneObj(val: unknown): any { + if (!val) { + return val; + } + + return JSON.parse( + JSON.stringify(val) + ); +} diff --git a/src/utils/internal/deepMerge.ts b/src/utils/internal/deepMerge.ts new file mode 100644 index 0000000..9cfbe12 --- /dev/null +++ b/src/utils/internal/deepMerge.ts @@ -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 . + +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; +}