@@ -5,20 +5,43 @@ import * as npmControllers from "../controllers/npm";
5
5
6
6
const apiRouter = express . Router ( ) ;
7
7
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
+
8
31
apiRouter . post ( "/runJs" , jsControllers . runJavascript ) ;
9
32
apiRouter . post ( "/batchRunJs" , jsControllers . batchRunJavascript ) ;
10
33
11
- apiRouter . get ( "/plugins" , pluginControllers . listPlugins ) ;
34
+ apiRouter . get ( "/plugins" , cacheMiddleware , pluginControllers . listPlugins ) ;
12
35
apiRouter . post ( "/runPluginQuery" , pluginControllers . runPluginQuery ) ;
13
- apiRouter . post ( "/getPluginDynamicConfig" , pluginControllers . getDynamicDef ) ;
36
+ apiRouter . post ( "/getPluginDynamicConfig" , cacheMiddleware , pluginControllers . getDynamicDef ) ;
14
37
apiRouter . post ( "/validatePluginDataSourceConfig" , pluginControllers . validatePluginDataSourceConfig ) ;
15
38
16
39
// routes for npm registry and package fetching with config called by the api-service
17
40
apiRouter . post ( "/npm/registry/*" , npmControllers . fetchRegistryWithConfig ) ;
18
41
apiRouter . post ( "/npm/package/*" , npmControllers . fetchPackageFileWithConfig ) ;
19
42
20
43
// 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);
23
46
24
47
export default apiRouter ;
0 commit comments