File tree 2 files changed +8280
-10969
lines changed
2 files changed +8280
-10969
lines changed Original file line number Diff line number Diff line change @@ -2,15 +2,37 @@ import express from "express";
2
2
import * as pluginControllers from "../controllers/plugins" ;
3
3
import jsControllers from "../controllers/runJavascript" ;
4
4
import * as npmControllers from "../controllers/npm" ;
5
+ import querystring from "querystring" ;
6
+ import { ParsedQs } from 'qs' ;
5
7
6
8
const apiRouter = express . Router ( ) ;
7
9
8
10
// In-memory cache object
9
11
const cache : { [ key : string ] : any } = { } ;
10
12
13
+ // Helper function to flatten Request Query object
14
+ function flatQuery ( query : ParsedQs ) {
15
+ let result : { [ key : string ] : string | number | boolean | string [ ] | null } = { } ;
16
+
17
+ for ( let key in query ) {
18
+ if ( typeof query [ key ] === 'object' && ! Array . isArray ( query [ key ] ) ) {
19
+ result [ key ] = JSON . stringify ( query [ key ] ) ;
20
+ } else {
21
+ result [ key ] = query [ key ] as any ;
22
+ }
23
+ }
24
+
25
+ return result ;
26
+ }
27
+
11
28
// Middleware to cache responses for specific routes
12
29
function cacheMiddleware ( req : express . Request , res : express . Response , next : express . NextFunction ) {
13
- const cacheKey = req . originalUrl ;
30
+
31
+ let cacheKey = req . path + "?" + querystring . stringify ( flatQuery ( req . query ) ) ;
32
+
33
+ if ( req . method === "POST" && req . is ( "application/json" ) && req . body ) {
34
+ cacheKey += JSON . stringify ( req . body ) ;
35
+ }
14
36
15
37
// Check if the response is already cached
16
38
if ( cache [ cacheKey ] ) {
You can’t perform that action at this time.
0 commit comments