Skip to content
This repository was archived by the owner on Jan 29, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/api/desktop/recommendations/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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),
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have much knowledge around Web repo but I'm assuming this is where web repo sets the cache time limit for recommendations:

https://github.com/Pocket/Web/blob/f50a32402077629b61c55930941814c442a6ff19/public_html/v3/firefox/global-recs.php#L33

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks correct to me.

async (req: Request, res: Response, next: NextFunction) => {
try {
const variables = handleQueryParameters(req.query);
Expand Down
6 changes: 4 additions & 2 deletions src/api/desktop/recommendations/recommendations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down
23 changes: 11 additions & 12 deletions src/api/lib/cacheControlHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
};
};
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very very tiny refactor to get rid of the eslint / prettier errors. Works as expected but I wasn't sure the original intent behind not having a return and having nested arrow functions 🤔

Can revert

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is equivalent.

It's just another syntax that can avoid nesting if your lint rules allow it.

Typically when I use higher order functions I would format it like this:

const CacheControlHandler =
  (cachePolicy: string, config: typeof ConfigFile) =>
  (req: Request, res: Response, next: NextFunction): void => {
    ...

The second function is interpreted as single expression and implicitly returns. It's nice and concise, but the eslint / prettier rules don't really like this at all though. Docs


export default CacheControlHandler;