/ clis / wikipedia / summary.js
summary.js
 1  import { CliError } from '@jackwener/opencli/errors';
 2  import { cli, Strategy } from '@jackwener/opencli/registry';
 3  import { formatSummaryRow, wikiFetch } from './utils.js';
 4  cli({
 5      site: 'wikipedia',
 6      name: 'summary',
 7      description: 'Get Wikipedia article summary',
 8      strategy: Strategy.PUBLIC,
 9      browser: false,
10      args: [
11          { name: 'title', positional: true, required: true, help: 'Article title (e.g. "Transformer (machine learning model)")' },
12          { name: 'lang', default: 'en', help: 'Language code (e.g. en, zh, ja)' },
13      ],
14      columns: ['title', 'description', 'extract', 'url'],
15      func: async (_page, args) => {
16          const lang = args.lang || 'en';
17          const title = encodeURIComponent(args.title.replace(/ /g, '_'));
18          const data = (await wikiFetch(lang, `/api/rest_v1/page/summary/${title}`));
19          if (!data?.title)
20              throw new CliError('NOT_FOUND', `Article "${args.title}" not found`, 'Try searching first: opencli wikipedia search <keyword>');
21          return [formatSummaryRow(data, lang)];
22      },
23  });