diff --git a/src/general.ts b/src/general.ts index ec2c2ad..837cb71 100644 --- a/src/general.ts +++ b/src/general.ts @@ -18,7 +18,13 @@ client.set('maxDataSize', 1024 * 1024); import Summary from './summary'; -export default async (url: URL.Url): Promise => { +export default async (url: URL.Url, lang: string = null): Promise => { + if (lang && !lang.match(/^[\w-]+(\s*,\s*[\w-]+)*$/)) lang = null; + + client.set('headers', { + 'Accept-Language': lang + }); + const res = await client.fetch(url.href); if (res.error) { diff --git a/src/index.ts b/src/index.ts index 24d3b2a..0daa6f4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,6 +19,11 @@ const builtinPlugins = Object.keys(_builtinPlugins) .map(key => _builtinPlugins[key]); type Options = { + /** + * Accept-Language for the request + */ + lang?: string; + /** * Whether follow redirects */ @@ -38,6 +43,7 @@ type Result = Summary & { }; const defaultOptions = { + lang: null, followRedirects: true, plugins: null } as Options; @@ -58,7 +64,7 @@ export default async (url: string, options?: Options): Promise => { const match = plugins.filter(plugin => plugin.test(_url))[0]; // Get summary - const summary = await (match ? match.summarize : general)(_url); + const summary = await (match ? match.summarize : general)(_url, opts.lang); if (summary == null) { throw 'failed summarize'; diff --git a/src/iplugin.ts b/src/iplugin.ts index c964895..ebfeb5f 100644 --- a/src/iplugin.ts +++ b/src/iplugin.ts @@ -3,7 +3,7 @@ import Summary from './summary'; interface IPlugin { test: (url: URL.Url) => boolean; - summarize: (url: URL.Url) => Promise; + summarize: (url: URL.Url, lang?: string) => Promise; } export default IPlugin; diff --git a/src/server/index.ts b/src/server/index.ts index 7628d67..e832e42 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -12,6 +12,7 @@ app.use(async ctx => { try { const summary = await summaly(ctx.query.url, { + lang: ctx.query.lang, followRedirects: false });