just-another-http-api 1.2.8 → 1.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/api.js +6 -3
  2. package/package.json +3 -2
  3. package/src/cache.js +18 -2
package/api.js CHANGED
@@ -36,6 +36,9 @@ async function createServer ( config ) {
36
36
  if ( config.websocket?.enabled ){
37
37
  app.register ( require ( '@fastify/websocket' ), config.websocket?.options );
38
38
  }
39
+ if (config.cookies?.enabled) {
40
+ app.register ( require ( '@fastify/cookie' ), config.cookies );
41
+ }
39
42
  await uploads.initialiseUploads ( app, config );
40
43
  await caching.initialiseCaching ( app, config );
41
44
  await auth.initialiseAuth ( app, config );
@@ -118,11 +121,11 @@ function fastifyHandlerWrapper ( handler, config, globalConfig, method ) {
118
121
  try {
119
122
  let response;
120
123
  if ( globalConfig?.cache?.enabled ){
121
- const request = { method: req.method, query: JSON.parse ( JSON.stringify ( req.query ) ), routeOptions: req.routeOptions };
124
+ const request = { method: req.method, query: JSON.parse ( JSON.stringify ( req.query ) ), routeOptions: req.routeOptions, params: JSON.parse ( JSON.stringify ( req.params ) ) };
122
125
  response = await caching.checkRequestCache ( app, request, reply, config, globalConfig );
123
-
126
+
124
127
  if ( !response ){
125
- response = await handler ( req );
128
+ response = await handler ( req, { invalidateRequest: ( _req ) => caching.invalidateRequestCache( app, _req, globalConfig ) } );
126
129
  await caching.setRequestCache ( app, request, response, config, globalConfig );
127
130
  }
128
131
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "just-another-http-api",
3
- "version": "1.2.8",
3
+ "version": "1.2.10",
4
4
  "description": "A framework built on top of fastify aimed at removing the need for any network or server configuration. ",
5
5
  "homepage": "https://github.com/OllieEdge/just-another-http-api#readme",
6
6
  "repository": {
@@ -27,6 +27,7 @@
27
27
  "dependencies": {
28
28
  "@aws-sdk/lib-storage": "^3.569.0",
29
29
  "@fastify/caching": "^8.3.0",
30
+ "@fastify/cookie": "^9.4.0",
30
31
  "@fastify/cors": "^9.0.1",
31
32
  "@fastify/jwt": "^7.2.3",
32
33
  "@fastify/redis": "^6.2.0",
@@ -44,4 +45,4 @@
44
45
  "eslint": "^9.2.0",
45
46
  "mocha": "^10.4.0"
46
47
  }
47
- }
48
+ }
package/src/cache.js CHANGED
@@ -1,4 +1,12 @@
1
- const getCacheKey = ( config, req ) => `${config?.cache?.redisPrefix || ''}:${req.method.toLowerCase ()}:${req.routeOptions.url}:${JSON.stringify ( req.query )}`;
1
+ const getCacheKey = (config, req) => {
2
+ let url = req.routeOptions.url;
3
+
4
+ for (const [ param, value ] of Object.entries( req.params || {} )) {
5
+ url = url.replace(`:${param}`, value);
6
+ }
7
+
8
+ return `${config?.cache?.redisPrefix || ''}:${req.method.toLowerCase()}:${url}:${JSON.stringify( req.query )}`;
9
+ };
2
10
 
3
11
  exports.initialiseCaching = async ( app, config ) => {
4
12
 
@@ -66,4 +74,12 @@ exports.setRequestCache = async ( app, req, response, handleConfig, globalConfig
66
74
  }
67
75
 
68
76
  }
69
- };
77
+ };
78
+
79
+ exports.invalidateRequestCache = async ( app, req, globalConfig ) => {
80
+ if ( globalConfig?.cache?.enabled ) {
81
+ const cacheKey = getCacheKey ( globalConfig, req );
82
+
83
+ await app.redis.del ( cacheKey );
84
+ }
85
+ };