diff --git a/source/page.ts b/source/page.ts index 0ff254c..788b748 100644 --- a/source/page.ts +++ b/source/page.ts @@ -665,7 +665,7 @@ export const langLinks = async (title: string, listOptions?: listOptions): Promi languageOptions = setPageIdOrTitleParam(languageOptions, title); const response = await request(languageOptions, listOptions?.redirect); const pageId = setPageId(languageOptions, response); - const result = response.query.pages[pageId].langlinks.map((link: any) => { + const result = (response.query.pages[pageId].langlinks ?? []).map((link: any) => { return { lang: link.lang, title: link['*'], @@ -881,4 +881,4 @@ export const citation = async (query: string, format?: citationFormat, language? } } -export default Page; \ No newline at end of file +export default Page; diff --git a/test/langlinks.test.ts b/test/langlinks.test.ts index 1753de5..961ddfb 100644 --- a/test/langlinks.test.ts +++ b/test/langlinks.test.ts @@ -58,6 +58,12 @@ test('Returns empty if no lang links are available', async () => { expect(result).toStrictEqual([]); }); +test('Returns empty if lang links object itself is not available', async () => { + requestMock.mockImplementation(async () => { return { query: { pages: {404: {}} } } }); + const result = await langLinks("Test"); + expect(result).toStrictEqual([]); +}); + test('Returns with results an array of langLinksResult object', async () => { requestMock.mockImplementation(async () => { return { query: { pages: langLinkskMock } } }); const result = await langLinks("Test");