Skip to content

Commit

Permalink
refactor: organizing withAuthSSR error handling (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
boris-w committed Jun 11, 2024
1 parent 992810a commit 130e5b7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 83 deletions.
15 changes: 13 additions & 2 deletions apps/nextjs-app/src/lib/withAuthSSR.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import type { ParsedUrlQuery } from 'querystring';
import type { IHttpError } from '@teable/core';
import type { GetServerSidePropsContext, GetServerSidePropsResult, PreviewData } from 'next';
import type {
GetServerSidePropsContext,
GetServerSidePropsResult,
PreviewData,
GetServerSideProps as NextGetServerSideProps,
} from 'next';
import { SsrApi } from '@/backend/api/rest/table.ssr';

export type GetServerSideProps<
Expand All @@ -16,7 +21,7 @@ export type GetServerSideProps<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export default function withAuthSSR<P extends { [key: string]: any }>(
handler: GetServerSideProps<P>
) {
): NextGetServerSideProps {
return async (context: GetServerSidePropsContext) => {
const req = context.req;
try {
Expand All @@ -42,6 +47,12 @@ export default function withAuthSSR<P extends { [key: string]: any }>(
},
};
}
if (error.status == 404) {
return {
notFound: true,
};
}
console.error(error);
throw error;
}
};
Expand Down
28 changes: 8 additions & 20 deletions apps/nextjs-app/src/pages/base/[baseId]/[tableId].tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { IHttpError } from '@teable/core';
import type { GetServerSideProps } from 'next';
import type { NextPageWithLayout } from '@/lib/type';
import withAuthSSR from '@/lib/withAuthSSR';
Expand All @@ -9,25 +8,14 @@ const Node: NextPageWithLayout = () => {

export const getServerSideProps: GetServerSideProps = withAuthSSR(async (context, ssrApi) => {
const { tableId, baseId, ...queryParams } = context.query;
try {
const queryString = new URLSearchParams(queryParams as Record<string, string>).toString();
const result = await ssrApi.getDefaultViewId(baseId as string, tableId as string);
return {
redirect: {
destination: `/base/${baseId}/${tableId}/${result.id}?${queryString}`,
permanent: false,
},
};
} catch (e) {
const error = e as IHttpError;
if (error.status < 500) {
return {
notFound: true,
};
}
console.error(error);
throw error;
}
const queryString = new URLSearchParams(queryParams as Record<string, string>).toString();
const result = await ssrApi.getDefaultViewId(baseId as string, tableId as string);
return {
redirect: {
destination: `/base/${baseId}/${tableId}/${result.id}?${queryString}`,
permanent: false,
},
};
});

export default Node;
66 changes: 27 additions & 39 deletions apps/nextjs-app/src/pages/base/[baseId]/[tableId]/[viewId].tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { IHttpError } from '@teable/core';
import type { ReactElement } from 'react';
import type { ITableProps } from '@/features/app/blocks/table/Table';
import { Table } from '@/features/app/blocks/table/Table';
Expand Down Expand Up @@ -30,53 +29,42 @@ const Node: NextPageWithLayout<ITableProps> = ({

export const getServerSideProps = withAuthSSR<IViewPageProps>(async (context, ssrApi) => {
const { tableId, viewId, baseId, recordId, fromNotify: notifyId } = context.query;
try {
let recordServerData;
if (recordId) {
if (notifyId) {
await ssrApi.updateNotificationStatus(notifyId as string, { isRead: true });
}
let recordServerData;
if (recordId) {
if (notifyId) {
await ssrApi.updateNotificationStatus(notifyId as string, { isRead: true });
}

recordServerData = await ssrApi.getRecord(tableId as string, recordId as string);
recordServerData = await ssrApi.getRecord(tableId as string, recordId as string);

if (!recordServerData) {
return {
redirect: {
destination: `/base/${baseId}/${tableId}/${viewId}`,
permanent: false,
},
};
}
}
const serverData = await getViewPageServerData(
ssrApi,
baseId as string,
tableId as string,
viewId as string
);
if (serverData) {
const { i18nNamespaces } = tableConfig;
if (!recordServerData) {
return {
props: {
...serverData,
...(recordServerData ? { recordServerData } : {}),
...(await getTranslationsProps(context, i18nNamespaces)),
redirect: {
destination: `/base/${baseId}/${tableId}/${viewId}`,
permanent: false,
},
};
}
}
const serverData = await getViewPageServerData(
ssrApi,
baseId as string,
tableId as string,
viewId as string
);
if (serverData) {
const { i18nNamespaces } = tableConfig;
return {
notFound: true,
props: {
...serverData,
...(recordServerData ? { recordServerData } : {}),
...(await getTranslationsProps(context, i18nNamespaces)),
},
};
} catch (e) {
const error = e as IHttpError;
if (error.status < 500) {
return {
notFound: true,
};
}
console.error(error);
throw error;
}
return {
notFound: true,
};
});

Node.getLayout = function getLayout(page: ReactElement, pageProps: IViewPageProps) {
Expand Down
32 changes: 10 additions & 22 deletions apps/nextjs-app/src/pages/base/[baseId]/[tableId]/design.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { IHttpError } from '@teable/core';
import type { ReactElement } from 'react';
import { Design } from '@/features/app/blocks/design/Design';
import { BaseLayout } from '@/features/app/layouts/BaseLayout';
Expand All @@ -16,30 +15,19 @@ const Node: NextPageWithLayout<IDesignPageProps> = (props) => {

export const getServerSideProps = withAuthSSR<IDesignPageProps>(async (context, ssrApi) => {
const { tableId, baseId } = context.query;
try {
const pageData = await getDesignPageServerData(ssrApi, baseId as string, tableId as string);
if (pageData) {
const { i18nNamespaces } = tableConfig;
return {
props: {
...pageData,
...(await getTranslationsProps(context, i18nNamespaces)),
},
};
}
const pageData = await getDesignPageServerData(ssrApi, baseId as string, tableId as string);
if (pageData) {
const { i18nNamespaces } = tableConfig;
return {
notFound: true,
props: {
...pageData,
...(await getTranslationsProps(context, i18nNamespaces)),
},
};
} catch (e) {
const error = e as IHttpError;
if (error.status < 500) {
return {
notFound: true,
};
}
console.error(error);
throw error;
}
return {
notFound: true,
};
});

Node.getLayout = function getLayout(page: ReactElement, pageProps: IViewPageProps) {
Expand Down

0 comments on commit 130e5b7

Please sign in to comment.