Skip to content

Commit

Permalink
pdf methods added
Browse files Browse the repository at this point in the history
  • Loading branch information
dopecodez committed Aug 19, 2021
1 parent 66aa30a commit 22f5530
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 7 deletions.
7 changes: 7 additions & 0 deletions source/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,10 @@ export class eventsError extends wikiError {
this.name = 'eventsError';
}
}

export class pdfError extends wikiError {
constructor(message: string) {
super(message);
this.name = 'pdfError';
}
}
27 changes: 23 additions & 4 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import request, { makeRestRequest, setAPIUrl } from './request'
import { pageOptions, searchOptions, geoOptions, listOptions, eventOptions, randomFormats } from './optionTypes';
import { pageOptions, searchOptions, geoOptions, listOptions, eventOptions, randomFormats, pdfOptions } from './optionTypes';
import Page, {
intro, images, html, content, categories, links, coordinates, langLinks,
references, infobox, tables, summary, related, media, mobileHtml
references, infobox, tables, summary, related, media, mobileHtml, pdf
} from './page';
import { coordinatesResult, eventResult, geoSearchResult, imageResult, langLinksResult, languageResult,
mobileSections, relatedResult,
title, wikiMediaResult, wikiSearchResult, wikiSummary, notFound } from './resultTypes';
import {
categoriesError,
contentError, coordinatesError, eventsError, geoSearchError, htmlError, imageError, infoboxError,
introError, linksError, mediaError, pageError, relatedError, searchError, summaryError, wikiError
introError, linksError, mediaError, pageError, relatedError, searchError, summaryError, wikiError,
pdfError
} from './errors';
import { MSGS } from './messages';
import { getCurrentDay, getCurrentMonth, setPageId, setPageIdOrTitleParam, setTitleForPage } from './utils';
Expand Down Expand Up @@ -553,7 +554,6 @@ wiki.random = async (format?: randomFormats): Promise<wikiSummary | title | rela
* @param pageOptions - Whether to redirect in case of 302
* @returns Returns HTML string
*/

wiki.mobileHtml = async (title: string, pageOptions?: pageOptions): Promise<notFound | string> => {
try {
if (pageOptions?.autoSuggest) {
Expand All @@ -566,6 +566,25 @@ wiki.mobileHtml = async (title: string, pageOptions?: pageOptions): Promise<notF
}
}

/**
* Returns pdf of a given page
*
* @param title - The title of the page to query
* @param pdfOptions - {@link pdfOptions | pdfOptions }
* @returns Returns pdf format
*/
wiki.pdf = async (title: string, pdfOptions?: pdfOptions): Promise<any> => {
try {
if (pdfOptions?.autoSuggest) {
title = await setTitleForPage(title);
}
const result = await pdf(title, pdfOptions);
return result;
} catch (error) {
throw new pdfError(error);
}
}

export default wiki;
// For CommonJS default export support
module.exports = wiki;
Expand Down
14 changes: 13 additions & 1 deletion source/optionTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,16 @@ export type eventTypes =
'all' | 'selected' | 'births' | 'deaths' | 'events' | 'holidays'

export type randomFormats =
'title' | 'summary' | 'related' | 'mobile-sections' | 'mobile-sections-lead'
'title' | 'summary' | 'related' | 'mobile-sections' | 'mobile-sections-lead'

export type format =
'a4' | 'letter' | 'legal'

export type type =
'desktop' | 'mobile'

export interface pdfOptions {
autoSuggest?: boolean
format?: format
type?: type
}
34 changes: 32 additions & 2 deletions source/page.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { categoriesError, contentError, coordinatesError, htmlError, imageError, wikiError,
import { categoriesError, contentError, coordinatesError, htmlError, imageError, wikiError, pdfError,
infoboxError, introError, linksError, mediaError, preloadError, relatedError, summaryError } from './errors';
import request, { makeRestRequest } from './request';
import { coordinatesResult, imageResult, langLinksResult, notFound, pageResult, relatedResult, wikiMediaResult, wikiSummary } from './resultTypes';
import { setPageId, setPageIdOrTitleParam } from './utils';
import { listOptions, pageOptions } from './optionTypes';
import { listOptions, pageOptions, pdfOptions } from './optionTypes';
import { MSGS } from './messages';

// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down Expand Up @@ -394,6 +394,16 @@ export class Page {
}
}

public pdf = async (pdfOptions: pdfOptions): Promise<any> => {
try {
const result = await pdf(this.title, pdfOptions)

return result;
} catch (error) {
throw new pdfError(error);
}
}

public async runMethod(functionName: string): Promise<any> {
try {
const result = await eval(`this.${functionName}()`);
Expand Down Expand Up @@ -823,4 +833,24 @@ export const mobileHtml = async (title: string, redirect = true): Promise<notFou
}
}

/**
* Returns pdf of a given page
*
* @param title - The title of the page to query
* @param pdfOptions - {@link pdfOptions | pdfOptions }
* @returns Returns pdf format
*/
export const pdf = async (title: string, pdfOptions?: pdfOptions): Promise<any> => {
try {
let path = `page/pdf/${title}`;
pdfOptions?.format ? path += `/${pdfOptions.format}` : null;
pdfOptions?.type ? path += `/${pdfOptions.type}` : null;

const result = await makeRestRequest(path);
return result;
} catch (error) {
throw new pdfError(error);
}
}

export default Page;
1 change: 1 addition & 0 deletions source/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export async function makeRestRequest(path: string, redirect = true): Promise<an
'User-Agent': USER_AGENT
}
}
console.log(encodeURI(REST_API_URL + path));
const response = await fetch(encodeURI(REST_API_URL + path), options);

let result = await response.text();
Expand Down

0 comments on commit 22f5530

Please sign in to comment.