From 9020b0870cba8e3c0d723ccd3dd76ce2afcc0eb1 Mon Sep 17 00:00:00 2001 From: Yuki Takei Date: Tue, 8 Oct 2024 12:47:54 +0000 Subject: [PATCH] improve typings --- .../server/routes/apiv3/page/update-page.ts | 5 +-- apps/app/src/server/service/page/index.ts | 34 +++++++++---------- .../src/server/service/page/page-service.ts | 8 +++-- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/apps/app/src/server/routes/apiv3/page/update-page.ts b/apps/app/src/server/routes/apiv3/page/update-page.ts index 1c3dbb40974..20c405b3e33 100644 --- a/apps/app/src/server/routes/apiv3/page/update-page.ts +++ b/apps/app/src/server/routes/apiv3/page/update-page.ts @@ -8,6 +8,7 @@ import { isTopPage, isUsersProtectedPages } from '@growi/core/dist/utils/page-pa import type { Request, RequestHandler } from 'express'; import type { ValidationChain } from 'express-validator'; import { body } from 'express-validator'; +import type { HydratedDocument } from 'mongoose'; import mongoose from 'mongoose'; import { SupportedAction, SupportedTargetModel } from '~/interfaces/activity'; @@ -68,7 +69,7 @@ export const updatePageHandlersFactory: UpdatePageHandlersFactory = (crowi) => { ]; - async function postAction(req: UpdatePageRequest, res: ApiV3Response, updatedPage: PageDocument, previousRevision: IRevisionHasId | null) { + async function postAction(req: UpdatePageRequest, res: ApiV3Response, updatedPage: HydratedDocument, previousRevision: IRevisionHasId | null) { // Reflect the updates in ydoc const origin = req.body.origin; if (origin === Origin.View || origin === undefined) { @@ -168,7 +169,7 @@ export const updatePageHandlersFactory: UpdatePageHandlersFactory = (crowi) => { return res.apiv3Err(new ErrorV3('Posted param "revisionId" is outdated.', PageUpdateErrorCode.CONFLICT, undefined, { returnLatestRevision }), 409); } - let updatedPage: PageDocument; + let updatedPage: HydratedDocument; let previousRevision: IRevisionHasId | null; try { const { diff --git a/apps/app/src/server/service/page/index.ts b/apps/app/src/server/service/page/index.ts index 3b5fdc7acb7..6daba7b9639 100644 --- a/apps/app/src/server/service/page/index.ts +++ b/apps/app/src/server/service/page/index.ts @@ -96,14 +96,10 @@ class PageCursorsForDescendantsFactory { private initialCursor: Cursor | never[]; // TODO: wait for mongoose update - private Page: PageModel; - constructor(user: any, rootPage: any, shouldIncludeEmpty: boolean) { this.user = user; this.rootPage = rootPage; this.shouldIncludeEmpty = shouldIncludeEmpty; - - this.Page = mongoose.model('Page') as unknown as PageModel; } // prepare initial cursor @@ -150,9 +146,10 @@ class PageCursorsForDescendantsFactory { return []; } - const { PageQueryBuilder } = this.Page; + const Page = mongoose.model, PageModel>('Page'); + const { PageQueryBuilder } = Page; - const builder = new PageQueryBuilder(this.Page.find(), this.shouldIncludeEmpty); + const builder = new PageQueryBuilder(Page.find(), this.shouldIncludeEmpty); builder.addConditionToFilteringByParentId(page._id); const cursor = builder.query.lean().cursor({ batchSize: BULK_REINDEX_SIZE }) as Cursor; @@ -3653,13 +3650,13 @@ class PageService implements IPageService { // --------- Create --------- - private async preparePageDocumentToCreate(path: string, shouldNew: boolean): Promise { - const Page = mongoose.model('Page') as unknown as PageModel; + private async preparePageDocumentToCreate(path: string, shouldNew: boolean): Promise> { + const Page = mongoose.model, PageModel>('Page'); const emptyPage = await Page.findOne({ path, isEmpty: true }); // Use empty page if exists, if not, create a new page - let page; + let page: HydratedDocument; if (shouldNew) { page = new Page(); } @@ -3773,7 +3770,7 @@ class PageService implements IPageService { * Create a page * Set options.isSynchronously to true to await all process when you want to run this method multiple times at short intervals. */ - async create(_path: string, body: string, user: HasObjectId, options: IOptionsForCreate = {}): Promise { + async create(_path: string, body: string, user: HasObjectId, options: IOptionsForCreate = {}): Promise> { // Switch method const isV5Compatible = this.crowi.configManager.getConfig('crowi', 'app:isV5Compatible'); if (!isV5Compatible) { @@ -3784,7 +3781,7 @@ class PageService implements IPageService { const path: string = generalXssFilter.process(_path); // sanitize path // Retrieve closest ancestor document - const Page = mongoose.model('Page'); + const Page = mongoose.model, PageModel>('Page'); const closestAncestor = await Page.findNonEmptyClosestAncestor(path); // Determine grantData @@ -3902,7 +3899,7 @@ class PageService implements IPageService { * V4 compatible create method */ private async createV4(path, body, user, options: any = {}) { - const Page = mongoose.model('Page') as unknown as PageModel; + const Page = mongoose.model, PageModel>('Page'); const format = options.format || 'markdown'; const grantUserGroupIds = options.grantUserGroupIds || null; @@ -4078,7 +4075,7 @@ class PageService implements IPageService { } async updatePageSubOperation(page, user, exPage, options: IOptionsForUpdate, pageOpId: ObjectIdLike): Promise { - const Page = mongoose.model('Page') as unknown as PageModel; + const Page = mongoose.model, PageModel>('Page'); const currentPage = page; @@ -4143,13 +4140,13 @@ class PageService implements IPageService { } async updatePage( - pageData: PageDocument, + pageData: HydratedDocument, body: string | null, previousBody: string | null, user: IUserHasId, options: IOptionsForUpdate = {}, - ): Promise { - const Page = mongoose.model('Page') as unknown as PageModel; + ): Promise> { + const Page = mongoose.model, PageModel>('Page'); const wasOnTree = pageData.parent != null || isTopPage(pageData.path); const isV5Compatible = this.crowi.configManager.getConfig('crowi', 'app:isV5Compatible'); @@ -4291,8 +4288,9 @@ class PageService implements IPageService { } - async updatePageV4(pageData: PageDocument, body, previousBody, user, options: IOptionsForUpdate = {}): Promise { - const Page = mongoose.model('Page') as unknown as PageModel; + async updatePageV4( + pageData: HydratedDocument, body, previousBody, user, options: IOptionsForUpdate = {}, + ): Promise> { // use the previous data if absent const grant = options.grant || pageData.grant; diff --git a/apps/app/src/server/service/page/page-service.ts b/apps/app/src/server/service/page/page-service.ts index 90e80a517a4..b1f50801410 100644 --- a/apps/app/src/server/service/page/page-service.ts +++ b/apps/app/src/server/service/page/page-service.ts @@ -4,7 +4,7 @@ import type { HasObjectId, IPageInfo, IPageInfoForEntity, IUser, } from '@growi/core'; -import type { Types } from 'mongoose'; +import type { HydratedDocument, Types } from 'mongoose'; import type { IOptionsForCreate, IOptionsForUpdate } from '~/interfaces/page'; import type { PopulatedGrantedGroup } from '~/interfaces/page-grant'; @@ -13,9 +13,11 @@ import type { ObjectIdLike } from '~/server/interfaces/mongoose-utils'; import type { PageDocument } from '~/server/models/page'; export interface IPageService { - create(path: string, body: string, user: HasObjectId, options: IOptionsForCreate): Promise, + create(path: string, body: string, user: HasObjectId, options: IOptionsForCreate): Promise>, forceCreateBySystem(path: string, body: string, options: IOptionsForCreate): Promise, - updatePage(pageData: PageDocument, body: string | null, previousBody: string | null, user: IUser, options: IOptionsForUpdate,): Promise, + updatePage( + pageData: HydratedDocument, body: string | null, previousBody: string | null, user: IUser, options: IOptionsForUpdate + ): Promise>, updateDescendantCountOfAncestors: (pageId: ObjectIdLike, inc: number, shouldIncludeTarget: boolean) => Promise, deleteCompletelyOperation: (pageIds: ObjectIdLike[], pagePaths: string[]) => Promise, getEventEmitter: () => EventEmitter,