/ clis / nowcoder / suggest.js
suggest.js
 1  import { cli } from '@jackwener/opencli/registry';
 2  
 3  cli({
 4      site: 'nowcoder',
 5      name: 'suggest',
 6      description: 'Search suggestions',
 7      domain: 'www.nowcoder.com',
 8      args: [
 9          { name: 'query', positional: true, required: true, help: 'Search keyword' },
10      ],
11      columns: ['rank', 'suggestion', 'type'],
12      pipeline: [
13          { navigate: 'https://www.nowcoder.com' },
14          { evaluate: `(async () => {
15    const query = \${{ args.query | json }};
16    const r = await fetch('https://gw-c.nowcoder.com/api/sparta/search/suggest', {
17      method: 'POST',
18      credentials: 'include',
19      headers: {'Content-Type': 'application/json'},
20      body: JSON.stringify({query})
21    });
22    const d = await r.json();
23    if (!d.success) throw new Error(d.msg || 'suggest failed');
24    return (d.data?.records || []).map((item, i) => ({
25      rank: i + 1,
26      suggestion: item.name || '',
27      type: item.typeName || 'general',
28    }));
29  })()
30  ` },
31          { limit: '10' },
32      ],
33  });