Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix undefined behaviour for pages with numbers as title (e.g 7) #52

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
62 changes: 31 additions & 31 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { getCurrentDay, getCurrentMonth, getCurrentYear, setPageId, setPageIdOrT
* Internally calls wiki.page
*
*/
const wiki = async (title: string, pageOptions?: pageOptions): Promise<Page> => {
const wiki = async (title: string|number, pageOptions?: pageOptions): Promise<Page> => {
return wiki.page(title, pageOptions);
}

Expand All @@ -39,7 +39,7 @@ const wiki = async (title: string, pageOptions?: pageOptions): Promise<Page> =>
* @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<wikiSearchResult> => {
wiki.search = async (query: string|number, searchOptions?: searchOptions): Promise<wikiSearchResult> => {
try {
const searchParams: any = {
'list': 'search',
Expand Down Expand Up @@ -69,10 +69,10 @@ wiki.search = async (query: string, searchOptions?: searchOptions): Promise<wiki
* @param pageOptions - Whether to redirect, autoSuggest or preload any fields {@link pageOptions | pageOptions }
* @returns The intro string
*/
wiki.page = async (title: string, pageOptions?: pageOptions): Promise<Page> => {
wiki.page = async (title: string|number, pageOptions?: pageOptions): Promise<Page> => {
try {
if (pageOptions?.autoSuggest) {
title = await setTitleForPage(title);
title = await setTitleForPage(title as string);
}
let pageParams: any = {
prop: 'info|pageprops',
Expand Down Expand Up @@ -112,10 +112,10 @@ wiki.page = async (title: string, pageOptions?: pageOptions): Promise<Page> => {
* @param pageOptions - Whether to redirect in case of 302
* @returns The intro string
*/
wiki.intro = async (title: string, pageOptions?: pageOptions): Promise<string> => {
wiki.intro = async (title: string|number, pageOptions?: pageOptions): Promise<string> => {
try {
if (pageOptions?.autoSuggest) {
title = await setTitleForPage(title);
title = await setTitleForPage(title as string);
}
const result = await intro(title, pageOptions?.redirect);
return result;
Expand All @@ -134,10 +134,10 @@ wiki.intro = async (title: string, pageOptions?: pageOptions): Promise<string> =
* @param listOptions - {@link listOptions | listOptions }
* @returns an array of imageResult {@link imageResult | imageResult }
*/
wiki.images = async (title: string, listOptions?: listOptions): Promise<Array<imageResult>> => {
wiki.images = async (title: string|number, listOptions?: listOptions): Promise<Array<imageResult>> => {
try {
if (listOptions?.autoSuggest) {
title = await setTitleForPage(title);
title = await setTitleForPage(title as string);
}
const result = await images(title, listOptions);
return result;
Expand All @@ -159,7 +159,7 @@ wiki.images = async (title: string, listOptions?: listOptions): Promise<Array<im
wiki.summary = async (title: string, pageOptions?: pageOptions): Promise<wikiSummary> => {
try {
if (pageOptions?.autoSuggest) {
title = await setTitleForPage(title);
title = await setTitleForPage(title) as string;
}
const result = await summary(title, pageOptions?.redirect);
return result;
Expand All @@ -180,10 +180,10 @@ wiki.summary = async (title: string, pageOptions?: pageOptions): Promise<wikiSum
*
* @beta
*/
wiki.html = async (title: string, pageOptions?: pageOptions): Promise<string> => {
wiki.html = async (title: string|number, pageOptions?: pageOptions): Promise<string> => {
try {
if (pageOptions?.autoSuggest) {
title = await setTitleForPage(title);
title = await setTitleForPage(title as string);
}
const result = await html(title, pageOptions?.redirect);
return result;
Expand All @@ -202,10 +202,10 @@ wiki.html = async (title: string, pageOptions?: pageOptions): Promise<string> =>
* @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<string> => {
wiki.content = async (title: string|number, pageOptions?: pageOptions): Promise<string> => {
try {
if (pageOptions?.autoSuggest) {
title = await setTitleForPage(title);
title = await setTitleForPage(title as string);
}
const response = await content(title, pageOptions?.redirect);
return response.result;
Expand All @@ -224,10 +224,10 @@ wiki.content = async (title: string, pageOptions?: pageOptions): Promise<string>
* @param listOptions - {@link listOptions | listOptions }
* @returns The categories as an array of string
*/
wiki.categories = async (title: string, listOptions?: listOptions): Promise<Array<string>> => {
wiki.categories = async (title: string|number, listOptions?: listOptions): Promise<Array<string>> => {
try {
if (listOptions?.autoSuggest) {
title = await setTitleForPage(title);
title = await setTitleForPage(title as string);
}
const response = await categories(title, listOptions);
return response;
Expand All @@ -252,7 +252,7 @@ wiki.categories = async (title: string, listOptions?: listOptions): Promise<Arra
wiki.related = async (title: string, pageOptions?: pageOptions): Promise<relatedResult> => {
try {
if (pageOptions?.autoSuggest) {
title = await setTitleForPage(title);
title = await setTitleForPage(title) as string;
}
const response = await related(title, pageOptions?.redirect);
return response;
Expand All @@ -277,7 +277,7 @@ wiki.related = async (title: string, pageOptions?: pageOptions): Promise<related
wiki.media = async (title: string, pageOptions?: pageOptions): Promise<wikiMediaResult> => {
try {
if (pageOptions?.autoSuggest) {
title = await setTitleForPage(title);
title = await setTitleForPage(title) as string;
}
const response = await media(title, pageOptions?.redirect);
return response;
Expand All @@ -296,10 +296,10 @@ wiki.media = async (title: string, pageOptions?: pageOptions): Promise<wikiMedia
* @param listOptions - {@link listOptions | listOptions }
* @returns The links as an array of string
*/
wiki.links = async (title: string, listOptions?: listOptions): Promise<Array<string>> => {
wiki.links = async (title: string|number, listOptions?: listOptions): Promise<Array<string>> => {
try {
if (listOptions?.autoSuggest) {
title = await setTitleForPage(title);
title = await setTitleForPage(title as string);
}
const response = await links(title, listOptions);
return response;
Expand All @@ -318,10 +318,10 @@ wiki.links = async (title: string, listOptions?: listOptions): Promise<Array<str
* @param listOptions - {@link listOptions | listOptions }
* @returns The references as an array of string
*/
wiki.references = async (title: string, listOptions?: listOptions): Promise<Array<string>> => {
wiki.references = async (title: string|number, listOptions?: listOptions): Promise<Array<string>> => {
try {
if (listOptions?.autoSuggest) {
title = await setTitleForPage(title);
title = await setTitleForPage(title as string);
}
const response = await references(title, listOptions);
return response;
Expand All @@ -340,10 +340,10 @@ wiki.references = async (title: string, listOptions?: listOptions): Promise<Arra
* @param pageOptions - Whether to redirect in case of 302
* @returns The coordinates as {@link coordinatesResult | coordinatesResult}
*/
wiki.coordinates = async (title: string, pageOptions?: pageOptions): Promise<coordinatesResult | null> => {
wiki.coordinates = async (title: string|number, pageOptions?: pageOptions): Promise<coordinatesResult | null> => {
try {
if (pageOptions?.autoSuggest) {
title = await setTitleForPage(title);
title = await setTitleForPage(title as string);
}
const response = await coordinates(title, pageOptions?.redirect);
return response;
Expand All @@ -362,10 +362,10 @@ wiki.coordinates = async (title: string, pageOptions?: pageOptions): Promise<coo
* @param listOptions - {@link listOptions | listOptions }
* @returns The links as an array of {@link langLinksResult | langLinksResult }
*/
wiki.langLinks = async (title: string, listOptions?: listOptions): Promise<Array<langLinksResult>> => {
wiki.langLinks = async (title: string|number, listOptions?: listOptions): Promise<Array<langLinksResult>> => {
try {
if (listOptions?.autoSuggest) {
title = await setTitleForPage(title);
title = await setTitleForPage(title as string);
}
const response = await langLinks(title, listOptions);
return response;
Expand All @@ -384,10 +384,10 @@ wiki.langLinks = async (title: string, listOptions?: listOptions): Promise<Array
* @param pageOptions - Whether to redirect in case of 302
* @returns The info as JSON object
*/
wiki.infobox = async (title: string, pageOptions?: pageOptions): Promise<any> => {
wiki.infobox = async (title: string|number, pageOptions?: pageOptions): Promise<any> => {
try {
if (pageOptions?.autoSuggest) {
title = await setTitleForPage(title);
title = await setTitleForPage(title as string);
}
const response = await infobox(title, pageOptions?.redirect);
return response;
Expand All @@ -406,10 +406,10 @@ wiki.infobox = async (title: string, pageOptions?: pageOptions): Promise<any> =>
* @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<Array<any>> => {
wiki.tables = async (title: string|number, pageOptions?: pageOptions): Promise<Array<any>> => {
try {
if (pageOptions?.autoSuggest) {
title = await setTitleForPage(title);
title = await setTitleForPage(title as string);
}
const response = await tables(title, pageOptions?.redirect);
return response;
Expand Down Expand Up @@ -581,7 +581,7 @@ wiki.random = async (format?: randomFormats): Promise<wikiSummary | title | rela
wiki.mobileHtml = async (title: string, pageOptions?: pageOptions): Promise<notFound | string> => {
try {
if (pageOptions?.autoSuggest) {
title = await setTitleForPage(title);
title = await setTitleForPage(title) as string;
}
const result = await mobileHtml(title, pageOptions?.redirect);
return result;
Expand All @@ -600,7 +600,7 @@ wiki.mobileHtml = async (title: string, pageOptions?: pageOptions): Promise<notF
wiki.pdf = async (title: string, pdfOptions?: pdfOptions): Promise<any> => {
try {
if (pdfOptions?.autoSuggest) {
title = await setTitleForPage(title);
title = await setTitleForPage(title) as string;
}
const result = await pdf(title, pdfOptions);
return result;
Expand Down
30 changes: 15 additions & 15 deletions source/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Array<imageResult>> => {
export const images = async (title: string|number, listOptions?: listOptions): Promise<Array<imageResult>> => {
try {
let imageOptions: any = {
generator: 'images',
Expand Down Expand Up @@ -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<string> => {
export const intro = async (title: string|number, redirect = true): Promise<string> => {
try {
let introOptions: any = {
prop: 'extracts',
Expand Down Expand Up @@ -491,7 +491,7 @@ export const intro = async (title: string, redirect = true): Promise<string> =>
*
* @beta
*/
export const html = async (title: string, redirect = true): Promise<string> => {
export const html = async (title: string|number, redirect = true): Promise<string> => {
try {
let htmlOptions: any = {
'prop': 'revisions',
Expand All @@ -518,7 +518,7 @@ export const html = async (title: string, redirect = true): Promise<string> => {
* @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<any> => {
export const content = async (title: string|number, redirect = true): Promise<any> => {
try {
let contentOptions: any = {
'prop': 'extracts|revisions',
Expand Down Expand Up @@ -552,7 +552,7 @@ export const content = async (title: string, redirect = true): Promise<any> => {
* @param listOptions - {@link listOptions | listOptions }
* @returns The categories as an array of string
*/
export const categories = async (title: string, listOptions?: listOptions): Promise<Array<string>> => {
export const categories = async (title: string|number, listOptions?: listOptions): Promise<Array<string>> => {
try {
let categoryOptions: any = {
prop: 'categories',
Expand All @@ -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<Array<string>> => {
export const links = async (title: string|number, listOptions?: listOptions): Promise<Array<string>> => {
try {
let linksOptions: any = {
prop: 'links',
Expand All @@ -604,7 +604,7 @@ export const links = async (title: string, listOptions?: listOptions): Promise<A
* @param listOptions - {@link listOptions | listOptions }
* @returns The references as an array of string
*/
export const references = async (title: string, listOptions?: listOptions): Promise<Array<string>> => {
export const references = async (title: string|number, listOptions?: listOptions): Promise<Array<string>> => {
try {
let extLinksOptions: any = {
prop: 'extlinks',
Expand All @@ -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<coordinatesResult> => {
export const coordinates = async (title: string|number, redirect = true): Promise<coordinatesResult> => {
try {
let coordinatesOptions: any = {
prop: 'coordinates',
Expand All @@ -655,7 +655,7 @@ export const coordinates = async (title: string, redirect = true): Promise<coord
* @param listOptions - {@link listOptions | listOptions }
* @returns The links as an array of {@link langLinksResult | langLinksResult }
*/
export const langLinks = async (title: string, listOptions?: listOptions): Promise<Array<langLinksResult>> => {
export const langLinks = async (title: string|number, listOptions?: listOptions): Promise<Array<langLinksResult>> => {
try {
let languageOptions: any = {
prop: 'langlinks',
Expand Down Expand Up @@ -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<any> => {
export const infobox = async (title: string|number, redirect = true): Promise<any> => {
try {
const infoboxOptions: any = {
prop: 'revisions',
Expand All @@ -713,7 +713,7 @@ export const infobox = async (title: string, redirect = true): Promise<any> => {
* @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<Array<any>> => {
export const tables = async (title: string|number, redirect = true): Promise<Array<any>> => {
try {
const tableOptions: any = {
prop: 'revisions',
Expand All @@ -738,7 +738,7 @@ export const tables = async (title: string, redirect = true): Promise<Array<any>
* @returns The rawInfo of the page
*
*/
export const rawInfo = async (title: string, options: any, redirect = true): Promise<any> => {
export const rawInfo = async (title: string|number, options: any, redirect = true): Promise<any> => {
try {
options = setPageIdOrTitleParam(options, title);
const response = await request(options, redirect);
Expand All @@ -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}
*/
Expand All @@ -783,7 +783,7 @@ export const summary = async (title: string, redirect = true): Promise<wikiSumma
* @remarks
* Called in page object and also through index
*
* @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 related pages and summary as an array of {@link wikiSummary | wikiSummary}
*
Expand All @@ -806,7 +806,7 @@ export const related = async (title: string, redirect = true): Promise<relatedRe
* @remarks
* Called in page object and also through index
*
* @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 related pages and summary as an array of {@link wikiMediaResult | wikiMediaResult}
*
Expand Down
6 changes: 3 additions & 3 deletions source/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -59,4 +59,4 @@ export function getCurrentDay(): number {
const date = new Date();
const day = date.getDate();
return day;
}
}
Loading