/ clis / apple-podcasts / search.js
search.js
 1  import { cli, Strategy } from '@jackwener/opencli/registry';
 2  import { CliError } from '@jackwener/opencli/errors';
 3  import { itunesFetch } from './utils.js';
 4  cli({
 5      site: 'apple-podcasts',
 6      name: 'search',
 7      description: 'Search Apple Podcasts',
 8      strategy: Strategy.PUBLIC,
 9      browser: false,
10      args: [
11          { name: 'query', positional: true, required: true, help: 'Search keyword' },
12          { name: 'limit', type: 'int', default: 10, help: 'Max results' },
13      ],
14      columns: ['id', 'title', 'author', 'episodes', 'genre', 'url'],
15      func: async (_page, args) => {
16          const term = encodeURIComponent(args.query);
17          const limit = Math.max(1, Math.min(Number(args.limit), 25));
18          const data = await itunesFetch(`/search?term=${term}&media=podcast&limit=${limit}`);
19          if (!data.results?.length)
20              throw new CliError('NOT_FOUND', 'No podcasts found', `Try a different keyword`);
21          return data.results.map((p) => ({
22              id: p.collectionId,
23              title: p.collectionName,
24              author: p.artistName,
25              episodes: p.trackCount ?? '-',
26              genre: p.primaryGenreName ?? '-',
27              url: p.collectionViewUrl || '',
28          }));
29      },
30  });