refactor(server/utils): strengthen typing

mongoose middleware helper functions are now properly typed
This commit is contained in:
☙◦ The Tablet ❀ GamerGirlandCo ◦❧ 2023-12-06 21:30:22 -05:00
parent 9dbf0548d7
commit faaf385bde
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C

@ -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<T>(
field: string,
model: string,
): PreMiddlewareFunction<Query<T, T>> {
return function (next: () => any) {
this.populate(field);
this.populate(field, undefined, model);
next();
};
}
export function populateSelected(field: string, selection: string) {
export function populateSelected<T>(
field: string,
model: string,
selection: string,
): PreMiddlewareFunction<Query<T, T>> {
return function (next: () => any) {
this.populate(field, selection);
this.populate(field, selection, model);
next();
};
}