type MaybeResponse = Response | null; type RouteHandler = (req: Request, url: URL) => Promise; type RouteHandlerWithServer = ( req: Request, url: URL, server: TServer, ) => Promise; type RouteHandlerWithServerDeps = ( req: Request, url: URL, server: TServer, deps: TDeps, ) => Promise; type RouteHandlerWithDeps = ( req: Request, url: URL, deps: TDeps, ) => Promise; type HttpDispatchDeps< TServer, TNotificationsConfig, TBuild, TDeploy, TAutoDeploy, TWinRE, TFileDownload, TPlugin, TFileShare, TMisc, TAssets, TPage, TClient, TWsUpgrade, > = { metrics: { withHttpMetrics: (fn: () => Promise) => Promise }; CORS_HEADERS: Record; handleAuthRoutes: RouteHandlerWithServer; handleNotificationsConfigRoutes: RouteHandlerWithServerDeps; handleAutoScriptsRoutes: RouteHandler; handleAutoDeployRoutes: RouteHandlerWithDeps; handleEnrollmentRoutes: RouteHandler; handleSolRoutes: RouteHandler; handleUsersRoutes: RouteHandlerWithServer; handleBuildRoutes: RouteHandlerWithServerDeps; handleDeployRoutes: RouteHandlerWithServerDeps; handleWinRERoutes: RouteHandlerWithServerDeps; handleFileDownloadRoutes: RouteHandlerWithServerDeps; handlePluginRoutes: RouteHandlerWithDeps; handleFileShareRoutes: RouteHandlerWithDeps; handleMiscRoutes: RouteHandlerWithDeps; handleAssetsRoutes: RouteHandlerWithDeps; handlePageRoutes: RouteHandlerWithDeps; handleClientRoutes: RouteHandlerWithServerDeps; handleWsUpgradeRoutes: RouteHandlerWithServerDeps; routeDeps: { notificationsConfig: TNotificationsConfig; build: TBuild; deploy: TDeploy; autoDeploy: TAutoDeploy; winre: TWinRE; fileDownload: TFileDownload; plugin: TPlugin; fileShare: TFileShare; misc: TMisc; assets: TAssets; page: TPage; client: TClient; wsUpgrade: TWsUpgrade; }; }; export function createHttpFetchHandler< TServer, TNotificationsConfig, TBuild, TDeploy, TAutoDeploy, TWinRE, TFileDownload, TPlugin, TFileShare, TMisc, TAssets, TPage, TClient, TWsUpgrade, >( deps: HttpDispatchDeps< TServer, TNotificationsConfig, TBuild, TDeploy, TAutoDeploy, TWinRE, TFileDownload, TPlugin, TFileShare, TMisc, TAssets, TPage, TClient, TWsUpgrade >, ) { return async function fetchHandler(req: Request, server: unknown): Promise { return deps.metrics.withHttpMetrics(async () => { const routeServer = server as TServer; const url = new URL(req.url); if (req.method === "OPTIONS") { return new Response("", { headers: deps.CORS_HEADERS }); } const authRouteResponse = await deps.handleAuthRoutes(req, url, routeServer); if (authRouteResponse) return authRouteResponse; const notificationsConfigResponse = await deps.handleNotificationsConfigRoutes(req, url, routeServer, deps.routeDeps.notificationsConfig); if (notificationsConfigResponse) return notificationsConfigResponse; const autoScriptsResponse = await deps.handleAutoScriptsRoutes(req, url); if (autoScriptsResponse) return autoScriptsResponse; const autoDeployResponse = await deps.handleAutoDeployRoutes(req, url, deps.routeDeps.autoDeploy); if (autoDeployResponse) return autoDeployResponse; const enrollmentResponse = await deps.handleEnrollmentRoutes(req, url); if (enrollmentResponse) return enrollmentResponse; const solResponse = await deps.handleSolRoutes(req, url); if (solResponse) return solResponse; const usersResponse = await deps.handleUsersRoutes(req, url, routeServer); if (usersResponse) return usersResponse; const buildResponse = await deps.handleBuildRoutes(req, url, routeServer, deps.routeDeps.build); if (buildResponse) return buildResponse; const deployResponse = await deps.handleDeployRoutes(req, url, routeServer, deps.routeDeps.deploy); if (deployResponse) return deployResponse; const winreResponse = await deps.handleWinRERoutes(req, url, routeServer, deps.routeDeps.winre); if (winreResponse) return winreResponse; const fileDownloadResponse = await deps.handleFileDownloadRoutes(req, url, routeServer, deps.routeDeps.fileDownload); if (fileDownloadResponse) return fileDownloadResponse; const pluginResponse = await deps.handlePluginRoutes(req, url, deps.routeDeps.plugin); if (pluginResponse) return pluginResponse; const fileShareResponse = await deps.handleFileShareRoutes(req, url, deps.routeDeps.fileShare); if (fileShareResponse) return fileShareResponse; const miscResponse = await deps.handleMiscRoutes(req, url, deps.routeDeps.misc); if (miscResponse) return miscResponse; const assetsResponse = await deps.handleAssetsRoutes(req, url, deps.routeDeps.assets); if (assetsResponse) return assetsResponse; const pageResponse = await deps.handlePageRoutes(req, url, deps.routeDeps.page); if (pageResponse) return pageResponse; const clientRouteResponse = await deps.handleClientRoutes(req, url, routeServer, deps.routeDeps.client); if (clientRouteResponse) return clientRouteResponse; const wsUpgradeResponse = await deps.handleWsUpgradeRoutes(req, url, routeServer, deps.routeDeps.wsUpgrade); if (wsUpgradeResponse) return wsUpgradeResponse; return new Response("Not found", { status: 404 }); }); }; }