mirror of
https://github.com/Crazyco-xyz/48hr.email.git
synced 2026-01-08 18:59:36 +01:00
[Fix]: Fix API
This commit is contained in:
parent
a52ce51b6f
commit
8401310062
7 changed files with 33 additions and 21 deletions
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
Loading…
Add table
Reference in a new issue