From f232043407076d9ef5a4961629a620308ef485cc Mon Sep 17 00:00:00 2001 From: Justin Pham <113923596+justin-phxm@users.noreply.github.com> Date: Wed, 27 Nov 2024 22:08:25 -0700 Subject: [PATCH] add support for amplify SSR --- src/utils/amplify-utils.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/utils/amplify-utils.ts b/src/utils/amplify-utils.ts index 57007763..2bb90406 100644 --- a/src/utils/amplify-utils.ts +++ b/src/utils/amplify-utils.ts @@ -30,3 +30,32 @@ export async function AuthGetCurrentUserServer() { console.error(error); } } + +export type ClientType = { + [K in keyof T]: T[K] extends Function + ? never + : T[K] extends object + ? ClientType + : T[K]; +}; + +/** + * Required to serialize data from server components to client components + * + * Recursively remove functions from an object + */ +export function clientMod | any[]>( + obj: T, +): ClientType { + if (Array.isArray(obj)) { + return obj.map((item) => clientMod(item)) as ClientType; + } + const result: Record = {}; + for (const [key, value] of Object.entries(obj)) { + if (typeof value !== "function") { + result[key] = + value && typeof value === "object" ? clientMod(value) : value; + } + } + return result as ClientType; +}