diff --git a/src/general.ts b/src/general.ts index 7790394..ea4035b 100644 --- a/src/general.ts +++ b/src/general.ts @@ -12,9 +12,9 @@ import * as client from 'cheerio-httpcli'; client.set('referer', false); client.set('timeout', 10000); -import ISummary from './isummary'; +import Summary from './summary'; -export default async (url: URL.Url): Promise => { +export default async (url: URL.Url): Promise => { const res = await client.fetch(url.href); if (res.error) { diff --git a/src/index.ts b/src/index.ts index 61ab494..a56053c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,16 +6,19 @@ import * as URL from 'url'; import requireAll = require('require-all'); import tracer from 'trace-redirect'; -import ISummary from './isummary'; +import Summary from './summary'; import IPlugin from './iplugin'; import general from './general'; // Load builtin plugins -const builtinPlugins = requireAll({ +const _builtinPlugins = requireAll({ dirname: __dirname + '/plugins' }) as { [key: string]: IPlugin }; -export interface IOptions { +const builtinPlugins = Object.keys(_builtinPlugins) + .map(key => _builtinPlugins[key]); + +type Options = { /** * リダイレクトを追跡するか否か */ @@ -25,27 +28,25 @@ export interface IOptions { * Custom Plugins */ plugins?: IPlugin[]; -} +}; -type Result = ISummary & { +type Result = Summary & { url: string; }; /** * Summary an web page * @param {string} url URL of web page you want to summary - * @param {IOptions} options The options + * @param {Options} options The options * @return {Promise} Promised summary */ -export default async (url: string, options: IOptions): Promise => { +export default async (url: string, options: Options): Promise => { const opts = Object.assign({ followRedirects: true, plugins: null - }, options) as IOptions; + }, options) as Options; - const plugins = Object.keys(builtinPlugins) - .map(key => builtinPlugins[key]) - .concat(opts.plugins || []); + const plugins = builtinPlugins.concat(opts.plugins || []); // Follow redirects const actualUrl = opts.followRedirects ? await tracer(url) : url; diff --git a/src/plugins/amazon.ts b/src/plugins/amazon.ts index 7007c8f..2684c71 100644 --- a/src/plugins/amazon.ts +++ b/src/plugins/amazon.ts @@ -4,7 +4,7 @@ import * as client from 'cheerio-httpcli'; client.set('referer', false); client.set('timeout', 10000); -export function test (url: URL.Url) { +export function test(url: URL.Url) { return url.hostname === 'www.amazon.com' || url.hostname === 'www.amazon.co.jp' || url.hostname === 'www.amazon.ca' || @@ -21,7 +21,7 @@ export function test (url: URL.Url) { url.hostname === 'www.amazon.au' }; -export async function summary (url: URL.Url) { +export async function summary(url: URL.Url) { const res = await client.fetch(url.href); const $: client.CheerioStaticEx = res.$; diff --git a/src/plugins/wikipedia.ts b/src/plugins/wikipedia.ts index efea9b8..0dd42d6 100644 --- a/src/plugins/wikipedia.ts +++ b/src/plugins/wikipedia.ts @@ -5,11 +5,11 @@ import clip from './../utils/clip'; const log = debug('summaly:plugins:wikipedia'); -export function test (url: URL.Url) { +export function test(url: URL.Url) { return /\.wikipedia\.org$/.test(url.hostname); }; -export function summary (url: URL.Url) { +export function summary(url: URL.Url) { return new Promise((res, rej) => { const lang = url.host.split('.')[0]; const title = url.pathname.split('/')[2]; diff --git a/src/isummary.ts b/src/summary.ts similarity index 67% rename from src/isummary.ts rename to src/summary.ts index c6567b4..e5921fd 100644 --- a/src/isummary.ts +++ b/src/summary.ts @@ -1,4 +1,4 @@ -interface ISummary { +type Summary = { description: string; icon: string; sitename: string; @@ -6,4 +6,4 @@ interface ISummary { title: string; } -export default ISummary; +export default Summary;