fix: type safety for wikiFetch and arxiv abstract truncation
- wikiFetch return Promise<unknown> instead of Promise<any> - Add WikiSearchResult type, remove r: any - Type wikiFetch responses with inline type assertions - Only append ... to abstract when actually truncated
This commit is contained in:
1 parent
3cda14a2ab
commit
4600b9d46d
4 files changed
+7
-5
No files matched your search
@@ -54,7 +54,7 @@ export function parseEntries(xml: string): ArxivEntry[] {
|
||||
id: arxivId,
|
||||
title: extract(e, 'title').replace(/\s+/g, ' '),
|
||||
authors: extractAll(e, 'name').slice(0, 3).join(', '),
|
||||
abstract: extract(e, 'summary').replace(/\s+/g, ' ').slice(0, 200) + '...',
|
||||
abstract: (() => { const s = extract(e, 'summary').replace(/\s+/g, ' '); return s.length > 200 ? s.slice(0, 200) + '...' : s; })(),
|
||||
published: extract(e, 'published').slice(0, 10),
|
||||
url: `https://arxiv.org/abs/${arxivId}`,
|
||||
});
|
||||
|
||||
@@ -2,6 +2,8 @@ import { cli, Strategy } from '../../registry.js';
|
||||
import { CliError } from '../../errors.js';
|
||||
import { wikiFetch } from './utils.js';
|
||||
|
||||
interface WikiSearchResult { title: string; snippet: string; }
|
||||
|
||||
cli({
|
||||
site: 'wikipedia',
|
||||
name: 'search',
|
||||
@@ -18,10 +20,10 @@ cli({
|
||||
const limit = Math.max(1, Math.min(Number(args.limit), 50));
|
||||
const lang = args.lang || 'en';
|
||||
const q = encodeURIComponent(args.keyword);
|
||||
const data = await wikiFetch(lang, `/w/api.php?action=query&list=search&srsearch=${q}&srlimit=${limit}&format=json&utf8=1`);
|
||||
const data = await wikiFetch(lang, `/w/api.php?action=query&list=search&srsearch=${q}&srlimit=${limit}&format=json&utf8=1`) as { query?: { search?: WikiSearchResult[] } };
|
||||
const results = data?.query?.search;
|
||||
if (!results?.length) throw new CliError('NOT_FOUND', 'No articles found', 'Try a different keyword');
|
||||
return results.map((r: any) => ({
|
||||
return results.map((r) => ({
|
||||
title: r.title,
|
||||
snippet: r.snippet.replace(/<[^>]+>/g, '').slice(0, 120),
|
||||
url: `https://${lang}.wikipedia.org/wiki/${encodeURIComponent(r.title.replace(/ /g, '_'))}`,
|
||||
|
||||
@@ -16,7 +16,7 @@ cli({
|
||||
func: async (_page, args) => {
|
||||
const lang = args.lang || 'en';
|
||||
const title = encodeURIComponent(args.title.replace(/ /g, '_'));
|
||||
const data = await wikiFetch(lang, `/api/rest_v1/page/summary/${title}`);
|
||||
const data = await wikiFetch(lang, `/api/rest_v1/page/summary/${title}`) as { title?: string; description?: string; extract?: string; content_urls?: { desktop?: { page?: string } } };
|
||||
if (!data?.title) throw new CliError('NOT_FOUND', `Article "${args.title}" not found`, 'Try searching first: opencli wikipedia search <keyword>');
|
||||
return [{
|
||||
title: data.title,
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
import { CliError } from '../../errors.js';
|
||||
|
||||
export async function wikiFetch(lang: string, path: string): Promise<any> {
|
||||
export async function wikiFetch(lang: string, path: string): Promise<unknown> {
|
||||
const url = `https://${lang}.wikipedia.org${path}`;
|
||||
const resp = await fetch(url, {
|
||||
headers: { 'User-Agent': 'opencli/1.0 (https://github.com/jackwener/opencli)' },
|
||||
|
||||
Reference in new issue
Block a user