refactor(server/utils): strengthen typing
mongoose middleware helper functions are now properly typed
This commit is contained in:
		
							parent
							
								
									9dbf0548d7
								
							
						
					
					
						commit
						faaf385bde
					
				| @ -6,6 +6,7 @@ import { GridFSBucketReadStream } from "mongodb"; | |||||||
| import { stripHtml } from "string-strip-html"; | import { stripHtml } from "string-strip-html"; | ||||||
| import { IStory } from "~/models/stories"; | import { IStory } from "~/models/stories"; | ||||||
| import { ficsHidden } from "./server/ficmas"; | import { ficsHidden } from "./server/ficmas"; | ||||||
|  | import { PreMiddlewareFunction, Query } from "mongoose"; | ||||||
| 
 | 
 | ||||||
| // const { encode, decode } = iconv;
 | // const { encode, decode } = iconv;
 | ||||||
| 
 | 
 | ||||||
| @ -13,15 +14,22 @@ export function countWords(string: string) { | |||||||
| 	return stripHtml(string).result.split(/W+/).length; | 	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) { | 	return function (next: () => any) { | ||||||
| 		this.populate(field); | 		this.populate(field, undefined, model); | ||||||
| 		next(); | 		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) { | 	return function (next: () => any) { | ||||||
| 		this.populate(field, selection); | 		this.populate(field, selection, model); | ||||||
| 		next(); | 		next(); | ||||||
| 	}; | 	}; | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user