Explorar el Código

Improved types

Taichi Masuyama hace 4 años
padre
commit
7950ed75d8

+ 13 - 12
packages/app/src/interfaces/page.ts

@@ -2,29 +2,30 @@ import { IUser } from './user';
 import { IRevision } from './revision';
 import { ITag } from './tag';
 
+type Ref<T> = string | T;
+
 export type IPage = {
-  _id?: any,
   path: string,
   status: string,
-  revision: any & IRevision,
-  tags: (any & ITag)[],
-  creator: any & IUser,
+  revision: Ref<IRevision>,
+  tags: Ref<ITag>[],
+  creator: Ref<IUser>,
   createdAt: Date,
   updatedAt: Date,
-  seenUsers: any[],
-  parent: any & IPage,
+  seenUsers: Ref<IUser>[],
+  parent: Ref<IPage>,
   isEmpty: boolean,
   redirectTo: string,
   grant: number,
-  grantedUsers: any,
-  grantedGroup: any,
-  lastUpdateUser: any,
-  liker: any[],
+  grantedUsers: Ref<IUser>[],
+  grantedGroup: Ref<any>,
+  lastUpdateUser: Ref<IUser>,
+  liker: Ref<IUser>[],
   commentCount: number
   slackChannels: string,
   pageIdOnHackmd: string,
-  revisionHackmdSynced: any,
+  revisionHackmdSynced: Ref<IRevision>,
   hasDraftOnHackmd: boolean,
-  deleteUser: any,
+  deleteUser: Ref<IUser>,
   deletedAt: Date,
 }

+ 12 - 10
packages/app/src/server/models/page.ts

@@ -35,9 +35,9 @@ export interface PageDocument extends IPage, Document {}
 export interface PageModel extends Model<PageDocument> {
   createEmptyPagesByPaths(paths: string[]): Promise<void>
   getParentIdAndFillAncestors(path: string): Promise<string | null>
-  findByPathAndViewer(path: string | null, user, userGroups): Promise<IPage[]>
-  findSiblingsByPathAndViewer(path: string | null, user, userGroups): Promise<IPage[]>
-  findAncestorsById(path: string): Promise<IPage[]>
+  findByPathAndViewer(path: string | null, user, userGroups?): Promise<PageDocument[]>
+  findSiblingsByPathAndViewer(path: string | null, user, userGroups?): Promise<PageDocument[]>
+  findAncestorsByPath(path: string): Promise<PageDocument[]>
 }
 
 const ObjectId = mongoose.Schema.Types.ObjectId;
@@ -115,7 +115,7 @@ schema.statics.createEmptyPagesByPaths = async function(paths: string[]): Promis
   }
 };
 
-schema.statics.getParentIdAndFillAncestors = async function(path: string): Promise<string | null> {
+schema.statics.getParentIdAndFillAncestors = async function(path: string): Promise<Schema.Types.ObjectId> {
   const parentPath = nodePath.dirname(path);
 
   const parent = await this.findOne({ path: parentPath }); // find the oldest parent which must always be the true parent
@@ -163,7 +163,7 @@ schema.statics.getParentIdAndFillAncestors = async function(path: string): Promi
   return parentId;
 };
 
-const addViewerCondition = async(queryBuilder: PageQueryBuilder, user, userGroups): Promise<void> => {
+const addViewerCondition = async(queryBuilder: PageQueryBuilder, user, userGroups = null): Promise<void> => {
   let relatedUserGroups = userGroups;
   if (user != null && relatedUserGroups == null) {
     const UserGroupRelation: any = mongoose.model('UserGroupRelation');
@@ -173,7 +173,9 @@ const addViewerCondition = async(queryBuilder: PageQueryBuilder, user, userGroup
   queryBuilder.addConditionToFilteringByViewer(user, relatedUserGroups, true);
 };
 
-schema.statics.findByPathAndViewer = async function(path: string | null, user, userGroups, useFindOne = true): Promise<IPage | IPage[] | null> {
+schema.statics.findByPathAndViewer = async function(
+    path: string | null, user, userGroups = null, useFindOne = true,
+): Promise<PageDocument | PageDocument[] | null> {
   if (path == null) {
     throw new Error('path is required.');
   }
@@ -185,7 +187,7 @@ schema.statics.findByPathAndViewer = async function(path: string | null, user, u
   return queryBuilder.query.exec();
 };
 
-schema.statics.findSiblingsByPathAndViewer = async function(path: string | null, user, userGroups): Promise<IPage[]> {
+schema.statics.findSiblingsByPathAndViewer = async function(path: string | null, user, userGroups): Promise<PageDocument[]> {
   if (path == null) {
     throw new Error('path is required.');
   }
@@ -205,12 +207,12 @@ schema.statics.findSiblingsByPathAndViewer = async function(path: string | null,
   return queryBuilder.query.lean().exec();
 };
 
-schema.statics.findAncestorsByPath = async function(path: string): Promise<IPage[]> {
+schema.statics.findAncestorsByPath = async function(path: string): Promise<PageDocument[]> {
   const ancestorPaths = collectAncestorPaths(path);
 
   // Do not populate
   const queryBuilder = new PageQueryBuilder(this.find());
-  const _ancestors: IPage[] = await queryBuilder
+  const _ancestors: PageDocument[] = await queryBuilder
     .addConditionToListByPathsArray(ancestorPaths)
     .addConditionToSortAncestorPages()
     .query
@@ -218,7 +220,7 @@ schema.statics.findAncestorsByPath = async function(path: string): Promise<IPage
     .exec();
 
   // no same path pages
-  const ancestorsMap: Map<string, IPage> = new Map();
+  const ancestorsMap: Map<string, PageDocument> = new Map();
   _ancestors.forEach(page => ancestorsMap.set(page.path, page));
   const ancestors = Array.from(ancestorsMap.values());
 

+ 6 - 6
packages/app/src/server/routes/apiv3/page-tree.ts

@@ -4,7 +4,7 @@ import express, {
 import { pagePathUtils } from '@growi/core';
 import { query } from 'express-validator';
 
-import { IPage } from '../../../interfaces/page';
+import { PageDocument, PageModel } from '../../models/page';
 import ErrorV3 from '../../models/vo/error-apiv3';
 import loggerFactory from '../../../utils/logger';
 import Crowi from '../../crowi';
@@ -46,13 +46,13 @@ export default (crowi: Crowi): Router => {
   router.get('/pages', accessTokenParser, loginRequiredStrictly, ...validator.getPagesAroundTarget, async(req: AuthorizedRequest, res: ApiV3Response): Promise<any> => {
     const { id, path } = req.query;
 
-    const Page = crowi.model('Page');
+    const Page: PageModel = crowi.model('Page');
 
-    let siblings: IPage[];
-    let ancestors: IPage[];
+    let siblings: PageDocument[];
+    let ancestors: PageDocument[];
     try {
-      siblings = await Page.findSiblingsByPathAndViewer(path, req.user);
-      ancestors = await Page.findAncestorsByPath(path);
+      siblings = await Page.findSiblingsByPathAndViewer(path as string, req.user);
+      ancestors = await Page.findAncestorsByPath(path as string);
     }
     catch (err) {
       logger.error('Error occurred while finding pages.', err);