From faaf385bde004f3825ac9a17435b2d7d78a98713 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Wed, 6 Dec 2023 21:30:22 -0500 Subject: [PATCH] refactor(server/utils): strengthen typing mongoose middleware helper functions are now properly typed --- lib/functions.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/functions.ts b/lib/functions.ts index 42f436a..5298d2b 100644 --- a/lib/functions.ts +++ b/lib/functions.ts @@ -6,6 +6,7 @@ import { GridFSBucketReadStream } from "mongodb"; import { stripHtml } from "string-strip-html"; import { IStory } from "~/models/stories"; import { ficsHidden } from "./server/ficmas"; +import { PreMiddlewareFunction, Query } from "mongoose"; // const { encode, decode } = iconv; @@ -13,15 +14,22 @@ export function countWords(string: string) { return stripHtml(string).result.split(/W+/).length; } -export function populate(field: string) { +export function populate( + field: string, + model: string, +): PreMiddlewareFunction> { return function (next: () => any) { - this.populate(field); + this.populate(field, undefined, model); next(); }; } -export function populateSelected(field: string, selection: string) { +export function populateSelected( + field: string, + model: string, + selection: string, +): PreMiddlewareFunction> { return function (next: () => any) { - this.populate(field, selection); + this.populate(field, selection, model); next(); }; }