in packages/pressreader/src/processEdition.ts [154:193]
export async function fetchArticleData(
id: string,
capiConfig: CapiConfig,
): Promise<CapiItem | undefined> {
const url = CapiItemUrlFromId(id, capiConfig);
const resp = await fetch(url);
if (resp.status == 404) {
console.warn(`Article not found: ${id}`);
return undefined;
}
if (resp.status != 200) {
throw new Error('Failed to fetch article data');
}
const { response: data } = (await resp.json()) as unknown as {
response: unknown;
};
/**
* One known error is when the article exists but the API key does not
* carry the right permissions to access it. We can't know whether an
* article falls into this category beforehand, but this shouldn't
* be a treated as a fatal error.
*/
if (isKnownCapiError(data)) {
if (
data.message ==
'You are not permitted to access this content via your current user tier.'
) {
return undefined;
}
throw new Error(
`CAPI error: ${
data.message ?? 'no message set in CAPI response'
}. Requested item: ${id}`,
);
}
if (!isCapiItemResponse(data)) {
throw new Error(`CAPI response is not valid: ${id}`);
}
return capiResponseToCapiItem(data);
}