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

refactor!: overhaul core classes and remove redundant code #388

Merged
merged 24 commits into from
Apr 28, 2023

Conversation

LuanRT
Copy link
Owner

@LuanRT LuanRT commented Apr 24, 2023

What

To request video info, YouTube.js used to rely on a function from Actions called getVideoInfo. In fact, it was the only function that could make requests to /player in v1 - v2.

After a while, Actions#execute was implemented, thus rendering getVideoInfo redundant. In this PR, that function is removed and endpoints are organized into individual files so we don't have to declare a payload object every time we want to send a request.

Additional Improvements

  • Player.ts - Client version is now appended to deciphered URLs.
  • HTTPClient.ts - Set userAgent, osName, osVersion and platform for Android clients.
  • From now on, userAgent is only included in the payload of Android clients.
  • Clients have been moved to a separate folder and to import them you must use the Clients namespace.
  • Mixins are also in their own folder now and can only be imported through the Mixins namespace.

@LuanRT LuanRT changed the title feat: organize endpoints into classes feat: organize endpoints into classes & remove redundant code Apr 24, 2023
@absidue
Copy link
Collaborator

absidue commented Apr 24, 2023

As those classes contain purely static properties and have no state, they could be converted to const variables and functions instead. While they wouldn't get treeshaken, as they are referenced by other parts of YouTube.js that don't get treeshaken, it would still benefit projects that import YouTube.js and use a bundler. Bundlers can inline functions and variables and they also tend not to mangle the names of class properties (javascript is very dynamic so renaming class properties could break stuff), but they'll happily mangle the names of functions and variables.

Just thought I would mention it, as this is a new feature, so making changes to this PR now wouldn't be breaking any existing functionality.

you could use a naming scheme like this (feel free to pick a completely different one of course):
variables: <NAME>_ENDPOINT_PATH e.g. BROWSE_ENDPOINT_PATH
functions: build<Name>EndpointPayload e.g. buildBrowseEndpointPayload

alternatively you could also just use build and PATH and then namespace the imports, would still be better than (unnecessarily) wrapping them in a class.

Here is my manual "bundling" of the BrowseEndpoint based on the YouTube.js output and what I've seen bundlers do:

class with static props:

class B{static build(o){return{...{browseId:o.browse_id,params:o.params,continuation:o.continuation,client:o.client}}}}B.PATH='/browse'

function and variable (these will probably also get inlined if they only get used once, so the Guide one for example):

const P='/browse';function b(o){return{...{browseId:o.browse_id,params:o.params,continuation:o.continuation,client:o.client}}}

@LuanRT
Copy link
Owner Author

LuanRT commented Apr 24, 2023

@absidue
Thank you for the suggestions.

I won't be going with pure functions such as buildNotificationGetUnseenCountEndpoint because it would be too verbose and difficult to maintain. I'm thinking of refactoring this to a single file called Endpoints and put everything there as objects.

Here's an example:

// ...

export const BrowseEndpoint = {
  PATH: "/browse" as const,
  /**
   * Builds a `/browse` request payload.
   * @param opts - The options to use.
   * @returns The payload.
   */
  build: (opts: BrowseEndpointOptions): IBrowseRequest => {
    return {
      ...{
        browseId: opts.browse_id,
        //....
      },
    };
  }
};

And since TypeScript 5.0 supports the export type * from 'mod'..... - Edit: Actually, just export * from './Endpoints' will do.

Let me know if you have anything else in mind.

@LuanRT LuanRT changed the title feat: organize endpoints into classes & remove redundant code feat: organize endpoints into objects & remove redundant code Apr 25, 2023
@LuanRT LuanRT changed the title feat: organize endpoints into objects & remove redundant code feat: organize endpoints & remove redundant code Apr 26, 2023
@absidue
Copy link
Collaborator

absidue commented Apr 26, 2023

Looks good to me 😄

LuanRT added 3 commits April 26, 2023 18:27
This should reduce confusion for those looking at the codebase for the first time.
@github-actions github-actions bot added the tests label Apr 28, 2023
@LuanRT
Copy link
Owner Author

LuanRT commented Apr 28, 2023

Should probably rename this to something else. I ended up refactoring way more than I first intended.

LuanRT added 5 commits April 28, 2023 06:46
YouTube will sometimes not return the filter chips, which is normal. But then that would cause the test to fail :/
@LuanRT LuanRT changed the title feat: organize endpoints & remove redundant code refactor: overhaul core classes and remove redundant code Apr 28, 2023
@LuanRT LuanRT marked this pull request as ready for review April 28, 2023 21:45
@LuanRT LuanRT changed the title refactor: overhaul core classes and remove redundant code refactor!: overhaul core classes and remove redundant code Apr 28, 2023
@LuanRT LuanRT merged commit 95e0294 into main Apr 28, 2023
@LuanRT LuanRT deleted the refactor/endpoint-builders branch April 28, 2023 22:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants