💬 チャットサービス
チャットサービスは、チャンネル、サブスクリプション、メッセージング機能を含むリアルタイム通信のための包括的なソリューションを提供します。チャンネル管理とサブスクリプション管理を単一の一貫したAPIに統合しています。
🚀 クイックスタート
import express from 'express';
import { middlewares, services, drivers } from '@nodeblocks/backend-sdk';
const { nodeBlocksErrorMiddleware } = middlewares;
const { chatService } = services;
const { getMongoClient } = drivers;
const client = getMongoClient('mongodb://localhost:27017', 'dev');
express()
.use(
chatService(
{
identities: client.collection('identities'),
chatChannels: client.collection('chatChannels'),
chatMessages: client.collection('chatMessages'),
subscriptions: client.collection('subscriptions'),
},
{
authSecrets: {
authEncSecret: 'your-encryption-secret',
authSignSecret: 'your-signing-secret',
},
identity: {
typeIds: {
admin: '100',
guest: '000',
regular: '001',
},
},
}
)
)
.use(nodeBlocksErrorMiddleware())
.listen(8089, () => console.log('Server running'));
📋 エンドポイント概要
チャンネル操作
メソッド | パス | 説明 |
---|---|---|
POST | /channels | 新しいチャンネルを作成 |
GET | /channels | すべてのチャンネルを一覧取得 |
GET | /channels/:channelId | 特定のチャンネルを取得 |
PATCH | /channels/:channelId | チャンネルを更新 |
DELETE | /channels/:channelId | チャンネルを削除 |
サブスクリプション操作
メソッド | パス | 説明 |
---|---|---|
POST | /subscriptions | 新しいサブスクリプションを作成 |
GET | /subscriptions | すべてのサブスクリプションを一覧取得 |
GET | /subscriptions/:subscriptionId | 特定のサブスクリプションを取得 |
DELETE | /subscriptions/:subscriptionId | サブスクリプションを削除 |
メッセージ操作
メソッド | パス | 説明 |
---|---|---|
POST | /messages | 新しいメッセージを作成 |
GET | /messages?channelId=:channelId | チャンネル内のメッセージを一覧取得 |
GET | /messages/:messageId | 特定のメッセージを取得 |
PATCH | /messages/:messageId | メッセージを更新 |
DELETE | /messages/:messageId | メッセージを削除 |
🗄️ エンティティスキーマ
チャンネルエンティティ
{
"name": "string",
"ownerId": "string",
"createdAt": "string (datetime)",
"id": "string",
"updatedAt": "string (datetime)"
}
フィールド詳細:
フィールド | 型 | 自動生成 | 必須 | 説明 |
---|---|---|---|---|
name | string | ❌ | ✅ | チャンネル名/タイトル |
ownerId | string | ❌ | ✅ | このチャンネルの所有者となるアイデンティティのID |
createdAt | datetime | ✅ | ✅ | 作成日時 |
id | string | ✅ | ✅ | 一意識別子(UUID) |
updatedAt | datetime | ✅ | ✅ | 最終更新日時 |
サブスクリプションエンティティ
{
"approved": "boolean",
"channelId": "string",
"permissions": ["string"],
"subscribedAt": "string (datetime)",
"subscribedId": "string",
"createdAt": "string (datetime)",
"id": "string",
"updatedAt": "string (datetime)"
}
フィールド詳細:
フィールド | 型 | 自動生成 | 必須 | 説明 |
---|---|---|---|---|
approved | boolean | ❌ | ❌ | サブスクリプションが承認済みかどうか |
channelId | string | ❌ | ✅ | 対象チャンネルのID |
permissions | array | ❌ | ❌ | 権限文字列の配列(例: ["read", "write"]) |
subscribedAt | datetime | ❌ | ❌ | サブスクリプション作成日時 |
subscribedId | string | ❌ | ❌ | 購読するアイデンティティのID |
createdAt | datetime | ✅ | ✅ | 作成日時 |
id | string | ✅ | ✅ | 一意識別子(UUID) |
updatedAt | datetime | ✅ | ✅ | 最終更新日時 |
メッセージエンティティ
{
"channelId": "string",
"content": "string",
"senderId": "string",
"title": "string",
"createdAt": "string (datetime)",
"id": "string",
"updatedAt": "string (datetime)"
}
フィールド詳細:
フィールド | 型 | 自動生成 | 必須 | 説明 |
---|---|---|---|---|
channelId | string | ❌ | ✅ | メッセージが送信されるチャンネルのID |
content | string | ❌ | ✅ | メッセージ本文 |
senderId | string | ❌ | ✅ | メッセージ送信者のアイデンティティID |
title | string | ❌ | ❌ | 任意のメッセージタイトル |
createdAt | datetime | ✅ | ✅ | 作成日時 |
id | string | ✅ | ✅ | 一意識別子(UUID) |
updatedAt | datetime | ✅ | ✅ | 最終更新日時 |
📝 注意: 自動生成フィールドはサービス側で設定され、作成/更新リクエストに含めないでください。スキーマは
additionalProperties: false
を強制し、定義されたフィールドのみを許可します。
🔐 認証ヘッダー
保護されたエンドポイントでは、次のヘッダーを含めてください:
Authorization: Bearer <access_token>
x-nb-fingerprint: <device_fingerprint>
⚠️ 重要: 認可時にフィンガープリントを指定した場合、認証済みのすべてのリクエストで
x-nb-fingerprint
ヘッダーが必須です。欠如している場合は 401 Unauthorized が返ります。
🔧 APIエンドポイント
📺 チャンネル操作
1. チャンネル作成
指定された情報で新しいチャットチャンネルを作成します。
リクエスト:
- Method:
POST
- Path:
/channels
- ヘッダー:
Content-Type: application/json
Authorization: Bearer <token>
x-nb-fingerprint: <device-fingerprint>
- 認可: ベアラートークン必須
バリデーション:
- スキーマ検証: 自動適用(name と ownerId が必須、追加プロパティなし)
- ルートバリデーション:
- 認証済みリクエスト(ベアラー)必須
- 管理者ロールまたは本人(ownerId が認証ユーザーと一致)
リクエストボディ:
フィールド | 型 | 必須 | 説明 |
---|---|---|---|
name | string | ✅ | チャンネル名/タイトル |
ownerId | string | ✅ | このチャンネルを所有するユーザーのID |
レスポンスボディ:
フィールド | 型 | 説明 |
---|---|---|
name | string | チャンネル名/タイトル |
ownerId | string | このチャンネルを所有するユーザーのID |
createdAt | string | 作成日時 |
id | string | チャンネル一意ID |
updatedAt | string | 最終更新日時 |
リクエスト例:
curl -X POST http://localhost:8089/channels \
-H "Content-Type: application/json" \
-d '{
"name": "Project Updates",
"ownerId": "user-456"
}'
成功レスポンス:
HTTP/1.1 201 Created
Content-Type: application/json
{
"name": "Project Updates",
"ownerId": "user-456",
"createdAt": "2025-06-24T08:33:40.146Z",
"id": "af62eac3-06aa-481a-8c44-a029e96de2ed",
"updatedAt": "2025-06-24T08:33:40.146Z"
}
バリデーションエラー例:
必須フィールド不足:
{
"error": {
"message": "must have ownerId when creating a new channel"
}
}
スキーマ検証エラー:
{
"error": {
"message": "Validation Error",
"data": [
"request body must have required property 'ownerId'"
]
}
}
追加プロパティエラー:
{
"error": {
"message": "Validation Error",
"data": [
"request body must NOT have additional properties"
]
}
}
{
"error": {
"message": "Validation Error",
"data": [
"request body must NOT have additional properties",
"request body must NOT have additional properties"
]
}
}
2. チャンネル一覧
フィルタやページングを指定して全チャンネルを取得します。
リクエスト:
- Method:
GET
- Path:
/channels
- ヘッダー:
Authorization: Bearer <token>
x-nb-fingerprint: <device-fingerprint>
- 認可: ベアラートークン必須
バリデーション:
- スキーマ検証: name, ownerId とページング(page, limit)のクエリ検証
- ルートバリデーション:
- 認証済みリクエスト(ベアラー)必須
- 管理者ロール、または本人(自分のチャンネルを取得する場合)
クエリパラメータ:
パラメータ | 型 | 必須 | 説明 |
---|---|---|---|
name | string | ❌ | チャンネル名でフィルタ |
ownerId | string | ❌ | 所有者IDでフィルタ |
page | number | ❌ | ページ番号 |
limit | number | ❌ | 1ページあたりの件数 |
レスポンスボディ:
フィールド | 型 | 説明 |
---|---|---|
name | string | チャンネル名/タイトル |
ownerId | string | このチャンネルを所有するユーザーのID |
createdAt | string | 作成日時 |
id | string | チャンネル一意ID |
updatedAt | string | 最終更新日時 |
リクエスト例:
curl http://localhost:8089/channels
成功レスポンス:
HTTP/1.1 200 OK
Content-Type: application/json
[
{
"name": "Project Updates",
"ownerId": "user-456",
"createdAt": "2025-06-24T08:33:40.146Z",
"id": "af62eac3-06aa-481a-8c44-a029e96de2ed",
"updatedAt": "2025-06-24T08:33:40.146Z"
}
]
3. チャンネル取得
ID で特定のチャンネルを取得します。
リクエスト:
- Method:
GET
- Path:
/channels/:channelId
- ヘッダー:
Authorization: Bearer <token>
x-nb-fingerprint: <device-fingerprint>
- 認可: ベアラートークン必須
バリデーション:
- スキーマ検証: チャンネルIDのパスパラメータ検証
- ルートバリデーション:
- 認証済みリクエスト(ベアラー)必須
- 管理者ロール、チャンネル所有者、または当該チャンネルの有効なサブスクリプション
リクエスト例:
curl http://localhost:8089/channels/af62eac3-06aa-481a-8c44-a029e96de2ed
4. チャンネル更新
既存のチャンネルのプロパティを更新します。
リクエスト:
- Method:
PATCH
- Path:
/channels/:channelId
- ヘッダー:
Content-Type: application/json
Authorization: Bearer <token>
x-nb-fingerprint: <device-fingerprint>
- 認可: ベアラートークン必須
バリデーション:
- スキーマ検証: 部分更新スキーマ(全フィールド任意、追加プロパティなし)
- ルートバリデーション:
- 認証済みリクエスト(ベアラー)必須
- 管理者ロールまたはチャンネル所有者
リクエストボディ:
フィールド | 型 | 必須 | 説明 |
---|---|---|---|
name | string | ❌ | 新しいチャンネル名/タイトル |
ownerId | string | ❌ | 新しい所有者ID |
レスポンスボディ:
フィールド | 型 | 説明 |
---|---|---|
name | string | 更新後のチャンネル名/タイトル |
ownerId | string | 更新後の所有者ID |
createdAt | string | 作成日時 |
id | string | チャンネル一意ID |
updatedAt | string | 最終更新日時 |
リクエスト例:
curl -X PATCH http://localhost:8089/channels/af62eac3-06aa-481a-8c44-a029e96de2ed \
-H "Content-Type: application/json" \
-d '{
"name": "Updated Project Updates"
}'
5. チャンネル削除
チャンネルと関連データを削除します。
リクエスト:
- Method:
DELETE
- Path:
/channels/:channelId
- ヘッダー:
Authorization: Bearer <token>
x-nb-fingerprint: <device-fingerprint>
- 認可: ベアラートークン必須
バリデーション:
- スキーマ検証: チャンネルIDのパスパラメータ検証
- ルートバリデーション:
- 認証済みリクエスト(ベアラー)必須
- 管理者ロールまたはチャンネル所有者
レスポンスボディ:
フィールド | 型 | 説明 |
---|---|---|
なし | - | 成功時はレスポンスボディなし |
リクエスト例:
curl -X DELETE http://localhost:8089/channels/af62eac3-06aa-481a-8c44-a029e96de2ed
成功レスポンス:
- ステータス:
204 No Content
- ボディ: (空)
Not Found エラー例:
{
"error": {
"message": "Channel not found"
}
}
📋 サブスクリプション操作
6. サブスクリプション作成
チャンネルへの新規サブスクリプションを作成します。
リクエスト:
- Method:
POST
- Path:
/subscriptions
- ヘッダー:
Content-Type: application/json
Authorization: Bearer <token>
x-nb-fingerprint: <device-fingerprint>
- 認可: ベアラートークン必須
バリデーション:
- スキーマ検証: 自動適用(channelId と subscribedId が必須、追加プロパティなし)
- ルートバリデーション:
- 認証済みリクエスト(ベアラー)必須
- 管理者ロールまたは本人(購読者が認証ユーザー)
リクエストボディ:
フィールド | 型 | 必須 | 説明 |
---|---|---|---|
channelId | string | ✅ | 購読対象のチャンネルID |
approved | boolean | ❌ | サブスクリプションが承認済みか |
permissions | array | ❌ | 権限文字列の配列 |
subscribedAt | string | ❌ | サブスクリプション作成日時 |
subscribedId | string | ❌ | 購読ユーザーのID |
レスポンスボディ:
フィールド | 型 | 説明 |
---|---|---|
channelId | string | 購読対象のチャンネルID |
subscribedId | string | 購読ユーザーのID |
approved | boolean | サブスクリプションが承認済みか |
permissions | array | 権限文字列の配列 |
subscribedAt | string | サブスクリプション作成日時 |
createdAt | string | 作成日時 |
id | string | サブスクリプション一意ID |
updatedAt | string | 最終更新日時 |
リクエスト例:
curl -X POST http://localhost:8089/subscriptions \
-H "Content-Type: application/json" \
-d '{
"channelId": "af62eac3-06aa-481a-8c44-a029e96de2ed",
"subscribedId": "user-456",
"approved": true,
"permissions": ["read", "write"]
}'
成功レスポンス:
HTTP/1.1 201 Created
Content-Type: application/json
{
"channelId": "af62eac3-06aa-481a-8c44-a029e96de2ed",
"subscribedId": "user-456",
"approved": true,
"permissions": ["read", "write"],
"createdAt": "2025-06-27T02:16:07.916Z",
"id": "b17501d6-2576-4a18-86f5-3545da75e678",
"updatedAt": "2025-06-27T02:16:07.916Z"
}
バリデーションエラー例:
{
"error": {
"message": "Validation Error",
"data": [
"request body must have required property 'channelId'",
"request body must have required property 'subscribedId'",
]
}
}
{
"error": {
"message": "Validation Error",
"data": [
"request body must NOT have additional properties",
]
}
}
7. サブスクリプション一覧
フィルタやページングを指定してサブスクリプションを取得します。
リクエスト:
- Method:
GET
- Path:
/subscriptions
- ヘッダー:
Authorization: Bearer <token>
x-nb-fingerprint: <device-fingerprint>
- 認可: ベアラートークン必須
バリデーション:
- スキーマ検証: approved, channelId, subscribedAt, subscribedId とページング(page, limit)のクエリ検証
- ルートバリデーション:
- 認証済みリクエスト(ベアラー)必須
- 管理者、当該チャンネルの有効なサブスクリプション、チャンネル所有者、または本人(購読者)
クエリパラメータ:
パラメータ | 型 | 必須 | 説明 |
---|---|---|---|
approved | boolean | ❌ | 承認状態でフィルタ |
channelId | string | ❌ | チャンネルIDでフィルタ |
subscribedId | string | ❌ | 購読者IDでフィルタ |
subscribedAt | string | ❌ | 購読日時でフィルタ |
page | number | ❌ | ページ番号 |
limit | number | ❌ | 1ページあたりの件数 |
レスポンスボディ:
フィールド | 型 | 説明 |
---|---|---|
channelId | string | 購読対象のチャンネルID |
subscribedId | string | 購読ユーザーのID |
approved | boolean | サブスクリプションが承認済みか |
permissions | array | 権限文字列の配列 |
subscribedAt | string | サブスクリプション作成日時 |
createdAt | string | 作成日時 |
id | string | サブスクリプション一意ID |
updatedAt | string | 最終更新日時 |
リクエスト例:
curl http://localhost:8089/subscriptions
8. サブスクリプション取得
ID で特定のサブスクリプションを取得します。
リクエスト:
- Method:
GET
- Path:
/subscriptions/:subscriptionId
- ヘッダー:
Authorization: Bearer <token>
x-nb-fingerprint: <device-fingerprint>
- 認可: ベアラートークン必須
バリデーション:
- スキーマ検証: サブスクリプションIDのパスパラメータ検証
- ルートバリデーション:
- 認証済みリクエスト(ベアラー)必須
- 管理者ロールまたはサブスクリプション所有者
リクエスト例:
curl http://localhost:8089/subscriptions/b17501d6-2576-4a18-86f5-3545da75e678
9. サブスクリプション削除
サブスクリプションを削除(チャンネルの購読解除)します。
リクエスト:
- Method:
DELETE
- Path:
/subscriptions/:subscriptionId
- ヘッダー:
Authorization: Bearer <token>
x-nb-fingerprint: <device-fingerprint>
- 認可: ベアラートークン必須
バリデーション:
- スキーマ検証: サブスクリプションIDのパスパラメータ検証
- ルートバリデーション:
- 認証済みリクエスト(ベアラー)必須
- 管理者ロールまたはサブスクリプション所有者
レスポンスボディ:
フィールド | 型 | 説明 |
---|---|---|
なし | - | 成功時はレスポンスボディなし |
リクエスト例:
curl -X DELETE http://localhost:8089/subscriptions/b17501d6-2576-4a18-86f5-3545da75e678
成功レスポンス:
- ステータス:
204 No Content
- ボディ: (空)
Not Found エラー例:
{
"error": {
"message": "Subscription not found"
}
}
💬 メッセージ操作
10. メッセージ作成
チャンネルに新しいメッセージを送信します。
リクエスト:
- Method:
POST
- Path:
/messages
- ヘッダー:
Content-Type: application/json
Authorization: Bearer <token>
x-nb-fingerprint: <device-fingerprint>
- 認可: ベアラートークン必須
バリデーション:
- スキーマ検証: 自動適用(content, senderId, channelId が必須、追加プロパティなし)
- ルートバリデーション:
- 認証済みリクエスト(ベアラー)必須
- 送信者本人であること
- 当該チャンネルの有効なサブスクリプションがあること
リクエストボディ:
フィールド | 型 | 必須 | 説明 |
---|---|---|---|
channelId | string | ✅ | メッセージが送信されるチャンネルのID |
content | string | ✅ | メッセージ本文 |
senderId | string | ✅ | メッセージ送信者のユーザーID |
title | string | ❌ | 任意のメッセージタイトル |
レスポンスボディ:
フィールド | 型 | 説明 |
---|---|---|
channelId | string | メッセージが送信されるチャンネルのID |
content | string | メッセージ本文 |
senderId | string | メッセージ送信者のユーザーID |
title | string | 任意のメッセージタイトル |
createdAt | string | 作成日時 |
id | string | メッセージ一意ID |
updatedAt | string | 最終更新日時 |
リクエスト例:
curl -X POST http://localhost:8089/messages \
-H "Content-Type: application/json" \
-d '{"channelId": "test-channel-123", "content": "Hello world!", "senderId": "user-456"}'
成功レスポンス:
{
"channelId": "test-channel-123",
"content": "Hello world!",
"senderId": "user-456",
"createdAt": "2025-07-01T03:22:06.178Z",
"id": "2b43d66c-9cab-434b-9cb7-18de8d3ec9c9",
"updatedAt": "2025-07-01T03:22:06.178Z"
}
バリデーションエラー例:
{
"error": {
"message": "Validation Error",
"data": [
"request body must have required property 'senderId'",
"request body must have required property 'channelId'"
]
}
}
11. メッセージ一覧
特定のチャンネルのメッセージを取得します。
リクエスト:
- Method:
GET
- Path:
/messages?channelId=:channelId
- ヘッダー:
Authorization: Bearer <token>
x-nb-fingerprint: <device-fingerprint>
- 認可: ベアラートークン必須
バリデーション:
- スキーマ検証: channelId(必須)、任意の content, senderId, title、およびページング(page, limit)のクエリ検証
- ルートバリデーション:
- 認証済みリクエスト(ベアラー)必須
- 管理者、当該チャンネルの有効なサブスクリプション、または送信者本人
クエリパラメータ:
パラメータ | 型 | 必須 | 説明 |
---|---|---|---|
channelId | string | ✅ | 取得対象のチャンネルID |
content | string | ❌ | メッセージ本文でフィルタ |
senderId | string | ❌ | 送信者IDでフィルタ |
title | string | ❌ | メッセージタイトルでフィルタ |
page | number | ❌ | ページ番号 |
limit | number | ❌ | 1ページあたりの件数 |
レスポンスボディ:
フィールド | 型 | 説明 |
---|---|---|
channelId | string | メッセージが送信されるチャンネルのID |
content | string | メッセージ本文 |
senderId | string | メッセージ送信者のユーザーID |
title | string | 任意のメッセージタイトル |
createdAt | string | 作成日時 |
id | string | メッセージ一意ID |
updatedAt | string | 最終更新日時 |
リクエスト例:
curl "http://localhost:8089/messages?channelId=test-channel-123"
成功レスポンス:
[
{
"channelId": "test-channel-123",
"content": "Updated message content",
"senderId": "user-456",
"createdAt": "2025-07-01T03:22:06.178Z",
"id": "2b43d66c-9cab-434b-9cb7-18de8d3ec9c9",
"updatedAt": "2025-07-01T03:22:34.413Z"
}
]
バリデーションエラー例:
{
"error": {
"message": "Validation Error",
"data": [
"query parameter 'channelId' is required"
]
}
}
12. メッセージ取得(ID指定)
ID で特定のメッセージを取得します。
リクエスト:
- Method:
GET
- Path:
/messages/:messageId
- ヘッダー:
Authorization: Bearer <token>
x-nb-fingerprint: <device-fingerprint>
- 認可: ベアラートークン必須
バリデーション:
- スキーマ検証: メッセージIDのパスパラメータ検証
- ルートバリデーション:
- 認証済みリクエスト(ベアラー)必須
- 管理者ロールまたはメッセージ所有者
リクエスト例:
curl http://localhost:8089/messages/2b43d66c-9cab-434b-9cb7-18de8d3ec9c9
成功レスポンス:
{
"channelId": "test-channel-123",
"content": "Updated message content",
"senderId": "user-456",
"createdAt": "2025-07-01T03:22:06.178Z",
"id": "2b43d66c-9cab-434b-9cb7-18de8d3ec9c9",
"updatedAt": "2025-07-01T03:22:34.413Z"
}
Not Found エラー例:
{
"error": {
"message": "Message not found"
}
}
13. メッセージ更新
既存のメッセージ内容を更新します。
リクエスト:
- Method:
PATCH
- Path:
/messages/:messageId
- ヘッダー:
Content-Type: application/json
Authorization: Bearer <token>
x-nb-fingerprint: <device-fingerprint>
- 認可: ベアラートークン必須
バリデーション:
- スキーマ検証: 部分更新スキーマ(全フィールド任意、メッセージIDのパス検証、追加プロパティなし)
- ルートバリデーション:
- 認証済みリクエスト(ベアラー)必須
- 管理者ロールまたはメッセージ所有者
リクエストボディ:
フィールド | 型 | 必須 | 説明 |
---|---|---|---|
content | string | ❌ | 更新後のメッセージ本文 |
title | string | ❌ | 更新後のメッセージタイトル |
レスポンスボディ:
フィールド | 型 | 説明 |
---|---|---|
channelId | string | メッセージが送信されるチャンネルのID |
content | string | 更新後のメッセージ本文 |
senderId | string | メッセージ送信者のユーザーID |
title | string | 更新後のメッセージタイトル |
createdAt | string | 作成日時 |
id | string | メッセージ一意ID |
updatedAt | string | 最終更新日時 |
リクエスト例:
curl -X PATCH http://localhost:8089/messages/2b43d66c-9cab-434b-9cb7-18de8d3ec9c9 \
-H "Content-Type: application/json" \
-d '{"content": "Updated message content"}'
成功レスポンス:
{
"channelId": "test-channel-123",
"content": "Updated message content",
"senderId": "user-456",
"createdAt": "2025-07-01T03:22:06.178Z",
"id": "2b43d66c-9cab-434b-9cb7-18de8d3ec9c9",
"updatedAt": "2025-07-01T03:22:34.413Z"
}
Not Found エラー例:
{
"error": {
"message": "Chat message not found"
}
}
14. メッセージ削除
チャンネルからメッセージを削除します。
リクエスト:
- Method:
DELETE
- Path:
/messages/:messageId
- ヘッダー:
Authorization: Bearer <token>
x-nb-fingerprint: <device-fingerprint>
- 認可: ベアラートークン必須
バリデーション:
- スキーマ検証: メッセージIDのパスパラメータ検証
- ルートバリデーション:
- 認証済みリクエスト(ベアラー)必須
- 管理者ロールまたはメッセージ所有者
レスポンスボディ:
フィールド | 型 | 説明 |
---|---|---|
なし | - | 成功時はレスポンスボディなし |
リクエスト例:
curl -X DELETE http://localhost:8089/messages/81950444-5223-47c8-8a8c-0604c2955f15
成功レスポンス:
- ステータス:
204 No Content
- ボディ: (空)
Not Found Error Example:
{
"error": {
"message": "Chat message not found"
}
}
⚙️ 設定オプション
サービス設定
interface ChatServiceConfiguration {
authSecrets: {
authEncSecret: string; // JWT encryption secret
authSignSecret: string; // JWT signing secret
};
identity?: {
typeIds?: {
admin: string; // Admin user type identifier
guest: string; // Guest user type identifier
regular: string; // Regular user type identifier
};
};
}
設定詳細
チャットサービスの設定は、セキュリティとユーザー種別管理の論理グループに整理されています。
🔐 セキュリティ設定
authSecrets
- JWT トークンのセキュリティシークレット
- 型:
{ authEncSecret: string; authSignSecret: string }
- 説明: JWT の暗号化および署名に使用する秘密鍵(トークン検証に使用)
- 必須: 本番環境では必須
- 子プロパティ:
authEncSecret
: JWT ペイロード暗号化の秘密鍵authSignSecret
: JWT 署名検証の秘密鍵
👥 ユーザー種別設定
user.typeIds
- ユーザー種別識別子の設定
- 型:
{ admin?: string; guest?: string; user?: string }
- 説明: ロールベースアクセス制御のためのカスタムユーザー種別識別子
- デフォルト:
undefined
(デフォルトの種別検証を使用) - 子プロパティ:
admin
: 管理者ユーザー種別の識別子- 型:
string
- 説明: 管理者ユーザーのカスタム識別子
- 利用例: 管理操作のロールベースアクセス制御
- 例:
"admin"
,"administrator"
,"superuser"
- 型:
guest
: ゲストユーザー種別の識別子- 型:
string
- 説明: ゲストユーザーのカスタム識別子
- 利用例: 未認証/一時ユーザーの限定的アクセス
- 例:
"guest"
,"visitor"
,"anonymous"
- 型:
user
: 一般ユーザー種別の識別子- 型:
string
- 説明: 一般ユーザーのカスタム識別子
- 利用例: 標準的なユーザー権限
- 例:
"user"
,"member"
,"customer"
- 型:
設定例
const chatConfig = {
authSecrets: {
authEncSecret: process.env.AUTH_ENC_SECRET || 'your-enc-secret',
authSignSecret: process.env.AUTH_SIGN_SECRET || 'your-sign-secret'
},
user: {
typeIds: {
admin: 'administrator',
guest: 'visitor',
user: 'member'
}
}
};
🚨 エラーハンドリング
チャットサービスのエラーは、適切なHTTPステータスコードとJSON形式で返されます。
代表的なエラーコード
ステータス | エラーメッセージ | 説明 |
---|---|---|
400 | Validation Error | リクエストボディの形式不正または必須項目不足 |
400 | must have ownerId when creating a new channel | リクエストボディに ownerId が存在しない |
400 | request body must have required property 'name' | リクエストボディに name が存在しない |
400 | request body must have required property 'ownerId' | リクエストボディに ownerId が存在しない |
400 | request body must have required property 'channelId' | リクエストボディに channelId が存在しない |
400 | request body must have required property 'subscribedId' | リクエストボディに subscribedId が存在しない |
400 | request body must have required property 'content' | リクエストボディに content が存在しない |
400 | request body must have required property 'senderId' | リクエストボディに senderId が存在しない |
400 | request body must NOT have additional properties | サポートされないフィールドが含まれている |
400 | query parameter 'channelId' is required | 必須のクエリパラメータが不足 |
400 | Failed to create channel | 追加IDが返らず作成に失敗 |
400 | Failed to update channel | 変更が検出されず更新に失敗 |
400 | Failed to delete channel | 削除処理に失敗 |
400 | Failed to create subscription | 追加IDが返らず作成に失敗 |
400 | Failed to delete subscription | 削除処理に失敗 |
400 | Failed to create message | 追加IDが返らず作成に失敗 |
400 | Failed to update message | 変更が検出されず更新に失敗 |
400 | Failed to delete message | 削除処理に失敗 |
401 | token could not be verified | 認可トークンがない/無効 |
401 | Authentication failed | 認証トークンがない/無効 |
401 | Token fails security check | トークンのセキュリティ検証に失敗 |
403 | User is not authorized to access this resource | 権限不足(管理者アクセスが必要) |
404 | Channel not found | 対象のチャンネルが存在しない |
404 | Subscription not found | 対象のサブスクリプションが存在しない |
404 | Message not found | 対象のメッセージが存在しない |
404 | Chat message not found | 対象のチャットメッセージが存在しない |
500 | Failed to create channel | 作成中のDB接続問題/予期せぬ失敗 |
500 | Failed to get channel | 取得中のDB接続問題/予期せぬ失敗 |
500 | Failed to find channels | 一覧取得中のDB接続問題/フィルタ不正/予期せぬ失敗 |
500 | Failed to update channel | 更新中のDB接続問題/予期せぬ失敗 |
500 | Failed to delete channel | 削除中のDB接続問題/予期せぬ失敗 |
500 | Failed to create subscription | 作成中のDB接続問題/予期せぬ失敗 |
500 | Failed to get subscription | 取得中のDB接続問題/予期せぬ失敗 |
500 | Failed to find subscriptions | 一覧取得中のDB接続問題/フィルタ不正/予期せぬ失敗 |
500 | Failed to delete subscription | 削除中のDB接続問題/予期せぬ失敗 |
500 | Failed to create message | 作成中のDB接続問題/予期せぬ失敗 |
500 | Failed to get message | 取得中のDB接続問題/予期せぬ失敗 |
500 | Failed to find messages | 一覧取得中のDB接続問題/フィルタ不正/予期せぬ失敗 |
500 | Failed to update message | 更新中のDB接続問題/予期せぬ失敗 |
500 | Failed to delete message | 削除中のDB接続問題/予期せぬ失敗 |
エラーレスポンス形式
{
"error": {
"message": "Error message description",
"data": ["Additional error details"]
}
}
Validation Errors には追加情報が含まれます:
{
"error": {
"message": "Validation Error",
"data": [
"request body must have required property 'name'",
"request body must have required property 'ownerId'"
]
}
}
🔗 関連ドキュメント
- ユーザーサービス - チャット参加者のユーザー管理
- 組織サービス - チームベースのチャット編成
- 認証サービス - 認証と認可
- エラーハンドリング - エラーパターンの理解
- スキーマコンポーネント - データ検証の概念
- カスタムサービスチュートリアル - チャット機能の拡張