Skip to content

Commit

Permalink
add noCache middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
ebubae committed Nov 9, 2024
1 parent 428f31e commit e11bc9f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
7 changes: 2 additions & 5 deletions src/middleware/cache.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { NextFunction, Request, Response } from 'express';

export default (maxAge = 302400, smaxAge = 604800, noCache = false) =>
export default (maxAge = 302400, smaxAge = 604800) =>
(req: Request, res: Response, next: NextFunction) => {
res.set(
'Cache-Control',
noCache ? 'no-store' : `public, max-age=${maxAge}, s-maxage=${smaxAge}`
);
res.set('Cache-Control', `public, max-age=${maxAge}, s-maxage=${smaxAge}`);
return next();
};
3 changes: 2 additions & 1 deletion src/routers/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import attachRedisClient from '../middleware/attachRedisClient';
import analytics from '../middleware/analytics';
import developerAuthorization from '../middleware/developerAuthorization';
import testRouter from './testRouter';
import noCache from 'src/middleware/noCache';

Check failure on line 17 in src/routers/router.ts

View workflow job for this annotation

GitHub Actions / Deploy

Cannot find module 'src/middleware/noCache' or its corresponding type declarations.

const router = Router();

Expand All @@ -33,7 +34,7 @@ router.get('/examples', validateApiKey, attachRedisClient, getExamples);
router.get('/examples/:id', validateApiKey, validId, attachRedisClient, getExample);

router.get('/developers/:id', developerAuthorization, getDeveloper);
router.post('/developers', developerRateLimiter, validateDeveloperBody, postDeveloper);
router.post('/developers', noCache, developerRateLimiter, validateDeveloperBody, postDeveloper);
router.put('/developers', developerRateLimiter, validateUpdateDeveloperBody, putDeveloper);

router.get('/stats', validateAdminApiKey, attachRedisClient, getStats);
Expand Down
6 changes: 3 additions & 3 deletions src/routers/routerV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import validId from '../middleware/validId';
import validateApiKey from '../middleware/validateApiKey';
import analytics from '../middleware/analytics';
import attachRedisClient from '../middleware/attachRedisClient';
import cache from '../middleware/cache';
import noCache from '../middleware/noCache';

Check failure on line 11 in src/routers/routerV2.ts

View workflow job for this annotation

GitHub Actions / Deploy

Cannot find module '../middleware/noCache' or its corresponding type declarations.

const routerV2 = Router();

Expand All @@ -27,8 +27,8 @@ routerV2.get(
);

// Speech-to-Text
routerV2.post('/speech-to-text', analytics, validateApiKey, getTranscription);
routerV2.post('/translate', cache(0, 0, true), analytics, validateApiKey, getTranslation);
routerV2.post('/speech-to-text', noCache, analytics, validateApiKey, getTranscription);
routerV2.post('/translate', noCache, analytics, validateApiKey, getTranslation);

// Redirects to V1
routerV2.post('/developers', (_, res) => res.redirect('/api/v1/developers'));
Expand Down

0 comments on commit e11bc9f

Please sign in to comment.