diff --git a/src/api/desktop/recommendations/index.ts b/src/api/desktop/recommendations/index.ts index 73197a6c8..a3a946703 100644 --- a/src/api/desktop/recommendations/index.ts +++ b/src/api/desktop/recommendations/index.ts @@ -1,4 +1,6 @@ import express, { NextFunction, Request, Response } from 'express'; +import config from '../../../config'; +import CacheControlHandler from '../../lib/cacheControlHandler'; import ConsumerKeyHandler from '../../../auth/consumerKeyHandler'; import { GraphQLErrorHandler } from '../../error/graphQLErrorHandler'; import { handleQueryParameters } from './inputs'; @@ -7,12 +9,14 @@ import Recommendations from '../../../graphql-proxy/recommendations/recommendati import { forwardHeadersMiddleware } from '../../../graphql-proxy/lib/client'; import { RecommendationsQueryVariables } from '../../../generated/graphql/types'; import { RecommendationsResponse, responseTransformer } from './response'; + const router = express.Router(); router.get( '/v1/recommendations', // request must include a consumer ConsumerKeyHandler, + CacheControlHandler('public, max-age=1800', config), async (req: Request, res: Response, next: NextFunction) => { try { const variables = handleQueryParameters(req.query); diff --git a/src/api/desktop/recommendations/recommendations.spec.ts b/src/api/desktop/recommendations/recommendations.spec.ts index f53784c95..ba69f26f0 100644 --- a/src/api/desktop/recommendations/recommendations.spec.ts +++ b/src/api/desktop/recommendations/recommendations.spec.ts @@ -11,7 +11,7 @@ import { components } from '../../../generated/openapi/types'; type Recommendation = components['schemas']['Recommendation']; /** - * This covers some happy path testinf ro complete server + * This covers some happy path testing for complete server * middleware composition. Unit tests still catch the finer * details, this just ensures middleware is all working together. * @@ -72,9 +72,11 @@ describe('recommendations API server', () => { const res = await request(app) .get(`/desktop/v1/recommendations?${params.toString()}`) .set(authHeaders) - .send(); + .send() + .expect('Cache-control', 'public, max-age=1800'); // assert the Cache-control header is overwritten by the /v1/recommendations route expect(res.status).toEqual(200); + // response ins json const parsedRes = JSON.parse(res.text); expect(parsedRes.data?.length).toEqual(1); diff --git a/src/api/lib/cacheControlHandler.ts b/src/api/lib/cacheControlHandler.ts index 1f323d56c..85c6db830 100644 --- a/src/api/lib/cacheControlHandler.ts +++ b/src/api/lib/cacheControlHandler.ts @@ -2,10 +2,6 @@ import { NextFunction, Request, Response } from 'express'; import ConfigFile from '../../config'; -// eslint / prettier config cannot cope with functions that return -// functions like this. Thrashes between 2 states. -// prettier-ignore - /** * Sets the value of the 'Cache-Control' response header for all downstream * express handlers. @@ -18,13 +14,16 @@ import ConfigFile from '../../config'; * * @param cachePolicy string value to set CacheControl response header */ -const CacheControlHandler = - (cachePolicy: string, config: typeof ConfigFile) => - (req: Request, res: Response, next: NextFunction): void => { - if (config.app.environment !== 'development') { - res.set('Cache-control', cachePolicy); - } - return next(); - }; +const CacheControlHandler = ( + cachePolicy: string, + config: typeof ConfigFile +) => { + return (req: Request, res: Response, next: NextFunction): void => { + if (config.app.environment !== 'development') { + res.set('Cache-control', cachePolicy); + } + return next(); + }; +}; export default CacheControlHandler;