Skip to content

Commit

Permalink
add support for amplify SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-phxm committed Nov 28, 2024
1 parent 752516d commit f232043
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/utils/amplify-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,32 @@ export async function AuthGetCurrentUserServer() {
console.error(error);
}
}

export type ClientType<T> = {
[K in keyof T]: T[K] extends Function
? never
: T[K] extends object
? ClientType<T[K]>
: T[K];
};

/**
* Required to serialize data from server components to client components
*
* Recursively remove functions from an object
*/
export function clientMod<T extends Record<string, any> | any[]>(
obj: T,
): ClientType<T> {
if (Array.isArray(obj)) {
return obj.map((item) => clientMod(item)) as ClientType<T>;
}
const result: Record<string, any> = {};
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<T>;
}

0 comments on commit f232043

Please sign in to comment.