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

Develop #8

Merged
merged 2 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ main();
## Supported endpoints
We are striving to support more endpoints, but currently, there are many unsupported endpoints. If the endpoint you want to use is not supported, please open an [issue](https://github.com/suzuki3jp/youtubes.js/issues/new) to request it. We plan to prioritize adding support for the most requested endpoints.

×: Not supported
⚠️: Partially supported
×: Not supported
⚠️: Partially supported
✅: Fully supported

| Endpoint | list (GET) | insert (POST) | update (PUT) | delete (DELETE) |
Expand All @@ -73,7 +73,7 @@ We are striving to support more endpoints, but currently, there are many unsuppo
| [Members](https://developers.google.com/youtube/v3/docs/members) | × | - | - | - |
| [MembershipsLevels](https://developers.google.com/youtube/v3/docs/membershipsLevels) | × | - | - | - |
| [PlaylistImages](https://developers.google.com/youtube/v3/docs/playlistImages) | × | × | × | × |
| [PlaylistItems](https://developers.google.com/youtube/v3/docs/playlistItems) | ⚠️ | × | × | × |
| [PlaylistItems](https://developers.google.com/youtube/v3/docs/playlistItems) | ⚠️ | | × | × |
| [Playlists](https://developers.google.com/youtube/v3/docs/playlists) | ✅ | ✅ | ✅ | ✅ |
| [Search](https://developers.google.com/youtube/v3/docs/search) | × | - | - | - |
| [Subscriptions](https://developers.google.com/youtube/v3/docs/subscriptions) | × | × | - | × |
Expand Down
2 changes: 2 additions & 0 deletions src/entities/playlist-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,6 @@ export interface PlaylistItemData {
videoOwnerChannelId: string;
videoOwnerChannelName: string;
position: number;

// `contentDetails.note` is never included. idk why
}
54 changes: 54 additions & 0 deletions src/managers/PlaylistItemManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { OAuthProviders } from "../OAuthProvider";
import { Pagination } from "../Pagination";
import {
type PlaylistItem,
playlistItemFrom,
playlistItemFromMany,
} from "../entities/playlist-item";
import { LikelyBugError, type YouTubesJsErrors } from "../errors";
Expand Down Expand Up @@ -73,9 +74,62 @@ export class PlaylistItemManager {
}),
);
}

/**
* Adds a item to a playlist.
*
* - The operation uses 50 quota units.
*
* @param options - The options to create a playlist item.
* @returns - The created playlist item.
*/
public async create(
options: CreatePlaylistItemOptions,
): Promise<Result<PlaylistItem, YouTubesJsErrors>> {
const { playlistId, videoId, position } = options;

const rawData = await wrapGaxios(
this.client.playlistItems.insert({
part: this.ALL_PARTS,
requestBody: {
snippet: {
playlistId,
resourceId: {
kind: "youtube#video",
videoId,
},
position,
},
},
}),
);
if (rawData.isErr()) return Err(rawData.data);
const item = playlistItemFrom(rawData.data, this.logger);
if (item.isErr()) return Err(item.data);

return Ok(item.data);
}
}

interface PlaylistItemManagerOptions {
oauth: OAuthProviders;
logger: Logger;
}

export interface CreatePlaylistItemOptions {
/**
* The ID of the playlist to add the item to.
*/
playlistId: string;

/**
* The ID of the video to add to the playlist.
*/
videoId: string;

/**
* The position of the item in the playlist.
* It must be `0 <= position <= N`, where N is the number of items in the playlist. Otherwise, the YouTube API will return an error.
*/
position?: number;
}
Loading