mirror of
https://github.com/Crazyco-xyz/48hr.email.git
synced 2026-01-09 11:19: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 express = require('express')
|
||||||
|
const router = express.Router()
|
||||||
const { body, validationResult } = require('express-validator')
|
const { body, validationResult } = require('express-validator')
|
||||||
const createAuthenticator = require('../middleware/authenticator')
|
const createAuthenticator = require('../middleware/authenticator')
|
||||||
const { ApiError } = require('../middleware/error-handler')
|
const { ApiError } = require('../middleware/error-handler')
|
||||||
|
|
@ -14,8 +15,9 @@ const { ApiError } = require('../middleware/error-handler')
|
||||||
* POST /token - Generate/regenerate API token
|
* POST /token - Generate/regenerate API token
|
||||||
* DELETE /token - Revoke API token
|
* DELETE /token - Revoke API token
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function createAccountRouter(dependencies) {
|
function createAccountRouter(dependencies) {
|
||||||
const router = express.Router()
|
// Ensure router is declared before any usage
|
||||||
const {
|
const {
|
||||||
authService,
|
authService,
|
||||||
userRepository,
|
userRepository,
|
||||||
|
|
@ -24,6 +26,8 @@ function createAccountRouter(dependencies) {
|
||||||
config
|
config
|
||||||
} = dependencies
|
} = dependencies
|
||||||
|
|
||||||
|
// All router usage is below this line
|
||||||
|
|
||||||
// Check if auth is enabled
|
// Check if auth is enabled
|
||||||
if (!authService || !config.user.authEnabled) {
|
if (!authService || !config.user.authEnabled) {
|
||||||
router.all('*', (req, res) => {
|
router.all('*', (req, res) => {
|
||||||
|
|
@ -291,4 +295,4 @@ function createAccountRouter(dependencies) {
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = createAccountRouter
|
module.exports = createAccountRouter
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
|
const router = express.Router()
|
||||||
const { body, validationResult } = require('express-validator')
|
const { body, validationResult } = require('express-validator')
|
||||||
const { ApiError } = require('../middleware/error-handler')
|
const { ApiError } = require('../middleware/error-handler')
|
||||||
|
|
||||||
|
|
@ -10,7 +11,6 @@ const { ApiError } = require('../middleware/error-handler')
|
||||||
* GET /session - Get current session info
|
* GET /session - Get current session info
|
||||||
*/
|
*/
|
||||||
function createAuthRouter(dependencies) {
|
function createAuthRouter(dependencies) {
|
||||||
const router = express.Router()
|
|
||||||
const { authService, config } = dependencies
|
const { authService, config } = dependencies
|
||||||
|
|
||||||
// Check if auth is enabled
|
// Check if auth is enabled
|
||||||
|
|
@ -147,4 +147,4 @@ function createAuthRouter(dependencies) {
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = createAuthRouter
|
module.exports = createAuthRouter
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
|
const router = express.Router()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configuration API Routes (Public)
|
* Configuration API Routes (Public)
|
||||||
|
|
@ -7,6 +8,9 @@ const express = require('express')
|
||||||
* GET /features - Get enabled features
|
* GET /features - Get enabled features
|
||||||
*/
|
*/
|
||||||
function createConfigRouter(dependencies) {
|
function createConfigRouter(dependencies) {
|
||||||
|
// Ensure router is declared before any usage
|
||||||
|
const router = express.Router()
|
||||||
|
const { config } = dependencies
|
||||||
// API enabled toggle
|
// API enabled toggle
|
||||||
router.use((req, res, next) => {
|
router.use((req, res, next) => {
|
||||||
if (!config.apiEnabled) {
|
if (!config.apiEnabled) {
|
||||||
|
|
@ -14,8 +18,6 @@ function createConfigRouter(dependencies) {
|
||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
const router = express.Router()
|
|
||||||
const { config } = dependencies
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GET /domains - Get allowed email domains
|
* GET /domains - Get allowed email domains
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
|
const router = express.Router()
|
||||||
const createAuthenticator = require('../middleware/authenticator')
|
const createAuthenticator = require('../middleware/authenticator')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -9,7 +10,6 @@ const createAuthenticator = require('../middleware/authenticator')
|
||||||
* GET /:address/:uid/attachment/:checksum - Download attachment
|
* GET /:address/:uid/attachment/:checksum - Download attachment
|
||||||
*/
|
*/
|
||||||
function createInboxRouter(dependencies) {
|
function createInboxRouter(dependencies) {
|
||||||
const router = express.Router()
|
|
||||||
const { mailProcessingService, apiTokenRepository } = dependencies
|
const { mailProcessingService, apiTokenRepository } = dependencies
|
||||||
|
|
||||||
const { optionalAuth } = createAuthenticator(apiTokenRepository)
|
const { optionalAuth } = createAuthenticator(apiTokenRepository)
|
||||||
|
|
@ -138,4 +138,4 @@ function createInboxRouter(dependencies) {
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = createInboxRouter
|
module.exports = createInboxRouter
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
|
const router = express.Router()
|
||||||
const { body, validationResult } = require('express-validator')
|
const { body, validationResult } = require('express-validator')
|
||||||
const createAuthenticator = require('../middleware/authenticator')
|
const createAuthenticator = require('../middleware/authenticator')
|
||||||
|
|
||||||
|
|
@ -10,8 +11,7 @@ const createAuthenticator = require('../middleware/authenticator')
|
||||||
* GET /:address/status - Check if inbox is locked
|
* GET /:address/status - Check if inbox is locked
|
||||||
*/
|
*/
|
||||||
function createLocksRouter(dependencies) {
|
function createLocksRouter(dependencies) {
|
||||||
const router = express.Router()
|
const { inboxLock, userRepository, apiTokenRepository, config } = dependencies
|
||||||
const { inboxLock, apiTokenRepository, config } = dependencies
|
|
||||||
|
|
||||||
if (!inboxLock || !config.user.authEnabled) {
|
if (!inboxLock || !config.user.authEnabled) {
|
||||||
router.all('*', (req, res) => {
|
router.all('*', (req, res) => {
|
||||||
|
|
@ -20,7 +20,7 @@ function createLocksRouter(dependencies) {
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
const { requireAuth, optionalAuth } = createAuthenticator(apiTokenRepository)
|
const { requireAuth, optionalAuth } = createAuthenticator(userRepository)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GET / - List user's locked inboxes
|
* GET / - List user's locked inboxes
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
|
const router = express.Router()
|
||||||
const { body, validationResult } = require('express-validator')
|
const { body, validationResult } = require('express-validator')
|
||||||
const createAuthenticator = require('../middleware/authenticator')
|
const createAuthenticator = require('../middleware/authenticator')
|
||||||
const { ApiError } = require('../middleware/error-handler')
|
const { ApiError } = require('../middleware/error-handler')
|
||||||
|
|
@ -11,7 +12,7 @@ const { ApiError } = require('../middleware/error-handler')
|
||||||
* POST /forward-all - Forward all emails in inbox
|
* POST /forward-all - Forward all emails in inbox
|
||||||
*/
|
*/
|
||||||
function createMailRouter(dependencies) {
|
function createMailRouter(dependencies) {
|
||||||
const router = express.Router()
|
// Ensure router is declared before any usage
|
||||||
const {
|
const {
|
||||||
mailProcessingService,
|
mailProcessingService,
|
||||||
apiTokenRepository,
|
apiTokenRepository,
|
||||||
|
|
@ -21,6 +22,8 @@ function createMailRouter(dependencies) {
|
||||||
|
|
||||||
const { requireAuth, optionalAuth } = createAuthenticator(apiTokenRepository)
|
const { requireAuth, optionalAuth } = createAuthenticator(apiTokenRepository)
|
||||||
|
|
||||||
|
// All router usage is below this line
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DELETE /inbox/:address/:uid - Delete single email
|
* DELETE /inbox/:address/:uid - Delete single email
|
||||||
*/
|
*/
|
||||||
|
|
@ -154,8 +157,8 @@ function createMailRouter(dependencies) {
|
||||||
body('destinationEmail').isEmail().normalizeEmail(),
|
body('destinationEmail').isEmail().normalizeEmail(),
|
||||||
async(req, res, next) => {
|
async(req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const errors = validationResult(req)
|
const validationErrors = validationResult(req)
|
||||||
if (!errors.isEmpty()) {
|
if (!validationErrors.isEmpty()) {
|
||||||
return res.apiError('Invalid input parameters', 'VALIDATION_ERROR', 400)
|
return res.apiError('Invalid input parameters', 'VALIDATION_ERROR', 400)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -191,7 +194,7 @@ function createMailRouter(dependencies) {
|
||||||
|
|
||||||
// Forward all emails
|
// Forward all emails
|
||||||
let successCount = 0
|
let successCount = 0
|
||||||
const errors = []
|
const forwardErrors = []
|
||||||
|
|
||||||
for (const mail of mailsToForward) {
|
for (const mail of mailsToForward) {
|
||||||
try {
|
try {
|
||||||
|
|
@ -203,10 +206,10 @@ function createMailRouter(dependencies) {
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
successCount++
|
successCount++
|
||||||
} else {
|
} else {
|
||||||
errors.push({ uid: mail.uid, error: result.error })
|
forwardErrors.push({ uid: mail.uid, error: result.error })
|
||||||
}
|
}
|
||||||
} catch (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)`,
|
message: `Forwarded ${successCount} of ${mailsToForward.length} email(s)`,
|
||||||
forwarded: successCount,
|
forwarded: successCount,
|
||||||
total: mailsToForward.length,
|
total: mailsToForward.length,
|
||||||
errors: errors.length > 0 ? errors : undefined
|
errors: forwardErrors.length > 0 ? forwardErrors : undefined
|
||||||
})
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(error)
|
next(error)
|
||||||
|
|
@ -222,7 +225,8 @@ function createMailRouter(dependencies) {
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = createMailRouter
|
module.exports = createMailRouter
|
||||||
|
|
@ -1,12 +1,14 @@
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
|
const router = express.Router()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Statistics API Routes
|
* Statistics API Routes
|
||||||
* GET / - Get lightweight statistics
|
* GET / - Get lightweight statistics
|
||||||
* GET /enhanced - Get full statistics with historical data
|
* GET /enhanced - Get full statistics with historical data
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function createStatsRouter(dependencies) {
|
function createStatsRouter(dependencies) {
|
||||||
const router = express.Router()
|
// Ensure router is declared before any usage
|
||||||
const { statisticsStore, mailProcessingService, imapService, config } = dependencies
|
const { statisticsStore, mailProcessingService, imapService, config } = dependencies
|
||||||
|
|
||||||
if (!config.http.statisticsEnabled) {
|
if (!config.http.statisticsEnabled) {
|
||||||
|
|
@ -51,4 +53,4 @@ function createStatsRouter(dependencies) {
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = createStatsRouter
|
module.exports = createStatsRouter
|
||||||
Loading…
Add table
Reference in a new issue