samuraitruong / coingecko-api-v3

The nodejs api library for accessing coingecko api v3 , develop with typescript
https://samuraitruong.github.io/coingecko-api-v3/
MIT License
54 stars 30 forks source link

TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received an instance of Object #6

Closed fireridlle closed 2 years ago

fireridlle commented 3 years ago
    const coinGeckoClient = new CoinGeckoClient({
      timeout: 10000,
      autoRetry: true,
    });

   coinGeckoClient.exchangeIdTickers({
        id: 'binance',
        page: 1,
      });

Get an error

TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received an instance of Object

node version 14.16.0

erkanercan commented 3 years ago

You can also get the same error with

const client = new CoinGeckoClient({
    timeout: 10000,
    autoRetry: true,
  });

  useEffect(() => {
    console.log(client.ping());
  }, []);

@samuraitruong

samuraitruong commented 3 years ago

@erkanercan I haven't test this with react. Will fix the issue

samuraitruong commented 3 years ago

@fireridlle

below are my unit test



 it('/exchange/id/tickers should successful', async () => {
      const aave = await client.exchangeIdTickers({ id: 'aave' });
      expect(aave.name).toEqual('Aave');
      expect(aave.tickers.length).toBeGreaterThan(1);
    });```

I cant reproduce the issue, If you can add stack trace error it will help. I doubt that it falls into retrying and something is not quite right in that flow
boomdev commented 2 years ago

This is due to https.request() not recognizing the 3rd argument in agent-base v4. So if you have a dependency that uses agent-base v4 (for example mailgun-js) then this lib will fail... Found this: https://github.com/microsoft/vscode/issues/93167

boomdev commented 2 years ago

Not sure if this should be fixed here or if there is a better way to do this, but by changing your code to the below, it works: In httpGet() :

    const { host, pathname, search } = new URL(url);
    const options = {
      host,
      path: pathname + search,
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      },
      timeout: this.options.timeout, // in ms
    };
    const parseJson = (input: string) => {
      try {
        return JSON.parse(input);
      } catch (err) {
        return input;
      }
    };

Then just use options object:

const req = https.request(options, (res) => { ...

samuraitruong commented 2 years ago

closed #9