@@ -5,20 +5,43 @@ import * as npmControllers from "../controllers/npm";
55
66const apiRouter = express . Router ( ) ;
77
8+ // In-memory cache object
9+ const cache : { [ key : string ] : any } = { } ;
10+
11+ // Middleware to cache responses for specific routes
12+ function cacheMiddleware ( req : express . Request , res : express . Response , next : express . NextFunction ) {
13+ const cacheKey = req . originalUrl ;
14+
15+ // Check if the response is already cached
16+ if ( cache [ cacheKey ] ) {
17+ return res . json ( cache [ cacheKey ] ) ; // Return cached JSON object if it exists
18+ }
19+
20+ // Override res.json instead of res.send to store response in cache as JSON
21+ const originalJson = res . json . bind ( res ) ;
22+ res . json = ( body : any ) => {
23+ cache [ cacheKey ] = body ; // Cache the JSON response
24+ originalJson ( body ) ; // Send the JSON response
25+ return res ;
26+ } ;
27+
28+ next ( ) ;
29+ }
30+
831apiRouter . post ( "/runJs" , jsControllers . runJavascript ) ;
932apiRouter . post ( "/batchRunJs" , jsControllers . batchRunJavascript ) ;
1033
11- apiRouter . get ( "/plugins" , pluginControllers . listPlugins ) ;
34+ apiRouter . get ( "/plugins" , cacheMiddleware , pluginControllers . listPlugins ) ;
1235apiRouter . post ( "/runPluginQuery" , pluginControllers . runPluginQuery ) ;
13- apiRouter . post ( "/getPluginDynamicConfig" , pluginControllers . getDynamicDef ) ;
36+ apiRouter . post ( "/getPluginDynamicConfig" , cacheMiddleware , pluginControllers . getDynamicDef ) ;
1437apiRouter . post ( "/validatePluginDataSourceConfig" , pluginControllers . validatePluginDataSourceConfig ) ;
1538
1639// routes for npm registry and package fetching with config called by the api-service
1740apiRouter . post ( "/npm/registry/*" , npmControllers . fetchRegistryWithConfig ) ;
1841apiRouter . post ( "/npm/package/*" , npmControllers . fetchPackageFileWithConfig ) ;
1942
2043// temporary routes for testing npm registry and package routes by directly calling the node-service
21- apiRouter . get ( "/npm/registry/*" , npmControllers . fetchRegistry ) ;
22- apiRouter . get ( "/npm/package/*" , npmControllers . fetchPackageFile ) ;
44+ // apiRouter.get("/npm/registry/*", npmControllers.fetchRegistry);
45+ // apiRouter.get("/npm/package/*", npmControllers.fetchPackageFile);
2346
2447export default apiRouter ;
0 commit comments