@todoist/comms-sdk 0.0.1 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +182 -39
  3. package/dist/esm/authentication.js +247 -0
  4. package/dist/esm/clients/add-comment-helper.js +41 -0
  5. package/dist/esm/clients/attachments-client.js +99 -0
  6. package/dist/esm/clients/base-client.js +30 -0
  7. package/dist/esm/clients/channels-client.js +209 -0
  8. package/dist/esm/clients/comments-client.js +177 -0
  9. package/dist/esm/clients/conversation-messages-client.js +167 -0
  10. package/dist/esm/clients/conversations-client.js +281 -0
  11. package/dist/esm/clients/groups-client.js +170 -0
  12. package/dist/esm/clients/hooks-client.js +50 -0
  13. package/dist/esm/clients/inbox-client.js +186 -0
  14. package/dist/esm/clients/notify-audience.js +43 -0
  15. package/dist/esm/clients/reactions-client.js +96 -0
  16. package/dist/esm/clients/search-client.js +140 -0
  17. package/dist/esm/clients/threads-client.js +363 -0
  18. package/dist/esm/clients/users-client.js +368 -0
  19. package/dist/esm/clients/workspace-users-client.js +299 -0
  20. package/dist/esm/clients/workspaces-client.js +177 -0
  21. package/dist/esm/comms-api.js +67 -0
  22. package/dist/esm/consts/endpoints.js +31 -0
  23. package/dist/esm/index.js +20 -0
  24. package/dist/esm/migration.js +92 -0
  25. package/dist/esm/testUtils/msw-handlers.js +45 -0
  26. package/dist/esm/testUtils/msw-setup.js +32 -0
  27. package/dist/esm/testUtils/obsidian-fetch-adapter.js +50 -0
  28. package/dist/esm/testUtils/test-defaults.js +103 -0
  29. package/dist/esm/transport/fetch-with-retry.js +240 -0
  30. package/dist/esm/transport/http-client.js +53 -0
  31. package/dist/esm/transport/http-dispatcher.js +149 -0
  32. package/dist/esm/types/api-version.js +5 -0
  33. package/dist/esm/types/entities.js +508 -0
  34. package/dist/esm/types/enums.js +34 -0
  35. package/dist/esm/types/errors.js +82 -0
  36. package/dist/esm/types/http.js +1 -0
  37. package/dist/esm/types/index.js +6 -0
  38. package/dist/esm/types/requests.js +184 -0
  39. package/dist/esm/utils/case-conversion.js +47 -0
  40. package/dist/esm/utils/conversation-title.js +110 -0
  41. package/dist/esm/utils/headers.js +29 -0
  42. package/dist/esm/utils/index.js +5 -0
  43. package/dist/esm/utils/multipart-upload.js +198 -0
  44. package/dist/esm/utils/reply-recipients.js +111 -0
  45. package/dist/esm/utils/timestamp-conversion.js +54 -0
  46. package/dist/esm/utils/url-helpers.js +202 -0
  47. package/dist/esm/utils/uuidv7.js +170 -0
  48. package/dist/types/authentication.d.ts +183 -0
  49. package/dist/types/clients/add-comment-helper.d.ts +32 -0
  50. package/dist/types/clients/attachments-client.d.ts +55 -0
  51. package/dist/types/clients/base-client.d.ts +37 -0
  52. package/dist/types/clients/channels-client.d.ts +211 -0
  53. package/dist/types/clients/comments-client.d.ts +228 -0
  54. package/dist/types/clients/conversation-messages-client.d.ts +201 -0
  55. package/dist/types/clients/conversations-client.d.ts +366 -0
  56. package/dist/types/clients/groups-client.d.ts +148 -0
  57. package/dist/types/clients/hooks-client.d.ts +29 -0
  58. package/dist/types/clients/inbox-client.d.ts +478 -0
  59. package/dist/types/clients/notify-audience.d.ts +22 -0
  60. package/dist/types/clients/reactions-client.d.ts +57 -0
  61. package/dist/types/clients/search-client.d.ts +71 -0
  62. package/dist/types/clients/threads-client.d.ts +547 -0
  63. package/dist/types/clients/users-client.d.ts +280 -0
  64. package/dist/types/clients/workspace-users-client.d.ts +172 -0
  65. package/dist/types/clients/workspaces-client.d.ts +154 -0
  66. package/dist/types/comms-api.d.ts +73 -0
  67. package/dist/types/consts/endpoints.d.ts +27 -0
  68. package/dist/types/index.d.ts +21 -0
  69. package/dist/types/migration.d.ts +78 -0
  70. package/dist/types/testUtils/msw-handlers.d.ts +28 -0
  71. package/dist/types/testUtils/msw-setup.d.ts +1 -0
  72. package/dist/types/testUtils/obsidian-fetch-adapter.d.ts +29 -0
  73. package/dist/types/testUtils/test-defaults.d.ts +17 -0
  74. package/dist/types/transport/fetch-with-retry.d.ts +4 -0
  75. package/dist/types/transport/http-client.d.ts +14 -0
  76. package/dist/types/transport/http-dispatcher.d.ts +46 -0
  77. package/dist/types/types/api-version.d.ts +6 -0
  78. package/dist/types/types/entities.d.ts +3081 -0
  79. package/dist/types/types/enums.d.ts +55 -0
  80. package/dist/types/types/errors.d.ts +57 -0
  81. package/dist/types/types/http.d.ts +60 -0
  82. package/dist/types/types/index.d.ts +6 -0
  83. package/dist/types/types/requests.d.ts +604 -0
  84. package/dist/types/utils/case-conversion.d.ts +8 -0
  85. package/dist/types/utils/conversation-title.d.ts +84 -0
  86. package/dist/types/utils/headers.d.ts +13 -0
  87. package/dist/types/utils/index.d.ts +6 -0
  88. package/dist/types/utils/multipart-upload.d.ts +59 -0
  89. package/dist/types/utils/reply-recipients.d.ts +60 -0
  90. package/dist/types/utils/timestamp-conversion.d.ts +13 -0
  91. package/dist/types/utils/url-helpers.d.ts +129 -0
  92. package/dist/types/utils/uuidv7.d.ts +42 -0
  93. package/package.json +80 -8
