Files
wehub-resource-sync 9b395f5cc3
E2E Headed Chrome / e2e-headed (macos-15) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (ubuntu-latest) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (windows-latest) (push) Has been cancelled
CI / build (macos-latest) (push) Has been cancelled
CI / build (ubuntu-latest) (push) Has been cancelled
CI / build (windows-latest) (push) Has been cancelled
CI / unit-test (push) Has been cancelled
CI / bun-test (push) Has been cancelled
CI / adapter-test (push) Has been cancelled
CI / smoke-test (macos-latest) (push) Has been cancelled
CI / smoke-test (ubuntu-latest) (push) Has been cancelled
Security Audit / audit (push) Has been cancelled
Build Chrome Extension / build (push) Has been cancelled
Trigger Website Rebuild (Docs Updated) / dispatch (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:39:48 +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) }));
},
});