Skip to content

Commit 2726df8

Browse files
Merge pull request #1405 from guardian/ahe/parse-newspaper-archive
Newspaper archive endpoint: add parsing
2 parents b516bc6 + 0fe40a4 commit 2726df8

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

server/routes/newspaperArchive.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Router } from 'express';
22
import type { Request, Response } from 'express';
33
import fetch from 'node-fetch';
4+
import { z } from 'zod';
45
import { authorizationOrCookieHeader } from '../apiProxy';
56
import { s3ConfigPromise } from '../awsIntegration';
67
import { conf } from '../config';
@@ -13,9 +14,13 @@ type NewspapersRequestBody = {
1314
};
1415

1516
// { url: "https://<subdomain>.newspapers.com/…?tpa=<token>" }
16-
type NewspapersResponseBody = {
17-
url: string;
18-
};
17+
const NewspapersResponseSchema = z.object({
18+
url: z.string(),
19+
});
20+
21+
const UserAttributesSchema = z.object({
22+
contentAccess: z.record(z.string(), z.boolean()),
23+
});
1924

2025
type NewspaperArchiveConfig = {
2126
authString: string;
@@ -64,8 +69,9 @@ router.get('/auth', async (req: Request, res: Response) => {
6469
},
6570
);
6671

67-
// ToDo: we have zod on the server, we could parse the responses with that
68-
const responseJson = (await response.json()) as NewspapersResponseBody;
72+
const responseJson = NewspapersResponseSchema.parse(
73+
await response.json(),
74+
);
6975

7076
const archiveReturnUrlString = req.query['ncom-return-url'];
7177
if (
@@ -92,7 +98,9 @@ export { router };
9298

9399
async function checkSupporterEntitlement(req: Request): Promise<boolean> {
94100
const supporterAttributesResponse = await getSupporterStatus(req);
95-
const supporterAttributes = await supporterAttributesResponse.json();
101+
const supporterAttributes = UserAttributesSchema.parse(
102+
await supporterAttributesResponse.json(),
103+
);
96104

97105
// ToDo: this should return a flag that represents either Tier 3 or a newspaperArchive specific entitlement
98106
return (

0 commit comments

Comments
 (0)