MemberListPager
MemberListPager は、Client.getCommunityMemberList() のカーソルページネーションを自動的に追跡し、for await...of で全メンバーを順に取得できる非同期ジェネレーターを提供します。
インストール
Section titled “インストール”追加インストールは不要です。mixi2-js に同梱されています。
// ESMimport { MemberListPager } from 'mixi2-js/helpers';// CJSconst { MemberListPager } = require('mixi2-js/helpers');import { MemberListPager } from '@otoneko1102/mixi2-js/helpers';なぜ MemberListPager を使うのか?
Section titled “なぜ MemberListPager を使うのか?”getCommunityMemberList はカーソルベースのページネーションで結果を返します。手動でカーソルを管理するのは冗長です:
// ❌ 手動でページネーションlet cursor: string | undefined;const allMembers: User[] = [];do { const result = await client.getCommunityMemberList({ communityId, paginationCursor: cursor }); allMembers.push(...result.members); cursor = result.nextPaginationCursor;} while (cursor);MemberListPager を使えば for await で自然に書けます:
// ✅ シンプルに全件取得const pager = new MemberListPager(client);for await (const member of pager.iterate({ communityId })) { console.log(member.displayName);}基本的な使い方
Section titled “基本的な使い方”import { Client } from 'mixi2-js';import { MemberListPager } from 'mixi2-js/helpers';import { Client } from '@otoneko1102/mixi2-js';import { MemberListPager } from '@otoneko1102/mixi2-js/helpers';const pager = new MemberListPager(client);
// 全メンバーをイテレートfor await (const member of pager.iterate({ communityId: 'community-id' })) { console.log(member.userId, member.displayName);}全件を配列に収集する
Section titled “全件を配列に収集する”const members: User[] = [];for await (const member of pager.iterate({ communityId: 'community-id' })) { members.push(member);}console.log(`メンバー数: ${members.length}`);最大ページ数を制限する
Section titled “最大ページ数を制限する”大量メンバーのコミュニティで一部だけ取得したい場合は maxPages オプションを使います。
// 最初の 3 ページ分だけ取得for await (const member of pager.iterate( { communityId: 'community-id' }, { maxPages: 3 })) { console.log(member.displayName);}途中から再開する
Section titled “途中から再開する”初回取得時のカーソルを保存しておき、次回のリクエストに渡すことで途中から再開できます。
// 最初の 1 ページを手動で取得してカーソルを保存const first = await client.getCommunityMemberList({ communityId: 'community-id' });const savedCursor = first.nextPaginationCursor;
// 後で続きから再開if (savedCursor) { for await (const member of pager.iterate({ communityId: 'community-id', paginationCursor: savedCursor })) { console.log(member.displayName); }}API リファレンス
Section titled “API リファレンス”コンストラクタ
Section titled “コンストラクタ”new MemberListPager(client: Client)| 引数 | 型 | 説明 |
|---|---|---|
client | Client | API クライアント |
iterate(request, options?)
Section titled “iterate(request, options?)”iterate( request: GetCommunityMemberListRequest, options?: MemberListPagerOptions): AsyncGenerator<User>| 引数 | 型 | 説明 |
|---|---|---|
request | GetCommunityMemberListRequest | コミュニティ ID と初期カーソル |
options | MemberListPagerOptions | オプション(maxPages など) |
MemberListPagerOptions
| フィールド | 型 | デフォルト | 説明 |
|---|---|---|---|
maxPages | number | 無制限 | 取得する最大ページ数 |