|
|
@@ -41,62 +41,16 @@ export async function createNextI18NextConfig(
|
|
|
);
|
|
|
}
|
|
|
|
|
|
-// Common user and redirect handling
|
|
|
-type RedirectResult = { redirect: { permanent: boolean; destination: string } };
|
|
|
-export function handleUserAndRedirects(context: GetServerSidePropsContext, props: Record<string, unknown>): RedirectResult | null {
|
|
|
- const req = context.req as CrowiRequest;
|
|
|
- const { user } = req;
|
|
|
-
|
|
|
- // Add current user if exists
|
|
|
- if (user != null) {
|
|
|
- props.currentUser = user.toObject();
|
|
|
- }
|
|
|
-
|
|
|
- // Check for redirect destination
|
|
|
- const redirectDestination = props.redirectDestination;
|
|
|
- if (typeof redirectDestination === 'string') {
|
|
|
- return {
|
|
|
- redirect: {
|
|
|
- permanent: false,
|
|
|
- destination: redirectDestination,
|
|
|
- },
|
|
|
- };
|
|
|
- }
|
|
|
-
|
|
|
- return null;
|
|
|
-}
|
|
|
-
|
|
|
-// Helper function to handle page data result with improved type safety
|
|
|
-export function handlePageDataResult<T, U>(
|
|
|
- result: GetServerSidePropsResult<T>,
|
|
|
- currentProps: U,
|
|
|
-): GetServerSidePropsResult<U & T> {
|
|
|
- if ('redirect' in result) {
|
|
|
- return result as GetServerSidePropsResult<U & T>;
|
|
|
- }
|
|
|
- if ('notFound' in result) {
|
|
|
- return result as GetServerSidePropsResult<U & T>;
|
|
|
- }
|
|
|
-
|
|
|
- // Ensure result.props exists and is not a Promise
|
|
|
- if (!('props' in result) || !result.props) {
|
|
|
- throw new Error('Invalid page data result - missing props');
|
|
|
- }
|
|
|
-
|
|
|
- // Type-safe props merging
|
|
|
- return {
|
|
|
- props: {
|
|
|
- ...currentProps,
|
|
|
- ...result.props,
|
|
|
- } as U & T,
|
|
|
- };
|
|
|
-}
|
|
|
-
|
|
|
// Type-safe GetServerSidePropsResult merger with overloads
|
|
|
export function mergeGetServerSidePropsResults<T1, T2>(
|
|
|
result1: GetServerSidePropsResult<T1>,
|
|
|
result2: GetServerSidePropsResult<T2>,
|
|
|
): GetServerSidePropsResult<T1 & T2>;
|
|
|
+export function mergeGetServerSidePropsResults<T1, T2, T3>(
|
|
|
+ result1: GetServerSidePropsResult<T1>,
|
|
|
+ result2: GetServerSidePropsResult<T2>,
|
|
|
+ result3: GetServerSidePropsResult<T3>,
|
|
|
+): GetServerSidePropsResult<T1 & T2 & T3>;
|
|
|
export function mergeGetServerSidePropsResults<T1, T2, T3, T4>(
|
|
|
result1: GetServerSidePropsResult<T1>,
|
|
|
result2: GetServerSidePropsResult<T2>,
|
|
|
@@ -108,7 +62,7 @@ export function mergeGetServerSidePropsResults<T1, T2, T3, T4>(
|
|
|
result2: GetServerSidePropsResult<T2>,
|
|
|
result3?: GetServerSidePropsResult<T3>,
|
|
|
result4?: GetServerSidePropsResult<T4>,
|
|
|
-): GetServerSidePropsResult<T1 & T2> | GetServerSidePropsResult<T1 & T2 & T3 & T4> {
|
|
|
+): GetServerSidePropsResult<T1 & T2> | GetServerSidePropsResult<T1 & T2 & T3> | GetServerSidePropsResult<T1 & T2 & T3 & T4> {
|
|
|
// Handle 2-argument case
|
|
|
if (result3 === undefined && result4 === undefined) {
|
|
|
if ('redirect' in result1) return result1;
|
|
|
@@ -128,34 +82,56 @@ export function mergeGetServerSidePropsResults<T1, T2, T3, T4>(
|
|
|
};
|
|
|
}
|
|
|
|
|
|
- // Handle 4-argument case
|
|
|
- if (result3 === undefined || result4 === undefined) {
|
|
|
- throw new Error('All 4 arguments required for 4-way merge');
|
|
|
+ // Handle 3-argument case
|
|
|
+ if (result4 === undefined && result3 !== undefined) {
|
|
|
+ if ('redirect' in result1) return result1;
|
|
|
+ if ('redirect' in result2) return result2;
|
|
|
+ if ('redirect' in result3) return result3;
|
|
|
+ if ('notFound' in result1) return result1;
|
|
|
+ if ('notFound' in result2) return result2;
|
|
|
+ if ('notFound' in result3) return result3;
|
|
|
+
|
|
|
+ if (!('props' in result1) || !('props' in result2) || !('props' in result3)) {
|
|
|
+ throw new Error('Invalid GetServerSidePropsResult - missing props');
|
|
|
+ }
|
|
|
+
|
|
|
+ return {
|
|
|
+ props: {
|
|
|
+ ...result1.props,
|
|
|
+ ...result2.props,
|
|
|
+ ...result3.props,
|
|
|
+ } as T1 & T2 & T3,
|
|
|
+ };
|
|
|
}
|
|
|
|
|
|
- if ('redirect' in result1) return result1;
|
|
|
- if ('redirect' in result2) return result2;
|
|
|
- if ('redirect' in result3) return result3;
|
|
|
- if ('redirect' in result4) return result4;
|
|
|
+ // Handle 4-argument case
|
|
|
+ if (result3 !== undefined && result4 !== undefined) {
|
|
|
+ if ('redirect' in result1) return result1;
|
|
|
+ if ('redirect' in result2) return result2;
|
|
|
+ if ('redirect' in result3) return result3;
|
|
|
+ if ('redirect' in result4) return result4;
|
|
|
+
|
|
|
+ if ('notFound' in result1) return result1;
|
|
|
+ if ('notFound' in result2) return result2;
|
|
|
+ if ('notFound' in result3) return result3;
|
|
|
+ if ('notFound' in result4) return result4;
|
|
|
|
|
|
- if ('notFound' in result1) return result1;
|
|
|
- if ('notFound' in result2) return result2;
|
|
|
- if ('notFound' in result3) return result3;
|
|
|
- if ('notFound' in result4) return result4;
|
|
|
+ if (!('props' in result1) || !('props' in result2)
|
|
|
+ || !('props' in result3) || !('props' in result4)) {
|
|
|
+ throw new Error('Invalid GetServerSidePropsResult - missing props');
|
|
|
+ }
|
|
|
|
|
|
- if (!('props' in result1) || !('props' in result2)
|
|
|
- || !('props' in result3) || !('props' in result4)) {
|
|
|
- throw new Error('Invalid GetServerSidePropsResult - missing props');
|
|
|
+ return {
|
|
|
+ props: {
|
|
|
+ ...result1.props,
|
|
|
+ ...result2.props,
|
|
|
+ ...result3.props,
|
|
|
+ ...result4.props,
|
|
|
+ } as T1 & T2 & T3 & T4,
|
|
|
+ };
|
|
|
}
|
|
|
|
|
|
- return {
|
|
|
- props: {
|
|
|
- ...result1.props,
|
|
|
- ...result2.props,
|
|
|
- ...result3.props,
|
|
|
- ...result4.props,
|
|
|
- } as T1 & T2 & T3 & T4,
|
|
|
- };
|
|
|
+ throw new Error('Invalid arguments for mergeGetServerSidePropsResults');
|
|
|
}
|
|
|
|
|
|
// Type-safe property extraction helper
|