From cba933732a5fd7eb06acd23636f9556c50514449 Mon Sep 17 00:00:00 2001 From: suzuki3jp Date: Mon, 20 Jan 2025 08:24:18 +0900 Subject: [PATCH] docs(UseCases): Expand use cases documentation for error handling, pagination and playlists --- docs/02-usecases.md | 75 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 2 deletions(-) diff --git a/docs/02-usecases.md b/docs/02-usecases.md index 5c8448f..d0c320c 100644 --- a/docs/02-usecases.md +++ b/docs/02-usecases.md @@ -1,2 +1,73 @@ -# Use cases -coming soon... \ No newline at end of file +# Use Cases + +## Note +All methods in `youtubes.js` return [`Result`](https://github.com/suzuki3jp/result4js) for safe error handling. +Learn more about error handling with `Result` [here](./01-introduction.md#handling-errors). + +## Pagination +The YouTube Data API v3 sometimes returns paginated responses. +For such endpoints, `youtubes.js` returns data wrapped in [`Pagination`](../src/Pagination.ts) to simplify requesting additional pages. + +```ts +import { ApiClient, StaticOAuthProvider } from "youtubes.js"; + +const oauth = new StaticOAuthProvider({ + accessToken: "ACCESS_TOKEN", +}); +const client = new ApiClient({ oauth }); + +const playlists = await client.playlists.getMine(); // Result, YouTubesJsErrors> +console.log(playlists.data); // The first page of playlists + +// Get the previous page +// Returns null if no previous page exists +const prevPage = await playlists.prev(); // Result, YouTubesJsErrors> | null + +// Get the next page +// Returns null if no next page exists +const nextPage = await playlists.next(); // Result, YouTubesJsErrors> | null + +// Get all pages +// **NOTE**: This method may consume unnecessary quotas, so be careful when using it in actual applications. +// We strongly recommend fetching the next page based on user actions (e.g., scrolling). +const allPages = await playlists.all() // Result, YouTubesJsErrors> +const playlists = await client.playlists.getByIds(["ID1", "ID2"]); // Result, YouTubesJsErrors> +const channelPlaylists = await client.playlists.getByChannelId("CHANNEL_ID"); // Result, YouTubesJsErrors> +``` \ No newline at end of file