lang option (#131)

This commit is contained in:
MeiMei 2019-06-18 16:55:01 +09:00 committed by syuilo
parent 58b6252442
commit 13ccfbd778
4 changed files with 16 additions and 3 deletions

View File

@ -18,7 +18,13 @@ client.set('maxDataSize', 1024 * 1024);
import Summary from './summary'; import Summary from './summary';
export default async (url: URL.Url): Promise<Summary> => { export default async (url: URL.Url, lang: string = null): Promise<Summary> => {
if (lang && !lang.match(/^[\w-]+(\s*,\s*[\w-]+)*$/)) lang = null;
client.set('headers', {
'Accept-Language': lang
});
const res = await client.fetch(url.href); const res = await client.fetch(url.href);
if (res.error) { if (res.error) {

View File

@ -19,6 +19,11 @@ const builtinPlugins = Object.keys(_builtinPlugins)
.map(key => _builtinPlugins[key]); .map(key => _builtinPlugins[key]);
type Options = { type Options = {
/**
* Accept-Language for the request
*/
lang?: string;
/** /**
* Whether follow redirects * Whether follow redirects
*/ */
@ -38,6 +43,7 @@ type Result = Summary & {
}; };
const defaultOptions = { const defaultOptions = {
lang: null,
followRedirects: true, followRedirects: true,
plugins: null plugins: null
} as Options; } as Options;
@ -58,7 +64,7 @@ export default async (url: string, options?: Options): Promise<Result> => {
const match = plugins.filter(plugin => plugin.test(_url))[0]; const match = plugins.filter(plugin => plugin.test(_url))[0];
// Get summary // Get summary
const summary = await (match ? match.summarize : general)(_url); const summary = await (match ? match.summarize : general)(_url, opts.lang);
if (summary == null) { if (summary == null) {
throw 'failed summarize'; throw 'failed summarize';

View File

@ -3,7 +3,7 @@ import Summary from './summary';
interface IPlugin { interface IPlugin {
test: (url: URL.Url) => boolean; test: (url: URL.Url) => boolean;
summarize: (url: URL.Url) => Promise<Summary>; summarize: (url: URL.Url, lang?: string) => Promise<Summary>;
} }
export default IPlugin; export default IPlugin;

View File

@ -12,6 +12,7 @@ app.use(async ctx => {
try { try {
const summary = await summaly(ctx.query.url, { const summary = await summaly(ctx.query.url, {
lang: ctx.query.lang,
followRedirects: false followRedirects: false
}); });