Skip to content

Commit

Permalink
Merge pull request #11205 from wellcomecollection/integration-field-e…
Browse files Browse the repository at this point in the history
…ndpoint

Integration field endpoint
  • Loading branch information
gestchild committed Sep 26, 2024
2 parents 6d6a181 + c41bef4 commit cd39bf7
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions content/webapp/pages/api/prismic/works.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { NextApiRequest, NextApiResponse } from 'next';

async function fetchWorks(page) {
try {
const works = await fetch(
`https://api.wellcomecollection.org/catalogue/v2/works?availabilities=online&pageSize=50&page=${page || 1}`,
{
headers: {
'Content-Type': 'application/json',
},
}
).then(resp => resp.json());
return works;
} catch (error) {
return undefined;
}
}

const WorksApi = async (
req: NextApiRequest,
res: NextApiResponse
): Promise<void> => {
const { page } = req.query;

const response = await fetchWorks(page);
// We format the response to be compatible with Prismic's integration field
// https://prismic.io/docs/integration#custom-api-format
const transformedResponse = {
results_size: 10000, // This is the most we can get from the catalogue API at present
results: response.results.map(result => {
const description = `${result.physicalDescription ? `${result.physicalDescription}: ` : ''} ${result.description ? result.description : ''}`;
return {
id: result.id,
title: result.title,
description,
image_url: result.thumbnail?.url,
blob: {
id: result.id,
title: result.title,
physicalDescription: result.physicalDescription,
description: result.description,
},
};
}),
};
res.setHeader('Content-Type', 'application/json');
res.setHeader('Access-Control-Allow-Origin', '*');

if (response.type === 'Error') {
res.status(response.httpStatus);
} else {
res.status(200);
}
res.json(transformedResponse);
};

export default WorksApi;

0 comments on commit cd39bf7

Please sign in to comment.