Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newspaper archive endpoint: add parsing #1405

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 14 additions & 6 deletions server/routes/newspaperArchive.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Router } from 'express';
import type { Request, Response } from 'express';
import fetch from 'node-fetch';
import { z } from 'zod';
import { authorizationOrCookieHeader } from '../apiProxy';
import { s3ConfigPromise } from '../awsIntegration';
import { conf } from '../config';
Expand All @@ -13,9 +14,13 @@ type NewspapersRequestBody = {
};

// { url: "https://<subdomain>.newspapers.com/…?tpa=<token>" }
type NewspapersResponseBody = {
url: string;
};
const NewspapersResponseSchema = z.object({
url: z.string(),
});

const UserAttributesSchema = z.object({
contentAccess: z.record(z.string(), z.boolean()),
});

type NewspaperArchiveConfig = {
authString: string;
Expand Down Expand Up @@ -64,8 +69,9 @@ router.get('/auth', async (req: Request, res: Response) => {
},
);

// ToDo: we have zod on the server, we could parse the responses with that
const responseJson = (await response.json()) as NewspapersResponseBody;
const responseJson = NewspapersResponseSchema.parse(
await response.json(),
);

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

async function checkSupporterEntitlement(req: Request): Promise<boolean> {
const supporterAttributesResponse = await getSupporterStatus(req);
const supporterAttributes = await supporterAttributesResponse.json();
const supporterAttributes = UserAttributesSchema.parse(
await supporterAttributesResponse.json(),
);

// ToDo: this should return a flag that represents either Tier 3 or a newspaperArchive specific entitlement
return (
Expand Down
Loading