[web] Add some types to web module (#27051)

* Update `WebModule` types

* Add `if (module.middleware)` so types are satisfied

GitOrigin-RevId: 875fa2710a65d557037771c3eb76ff3cb0e73429
This commit is contained in:
Antoine Clausse
2025-07-14 11:13:27 +02:00
committed by Copybot
parent 4c03ebe4ee
commit 10f4722641
2 changed files with 28 additions and 6 deletions

View File

@@ -1,3 +1,5 @@
// @ts-check
const fs = require('fs')
const Path = require('path')
const { promisify, callbackify } = require('util')
@@ -6,11 +8,17 @@ const Views = require('./Views')
const _ = require('lodash')
const Metrics = require('@overleaf/metrics')
/** @import { WebModule } from "../../../types/web-module" */
/** @import { RequestHandler } from "express" */
const MODULE_BASE_PATH = Path.join(__dirname, '/../../../modules')
/** @type {WebModule[]} */
const _modules = []
let _modulesLoaded = false
const _hooks = {}
/** @type {Record<string, RequestHandler[]>} */
const _middleware = {}
let _viewIncludes = {}
@@ -38,6 +46,7 @@ async function loadModulesImpl() {
const module = await import(
Path.join(MODULE_BASE_PATH, moduleName, 'index.mjs')
)
/** @type {WebModule & {name: string}} */
const loadedModule = module.default || module
loadedModule.name = moduleName
@@ -161,12 +170,14 @@ function attachHook(name, method) {
async function attachMiddleware() {
for (const module of await modules()) {
for (const middleware in module.middleware || {}) {
const method = module.middleware[middleware]
if (_middleware[middleware] == null) {
_middleware[middleware] = []
if (module.middleware) {
for (const middleware in module.middleware) {
const method = module.middleware[middleware]
if (_middleware[middleware] == null) {
_middleware[middleware] = []
}
_middleware[middleware].push(method)
}
_middleware[middleware].push(method)
}
}
}
@@ -186,6 +197,9 @@ async function fireHook(name, ...args) {
return results
}
/**
* @param {string} name
*/
async function getMiddleware(name) {
// ensure that modules are loaded if we need to call a middleware
if (!_modulesLoaded) {

View File

@@ -1,3 +1,5 @@
import type { RequestHandler } from 'express'
type LinkedFileAgent = {
createLinkedFile: (
projectId: string,
@@ -41,6 +43,11 @@ export type WebModule = {
privateApiRouter?: any,
publicApiRouter?: any
) => void
applyNonCsrfRouter?: (
webRouter: any,
privateApiRouter?: any,
publicApiRouter?: any
) => void
}
nonCsrfRouter?: {
apply: (webRouter: any, privateApiRouter: any, publicApiRouter: any) => void
@@ -49,7 +56,7 @@ export type WebModule = {
[name: string]: (args: any[]) => void
}
middleware?: {
[name: string]: (req: any, res: any, next: any) => void
[name: string]: RequestHandler
}
sessionMiddleware?: (webRouter: any, options: any) => void
start?: () => Promise<void>
@@ -57,4 +64,5 @@ export type WebModule = {
linkedFileAgents?: {
[name: string]: () => LinkedFileAgent
}
viewIncludes?: Record<string, string[]>
}