YouTube.js icon indicating copy to clipboard operation
YouTube.js copied to clipboard

Having trouble trying to override fetch

Open aletorrado opened this issue 7 months ago • 4 comments

Question

Hi, I've been doing some extremely simple tests just trying to override the fetch option, using the native fetch utility, and without adding any additional logic at the moment, but I'm getting errors this way.

This is the code I'm trying:

const innertube = await Innertube.create({
	fetch: async (requestInfo, init)=>{
		// This references to native fetch
		return fetch(requestInfo, init)
	},
});

And this is the error I'm getting:

TypeError: Failed to parse URL from [object Request]
    at fetch (/home/alejandro/newspa/node_modules/undici/index.js:112:13)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at HTTPClient.fetch (/home/alejandro/newspa/node_modules/youtubei.js/src/utils/HTTPClient.ts:155:22)
    at Actions.execute (/home/alejandro/newspa/node_modules/youtubei.js/src/core/Actions.ts:154:22)
    at async Promise.all (index 0)
    at Innertube.getInfo (/home/alejandro/newspa/node_modules/youtubei.js/src/Innertube.ts:111:22)
    at /home/alejandro/newspa/src/tservices/yt/youtubei.ts:15:12 {
  [cause]: TypeError: Invalid URL
      at new NodeError (node:internal/errors:405:5)
      at new URL (node:internal/url:778:13)
      at new Request (/home/alejandro/newspa/node_modules/undici/lib/web/fetch/request.js:115:21)
      at fetch (/home/alejandro/newspa/node_modules/undici/lib/web/fetch/index.js:140:21)
      at fetch (/home/alejandro/newspa/node_modules/undici/index.js:109:18)
      at HTTPClient.fetch (/home/alejandro/newspa/src/tservices/yt/youtubei.ts:12:24)
      at HTTPClient.fetch (/home/alejandro/newspa/node_modules/youtubei.js/src/utils/HTTPClient.ts:155:39)
      at Actions.execute (/home/alejandro/newspa/node_modules/youtubei.js/src/core/Actions.ts:154:46)
      at NavigationEndpoint.call (/home/alejandro/newspa/node_modules/youtubei.js/src/parser/classes/NavigationEndpoint.ts:128:22)
      at Innertube.getInfo (/home/alejandro/newspa/node_modules/youtubei.js/src/Innertube.ts:108:43)
      at /home/alejandro/newspa/src/tservices/yt/youtubei.ts:15:28
      at processTicksAndRejections (node:internal/process/task_queues:95:5) {
    input: '[object Request]',
    code: 'ERR_INVALID_URL'
  }
}

I've tried with Node v20.5.1 and v18.17.1 with the same results.

Am I doing something wrong? Thanks!

Other details

No response

Checklist

  • [x] I am running the latest version.
  • [x] I checked the documentation and found no answer.
  • [x] I have searched the existing issues and made sure this is not a duplicate.
  • [x] I have provided sufficient information.

aletorrado avatar May 08 '25 14:05 aletorrado

It doesn't work with the node-fetch library either. Debugging shows that youtubei.js sends an Undici Request object to the provided fetch function instead of the original Url , whereas the provided fetch function requires either the original Url or its own library's Request object.

gatecrasher777 avatar May 08 '25 16:05 gatecrasher777

I'm not using node-fetch. It's the native fetch, as stated in the comments.

aletorrado avatar May 08 '25 17:05 aletorrado

I'm not using node-fetch. It's the native fetch, as stated in the comments.

I know you are not using node-fetch. I'm saying it doesn't work with the native fetch. And it doesn't work with node-fetch either. And then I explain why it doesn't work in either case.

gatecrasher777 avatar May 08 '25 17:05 gatecrasher777

Try this:

import { Innertube, UniversalCache, Platform } from 'youtubei.js';

(async () => {
  const innertube = await Innertube.create({
    cache: new UniversalCache(true),
    fetch: async  (input, init) => {
      const url = typeof input === 'string'
        ? new URL(input)
        : input instanceof URL
          ? input
          : new URL(input.url);

      const request = new Request(
        url,
        input instanceof Platform.shim.Request? input : undefined
      );
      
      return fetch(request, init);
    }
  });
  
  const info = await innertube.getBasicInfo('zxLu32BizZg');
  console.log(info);
})();

LuanRT avatar May 08 '25 19:05 LuanRT

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Jul 08 '25 02:07 github-actions[bot]