Add user input for NPM module?

If I were to make some sort of wrapper for lets say pokeapi.co. Is there a way I can add user input?

I would want it to be something like:

FetchPokemon('Pokemon Name') and then it gets all the data for that pokemon.

Something like: https://pokeapi.co/api/v2/pokemon/ as the base URL then after there would be something like https://pokeapi.co/api/v2/pokemon/userinput. The userinput would be what the person put as “Pokemon Name”. Is there a tutorial or something that I can follow to do something like this?

2 Likes

Are you doing this in pure NodeJS? You could look up stdin. Or, you could export the function with optional parameters. Like,

function funcName(param) {
if (param) {
//parse input etc
} else if (!param) {
//o noes no args found ee
}
}
1 Like

Oh, thanks!

2 Likes

If it works, make sure to mark it as the solution!

What would param equal to?

My current code:

index.js

const { get } = require('https');
const { URL, URLSearchParams } = require('url')

function getContent(url) {
    return new Promise((resolve, reject) => {
      get(url, (res) => {
        const {statusCode} = res;
        if(statusCode !== 200) {
          res.resume();
          reject(`Request failed. Status code: ${statusCode}`);
        }
        res.setEncoding('utf8');
        let rawData = '';
        res.on('data', (chunk) => {rawData += chunk});
        res.on('end', () => {
          try {
            const parsedData = JSON.parse(rawData);
            resolve(parsedData);
          } catch(e) {
            reject(`Error: ${e.message}`);
          }
        });
      }).on('error', (err) => {
        reject(`Error: ${err.message}`);
      })
    });
  }

  class PokeClient {
      constructor() {
          let self = this;

          let baseURL = "https://pokeapi.co/api/v2/";
      }
  }

  module.exports = PokeClient;

index.d.ts:

declare class PokeClient {
    "main": {
        pokemon(opts: PokeClient.PokeQueryParams):Promise<PokeClient.PokeResult>
    }
}

declare namespace PokeClient { 

    export interface PokeQueryParams {
        text: string;
      }
      export interface PokeResult {
        pokemon: string;
      }
}

@HK420 param would equal the 1st argument in FetchPokemon()