routes.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * API 路由主入口(客户独立实例)
  3. * - Amazon:保留本地 SP-API 模块
  4. * - Sorftime / TikHub:仅走公司中转 Relay(src/relay + api/relay)
  5. * - 已移除:直连 sorftime / tikhub / eccang 模块
  6. */
  7. import express from 'npm:express';
  8. const router = express.Router();
  9. console.log('[API Routes] Initializing TARGET customer instance...');
  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', instance: 'TARGET-voc-instance' });
  16. });
  17. /**
  18. * 亚马逊相关接口
  19. * 必须携带店铺的 id
  20. */
  21. let createSpApiRouter;
  22. try {
  23. ({ createSpApiRouter } = await globalThis.loadModule(
  24. 'https://repos.fmode.cn/x/fmode-amazon-sp-api/0.1.3/fmode-amazon-sp-api.min.js?code=xxxxxxx'
  25. ));
  26. } catch (e) {
  27. console.warn('[API Routes] CDN 加载失败, 使用本地模块回退', e);
  28. ({ createSpApiRouter } = await import('../modules/fmode-amazon-sp-api/src/mod.ts'));
  29. }
  30. router.use('/amazon', createSpApiRouter());
  31. /**
  32. * Sorftime / TikHub:公司中转 Relay
  33. * 对外契约:
  34. * POST /api/sorftime/forward
  35. * POST /api/tikhub/forward
  36. */
  37. import sorftimeRelayRouter from './relay/sorftime-routes.ts';
  38. import tikhubRelayRouter from './relay/tikhub-routes.ts';
  39. router.use('/sorftime', sorftimeRelayRouter);
  40. router.use('/tikhub', tikhubRelayRouter);
  41. console.log('[API Routes] ✅ Sorftime/TikHub relay mounted (company server.fmode.cn)');
  42. // 定时任务管理路由(内部已改为打本机 /api/sorftime/forward)
  43. import scheduleRouter from './module/schedule/routes-schedule.ts';
  44. try {
  45. router.use('/schedule', scheduleRouter);
  46. } catch (e) {
  47. console.warn('[API Routes] scheduleRouter 加载失败', e);
  48. }
  49. router.get('/', (req, res) => {
  50. res.json({
  51. message: 'TARGET VOC Instance API Routes Loaded',
  52. version: '1.0.1-customer-relay',
  53. timestamp: new Date().toISOString(),
  54. availableRoutes: [
  55. '/api/amazon',
  56. '/api/sorftime/forward',
  57. '/api/sorftime/health',
  58. '/api/tikhub/forward',
  59. '/api/tikhub/health',
  60. '/api/schedule',
  61. ],
  62. removedModules: [
  63. 'fmode-sorftime-api (direct upstream)',
  64. 'fmode-tikhub-api (direct upstream)',
  65. 'fmode-tikhub-api-customize',
  66. 'fmode-eccang-api',
  67. ],
  68. relay: {
  69. companyBase: 'https://server.fmode.cn',
  70. sorftimeUpstream: '/api/voc-ecom/forward',
  71. tikhubUpstream: '/api/voc-social/*',
  72. },
  73. });
  74. });
  75. export default router;