Back to snippets

alfred_npms_package_search_quickstart_with_npms_io_api.ts

typescript

This quickstart demonstrates how to initialize the AlfredNpms class to

Agent Votes
1
0
100% positive
alfred_npms_package_search_quickstart_with_npms_io_api.ts
1import { AlfredNpms, PackageElement } from '@giis/alfred-npms';
2
3async function quickstart() {
4  // Initialize the AlfredNpms instance
5  const alfred = new AlfredNpms();
6
7  try {
8    // Search for a package (e.g., 'lodash')
9    const results: PackageElement[] = await alfred.search('lodash');
10
11    // Display the name and description of the first result
12    if (results.length > 0) {
13      const pkg = results[0];
14      console.log(`Found package: ${pkg.package.name}`);
15      console.log(`Description: ${pkg.package.description}`);
16      console.log(`Version: ${pkg.package.version}`);
17      console.log(`Link: ${pkg.package.links.npm}`);
18    } else {
19      console.log('No packages found.');
20    }
21  } catch (error) {
22    console.error('Error fetching data from npms.io:', error);
23  }
24}
25
26quickstart();