ryoji-s 2 лет назад
Родитель
Сommit
63eaf4f263
2 измененных файлов с 37 добавлено и 6 удалено
  1. 13 2
      apps/app/src/client/services/page-operation.ts
  2. 24 4
      apps/app/src/server/routes/apiv3/pages.js

+ 13 - 2
apps/app/src/client/services/page-operation.ts

@@ -88,7 +88,15 @@ export const resumeRenameOperation = async(pageId: string): Promise<void> => {
 };
 };
 
 
 // TODO: define return type
 // TODO: define return type
-export const createPage = async(pagePath: string, markdown: string, tmpParams: OptionsToSave) => {
+export const createPage = async(
+    pagePath: string,
+    markdown: string,
+    tmpParams: OptionsToSave,
+): Promise<{
+  page: any,
+  tags: any,
+  revision: any
+} | Record<string, never>> => {
   // clone
   // clone
   const params = Object.assign(tmpParams, {
   const params = Object.assign(tmpParams, {
     path: pagePath,
     path: pagePath,
@@ -98,7 +106,10 @@ export const createPage = async(pagePath: string, markdown: string, tmpParams: O
   const res = await apiv3Post('/pages/', params);
   const res = await apiv3Post('/pages/', params);
   const { page, tags, revision } = res.data;
   const { page, tags, revision } = res.data;
 
 
-  return { page, tags, revision };
+  if (page && tags && revision) {
+    return { page, tags, revision };
+  }
+  return {};
 };
 };
 
 
 // TODO: define return type
 // TODO: define return type

+ 24 - 4
apps/app/src/server/routes/apiv3/pages.js

@@ -176,6 +176,7 @@ module.exports = (crowi) => {
       body('slackChannels').if(value => value != null).isString().withMessage('slackChannels must be string'),
       body('slackChannels').if(value => value != null).isString().withMessage('slackChannels must be string'),
       body('pageTags').if(value => value != null).isArray().withMessage('pageTags must be array'),
       body('pageTags').if(value => value != null).isArray().withMessage('pageTags must be array'),
       body('shouldGeneratePath').optional().isBoolean().withMessage('shouldGeneratePath is must be boolean or undefined'),
       body('shouldGeneratePath').optional().isBoolean().withMessage('shouldGeneratePath is must be boolean or undefined'),
+      body('shouldReturnIfPathExists').optional().isBoolean().withMessage('shouldReturnIfPathExists is must be boolean or undefined'),
     ],
     ],
     renamePage: [
     renamePage: [
       body('pageId').isMongoId().withMessage('pageId is required'),
       body('pageId').isMongoId().withMessage('pageId is required'),
@@ -239,11 +240,15 @@ module.exports = (crowi) => {
     return [];
     return [];
   }
   }
 
 
-  async function generateUniquePath(basePath, index = 1) {
+  async function checkIsPathExists(path) {
     const Page = mongoose.model('Page');
     const Page = mongoose.model('Page');
-    const path = basePath + index;
     const response = await Page.findByPath(path);
     const response = await Page.findByPath(path);
-    const isPathExists = response != null;
+    return response != null;
+  }
+
+  async function generateUniquePath(basePath, index = 1) {
+    const path = basePath + index;
+    const isPathExists = await checkIsPathExists(path);
     if (isPathExists) {
     if (isPathExists) {
       return generateUniquePath(basePath, index + 1);
       return generateUniquePath(basePath, index + 1);
     }
     }
@@ -281,6 +286,9 @@ module.exports = (crowi) => {
    *                  shouldGeneratePath:
    *                  shouldGeneratePath:
    *                    type: boolean
    *                    type: boolean
    *                    description: Determine whether a new path should be generated
    *                    description: Determine whether a new path should be generated
+   *                  shouldReturnIfPathExists:
+   *                    type: boolean
+   *                    description: Determine whether a should return response if path exists
    *                required:
    *                required:
    *                  - body
    *                  - body
    *                  - path
    *                  - path
@@ -307,7 +315,7 @@ module.exports = (crowi) => {
    */
    */
   router.post('/', accessTokenParser, loginRequiredStrictly, excludeReadOnlyUser, addActivity, validator.createPage, apiV3FormValidator, async(req, res) => {
   router.post('/', accessTokenParser, loginRequiredStrictly, excludeReadOnlyUser, addActivity, validator.createPage, apiV3FormValidator, async(req, res) => {
     const {
     const {
-      body, grant, grantUserGroupId, overwriteScopesOfDescendants, isSlackEnabled, slackChannels, pageTags, shouldGeneratePath,
+      body, grant, grantUserGroupId, overwriteScopesOfDescendants, isSlackEnabled, slackChannels, pageTags, shouldGeneratePath, shouldReturnIfPathExists,
     } = req.body;
     } = req.body;
 
 
     let { path } = req.body;
     let { path } = req.body;
@@ -315,6 +323,18 @@ module.exports = (crowi) => {
     // check whether path starts slash
     // check whether path starts slash
     path = addHeadingSlash(path);
     path = addHeadingSlash(path);
 
 
+    if (shouldReturnIfPathExists) {
+      try {
+        const isPathExists = await checkIsPathExists(path);
+        if (isPathExists) {
+          return res.apiv3({}, 200);
+        }
+      }
+      catch (err) {
+        return res.apiv3Err(err);
+      }
+    }
+
     if (shouldGeneratePath) {
     if (shouldGeneratePath) {
       try {
       try {
         const rootPath = '/';
         const rootPath = '/';