Node.js Command Line Interface (CLI) app that takes an API key user input and outputs nomics cryptocurrency API data. Tutorial code (see 'Inspiration' below) updated to use ES6 module imports etc.
Note: to open web links in a new window use: ctrl+click on link
Postman used to test API using a simple GET request
node.js bin field used as executable file - maps command name to local file name. Important: file referenced in bin; ./bin/coindex.js starts with #!/usr/bin/env node, otherwise the scripts are started without the node executable!
coindex.js is the first file executed. This calls the files in the commands folder. The key.js file calls the lib class KeyManager which includes key set, view and delete functions. It also calls the utils const isRequired. The check.js file calls the lib classes KeyManager and CryptoAPI and uses axios to make a http call to the nomics cryptocurrency API.
Inquirer v8 CLI user interface, used to prompt user for API key in key.js file
configstore v6 used in KeyManager class to load and persist the API key user input
colors v1 to add colours to node.js (text colors: black, red, green, yellow, blue, magenta, cyan, white, grey. Other options available including bright colors etc.)
type nodejs-api-data to get top level commands list; key to manage API key, check to see crypto currency prices and help
type nodejs-api-data key to get API key commands list; set to add 32-character key, show to see API key and remove to delete the key
type nodejs-api-data key -h to get full list of key help commands
type nodejs-api-data key setto get add key prompt. Enter the key obtained from nomics cryptocurrency API
type nodejs-api-data key showto display the API key
type nodejs-api-data key deleteto delete the API key
type nodejs-api-data check priceto see list of crypto currency prices.
type nodejs-api-data check price --coin=BTC to see price and rank of BTC crypto (comma separated coin names (with NO spaces) used by API GET request - note: putting a coin name of ' ' (empty space) will return a list of ALL 2709 cryto coins!)
๐ป Code Examples
key.js function to set API key from user input. Note: I replaced the isRequired utility to use my own key from the .env file as default if no other key is set. I also added a check of the input string to ensure it is 12 characters long and only contains letters or numbers. Keys are now 32 characters long.
asyncset(){constkeyManager=newKeyManager();constinput=awaitinquirer.prompt([{type: "input",name: "key",message: "Enter API key obtained from https://nomics.com or press ENTER to use default API key. ".green},]);// If user enters an empty string my default API key from the .env file is usedif(input.key===""){constkey=keyManager.setKey(process.env.API_key);console.log("default API key used".blue);returnkey;}// check if user inputs a key that is alphanumeric & 32 characters longelseif(input.key.length===32&&input.key.match(alphaNumeric)){constkey=keyManager.setKey(input.key);console.log("API key set".blue);returnkey;}// user to input key again if the input key was not an empty string or it was not 32 characters longconsole.log("API key should be alphanumeric and 32 characters long. ".red+"Try again.".blue);return;},
๐ Features
npm link command used to make all commands work from any CLI directory