routes.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * API 路由主入口
  3. * 手动加载并挂载所有子路由模块
  4. */
  5. import express from 'npm:express';
  6. // 创建主路由
  7. const router = express.Router();
  8. console.log('[API Routes] Initializing...');
  9. // Debug middleware
  10. router.use((req, res, next) => {
  11. console.log(`[API Router] Hit: ${req.method} ${req.url} (Original: ${req.originalUrl})`);
  12. next();
  13. });
  14. router.get('/ping', (req, res) => {
  15. res.json({ message: 'pong' });
  16. });
  17. let createSpApiRouter;
  18. try {
  19. ({ createSpApiRouter } = await globalThis.loadModule(
  20. "https://repos.fmode.cn/x/fmode-amazon-sp-api/0.0.7/fmode-amazon-sp-api.min.js?code=xxxxxxx"
  21. ));
  22. } catch (e) {
  23. console.warn("[API Routes] CDN 加载失败, 使用本地模块回退", e);
  24. // ({ createSpApiRouter } = await import("../modules/fmode-amazon-sp-api/src/mod.ts"));
  25. }
  26. // 本地开发模式加载
  27. //const { createSpApiRouter } = await import("../modules/fmode-amazon-sp-api/src/mod.ts");
  28. // 挂载 SP-API 路由
  29. router.use('/amazon', createSpApiRouter());
  30. let createSorftimeRouter;
  31. try {
  32. ({ createSorftimeRouter } = await globalThis.loadModule(
  33. 'https://repos.fmode.cn/x/fmode-sorftime-api/0.0.1/fmode-sorftime-api.min.js?code=xxxxxxx'
  34. ));
  35. } catch (e) {
  36. console.warn("[API Routes] CDN 加载失败, 使用本地模块回退", e);
  37. // ({ createSorftimeRouter } = await import('../modules/fmode-sorftime-api/src/mod.ts'));
  38. }
  39. router.use('/sorftime', createSorftimeRouter());
  40. let createTikHubRoutes;
  41. try {
  42. ({ createTikHubRoutes } = await globalThis.loadModule(
  43. 'https://repos.fmode.cn/x/fmode-tikhub-api/0.0.1/fmode-tikhub-api.min.js?code=xxxxxxx'
  44. ));
  45. } catch (e) {
  46. console.warn("[API Routes] CDN 加载失败, 使用本地模块回退", e);
  47. // ({ createTikHubRoutes } = await import('../modules/fmode-tikhub-api/src/mod.ts'));
  48. }
  49. router.use(
  50. '/tikhub',
  51. createTikHubRoutes({
  52. basePath: '/api/tikhub',
  53. apiKey: 'tKIbAsEM8X+GmE2vHqGW7D/ICwK1Q5V4viKFrWiPB6HholGdLFqZJmmyNw=='
  54. })
  55. );
  56. router.get('/', (req, res) => {
  57. res.json({
  58. message: 'API Routes Loaded Successfully',
  59. version: '1.0.0',
  60. timestamp: new Date().toISOString(),
  61. availableRoutes: [
  62. '/api/amazon',
  63. '/api/amazon/test',
  64. '/api/sorftime',
  65. '/api/sorftime/test',
  66. '/api/tikhub',
  67. '/api/tikhub/health'
  68. ]
  69. });
  70. });
  71. export default router;