Skip to content

Commit

Permalink
Add mobileHtml(), get mobile-optimised HTML
Browse files Browse the repository at this point in the history
Returns HTML for mobile for a given page.

resolving #14
  • Loading branch information
friendofdog committed Apr 19, 2021
1 parent ba806cc commit f4a156d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
20 changes: 19 additions & 1 deletion source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Page, {
} from './page';
import { coordinatesResult, eventResult, geoSearchResult, imageResult, langLinksResult, languageResult,
mobileSections, relatedResult,
title, wikiMediaResult, wikiSearchResult, wikiSummary } from './resultTypes';
title, wikiMediaResult, wikiSearchResult, wikiSummary, notFound } from './resultTypes';
import {
categoriesError,
contentError, coordinatesError, eventsError, geoSearchError, htmlError, imageError, infoboxError,
Expand Down Expand Up @@ -546,6 +546,24 @@ wiki.random = async (format?: randomFormats): Promise<wikiSummary | title | rela
}
}

/**
* Returns mobile-optimised HTML of a page
*
* @param title - The title of the page to query
* @param redirect - Whether to redirect in case of 302
* @returns Returns HTML string
*/

wiki.mobileHtml = async (title: string, redirect?: boolean): Promise<notFound | string> => {
try {
const path = `page/mobile-html/${title}`;
const result = await makeRestRequest(path, redirect);
return result;
} catch (error) {
throw new htmlError(error);
}
}

export default wiki;
// For CommonJS default export support
module.exports = wiki;
Expand Down
10 changes: 9 additions & 1 deletion source/resultTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,12 @@ export interface section {
export interface htmlText {
html: string,
text: string
}
}

export interface notFound {
type: string,
title: string,
method: string,
detail: string,
uri: string
}
26 changes: 26 additions & 0 deletions test/mobileHtml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as request from '../source/request';
import wiki from "../source/index";
import { htmlString, notFoundJson } from './samples';
const requestMock = jest.spyOn(request, "makeRestRequest");

afterAll(() => {
requestMock.mockRestore();
});

test('Returns HTML when page successfully queried', async () => {
requestMock.mockImplementation(async () => { return htmlString });
const result = await wiki.mobileHtml("ACID");
expect(result).toStrictEqual(htmlString);
});

test('Returns notFound if page not found', async () => {
requestMock.mockImplementation(async () => { return notFoundJson });
const result = await wiki.mobileHtml("does-not-exist-on-wikipedia");
expect(result).toEqual(notFoundJson);
});

test('Returns empty body on redirect page with redirect set to false', async () => {
requestMock.mockImplementation(async () => { return null });
const result = await wiki.mobileHtml("homestar", false);
expect(result).toBeNull();
});
10 changes: 10 additions & 0 deletions test/samples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,13 @@ export const title = {
}
]
}

export const notFoundJson = {
type: "https://mediawiki.org/wiki/HyperSwitch/errors/not_found",
title: "Not found.",
method: "get",
detail: "Page or revision not found.",
uri: "/en.wikipedia.org/v1/page/mobile-html/does-not-exist-on-wikipedia"
}

export const htmlString = "<!DOCTYPE html><html>abcde</html>";

0 comments on commit f4a156d

Please sign in to comment.