Просмотр исходного кода

improve: enhance argument types for findPageAndMetaDataByViewer function

Shun Miyazawa 3 месяцев назад
Родитель
Сommit
75d15d6a32

+ 2 - 8
apps/app/src/pages/[[...path]]/page-data-props.ts

@@ -159,9 +159,7 @@ export async function getPageDataForInitial(
   const pageWithMeta = await findPageAndMetaDataByViewer(
     pageService,
     pageGrantService,
-    pageId,
-    resolvedPagePath,
-    user,
+    { pageId, path: resolvedPagePath, user },
   );
 
   // Handle URL conversion
@@ -285,11 +283,7 @@ export async function getPageDataForSameRoute(
   const pageWithMetaBasicOnly = await findPageAndMetaDataByViewer(
     pageService,
     pageGrantService,
-    pageId,
-    resolvedPagePath,
-    user,
-    false, // isSharedPage
-    true, // basicOnly
+    { pageId, path: resolvedPagePath, user, basicOnly: true },
   );
 
   const currentPathname = resolveFinalizedPathname(

+ 1 - 4
apps/app/src/pages/share/[[...path]]/page-data-props.ts

@@ -60,10 +60,7 @@ export const getPageDataForInitial = async (
   const pageWithMeta = await findPageAndMetaDataByViewer(
     pageService,
     pageGrantService,
-    pageId,
-    null,
-    undefined, // no user for share link
-    true, // isSharedPage
+    { pageId, path: null, isSharedPage: true },
   );
 
   // not found

+ 7 - 14
apps/app/src/server/routes/apiv3/page/index.ts

@@ -263,14 +263,12 @@ module.exports = (crowi: Crowi) => {
             return res.apiv3Err('ShareLink is not found', 404);
           }
           return respondWithSinglePage(
-            await findPageAndMetaDataByViewer(
-              pageService,
-              pageGrantService,
-              getIdStringForRef(shareLink.relatedPage),
+            await findPageAndMetaDataByViewer(pageService, pageGrantService, {
+              pageId: getIdStringForRef(shareLink.relatedPage),
               path,
               user,
-              true,
-            ),
+              isSharedPage: true,
+            }),
           );
         }
 
@@ -293,13 +291,11 @@ module.exports = (crowi: Crowi) => {
         }
 
         return respondWithSinglePage(
-          await findPageAndMetaDataByViewer(
-            pageService,
-            pageGrantService,
+          await findPageAndMetaDataByViewer(pageService, pageGrantService, {
             pageId,
             path,
             user,
-          ),
+          }),
         );
       } catch (err) {
         logger.error('get-page-failed', err);
@@ -595,10 +591,7 @@ module.exports = (crowi: Crowi) => {
         const { meta } = await findPageAndMetaDataByViewer(
           pageService,
           pageGrantService,
-          pageId,
-          null,
-          user,
-          isSharedPage,
+          { pageId, path: null, user, isSharedPage },
         );
 
         if (isIPageNotFoundInfo(meta)) {

+ 22 - 16
apps/app/src/server/service/page/find-page-and-meta-data-by-viewer.ts

@@ -25,11 +25,13 @@ import type { IPageService } from './page-service';
 export async function findPageAndMetaDataByViewer(
   pageService: IPageService,
   pageGrantService: IPageGrantService,
-  pageId: string | null,
-  path: string | null,
-  user: HydratedDocument<IUser> | undefined,
-  isSharedPage: boolean,
-  basicOnly: true,
+  opts: {
+    pageId: string | null; // either pageId or path must be specified
+    path: string | null; // either pageId or path must be specified
+    user?: HydratedDocument<IUser>;
+    isSharedPage?: boolean;
+    basicOnly: true;
+  },
 ): Promise<
   | IDataWithRequiredMeta<HydratedDocument<PageDocument>, IPageInfoBasic>
   | IDataWithRequiredMeta<null, IPageNotFoundInfo>
@@ -39,11 +41,12 @@ export async function findPageAndMetaDataByViewer(
 export async function findPageAndMetaDataByViewer(
   pageService: IPageService,
   pageGrantService: IPageGrantService,
-  pageId: string | null,
-  path: string | null,
-  user?: HydratedDocument<IUser>,
-  isSharedPage?: boolean,
-  basicOnly?: false,
+  opts: {
+    pageId: string | null; // either pageId or path must be specified
+    path: string | null; // either pageId or path must be specified
+    user?: HydratedDocument<IUser>;
+    isSharedPage?: boolean;
+  },
 ): Promise<
   | IDataWithRequiredMeta<HydratedDocument<PageDocument>, IPageInfoExt>
   | IDataWithRequiredMeta<null, IPageNotFoundInfo>
@@ -53,12 +56,13 @@ export async function findPageAndMetaDataByViewer(
 export async function findPageAndMetaDataByViewer(
   pageService: IPageService,
   pageGrantService: IPageGrantService,
-
-  pageId: string | null, // either pageId or path must be specified
-  path: string | null, // either pageId or path must be specified
-  user?: HydratedDocument<IUser>,
-  isSharedPage = false,
-  basicOnly = false,
+  opts: {
+    pageId: string | null; // either pageId or path must be specified
+    path: string | null; // either pageId or path must be specified
+    user?: HydratedDocument<IUser>;
+    isSharedPage?: boolean;
+    basicOnly?: boolean;
+  },
 ): Promise<
   | IDataWithRequiredMeta<
       HydratedDocument<PageDocument>,
@@ -66,6 +70,8 @@ export async function findPageAndMetaDataByViewer(
     >
   | IDataWithRequiredMeta<null, IPageNotFoundInfo>
 > {
+  const { pageId, path, user, isSharedPage = false, basicOnly = false } = opts;
+
   assert(pageId != null || path != null);
 
   const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>(