diff --git a/api/routes/account.js b/api/routes/account.js index 60a0edf..d904c06 100644 --- a/api/routes/account.js +++ b/api/routes/account.js @@ -1,4 +1,5 @@ const express = require('express') +const router = express.Router() const { body, validationResult } = require('express-validator') const createAuthenticator = require('../middleware/authenticator') const { ApiError } = require('../middleware/error-handler') @@ -14,8 +15,9 @@ const { ApiError } = require('../middleware/error-handler') * POST /token - Generate/regenerate API token * DELETE /token - Revoke API token */ + function createAccountRouter(dependencies) { - const router = express.Router() + // Ensure router is declared before any usage const { authService, userRepository, @@ -24,6 +26,8 @@ function createAccountRouter(dependencies) { config } = dependencies + // All router usage is below this line + // Check if auth is enabled if (!authService || !config.user.authEnabled) { router.all('*', (req, res) => { @@ -291,4 +295,4 @@ function createAccountRouter(dependencies) { return router } -module.exports = createAccountRouter +module.exports = createAccountRouter \ No newline at end of file diff --git a/api/routes/auth.js b/api/routes/auth.js index eec16ef..a04be50 100644 --- a/api/routes/auth.js +++ b/api/routes/auth.js @@ -1,4 +1,5 @@ const express = require('express') +const router = express.Router() const { body, validationResult } = require('express-validator') const { ApiError } = require('../middleware/error-handler') @@ -10,7 +11,6 @@ const { ApiError } = require('../middleware/error-handler') * GET /session - Get current session info */ function createAuthRouter(dependencies) { - const router = express.Router() const { authService, config } = dependencies // Check if auth is enabled @@ -147,4 +147,4 @@ function createAuthRouter(dependencies) { return router } -module.exports = createAuthRouter +module.exports = createAuthRouter \ No newline at end of file diff --git a/api/routes/config.js b/api/routes/config.js index 6351a5a..3f9a1fb 100644 --- a/api/routes/config.js +++ b/api/routes/config.js @@ -1,4 +1,5 @@ const express = require('express') +const router = express.Router() /** * Configuration API Routes (Public) @@ -7,6 +8,9 @@ const express = require('express') * GET /features - Get enabled features */ function createConfigRouter(dependencies) { + // Ensure router is declared before any usage + const router = express.Router() + const { config } = dependencies // API enabled toggle router.use((req, res, next) => { if (!config.apiEnabled) { @@ -14,8 +18,6 @@ function createConfigRouter(dependencies) { } next(); }); - const router = express.Router() - const { config } = dependencies /** * GET /domains - Get allowed email domains diff --git a/api/routes/inbox.js b/api/routes/inbox.js index 84fc123..df67e61 100644 --- a/api/routes/inbox.js +++ b/api/routes/inbox.js @@ -1,4 +1,5 @@ const express = require('express') +const router = express.Router() const createAuthenticator = require('../middleware/authenticator') /** @@ -9,7 +10,6 @@ const createAuthenticator = require('../middleware/authenticator') * GET /:address/:uid/attachment/:checksum - Download attachment */ function createInboxRouter(dependencies) { - const router = express.Router() const { mailProcessingService, apiTokenRepository } = dependencies const { optionalAuth } = createAuthenticator(apiTokenRepository) @@ -138,4 +138,4 @@ function createInboxRouter(dependencies) { return router } -module.exports = createInboxRouter +module.exports = createInboxRouter \ No newline at end of file diff --git a/api/routes/locks.js b/api/routes/locks.js index 32da828..eea682a 100644 --- a/api/routes/locks.js +++ b/api/routes/locks.js @@ -1,4 +1,5 @@ const express = require('express') +const router = express.Router() const { body, validationResult } = require('express-validator') const createAuthenticator = require('../middleware/authenticator') @@ -10,8 +11,7 @@ const createAuthenticator = require('../middleware/authenticator') * GET /:address/status - Check if inbox is locked */ function createLocksRouter(dependencies) { - const router = express.Router() - const { inboxLock, apiTokenRepository, config } = dependencies + const { inboxLock, userRepository, apiTokenRepository, config } = dependencies if (!inboxLock || !config.user.authEnabled) { router.all('*', (req, res) => { @@ -20,7 +20,7 @@ function createLocksRouter(dependencies) { return router } - const { requireAuth, optionalAuth } = createAuthenticator(apiTokenRepository) + const { requireAuth, optionalAuth } = createAuthenticator(userRepository) /** * GET / - List user's locked inboxes diff --git a/api/routes/mail.js b/api/routes/mail.js index f2ab4c6..425c167 100644 --- a/api/routes/mail.js +++ b/api/routes/mail.js @@ -1,4 +1,5 @@ const express = require('express') +const router = express.Router() const { body, validationResult } = require('express-validator') const createAuthenticator = require('../middleware/authenticator') const { ApiError } = require('../middleware/error-handler') @@ -11,7 +12,7 @@ const { ApiError } = require('../middleware/error-handler') * POST /forward-all - Forward all emails in inbox */ function createMailRouter(dependencies) { - const router = express.Router() + // Ensure router is declared before any usage const { mailProcessingService, apiTokenRepository, @@ -21,6 +22,8 @@ function createMailRouter(dependencies) { const { requireAuth, optionalAuth } = createAuthenticator(apiTokenRepository) + // All router usage is below this line + /** * DELETE /inbox/:address/:uid - Delete single email */ @@ -154,8 +157,8 @@ function createMailRouter(dependencies) { body('destinationEmail').isEmail().normalizeEmail(), async(req, res, next) => { try { - const errors = validationResult(req) - if (!errors.isEmpty()) { + const validationErrors = validationResult(req) + if (!validationErrors.isEmpty()) { return res.apiError('Invalid input parameters', 'VALIDATION_ERROR', 400) } @@ -191,7 +194,7 @@ function createMailRouter(dependencies) { // Forward all emails let successCount = 0 - const errors = [] + const forwardErrors = [] for (const mail of mailsToForward) { try { @@ -203,10 +206,10 @@ function createMailRouter(dependencies) { if (result.success) { successCount++ } else { - errors.push({ uid: mail.uid, error: result.error }) + forwardErrors.push({ uid: mail.uid, error: result.error }) } } catch (error) { - errors.push({ uid: mail.uid, error: error.message }) + forwardErrors.push({ uid: mail.uid, error: error.message }) } } @@ -214,7 +217,7 @@ function createMailRouter(dependencies) { message: `Forwarded ${successCount} of ${mailsToForward.length} email(s)`, forwarded: successCount, total: mailsToForward.length, - errors: errors.length > 0 ? errors : undefined + errors: forwardErrors.length > 0 ? forwardErrors : undefined }) } catch (error) { next(error) @@ -222,7 +225,8 @@ function createMailRouter(dependencies) { } ) + return router } -module.exports = createMailRouter +module.exports = createMailRouter \ No newline at end of file diff --git a/api/routes/stats.js b/api/routes/stats.js index ee91272..9e55817 100644 --- a/api/routes/stats.js +++ b/api/routes/stats.js @@ -1,12 +1,14 @@ const express = require('express') +const router = express.Router() /** * Statistics API Routes * GET / - Get lightweight statistics * GET /enhanced - Get full statistics with historical data */ + function createStatsRouter(dependencies) { - const router = express.Router() + // Ensure router is declared before any usage const { statisticsStore, mailProcessingService, imapService, config } = dependencies if (!config.http.statisticsEnabled) { @@ -51,4 +53,4 @@ function createStatsRouter(dependencies) { return router } -module.exports = createStatsRouter +module.exports = createStatsRouter \ No newline at end of file