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": { 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 { 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', () => {