Skip to content

Commit

Permalink
Merge pull request #55 from yg-i/master
Browse files Browse the repository at this point in the history
protect against the scenario when langlinks is undefined
  • Loading branch information
dopecodez authored Jan 13, 2024
2 parents b6dd295 + c65a4fc commit 121a05e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions source/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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['*'],
Expand Down Expand Up @@ -881,4 +881,4 @@ export const citation = async (query: string, format?: citationFormat, language?
}
}

export default Page;
export default Page;
6 changes: 6 additions & 0 deletions test/langlinks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 121a05e

Please sign in to comment.