PostBuilder
PostBuilder は CreatePostRequest をメソッドチェーンで直感的に組み立てるビルダーです。
インストール
Section titled “インストール”追加インストールは不要です。mixi2-js に同梱されています。
// ESMimport { PostBuilder } from 'mixi2-js/helpers';// CJSconst { PostBuilder } = require('mixi2-js/helpers');import { PostBuilder } from '@otoneko1102/mixi2-js/helpers';なぜ PostBuilder を使うのか?
Section titled “なぜ PostBuilder を使うのか?”通常のオブジェクトリテラルでは、返信と引用を同時に指定してしまうミスや、マスクの設定漏れなどが起きがちです。
// ❌ inReplyToPostId と quotedPostId を同時に指定するミスawait client.createPost({ text: 'Hello!', inReplyToPostId: 'p1', quotedPostId: 'p2', // 同時指定は不可});PostBuilder は reply() を呼ぶと自動的に quote をクリアし、逆も同様です。
// ✅ 安全に構築const request = new PostBuilder('Hello!') .reply('p1') .build();基本的な使い方
Section titled “基本的な使い方”import { PostBuilder } from 'mixi2-js/helpers';import { PostBuilder } from '@otoneko1102/mixi2-js/helpers';// シンプルなポストconst simple = new PostBuilder('Hello mixi2!').build();
// 返信ポストconst reply = new PostBuilder('返信です') .reply('target-post-id') .build();
// 引用ポストconst quote = new PostBuilder('引用コメント') .quote('quoted-post-id') .build();
// メディア付きポストconst withMedia = new PostBuilder('画像付き!') .media(['media-id-1', 'media-id-2']) .build();
// センシティブマスク付きconst sensitive = new PostBuilder('NSFW コンテンツ') .sensitive('注意: 刺激的な内容を含みます') .build();
// ネタバレ防止マスク付きconst spoiler = new PostBuilder('ネタバレあり') .spoiler('映画のネタバレを含みます') .build();
// Client に渡すawait client.createPost(simple);メソッドチェーン
Section titled “メソッドチェーン”すべてのメソッドは this を返すため、チェーンで呼び出せます。
const request = new PostBuilder('複合ポスト') .reply('post-id') .media(['m1', 'm2']) .sensitive('注意') .build();API リファレンス
Section titled “API リファレンス”コンストラクタ
Section titled “コンストラクタ”new PostBuilder(text: string)| 引数 | 型 | 説明 |
|---|---|---|
text | string | ポスト本文(最大 149 文字) |
| メソッド | 引数 | 戻り値 | 説明 |
|---|---|---|---|
reply(postId) | string | this | 返信先を設定(quote をクリア) |
quote(postId) | string | this | 引用先を設定(reply をクリア) |
media(mediaIdList) | string[] | this | 添付メディア ID を設定(最大 4 件) |
sensitive(caption?) | string? | this | センシティブマスクを設定 |
spoiler(caption?) | string? | this | ネタバレマスクを設定 |
mask(postMask) | PostMask | this | カスタムマスクを設定 |
publishing(type) | PostPublishingType | this | 配信設定を変更 |
build() | - | CreatePostRequest | リクエストオブジェクトを構築 |