Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import non-exported parts of the package #321

Closed
4 tasks done
KingRainbow44 opened this issue Feb 25, 2023 · 9 comments · Fixed by #318
Closed
4 tasks done

Import non-exported parts of the package #321

KingRainbow44 opened this issue Feb 25, 2023 · 9 comments · Fixed by #318
Labels
question Further information is requested Stale

Comments

@KingRainbow44
Copy link

KingRainbow44 commented Feb 25, 2023

Question

I'm trying to import classes from the package. As far as I'm concerned, you can only import them with the absolute path to the class. As of v3.0.0, and the change to ESModule, you can no longer import classes from the path.

Is it possible to import such classes?
An example imported classes: (and one interface)

import Music from "youtubei.js/dist/src/core/Music";
import Video from "youtubei.js/dist/src/parser/classes/Video";
import Format from "youtubei.js/dist/src/parser/classes/misc/Format";
import PlaylistVideo from "youtubei.js/dist/src/parser/classes/PlaylistVideo";
import { DownloadOptions } from "youtubei.js/dist/src/utils/FormatUtils";

Checklist

  • I am running the latest version.
  • I checked the documentation and found no answer.
  • I have searched the existing issues and made sure this is not a duplicate.
  • I have provided sufficient information.
@KingRainbow44 KingRainbow44 added the question Further information is requested label Feb 25, 2023
@KingRainbow44
Copy link
Author

It's worth noting that I want to import these classes to pass them as types in my project, as well as using operations such as instanceof (ex: if (track instanceof Video)) to check types.

@absidue
Copy link
Collaborator

absidue commented Feb 25, 2023

The majority of classes in YouTube.js are exported from root the library, just in a different way than most other libraries. Some might have been missed and aren't currently exported
Some of them are accessible like this:

import { ClientType } from 'youtube.js';

Others are exported in objects that you import:
src/parser/classes -> YTNodes
src/parser/classes/misc -> Misc
src/parser/youtube -> YT
src/parser/ykids -> YTKids
src/parser/ytmusic -> YTMusic

Why is it like this?

  • Firstly there are a bunch of class in different parts of the library that have the same names, so exporting everything directly wouldn't work without renaming them e.g. src/parser/youtube/Channel.ts, src/parser/classes/Channel.ts and src/parser/ytkids/Channel.ts
  • Secondly all of the classes in src/parser/classes, except in the misc folder, are named the way they are in the Innertube API responses, that is needed so that parser can correctly use the right classes, so changing any of their names would require a complete rewrite of the parser and the library, to use a matching system that uses some other system than class names to match the classes.
  • Thirdly as a lot of the library is dependent on names that YouTube picks, even if the similarly named classes in other parts of the library were renamed now, more classes would likely need to be renamed in the future when YouTube picks names that conflict with other existing classes.

@KingRainbow44
Copy link
Author

It's great that they are accessible through the root of the library, but it doesn't help when trying to pass the type as a function parameter.

Something like:

import Video from "youtubei.js/dist/src/parser/classes/Video";
import PlaylistVideo from "youtubei.js/dist/src/parser/classes/PlaylistVideo";
export function parseVideo(video: Video | PlaylistVideo)

would be translated to:

import { YTNodes } from "youtubei.js";
export function parseVideo(video: YTNodes.Video | YTNodes.PlaylistVideo)

which results in the following TypeScript error:
TS2503: Cannot find namespace 'YTNodes'.

Is there something that I'm missing specifically?

@absidue
Copy link
Collaborator

absidue commented Feb 26, 2023

You could do something like this:

import { YTNodes } from "youtubei.js";
const { Video, PlaylistVideo } = YTNodes;

export function parseVideo(video: Video | PlaylistVideo)

@LuanRT
Copy link
Owner

LuanRT commented Feb 26, 2023

@absidue, @KingRainbow44
This was an issue with TS not liking the way we were exporting the nodes. Should be fixed now (v3.1.0).

@absidue
Copy link
Collaborator

absidue commented Feb 26, 2023

@LuanRT that change only fixes the issue for YTNodes

see #321 (comment) for everything else, also tl;dr you'll have to rename a bunch of classes before you can export them, as exporting multiple identically named Channel classes (also others but that's just the example I chose) won't work.

@LuanRT LuanRT reopened this Feb 26, 2023
@LuanRT
Copy link
Owner

LuanRT commented Feb 27, 2023

@absidue
Reopening this as it may require more attention.

I might be missing something but I tested using those classes as types and it works as expected. If you are sure that's the case, could you provide some examples?

Here's the TypeScript code I used to test this:

import { Innertube, YT, YTKids } from 'youtubei.js';

export function test1(a: YT.Channel, b: YTKids.Channel) {}

export function test2(a: YT.Channel | YTKids.Channel) {}

@absidue
Copy link
Collaborator

absidue commented Feb 28, 2023

If it works that's great, although it should definitely be documented that the types in YouTube.js are exported that way, instead of them all being directly exported. @KingRainbow44 had to open an issue asking how to import classes and I only figured it out from digging through the source code and happening to have been involved in the refactoring pull request.

@github-actions
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested Stale
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants