Files
jackwener__opencli/clis/rest-countries/country.js
jakevin 39943c05e9 feat: 12 read adapters across 6 new sites + contract tests (round 7) (#1350)
* feat: 12 read adapters across 6 new sites + contract tests (round 7)

New sites: wikidata, lichess, rest-countries, nuget, flathub, oeis
- wikidata: search (wbsearchentities) + entity (Special:EntityData) — Q/P/L ids,
  localised label/description with English fallback
- lichess: user + top (perf rankings) — closed accounts → EmptyResultError, no
  silent disabled rows; 13 perf types validated
- rest-countries: country (substring) + region — population-sorted by default,
  flattened languages/currencies/capitals
- nuget: search + package (full version history) — registration page-walking
  for 100+ version histories; case-insensitive id with strict shape gate
- flathub: search + app — appId reverse-DNS, dual-shape timestamp coercion
  (search is unix-seconds int, /appstream is ISO string)
- oeis: search (paginated) + sequence — A-id zero-padded, 12-term preview with
  (+N) suffix, surfaces commentCount/formulaCount/etc instead of full graphs

Contract tests: 6 files × 7 assertions = 42 contract assertions, all green.
Live verified all 12 commands against real APIs.

Audits clean: typed-error 196=baseline, silent-column 103=baseline, 0 Round-7
listing-id-pairing violations (all 6 listings carry round-trip ids).

* fix(nuget): fail fast on malformed registration pages
2026-05-06 16:54:21 +08:00

66 lines
2.2 KiB
JavaScript

// rest-countries country — look up a country by common / official name.
//
// REST Countries' `name/<query>` endpoint matches as substring across both
// common and official names; multiple matches are returned (e.g. "guinea"
// matches Guinea, Guinea-Bissau, Equatorial Guinea, Papua New Guinea).
import { cli, Strategy } from '@jackwener/opencli/registry';
import { EmptyResultError } from '@jackwener/opencli/errors';
import {
COUNTRY_FIELDS,
REST_COUNTRIES_BASE,
projectCountry,
requireBoundedInt,
requireString,
restCountriesFetch,
} from './utils.js';
cli({
site: 'rest-countries',
name: 'country',
access: 'read',
description: 'Look up countries by name (common / official, substring match)',
domain: 'restcountries.com',
strategy: Strategy.PUBLIC,
browser: false,
args: [
{ name: 'name', positional: true, required: true, help: 'Country name (e.g. "japan", "united kingdom")' },
{ name: 'limit', type: 'int', default: 25, help: 'Max rows (1-250)' },
],
columns: [
'rank',
'commonName',
'officialName',
'cca2',
'cca3',
'ccn3',
'capital',
'region',
'subregion',
'population',
'area',
'languages',
'currencies',
'latitude',
'longitude',
'timezones',
'independent',
'unMember',
'landlocked',
'flag',
'url',
],
func: async (args) => {
const name = requireString(args.name, 'name');
const limit = requireBoundedInt(args.limit, 25, 250);
const url = `${REST_COUNTRIES_BASE}/name/${encodeURIComponent(name)}?fields=${COUNTRY_FIELDS}`;
const body = await restCountriesFetch(url, 'rest-countries country');
const list = Array.isArray(body) ? body : [];
if (!list.length) {
throw new EmptyResultError('rest-countries country', `No countries matched "${name}".`);
}
// Sort by population descending so the most "expected" hit is first.
const sorted = [...list].sort((a, b) => (b?.population ?? 0) - (a?.population ?? 0));
return sorted.slice(0, limit).map((c, i) => ({ rank: i + 1, ...projectCountry(c) }));
},
});