@@ -0,0 +1,43 @@
1
+ import { NOTIFY_AUDIENCE_GROUP_IDS, NOTIFY_AUDIENCES } from '../types/enums.js';
2
+ const SENTINEL_GROUP_IDS = new Set(Object.values(NOTIFY_AUDIENCE_GROUP_IDS));
3
+ function isNotifyAudience(value) {
4
+ return typeof value === 'string' && NOTIFY_AUDIENCES.includes(value);
5
+ }
6
+ function collectMarkerOffenses(field, values) {
7
+ if (!values)
8
+ return null;
9
+ const offending = values.filter((id) => SENTINEL_GROUP_IDS.has(id));
10
+ return offending.length > 0 ? { field, offending } : null;
11
+ }
12
+ /**
13
+ * Normalizes a `notifyAudience` flag into a sentinel `groups` entry
14
+ * (`EVERYONE` / `EVERYONE_IN_THREAD`) and rejects callers who put those
15
+ * reserved markers directly into `groups` / `directGroupMentions`.
16
+ *
17
+ * When `notifyAudience` is set it is stripped and its sentinel is appended to
18
+ * `groups`; the backend only understands the marker in `groups`. When it is
19
+ * unset only the empty flag is dropped, preserving any explicit
20
+ * `groups` / `directGroupMentions` (including a `null`) on the wire.
21
+ */
22
+ export function applyNotifyAudience(params) {
23
+ const offenses = [
24
+ collectMarkerOffenses('groups', params.groups ?? undefined),
25
+ collectMarkerOffenses('directGroupMentions', params.directGroupMentions ?? undefined),
26
+ ].filter((o) => o !== null);
27
+ if (offenses.length > 0) {
28
+ const details = offenses
29
+ .map(({ field, offending }) => `\`${field}\` contains ${offending.join(', ')}`)
30
+ .join('; ');
31
+ throw new Error(`Reserved broadcast marker IDs found: ${details}. Pass these via \`notifyAudience\` (e.g. \`notifyAudience: 'channel'\` for EVERYONE) instead of populating \`groups\` / \`directGroupMentions\` directly.`);
32
+ }
33
+ if (params.notifyAudience == null) {
34
+ const { notifyAudience: _unset, ...rest } = params;
35
+ return rest;
36
+ }
37
+ if (!isNotifyAudience(params.notifyAudience)) {
38
+ throw new Error(`Invalid \`notifyAudience\` value "${String(params.notifyAudience)}". Expected one of: ${NOTIFY_AUDIENCES.join(', ')}.`);
39
+ }
40
+ const sentinel = NOTIFY_AUDIENCE_GROUP_IDS[params.notifyAudience];
41
+ const { notifyAudience: _stripped, groups, ...rest } = params;
42
+ return { ...rest, groups: [...(groups ?? []), sentinel] };
43
+ }
@@ -0,0 +1,96 @@
1
+ import { ENDPOINT_REACTIONS } from '../consts/endpoints.js';
2
+ import { request } from '../transport/http-client.js';
3
+ import { BaseClient } from './base-client.js';
4
+ function reactionTarget(args) {
5
+ if (args.threadId)
6
+ return { threadId: args.threadId };
7
+ if (args.commentId)
8
+ return { commentId: args.commentId };
9
+ if (args.messageId)
10
+ return { messageId: args.messageId };
11
+ throw new Error('Must provide one of: threadId, commentId, or messageId');
12
+ }
13
+ /**
14
+ * Client for interacting with Comms reaction endpoints.
15
+ */
16
+ export class ReactionsClient extends BaseClient {
17
+ /**
18
+ * Adds an emoji reaction to a thread, comment, or conversation message.
19
+ *
20
+ * @param args - The arguments for adding a reaction.
21
+ * @param args.threadId - Optional thread ID.
22
+ * @param args.commentId - Optional comment ID.
23
+ * @param args.messageId - Optional message ID (for conversation messages).
24
+ * @param args.reaction - The reaction emoji to add.
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * await api.reactions.add({ threadId: '7YpL3oZ4kZ9vP7Q1tR2sX3z', reaction: '👍' })
29
+ * ```
30
+ */
31
+ add(args) {
32
+ return request({
33
+ httpMethod: 'POST',
34
+ baseUri: this.getBaseUri(),
35
+ relativePath: `${ENDPOINT_REACTIONS}/add`,
36
+ apiToken: this.apiToken,
37
+ payload: { ...reactionTarget(args), reaction: args.reaction },
38
+ headers: this.headers,
39
+ customFetch: this.customFetch,
40
+ }).then(() => undefined);
41
+ }
42
+ /**
43
+ * Gets reactions for a thread, comment, or conversation message.
44
+ *
45
+ * Returns an object with emoji reactions as keys and arrays of user IDs as
46
+ * values, or null if no reactions.
47
+ *
48
+ * @param args - The arguments for getting reactions.
49
+ * @param args.threadId - Optional thread ID.
50
+ * @param args.commentId - Optional comment ID.
51
+ * @param args.messageId - Optional message ID (for conversation messages).
52
+ * @returns A reaction object with emoji reactions as keys and arrays of user IDs as values, or null if no reactions.
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * const reactions = await api.reactions.get({ threadId: '7YpL3oZ4kZ9vP7Q1tR2sX3z' })
57
+ * // Returns: { "👍": [101, 202, 303], "❤️": [101, 202] }
58
+ * ```
59
+ */
60
+ get(args) {
61
+ return request({
62
+ httpMethod: 'GET',
63
+ baseUri: this.getBaseUri(),
64
+ relativePath: `${ENDPOINT_REACTIONS}/get`,
65
+ apiToken: this.apiToken,
66
+ payload: reactionTarget(args),
67
+ headers: this.headers,
68
+ customFetch: this.customFetch,
69
+ }).then((response) => response.data);
70
+ }
71
+ /**
72
+ * Removes an emoji reaction from a thread, comment, or conversation message.
73
+ *
74
+ * @param args - The arguments for removing a reaction.
75
+ * @param args.threadId - Optional thread ID.
76
+ * @param args.commentId - Optional comment ID.
77
+ * @param args.messageId - Optional message ID (for conversation messages).
78
+ * @param args.reaction - The reaction emoji to remove.
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * await api.reactions.remove({ threadId: '7YpL3oZ4kZ9vP7Q1tR2sX3z', reaction: '👍' })
83
+ * ```
84
+ */
85
+ remove(args) {
86
+ return request({
87
+ httpMethod: 'POST',
88
+ baseUri: this.getBaseUri(),
89
+ relativePath: `${ENDPOINT_REACTIONS}/remove`,
90
+ apiToken: this.apiToken,
91
+ payload: { ...reactionTarget(args), reaction: args.reaction },
92
+ headers: this.headers,
93
+ customFetch: this.customFetch,
94
+ }).then(() => undefined);
95
+ }
96
+ }
@@ -0,0 +1,140 @@
1
+ import { ENDPOINT_SEARCH } from '../consts/endpoints.js';
2
+ import { request } from '../transport/http-client.js';
3
+ import { SearchResultSchema, } from '../types/entities.js';
4
+ import { BaseClient } from './base-client.js';
5
+ /**
6
+ * Client for interacting with Comms search endpoints.
7
+ */
8
+ export class SearchClient extends BaseClient {
9
+ /**
10
+ * Searches across all threads and conversations in a workspace.
11
+ *
12
+ * @param args - The arguments for searching.
13
+ * @param args.query - The search query string. Optional when `mentionSelf: true` is set; required otherwise.
14
+ * @param args.workspaceId - The workspace ID to search in.
15
+ * @param args.type - Optional result type. `conversations` matches group chat titles, and its results have no `messageId`. When omitted, results are threads, comments, and messages.
16
+ * @param args.channelIds - Optional array of channel IDs to filter by.
17
+ * @param args.authorIds - Optional array of author user IDs to filter by.
18
+ * @param args.mentionSelf - Optional flag to filter by mentions of the current user. When true, `query` may be omitted.
19
+ * @param args.dateFrom - Optional start date for filtering (YYYY-MM-DD).
20
+ * @param args.dateTo - Optional end date for filtering (YYYY-MM-DD).
21
+ * @param args.limit - Optional limit on number of results returned.
22
+ * @param args.cursor - Optional cursor for pagination.
23
+ * @returns Search results with pagination.
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * const results = await api.search.search({
28
+ * query: 'important meeting',
29
+ * workspaceId: 123,
30
+ * })
31
+ * ```
32
+ */
33
+ search(args) {
34
+ const params = { workspace_id: args.workspaceId };
35
+ if (args.query !== undefined)
36
+ params.query = args.query;
37
+ if (args.type)
38
+ params.type = args.type;
39
+ if (args.channelIds)
40
+ params.channel_ids = args.channelIds;
41
+ if (args.authorIds)
42
+ params.author_ids = args.authorIds;
43
+ if (args.mentionSelf !== undefined)
44
+ params.mention_self = args.mentionSelf;
45
+ if (args.dateFrom)
46
+ params.date_from = args.dateFrom;
47
+ if (args.dateTo)
48
+ params.date_to = args.dateTo;
49
+ if (args.limit)
50
+ params.limit = args.limit;
51
+ if (args.cursor)
52
+ params.cursor_mark = args.cursor;
53
+ return request({
54
+ httpMethod: 'GET',
55
+ baseUri: this.getBaseUri(),
56
+ relativePath: ENDPOINT_SEARCH,
57
+ apiToken: this.apiToken,
58
+ payload: params,
59
+ headers: this.headers,
60
+ customFetch: this.customFetch,
61
+ }).then((response) => ({
62
+ ...response.data,
63
+ items: response.data.items.map((result) => SearchResultSchema.parse(result)),
64
+ }));
65
+ }
66
+ /**
67
+ * Searches within comments of a specific thread.
68
+ *
69
+ * @param args - The arguments for searching within a thread.
70
+ * @param args.query - The search query string.
71
+ * @param args.threadId - The thread ID to search in.
72
+ * @param args.limit - Optional limit on number of results returned.
73
+ * @param args.cursor - Optional cursor for pagination.
74
+ * @returns Comment IDs that match the search query.
75
+ *
76
+ * @example
77
+ * ```typescript
78
+ * const results = await api.search.searchThread({
79
+ * query: 'deadline',
80
+ * threadId: '7YpL3oZ4kZ9vP7Q1tR2sX3z',
81
+ * })
82
+ * ```
83
+ */
84
+ searchThread(args) {
85
+ const params = {
86
+ query: args.query,
87
+ thread_id: args.threadId,
88
+ };
89
+ if (args.limit)
90
+ params.limit = args.limit;
91
+ if (args.cursor)
92
+ params.cursor = args.cursor;
93
+ return request({
94
+ httpMethod: 'GET',
95
+ baseUri: this.getBaseUri(),
96
+ relativePath: `${ENDPOINT_SEARCH}/thread`,
97
+ apiToken: this.apiToken,
98
+ payload: params,
99
+ headers: this.headers,
100
+ customFetch: this.customFetch,
101
+ }).then((response) => response.data);
102
+ }
103
+ /**
104
+ * Searches within messages of a specific conversation.
105
+ *
106
+ * @param args - The arguments for searching within a conversation.
107
+ * @param args.query - The search query string.
108
+ * @param args.conversationId - The conversation ID to search in.
109
+ * @param args.limit - Optional limit on number of results returned.
110
+ * @param args.cursor - Optional cursor for pagination.
111
+ * @returns Message IDs that match the search query.
112
+ *
113
+ * @example
114
+ * ```typescript
115
+ * const results = await api.search.searchConversation({
116
+ * query: 'budget',
117
+ * conversationId: '7YpL3oZ4kZ9vP7Q1tR2sX42',
118
+ * })
119
+ * ```
120
+ */
121
+ searchConversation(args) {
122
+ const params = {
123
+ query: args.query,
124
+ conversation_id: args.conversationId,
125
+ };
126
+ if (args.limit)
127
+ params.limit = args.limit;
128
+ if (args.cursor)
129
+ params.cursor = args.cursor;
130
+ return request({
131
+ httpMethod: 'GET',
132
+ baseUri: this.getBaseUri(),
133
+ relativePath: `${ENDPOINT_SEARCH}/conversation`,
134
+ apiToken: this.apiToken,
135
+ payload: params,
136
+ headers: this.headers,
137
+ customFetch: this.customFetch,
138
+ }).then((response) => response.data);
139
+ }
140
+ }
@@ -0,0 +1,363 @@
1
+ import { z } from 'zod';
2
+ import { ENDPOINT_THREADS } from '../consts/endpoints.js';
3
+ import { request } from '../transport/http-client.js';
4
+ import { CommentSchema, createCommentSchema, createThreadSchema, StatusOkSchema, ThreadSchema, UnreadThreadSchema, } from '../types/entities.js';
5
+ import { resolveCreateId, resolveReferenceId } from '../utils/uuidv7.js';
6
+ import { addCommentRequest } from './add-comment-helper.js';
7
+ import { BaseClient } from './base-client.js';
8
+ import { applyNotifyAudience } from './notify-audience.js';
9
+ export const ThreadListSchema = z.array(ThreadSchema);
10
+ const GetUnreadResponseSchema = z.object({
11
+ data: z.array(UnreadThreadSchema),
12
+ version: z.number().int(),
13
+ inboxUnread: z.number().int().nullable().optional(),
14
+ });
15
+ /**
16
+ * Client for `/api/v1.0/threads/`. The SDK auto-generates the thread `id` on
17
+ * `createThread` when the caller doesn't supply one.
18
+ */
19
+ export class ThreadsClient extends BaseClient {
20
+ constructor() {
21
+ super(...arguments);
22
+ this.linkBaseUrl = this.getLinkBaseUrl();
23
+ // Reuse the shared singletons when no custom base is configured.
24
+ this.threadSchema = this.linkBaseUrl
25
+ ? createThreadSchema(this.linkBaseUrl)
26
+ : ThreadSchema;
27
+ this.threadListSchema = this.linkBaseUrl
28
+ ? z.array(this.threadSchema)
29
+ : ThreadListSchema;
30
+ this.commentSchema = this.linkBaseUrl
31
+ ? createCommentSchema(this.linkBaseUrl)
32
+ : CommentSchema;
33
+ }
34
+ /**
35
+ * Gets threads. At least one of `channelId` / `workspaceId` is required.
36
+ * `newerThan` / `olderThan` (`Date`) are converted to the
37
+ * `newer_than_ts` / `older_than_ts` epoch-second params on the wire.
38
+ *
39
+ * @param args - The arguments for getting threads.
40
+ * @param args.workspaceId - The workspace ID.
41
+ * @param args.channelId - Optional channel ID to narrow to a single channel.
42
+ * @param args.archived - Optional flag to include archived threads.
43
+ * @param args.newerThan - Optional date to get threads newer than.
44
+ * @param args.olderThan - Optional date to get threads older than.
45
+ * @param args.limit - Optional limit on number of threads returned.
46
+ * @returns An array of thread objects.
47
+ *
48
+ * @example
49
+ * ```typescript
50
+ * const threads = await api.threads.getThreads({
51
+ * workspaceId: 123,
52
+ * channelId: '7YpL3oZ4kZ9vP7Q1tR2sX44',
53
+ * })
54
+ * threads.forEach(t => console.log(t.title))
55
+ * ```
56
+ */
57
+ getThreads(args) {
58
+ const params = { workspaceId: args.workspaceId };
59
+ if (args.channelId != null)
60
+ params.channelId = args.channelId;
61
+ if (args.archived != null)
62
+ params.archived = args.archived;
63
+ if (args.limit != null)
64
+ params.limit = args.limit;
65
+ if (args.newerThan)
66
+ params.newer_than_ts = Math.floor(args.newerThan.getTime() / 1000);
67
+ if (args.olderThan)
68
+ params.older_than_ts = Math.floor(args.olderThan.getTime() / 1000);
69
+ return request({
70
+ httpMethod: 'GET',
71
+ baseUri: this.getBaseUri(),
72
+ relativePath: `${ENDPOINT_THREADS}/get`,
73
+ apiToken: this.apiToken,
74
+ payload: params,
75
+ headers: this.headers,
76
+ customFetch: this.customFetch,
77
+ }).then((response) => this.threadListSchema.parse(response.data));
78
+ }
79
+ /**
80
+ * Gets a single thread object by id.
81
+ *
82
+ * @param id - The thread ID.
83
+ * @returns The thread object.
84
+ */
85
+ getThread(id) {
86
+ return this.simple('GET', 'getone', { id }, this.threadSchema);
87
+ }
88
+ /**
89
+ * Creates a new thread in a channel. `id` is auto-generated if not supplied.
90
+ *
91
+ * @param args - The arguments for creating a thread.
92
+ * @param args.channelId - The channel ID.
93
+ * @param args.title - Optional thread title.
94
+ * @param args.content - The thread content.
95
+ * @param args.recipients - Optional array of user IDs to notify.
96
+ * @param args.groups - Optional array of custom group IDs to notify.
97
+ * @param args.notifyAudience - Optional broader audience to notify in addition to
98
+ * `recipients` and `groups`. `'channel'` notifies everyone in the channel;
99
+ * `'thread'` notifies everyone who has interacted with the thread — but a
100
+ * brand-new thread has no interactions yet, so the backend discards
101
+ * `'thread'` at create time. It is only meaningful on replies (`createComment`).
102
+ * @param args.attachments - Optional array of {@link Attachment}s (from `attachments.upload`).
103
+ * @returns The created thread object.
104
+ *
105
+ * @example
106
+ * ```typescript
107
+ * const thread = await api.threads.createThread({
108
+ * channelId: '7YpL3oZ4kZ9vP7Q1tR2sX44',
109
+ * title: 'New Feature Discussion',
110
+ * content: 'Let\'s discuss the new feature...',
111
+ * notifyAudience: 'channel', // tag "Everyone in channel"
112
+ * })
113
+ * ```
114
+ */
115
+ createThread(args) {
116
+ const normalized = applyNotifyAudience(args);
117
+ return this.simple('POST', 'add', {
118
+ ...normalized,
119
+ id: resolveCreateId(normalized.id),
120
+ channelId: resolveReferenceId(normalized.channelId, 'channelId'),
121
+ }, this.threadSchema);
122
+ }
123
+ /**
124
+ * Partial update of an existing thread.
125
+ *
126
+ * @param args - The arguments for updating a thread.
127
+ * @param args.id - The thread ID.
128
+ * @param args.title - Optional new thread title.
129
+ * @param args.content - Optional new thread content.
130
+ * @returns The updated thread object.
131
+ */
132
+ updateThread(args) {
133
+ return this.simple('POST', 'update', { ...args }, this.threadSchema);
134
+ }
135
+ /**
136
+ * Permanently deletes a thread.
137
+ *
138
+ * @param id - The thread ID.
139
+ */
140
+ deleteThread(id) {
141
+ return this.simple('POST', 'remove', { id }, StatusOkSchema);
142
+ }
143
+ /**
144
+ * Saves a thread (formerly "star").
145
+ *
146
+ * @param id - The thread ID.
147
+ */
148
+ saveThread(id) {
149
+ return this.simple('GET', 'save', { id }, StatusOkSchema);
150
+ }
151
+ /**
152
+ * Unsaves a thread (formerly "unstar").
153
+ *
154
+ * @param id - The thread ID.
155
+ */
156
+ unsaveThread(id) {
157
+ return this.simple('GET', 'unsave', { id }, StatusOkSchema);
158
+ }
159
+ /**
160
+ * Pins a thread.
161
+ *
162
+ * @param id - The thread ID.
163
+ */
164
+ pinThread(id) {
165
+ return this.simple('GET', 'pin', { id }, StatusOkSchema);
166
+ }
167
+ /**
168
+ * Unpins a thread.
169
+ *
170
+ * @param id - The thread ID.
171
+ */
172
+ unpinThread(id) {
173
+ return this.simple('GET', 'unpin', { id }, StatusOkSchema);
174
+ }
175
+ /**
176
+ * Moves a thread to another channel.
177
+ *
178
+ * @param args - The arguments for moving a thread.
179
+ * @param args.id - The thread ID.
180
+ * @param args.toChannel - The target channel ID.
181
+ * @returns The updated thread object.
182
+ */
183
+ moveToChannel(args) {
184
+ return this.simple('GET', 'move_to_channel', { ...args }, this.threadSchema);
185
+ }
186
+ /**
187
+ * Marks a thread as read.
188
+ *
189
+ * @param args - The arguments for marking a thread as read.
190
+ * @param args.id - The thread ID.
191
+ * @param args.objIndex - The index of the last known read message.
192
+ */
193
+ markRead(args) {
194
+ const { id, ...rest } = args;
195
+ return this.simple('POST', 'mark_read', { threadId: id, ...rest }, StatusOkSchema);
196
+ }
197
+ /**
198
+ * Marks a thread as unread.
199
+ *
200
+ * @param args - The arguments for marking a thread as unread.
201
+ * @param args.id - The thread ID.
202
+ * @param args.objIndex - The index of the last unread message. Use -1 to mark the whole thread as unread.
203
+ */
204
+ markUnread(args) {
205
+ const { id, ...rest } = args;
206
+ return this.simple('POST', 'mark_unread', { threadId: id, ...rest }, StatusOkSchema);
207
+ }
208
+ /**
209
+ * Marks a thread as unread for others. Useful to notify others about thread changes.
210
+ *
211
+ * @param args - The arguments for marking a thread as unread for others.
212
+ * @param args.id - The thread ID.
213
+ * @param args.objIndex - The index of the last unread message. Use -1 to mark the whole thread as unread.
214
+ */
215
+ markUnreadForOthers(args) {
216
+ return this.simple('POST', 'mark_unread_for_others', { ...args }, StatusOkSchema);
217
+ }
218
+ /**
219
+ * Marks every thread in a workspace or channel as read. Exactly one of
220
+ * `workspaceId` / `channelId` should be set.
221
+ *
222
+ * @param args - Either workspaceId or channelId (one is required).
223
+ * @param args.workspaceId - The workspace ID.
224
+ * @param args.channelId - The channel ID.
225
+ *
226
+ * @example
227
+ * ```typescript
228
+ * // Mark all in workspace
229
+ * await api.threads.markAllRead({ workspaceId: 123 })
230
+ *
231
+ * // Mark all in channel
232
+ * await api.threads.markAllRead({ channelId: '7YpL3oZ4kZ9vP7Q1tR2sX44' })
233
+ * ```
234
+ */
235
+ markAllRead(args) {
236
+ if (!args.workspaceId && !args.channelId) {
237
+ throw new Error('Either workspaceId or channelId is required');
238
+ }
239
+ return this.simple('POST', 'mark_all_read', { ...args }, StatusOkSchema);
240
+ }
241
+ /**
242
+ * Clears unread threads in a workspace.
243
+ *
244
+ * @param workspaceId - The workspace ID.
245
+ */
246
+ clearUnread(workspaceId) {
247
+ return this.simple('GET', 'clear_unread', { workspaceId }, StatusOkSchema);
248
+ }
249
+ /**
250
+ * Returns unread threads for a workspace, paired with the unread version
251
+ * counter and (optionally) the inbox unread count.
252
+ *
253
+ * @param workspaceId - The workspace ID.
254
+ * @returns Object containing the array of unread thread references, a version counter, and optionally the inbox unread count.
255
+ */
256
+ getUnread(workspaceId) {
257
+ return this.simple('GET', 'get_unread', { workspaceId }, GetUnreadResponseSchema);
258
+ }
259
+ /**
260
+ * Mutes a thread for a specified number of minutes.
261
+ * When muted, you will not get notified in your inbox about new comments.
262
+ *
263
+ * @param args - The arguments for muting a thread.
264
+ * @param args.id - The thread ID.
265
+ * @param args.minutes - Number of minutes to mute the thread.
266
+ * @returns The updated thread object.
267
+ *
268
+ * @example
269
+ * ```typescript
270
+ * const thread = await api.threads.muteThread({ id: '7YpL3oZ4kZ9vP7Q1tR2sX3z', minutes: 30 })
271
+ * ```
272
+ */
273
+ muteThread(args) {
274
+ return this.simple('GET', 'mute', { ...args }, this.threadSchema);
275
+ }
276
+ /**
277
+ * Unmutes a thread.
278
+ * You will start to see notifications in your inbox again when new comments are added.
279
+ *
280
+ * @param id - The thread ID.
281
+ * @returns The updated thread object.
282
+ */
283
+ unmuteThread(id) {
284
+ return this.simple('GET', 'unmute', { id }, this.threadSchema);
285
+ }
286
+ /**
287
+ * Closes a thread by adding a comment with a close action.
288
+ *
289
+ * @param args - The arguments for closing a thread.
290
+ * @param args.id - The thread ID.
291
+ * @param args.content - The comment content.
292
+ * @param args.attachments - Optional array of {@link Attachment} objects.
293
+ * @param args.actions - Optional array of action objects.
294
+ * @param args.recipients - Optional array of user IDs to notify directly.
295
+ * @param args.groups - Optional array of custom group IDs to notify.
296
+ * @param args.directMentions - Optional array of user IDs that were @-mentioned in
297
+ * `content`.
298
+ * @param args.notifyAudience - Optional broader audience to notify in addition to
299
+ * `recipients` and `groups`. `'channel'` notifies everyone in the channel;
300
+ * `'thread'` notifies everyone who has interacted with the thread.
301
+ * @returns The created comment object.
302
+ *
303
+ * @example
304
+ * ```typescript
305
+ * const comment = await api.threads.closeThread({
306
+ * id: '7YpL3oZ4kZ9vP7Q1tR2sX3z',
307
+ * content: 'Closing this thread — resolved.',
308
+ * })
309
+ * ```
310
+ */
311
+ closeThread(args) {
312
+ return this.addCommentWithAction(args, 'close');
313
+ }
314
+ /**
315
+ * Reopens a thread by adding a comment with a reopen action.
316
+ *
317
+ * @param args - The arguments for reopening a thread.
318
+ * @param args.id - The thread ID.
319
+ * @param args.content - The comment content.
320
+ * @param args.attachments - Optional array of {@link Attachment} objects.
321
+ * @param args.actions - Optional array of action objects.
322
+ * @param args.recipients - Optional array of user IDs to notify directly.
323
+ * @param args.groups - Optional array of custom group IDs to notify.
324
+ * @param args.directMentions - Optional array of user IDs that were @-mentioned in
325
+ * `content`.
326
+ * @param args.notifyAudience - Optional broader audience to notify in addition to
327
+ * `recipients` and `groups`. `'channel'` notifies everyone in the channel;
328
+ * `'thread'` notifies everyone who has interacted with the thread.
329
+ * @returns The created comment object.
330
+ *
331
+ * @example
332
+ * ```typescript
333
+ * const comment = await api.threads.reopenThread({
334
+ * id: '7YpL3oZ4kZ9vP7Q1tR2sX3z',
335
+ * content: 'Reopening — need further discussion.',
336
+ * })
337
+ * ```
338
+ */
339
+ reopenThread(args) {
340
+ return this.addCommentWithAction(args, 'reopen');
341
+ }
342
+ addCommentWithAction(args, threadAction) {
343
+ const { id, ...rest } = args;
344
+ return addCommentRequest({
345
+ baseUri: this.getBaseUri(),
346
+ apiToken: this.apiToken,
347
+ headers: this.headers,
348
+ customFetch: this.customFetch,
349
+ schema: this.commentSchema,
350
+ }, { threadId: resolveReferenceId(id, 'id'), ...rest }, { threadAction });
351
+ }
352
+ simple(httpMethod, suffix, params, schema) {
353
+ return request({
354
+ httpMethod,
355
+ baseUri: this.getBaseUri(),
356
+ relativePath: `${ENDPOINT_THREADS}/${suffix}`,
357
+ apiToken: this.apiToken,
358
+ payload: params,
359
+ headers: this.headers,
360
+ customFetch: this.customFetch,
361
+ }).then((response) => schema.parse(response.data));
362
+ }
363
+ }