Files
jackwener__opencli/clis/wikipedia/utils.js
jakevin d2974a9ff6 refactor(adapters): convert adapter layer from TypeScript to JavaScript (#928)
* refactor(adapters): convert adapter layer from TypeScript to JavaScript

Core framework stays TypeScript; adapter layer moves to JS-first.
Adapters are essentially "executable config + browser scripts" that
barely use TS features — this simplifies the build/distribution pipeline
by removing the dist/clis/ intermediate compilation step.

Changes:
- Convert all 753 adapter files in clis/ from .ts to .js
- Update tsconfig to exclude clis/ from compilation
- Simplify build-manifest to scan clis/*.js directly (no dist/clis/)
- Update discovery, main, fetch-adapters to load JS adapters from clis/
- Update generate-verified to output .js artifacts
- Update package.json files field: dist/clis/ → clis/
- Fix all test files for the .ts → .js transition

* fix(main): use findPackageRoot for BUILTIN_CLIS path

The previous relative path (../../clis from __dirname) only worked for
dist/src/main.js but broke dev mode (tsx src/main.ts) where __dirname
is <repo>/src — resolving to /clis instead of <repo>/clis.

Use findPackageRoot() which works for both dev and prod paths.
2026-04-10 14:52:18 +08:00

32 lines
1.2 KiB
JavaScript

/**
* Wikipedia adapter utilities.
*
* Uses the public MediaWiki REST API and Action API — no key required.
* REST API: https://en.wikipedia.org/api/rest_v1/
* Action API: https://en.wikipedia.org/w/api.php
*/
import { CliError } from '@jackwener/opencli/errors';
/** Maximum character length for article extract fields. */
export const EXTRACT_MAX_LEN = 300;
/** Maximum character length for short description fields. */
export const DESC_MAX_LEN = 80;
export async function wikiFetch(lang, path) {
const url = `https://${lang}.wikipedia.org${path}`;
const resp = await fetch(url, {
headers: { 'User-Agent': 'opencli/1.0 (https://github.com/jackwener/opencli)' },
});
if (!resp.ok) {
throw new CliError('FETCH_ERROR', `Wikipedia API HTTP ${resp.status}`, `Check your title or search term`);
}
return resp.json();
}
/** Map a WikiSummary API response to the standard output row. */
export function formatSummaryRow(data, lang) {
return {
title: data.title,
description: data.description ?? '-',
extract: (data.extract ?? '').slice(0, EXTRACT_MAX_LEN),
url: data.content_urls?.desktop?.page ?? `https://${lang}.wikipedia.org`,
};
}