Преглед изворни кода

support fetching multiple documents

Yuki Takei пре 3 година
родитељ
комит
97f3b32bd6

+ 14 - 5
packages/app/src/server/routes/apiv3/page-listing.ts

@@ -34,8 +34,11 @@ const validator = {
     query('id').isMongoId(),
     query('id').isMongoId(),
     query('path').isString(),
     query('path').isString(),
   ], 'id or path is required'),
   ], 'id or path is required'),
+  pageIdsOrPathRequired: oneOf([
+    query('pageIds').isArray(),
+    query('path').isString(),
+  ], 'pageIds or path is required'),
   infoParams: [
   infoParams: [
-    query('pageIds').isArray().withMessage('pageIds is required'),
     query('attachBookmarkCount').isBoolean().optional(),
     query('attachBookmarkCount').isBoolean().optional(),
     query('attachShortBody').isBoolean().optional(),
     query('attachShortBody').isBoolean().optional(),
   ],
   ],
@@ -44,7 +47,7 @@ const validator = {
 /*
 /*
  * Routes
  * Routes
  */
  */
-export default (crowi: Crowi): Router => {
+const routerFactory = (crowi: Crowi): Router => {
   const accessTokenParser = require('../../middlewares/access-token-parser')(crowi);
   const accessTokenParser = require('../../middlewares/access-token-parser')(crowi);
   const loginRequired = require('../../middlewares/login-required')(crowi, true);
   const loginRequired = require('../../middlewares/login-required')(crowi, true);
 
 
@@ -101,8 +104,10 @@ export default (crowi: Crowi): Router => {
   });
   });
 
 
   // eslint-disable-next-line max-len
   // eslint-disable-next-line max-len
-  router.get('/info', accessTokenParser, loginRequired, validator.infoParams, apiV3FormValidator, async(req: AuthorizedRequest, res: ApiV3Response) => {
-    const { pageIds, attachBookmarkCount: attachBookmarkCountParam, attachShortBody: attachShortBodyParam } = req.query;
+  router.get('/info', accessTokenParser, loginRequired, validator.pageIdsOrPathRequired, validator.infoParams, apiV3FormValidator, async(req: AuthorizedRequest, res: ApiV3Response) => {
+    const {
+      pageIds, path, attachBookmarkCount: attachBookmarkCountParam, attachShortBody: attachShortBodyParam,
+    } = req.query;
 
 
     const attachBookmarkCount: boolean = attachBookmarkCountParam === 'true';
     const attachBookmarkCount: boolean = attachBookmarkCountParam === 'true';
     const attachShortBody: boolean = attachShortBodyParam === 'true';
     const attachShortBody: boolean = attachShortBodyParam === 'true';
@@ -113,7 +118,9 @@ export default (crowi: Crowi): Router => {
     const pageService: PageService = crowi.pageService!;
     const pageService: PageService = crowi.pageService!;
 
 
     try {
     try {
-      const pages = await Page.findByIdsAndViewer(pageIds as string[], req.user, null, true);
+      const pages = pageIds != null
+        ? await Page.findByIdsAndViewer(pageIds as string[], req.user, null, true)
+        : await Page.findByPathAndViewer(path as string, req.user, null, false, true);
 
 
       const foundIds = pages.map(page => page._id);
       const foundIds = pages.map(page => page._id);
 
 
@@ -157,3 +164,5 @@ export default (crowi: Crowi): Router => {
 
 
   return router;
   return router;
 };
 };
+
+export default routerFactory;

+ 21 - 14
packages/app/src/server/routes/apiv3/page.js

@@ -170,7 +170,8 @@ module.exports = (crowi) => {
   const validator = {
   const validator = {
     getPage: [
     getPage: [
       query('pageId').optional().isString(),
       query('pageId').optional().isString(),
-      query('path').optional().isBoolean(),
+      query('path').optional().isString(),
+      query('findAll').optional().isBoolean(),
     ],
     ],
     likes: [
     likes: [
       body('pageId').isString(),
       body('pageId').isString(),
@@ -246,19 +247,23 @@ module.exports = (crowi) => {
    */
    */
   router.get('/', certifySharedPage, accessTokenParser, loginRequired, validator.getPage, apiV3FormValidator, async(req, res) => {
   router.get('/', certifySharedPage, accessTokenParser, loginRequired, validator.getPage, apiV3FormValidator, async(req, res) => {
     const { user } = req;
     const { user } = req;
-    const { pageId, path } = req.query;
+    const { pageId, path, findAll } = req.query;
 
 
     if (pageId == null && path == null) {
     if (pageId == null && path == null) {
-      return res.apiv3Err(new ErrorV3('Parameter path or pageId is required.', 'invalid-request'));
+      return res.apiv3Err(new ErrorV3('Either parameter of path or pageId is required.', 'invalid-request'));
     }
     }
 
 
     let page;
     let page;
+    let pages;
     try {
     try {
       if (pageId != null) { // prioritized
       if (pageId != null) { // prioritized
         page = await Page.findByIdAndViewer(pageId, user);
         page = await Page.findByIdAndViewer(pageId, user);
       }
       }
+      else if (!findAll) {
+        page = await Page.findByPathAndViewer(path, user, null, true);
+      }
       else {
       else {
-        page = await Page.findByPathAndViewer(path, user);
+        pages = await Page.findByPathAndViewer(path, user, null, false);
       }
       }
     }
     }
     catch (err) {
     catch (err) {
@@ -266,22 +271,24 @@ module.exports = (crowi) => {
       return res.apiv3Err(err, 500);
       return res.apiv3Err(err, 500);
     }
     }
 
 
-    if (page == null) {
+    if (page == null && (pages == null || pages.length === 0)) {
       return res.apiv3Err('Page is not found', 404);
       return res.apiv3Err('Page is not found', 404);
     }
     }
 
 
-    try {
-      page.initLatestRevisionField();
+    if (page != null) {
+      try {
+        page.initLatestRevisionField();
 
 
-      // populate
-      page = await page.populateDataToShowRevision();
-    }
-    catch (err) {
-      logger.error('populate-page-failed', err);
-      return res.apiv3Err(err, 500);
+        // populate
+        page = await page.populateDataToShowRevision();
+      }
+      catch (err) {
+        logger.error('populate-page-failed', err);
+        return res.apiv3Err(err, 500);
+      }
     }
     }
 
 
-    return res.apiv3({ page });
+    return res.apiv3({ page, pages });
   });
   });
 
 
   /**
   /**