UNPKG

get-wikipedia-article

Version:

Fetch Wikipedia article resources (e.g. title, body, links...).

40 lines (32 loc) 941 B
import { formatType, wikipediaLanguageType as languageType, } from 'fullfiller-common/src/types'; import { fetchResource } from './common/utils'; type response = { extract: string; }; /** * Fetch Wikipedia article body. * @param title Wikipedia article title. * @param format Which one of the 2 formats available in the Wikipedia API. * @param related Wikipedia related articles titles (to recommend in case of error). * @returns Wikipedia article body. */ async function getArticleBody( language: languageType, title: string, format: formatType ): Promise<string> { const queries = { action: 'query', prop: 'extracts', ...(format === 'plain' && { explaintext: undefined }), redirects: undefined, titles: encodeURIComponent(title), }; const resp = (await fetchResource(language, queries)) as unknown as response; const body = resp.extract; return body; } export default getArticleBody;