From 34857ecb7153af8278c5722d412cd4be4432d237 Mon Sep 17 00:00:00 2001 From: Philipp S Date: Sun, 2 Jul 2023 12:19:37 +0200 Subject: [PATCH 1/4] Update utils.ts --- source/utils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/utils.ts b/source/utils.ts index 33966b3..fb44e34 100644 --- a/source/utils.ts +++ b/source/utils.ts @@ -4,7 +4,7 @@ import { MSGS } from "./messages"; //check if input is string export function isString(title: any){ - return isNaN(title); + return typeof title === 'string' || title instanceof String; } //set title for page in case autoSuggest is true @@ -59,4 +59,4 @@ export function getCurrentDay(): number { const date = new Date(); const day = date.getDate(); return day; -} \ No newline at end of file +} From b280dc7c0dffa4b707063ec2d8a867accadd1fbd Mon Sep 17 00:00:00 2001 From: Philipp S Date: Sun, 2 Jul 2023 12:19:53 +0200 Subject: [PATCH 2/4] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5dce73e..2ba5091 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wikipedia", - "version": "2.1.0", + "version": "2.1.1", "description": "A JavaScript wrapper for the wikipedia apis", "main": "dist", "engines": { From f85379b770c59b2f0ccbf4383adb8598d8e6c0fe Mon Sep 17 00:00:00 2001 From: Philipp Seelos Date: Thu, 13 Jul 2023 10:40:36 +0200 Subject: [PATCH 3/4] Updated Typscript Type delcaration and modified tests --- source/utils.ts | 2 +- test/utils.test.ts | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/source/utils.ts b/source/utils.ts index fb44e34..54c2faa 100644 --- a/source/utils.ts +++ b/source/utils.ts @@ -20,7 +20,7 @@ export async function setTitleForPage(title: string) { } //Set page id or title param for legacy api queries -export function setPageIdOrTitleParam(params: any, title: string) { +export function setPageIdOrTitleParam(params: any, title: string|number) { if (isString(title)) { params.titles = title } else { diff --git a/test/utils.test.ts b/test/utils.test.ts index d70ee00..6b861ff 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -41,10 +41,16 @@ test('Sets title param for string titles', () => { expect(result.titles).toBe("Test"); }); -test('Sets page ids params for number titles', () => { +test('Handle number strings as titles', () => { const params = {} const result = setPageIdOrTitleParam(params, "112") - expect(result.pageids).toBe("112"); + expect(result.titles).toBe("112"); +}); + +test('Handle numbers as pageids', () => { + const params = {} + const result = setPageIdOrTitleParam(params, 112) + expect(result.pageids).toBe(112); }); test('Sets pageId from params if present', () => { From e7ace80c7001fe773ed7e671c675d4b3eb887b9f Mon Sep 17 00:00:00 2001 From: Philipp Seelos Date: Thu, 13 Jul 2023 11:49:50 +0200 Subject: [PATCH 4/4] Changed types to match new functionality --- source/index.ts | 62 ++++++++++++++++++++++++------------------------- source/page.ts | 30 ++++++++++++------------ 2 files changed, 46 insertions(+), 46 deletions(-) diff --git a/source/index.ts b/source/index.ts index eefe466..ece1b38 100644 --- a/source/index.ts +++ b/source/index.ts @@ -25,7 +25,7 @@ import { getCurrentDay, getCurrentMonth, getCurrentYear, setPageId, setPageIdOrT * Internally calls wiki.page * */ -const wiki = async (title: string, pageOptions?: pageOptions): Promise => { +const wiki = async (title: string|number, pageOptions?: pageOptions): Promise => { return wiki.page(title, pageOptions); } @@ -39,7 +39,7 @@ const wiki = async (title: string, pageOptions?: pageOptions): Promise => * @param searchOptions - The number of results and if suggestion needed {@link searchOptions | searchOptions } * @returns an array of {@link wikiSearchResult | wikiSearchResult } */ -wiki.search = async (query: string, searchOptions?: searchOptions): Promise => { +wiki.search = async (query: string|number, searchOptions?: searchOptions): Promise => { try { const searchParams: any = { 'list': 'search', @@ -69,10 +69,10 @@ wiki.search = async (query: string, searchOptions?: searchOptions): Promise => { +wiki.page = async (title: string|number, pageOptions?: pageOptions): Promise => { try { if (pageOptions?.autoSuggest) { - title = await setTitleForPage(title); + title = await setTitleForPage(title as string); } let pageParams: any = { prop: 'info|pageprops', @@ -112,10 +112,10 @@ wiki.page = async (title: string, pageOptions?: pageOptions): Promise => { * @param pageOptions - Whether to redirect in case of 302 * @returns The intro string */ -wiki.intro = async (title: string, pageOptions?: pageOptions): Promise => { +wiki.intro = async (title: string|number, pageOptions?: pageOptions): Promise => { try { if (pageOptions?.autoSuggest) { - title = await setTitleForPage(title); + title = await setTitleForPage(title as string); } const result = await intro(title, pageOptions?.redirect); return result; @@ -134,10 +134,10 @@ wiki.intro = async (title: string, pageOptions?: pageOptions): Promise = * @param listOptions - {@link listOptions | listOptions } * @returns an array of imageResult {@link imageResult | imageResult } */ -wiki.images = async (title: string, listOptions?: listOptions): Promise> => { +wiki.images = async (title: string|number, listOptions?: listOptions): Promise> => { try { if (listOptions?.autoSuggest) { - title = await setTitleForPage(title); + title = await setTitleForPage(title as string); } const result = await images(title, listOptions); return result; @@ -159,7 +159,7 @@ wiki.images = async (title: string, listOptions?: listOptions): Promise => { try { if (pageOptions?.autoSuggest) { - title = await setTitleForPage(title); + title = await setTitleForPage(title) as string; } const result = await summary(title, pageOptions?.redirect); return result; @@ -180,10 +180,10 @@ wiki.summary = async (title: string, pageOptions?: pageOptions): Promise => { +wiki.html = async (title: string|number, pageOptions?: pageOptions): Promise => { try { if (pageOptions?.autoSuggest) { - title = await setTitleForPage(title); + title = await setTitleForPage(title as string); } const result = await html(title, pageOptions?.redirect); return result; @@ -202,10 +202,10 @@ wiki.html = async (title: string, pageOptions?: pageOptions): Promise => * @param pageOptions - Whether to redirect in case of 302 * @returns The plain text as string and the parent and revision ids */ -wiki.content = async (title: string, pageOptions?: pageOptions): Promise => { +wiki.content = async (title: string|number, pageOptions?: pageOptions): Promise => { try { if (pageOptions?.autoSuggest) { - title = await setTitleForPage(title); + title = await setTitleForPage(title as string); } const response = await content(title, pageOptions?.redirect); return response.result; @@ -224,10 +224,10 @@ wiki.content = async (title: string, pageOptions?: pageOptions): Promise * @param listOptions - {@link listOptions | listOptions } * @returns The categories as an array of string */ -wiki.categories = async (title: string, listOptions?: listOptions): Promise> => { +wiki.categories = async (title: string|number, listOptions?: listOptions): Promise> => { try { if (listOptions?.autoSuggest) { - title = await setTitleForPage(title); + title = await setTitleForPage(title as string); } const response = await categories(title, listOptions); return response; @@ -252,7 +252,7 @@ wiki.categories = async (title: string, listOptions?: listOptions): Promise => { try { if (pageOptions?.autoSuggest) { - title = await setTitleForPage(title); + title = await setTitleForPage(title) as string; } const response = await related(title, pageOptions?.redirect); return response; @@ -277,7 +277,7 @@ wiki.related = async (title: string, pageOptions?: pageOptions): Promise => { try { if (pageOptions?.autoSuggest) { - title = await setTitleForPage(title); + title = await setTitleForPage(title) as string; } const response = await media(title, pageOptions?.redirect); return response; @@ -296,10 +296,10 @@ wiki.media = async (title: string, pageOptions?: pageOptions): Promise> => { +wiki.links = async (title: string|number, listOptions?: listOptions): Promise> => { try { if (listOptions?.autoSuggest) { - title = await setTitleForPage(title); + title = await setTitleForPage(title as string); } const response = await links(title, listOptions); return response; @@ -318,10 +318,10 @@ wiki.links = async (title: string, listOptions?: listOptions): Promise> => { +wiki.references = async (title: string|number, listOptions?: listOptions): Promise> => { try { if (listOptions?.autoSuggest) { - title = await setTitleForPage(title); + title = await setTitleForPage(title as string); } const response = await references(title, listOptions); return response; @@ -340,10 +340,10 @@ wiki.references = async (title: string, listOptions?: listOptions): Promise => { +wiki.coordinates = async (title: string|number, pageOptions?: pageOptions): Promise => { try { if (pageOptions?.autoSuggest) { - title = await setTitleForPage(title); + title = await setTitleForPage(title as string); } const response = await coordinates(title, pageOptions?.redirect); return response; @@ -362,10 +362,10 @@ wiki.coordinates = async (title: string, pageOptions?: pageOptions): Promise> => { +wiki.langLinks = async (title: string|number, listOptions?: listOptions): Promise> => { try { if (listOptions?.autoSuggest) { - title = await setTitleForPage(title); + title = await setTitleForPage(title as string); } const response = await langLinks(title, listOptions); return response; @@ -384,10 +384,10 @@ wiki.langLinks = async (title: string, listOptions?: listOptions): Promise => { +wiki.infobox = async (title: string|number, pageOptions?: pageOptions): Promise => { try { if (pageOptions?.autoSuggest) { - title = await setTitleForPage(title); + title = await setTitleForPage(title as string); } const response = await infobox(title, pageOptions?.redirect); return response; @@ -406,10 +406,10 @@ wiki.infobox = async (title: string, pageOptions?: pageOptions): Promise => * @param pageOptions - Whether to redirect in case of 302 * @returns The tables as arrays of JSON objects */ -wiki.tables = async (title: string, pageOptions?: pageOptions): Promise> => { +wiki.tables = async (title: string|number, pageOptions?: pageOptions): Promise> => { try { if (pageOptions?.autoSuggest) { - title = await setTitleForPage(title); + title = await setTitleForPage(title as string); } const response = await tables(title, pageOptions?.redirect); return response; @@ -581,7 +581,7 @@ wiki.random = async (format?: randomFormats): Promise => { try { if (pageOptions?.autoSuggest) { - title = await setTitleForPage(title); + title = await setTitleForPage(title) as string; } const result = await mobileHtml(title, pageOptions?.redirect); return result; @@ -600,7 +600,7 @@ wiki.mobileHtml = async (title: string, pageOptions?: pageOptions): Promise => { try { if (pdfOptions?.autoSuggest) { - title = await setTitleForPage(title); + title = await setTitleForPage(title) as string; } const result = await pdf(title, pdfOptions); return result; diff --git a/source/page.ts b/source/page.ts index 0ff254c..69e65be 100644 --- a/source/page.ts +++ b/source/page.ts @@ -430,7 +430,7 @@ export class Page { * @param listOptions - {@link listOptions | listOptions } * @returns an array of imageResult {@link imageResult | imageResult } */ -export const images = async (title: string, listOptions?: listOptions): Promise> => { +export const images = async (title: string|number, listOptions?: listOptions): Promise> => { try { let imageOptions: any = { generator: 'images', @@ -463,7 +463,7 @@ export const images = async (title: string, listOptions?: listOptions): Promise< * @param redirect - Whether to redirect in case of 302 * @returns The intro string */ -export const intro = async (title: string, redirect = true): Promise => { +export const intro = async (title: string|number, redirect = true): Promise => { try { let introOptions: any = { prop: 'extracts', @@ -491,7 +491,7 @@ export const intro = async (title: string, redirect = true): Promise => * * @beta */ -export const html = async (title: string, redirect = true): Promise => { +export const html = async (title: string|number, redirect = true): Promise => { try { let htmlOptions: any = { 'prop': 'revisions', @@ -518,7 +518,7 @@ export const html = async (title: string, redirect = true): Promise => { * @param redirect - Whether to redirect in case of 302 * @returns The plain text as string and the parent and revision ids */ -export const content = async (title: string, redirect = true): Promise => { +export const content = async (title: string|number, redirect = true): Promise => { try { let contentOptions: any = { 'prop': 'extracts|revisions', @@ -552,7 +552,7 @@ export const content = async (title: string, redirect = true): Promise => { * @param listOptions - {@link listOptions | listOptions } * @returns The categories as an array of string */ -export const categories = async (title: string, listOptions?: listOptions): Promise> => { +export const categories = async (title: string|number, listOptions?: listOptions): Promise> => { try { let categoryOptions: any = { prop: 'categories', @@ -577,7 +577,7 @@ export const categories = async (title: string, listOptions?: listOptions): Prom * @param listOptions - {@link listOptions | listOptions } * @returns The links as an array of string */ -export const links = async (title: string, listOptions?: listOptions): Promise> => { +export const links = async (title: string|number, listOptions?: listOptions): Promise> => { try { let linksOptions: any = { prop: 'links', @@ -604,7 +604,7 @@ export const links = async (title: string, listOptions?: listOptions): Promise> => { +export const references = async (title: string|number, listOptions?: listOptions): Promise> => { try { let extLinksOptions: any = { prop: 'extlinks', @@ -630,7 +630,7 @@ export const references = async (title: string, listOptions?: listOptions): Prom * @param redirect - Whether to redirect in case of 302 * @returns The coordinates as {@link coordinatesResult | coordinatesResult} */ -export const coordinates = async (title: string, redirect = true): Promise => { +export const coordinates = async (title: string|number, redirect = true): Promise => { try { let coordinatesOptions: any = { prop: 'coordinates', @@ -655,7 +655,7 @@ export const coordinates = async (title: string, redirect = true): Promise> => { +export const langLinks = async (title: string|number, listOptions?: listOptions): Promise> => { try { let languageOptions: any = { prop: 'langlinks', @@ -688,7 +688,7 @@ export const langLinks = async (title: string, listOptions?: listOptions): Promi * @param redirect - Whether to redirect in case of 302 * @returns The info as JSON object */ -export const infobox = async (title: string, redirect = true): Promise => { +export const infobox = async (title: string|number, redirect = true): Promise => { try { const infoboxOptions: any = { prop: 'revisions', @@ -713,7 +713,7 @@ export const infobox = async (title: string, redirect = true): Promise => { * @param redirect - Whether to redirect in case of 302 * @returns The tables as arrays of JSON objects */ -export const tables = async (title: string, redirect = true): Promise> => { +export const tables = async (title: string|number, redirect = true): Promise> => { try { const tableOptions: any = { prop: 'revisions', @@ -738,7 +738,7 @@ export const tables = async (title: string, redirect = true): Promise * @returns The rawInfo of the page * */ -export const rawInfo = async (title: string, options: any, redirect = true): Promise => { +export const rawInfo = async (title: string|number, options: any, redirect = true): Promise => { try { options = setPageIdOrTitleParam(options, title); const response = await request(options, redirect); @@ -762,7 +762,7 @@ export const rawInfo = async (title: string, options: any, redirect = true): Pro * @remarks * Called in page object and also through wiki default object * - * @param title - The title or page Id of the page + * @param title - The title of the page * @param redirect - Whether to redirect in case of 302 * @returns The summary of the page as {@link wikiSummary | wikiSummary} */ @@ -783,7 +783,7 @@ export const summary = async (title: string, redirect = true): Promise