@doist/comms-sdk 0.1.0-alpha.1 → 0.2.1

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 (67) hide show
  1. package/README.md +1 -1
  2. package/dist/cjs/clients/add-comment-helper.js +18 -2
  3. package/dist/cjs/clients/base-client.js +11 -5
  4. package/dist/cjs/clients/channels-client.js +142 -14
  5. package/dist/cjs/clients/comments-client.js +99 -14
  6. package/dist/cjs/clients/conversation-messages-client.js +91 -9
  7. package/dist/cjs/clients/conversations-client.js +166 -15
  8. package/dist/cjs/clients/groups-client.js +98 -5
  9. package/dist/cjs/clients/inbox-client.js +102 -16
  10. package/dist/cjs/clients/reactions-client.js +40 -2
  11. package/dist/cjs/clients/search-client.js +50 -0
  12. package/dist/cjs/clients/threads-client.js +238 -24
  13. package/dist/cjs/clients/users-client.js +138 -11
  14. package/dist/cjs/clients/workspace-users-client.js +110 -10
  15. package/dist/cjs/clients/workspaces-client.js +89 -8
  16. package/dist/cjs/comms-api.js +1 -0
  17. package/dist/cjs/consts/endpoints.js +8 -3
  18. package/dist/cjs/testUtils/test-defaults.js +3 -1
  19. package/dist/cjs/types/api-version.js +8 -0
  20. package/dist/cjs/types/entities.js +119 -98
  21. package/dist/cjs/types/index.js +1 -0
  22. package/dist/cjs/types/requests.js +0 -1
  23. package/dist/cjs/utils/url-helpers.js +3 -1
  24. package/dist/esm/clients/add-comment-helper.js +18 -2
  25. package/dist/esm/clients/base-client.js +11 -5
  26. package/dist/esm/clients/channels-client.js +143 -15
  27. package/dist/esm/clients/comments-client.js +100 -15
  28. package/dist/esm/clients/conversation-messages-client.js +92 -10
  29. package/dist/esm/clients/conversations-client.js +167 -16
  30. package/dist/esm/clients/groups-client.js +98 -5
  31. package/dist/esm/clients/inbox-client.js +102 -16
  32. package/dist/esm/clients/reactions-client.js +40 -2
  33. package/dist/esm/clients/search-client.js +50 -0
  34. package/dist/esm/clients/threads-client.js +239 -25
  35. package/dist/esm/clients/users-client.js +138 -11
  36. package/dist/esm/clients/workspace-users-client.js +110 -10
  37. package/dist/esm/clients/workspaces-client.js +90 -9
  38. package/dist/esm/comms-api.js +1 -0
  39. package/dist/esm/consts/endpoints.js +8 -3
  40. package/dist/esm/testUtils/test-defaults.js +2 -0
  41. package/dist/esm/types/api-version.js +5 -0
  42. package/dist/esm/types/entities.js +111 -97
  43. package/dist/esm/types/index.js +1 -0
  44. package/dist/esm/types/requests.js +0 -1
  45. package/dist/esm/utils/url-helpers.js +3 -1
  46. package/dist/types/clients/add-comment-helper.d.ts +20 -1
  47. package/dist/types/clients/base-client.d.ts +10 -0
  48. package/dist/types/clients/channels-client.d.ts +126 -6
  49. package/dist/types/clients/comments-client.d.ts +77 -6
  50. package/dist/types/clients/conversation-messages-client.d.ts +79 -5
  51. package/dist/types/clients/conversations-client.d.ts +146 -3
  52. package/dist/types/clients/groups-client.d.ts +98 -5
  53. package/dist/types/clients/inbox-client.d.ts +463 -5
  54. package/dist/types/clients/reactions-client.d.ts +40 -2
  55. package/dist/types/clients/search-client.d.ts +50 -0
  56. package/dist/types/clients/threads-client.d.ts +204 -8
  57. package/dist/types/clients/users-client.d.ts +138 -11
  58. package/dist/types/clients/workspace-users-client.d.ts +110 -10
  59. package/dist/types/clients/workspaces-client.d.ts +82 -7
  60. package/dist/types/comms-api.d.ts +3 -0
  61. package/dist/types/consts/endpoints.d.ts +6 -1
  62. package/dist/types/testUtils/test-defaults.d.ts +1 -0
  63. package/dist/types/types/api-version.d.ts +6 -0
  64. package/dist/types/types/entities.d.ts +1654 -126
  65. package/dist/types/types/index.d.ts +1 -0
  66. package/dist/types/types/requests.d.ts +2 -21
  67. package/package.json +1 -1
@@ -1,6 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SearchResultSchema = exports.SEARCH_RESULT_TYPES = exports.UnreadConversationSchema = exports.UnreadThreadSchema = exports.InboxThreadSchema = exports.ConversationMessageSchema = exports.WorkspaceUserSchema = exports.CommentSchema = exports.ConversationSchema = exports.GroupSchema = exports.ThreadSchema = exports.ChannelSchema = exports.WorkspaceSchema = exports.UserSchema = exports.BaseUserSchema = exports.AttachmentSchema = exports.StatusOkSchema = exports.SystemMessageSchema = void 0;
3
+ exports.SearchResultSchema = exports.SEARCH_RESULT_TYPES = exports.UnreadConversationSchema = exports.UnreadThreadSchema = exports.InboxThreadSchema = exports.ConversationMessageSchema = exports.ConversationMessageObjectSchema = exports.WorkspaceUserSchema = exports.CommentSchema = exports.CommentObjectSchema = exports.ConversationSchema = exports.ConversationObjectSchema = exports.GroupSchema = exports.ThreadSchema = exports.ThreadObjectSchema = exports.ChannelSchema = exports.ChannelObjectSchema = exports.WorkspaceSchema = exports.UserSchema = exports.BaseUserSchema = exports.AttachmentSchema = exports.StatusOkSchema = exports.SystemMessageSchema = void 0;
4
+ exports.createChannelSchema = createChannelSchema;
5
+ exports.createThreadSchema = createThreadSchema;
6
+ exports.createConversationSchema = createConversationSchema;
7
+ exports.createCommentSchema = createCommentSchema;
8
+ exports.createConversationMessageSchema = createConversationMessageSchema;
9
+ exports.createInboxThreadObjectSchema = createInboxThreadObjectSchema;
10
+ exports.createInboxThreadSchema = createInboxThreadSchema;
4
11
  const zod_1 = require("zod");
5
12
  const url_helpers_1 = require("../utils/url-helpers");
6
13
  const enums_1 = require("./enums");
@@ -100,8 +107,7 @@ exports.WorkspaceSchema = zod_1.z.object({
100
107
  .optional(),
101
108
  plan: zod_1.z.string().nullable().optional(),
102
109
  });
103
- exports.ChannelSchema = zod_1.z
104
- .object({
110
+ exports.ChannelObjectSchema = zod_1.z.object({
105
111
  id: zod_1.z.string(),
106
112
  name: zod_1.z.string(),
107
113
  description: zod_1.z.string().nullable().optional(),
@@ -119,15 +125,17 @@ exports.ChannelSchema = zod_1.z
119
125
  icon: zod_1.z.number().nullable().optional(),
120
126
  version: zod_1.z.number(),
121
127
  filters: zod_1.z.record(zod_1.z.string(), zod_1.z.string()).nullable().optional(),
122
- })
123
- .transform((data) => ({
124
- ...data,
125
- url: (0, url_helpers_1.getFullCommsURL)({ workspaceId: data.workspaceId, channelId: data.id }),
126
- }));
128
+ });
129
+ function createChannelSchema(linkBaseUrl) {
130
+ return exports.ChannelObjectSchema.transform((data) => ({
131
+ ...data,
132
+ url: (0, url_helpers_1.getFullCommsURL)({ workspaceId: data.workspaceId, channelId: data.id }, linkBaseUrl),
133
+ }));
134
+ }
135
+ exports.ChannelSchema = createChannelSchema();
127
136
  // Thread entity from API. `pinned` (boolean) and `pinnedTs` (epoch ms or
128
137
  // null) are both surfaced — `pinned` is kept for Zapier/webhook clients.
129
- exports.ThreadSchema = zod_1.z
130
- .object({
138
+ exports.ThreadObjectSchema = zod_1.z.object({
131
139
  id: zod_1.z.string(),
132
140
  title: zod_1.z.string(),
133
141
  content: zod_1.z.string(),
@@ -191,15 +199,18 @@ exports.ThreadSchema = zod_1.z
191
199
  })
192
200
  .nullable()
193
201
  .optional(),
194
- })
195
- .transform((data) => ({
196
- ...data,
197
- url: (0, url_helpers_1.getFullCommsURL)({
198
- workspaceId: data.workspaceId,
199
- channelId: data.channelId,
200
- threadId: data.id,
201
- }),
202
- }));
202
+ });
203
+ function createThreadSchema(linkBaseUrl) {
204
+ return exports.ThreadObjectSchema.transform((data) => ({
205
+ ...data,
206
+ url: (0, url_helpers_1.getFullCommsURL)({
207
+ workspaceId: data.workspaceId,
208
+ channelId: data.channelId,
209
+ threadId: data.id,
210
+ }, linkBaseUrl),
211
+ }));
212
+ }
213
+ exports.ThreadSchema = createThreadSchema();
203
214
  // Group entity from API. The broadcast markers `EVERYONE` and
204
215
  // `EVERYONE_IN_THREAD` can appear in place of a real `id` in group-bearing
205
216
  // fields on threads/comments/channels.
@@ -212,8 +223,7 @@ exports.GroupSchema = zod_1.z.object({
212
223
  version: zod_1.z.number(),
213
224
  });
214
225
  // Conversation entity from API.
215
- exports.ConversationSchema = zod_1.z
216
- .object({
226
+ exports.ConversationObjectSchema = zod_1.z.object({
217
227
  id: zod_1.z.string(),
218
228
  workspaceId: zod_1.z.number(),
219
229
  userIds: zod_1.z.array(zod_1.z.number()),
@@ -250,13 +260,15 @@ exports.ConversationSchema = zod_1.z
250
260
  })
251
261
  .nullable()
252
262
  .optional(),
253
- })
254
- .transform((data) => ({
255
- ...data,
256
- url: (0, url_helpers_1.getFullCommsURL)({ workspaceId: data.workspaceId, conversationId: data.id }),
257
- }));
258
- exports.CommentSchema = zod_1.z
259
- .object({
263
+ });
264
+ function createConversationSchema(linkBaseUrl) {
265
+ return exports.ConversationObjectSchema.transform((data) => ({
266
+ ...data,
267
+ url: (0, url_helpers_1.getFullCommsURL)({ workspaceId: data.workspaceId, conversationId: data.id }, linkBaseUrl),
268
+ }));
269
+ }
270
+ exports.ConversationSchema = createConversationSchema();
271
+ exports.CommentObjectSchema = zod_1.z.object({
260
272
  id: zod_1.z.string(),
261
273
  content: zod_1.z.string(),
262
274
  creator: zod_1.z.number(),
@@ -280,16 +292,19 @@ exports.CommentSchema = zod_1.z
280
292
  deletedBy: zod_1.z.number().nullable().optional(),
281
293
  version: zod_1.z.number().nullable().optional(),
282
294
  actions: zod_1.z.array(zod_1.z.unknown()).nullable().optional(),
283
- })
284
- .transform((data) => ({
285
- ...data,
286
- url: (0, url_helpers_1.getFullCommsURL)({
287
- workspaceId: data.workspaceId,
288
- channelId: data.channelId,
289
- threadId: data.threadId,
290
- commentId: data.id,
291
- }),
292
- }));
295
+ });
296
+ function createCommentSchema(linkBaseUrl) {
297
+ return exports.CommentObjectSchema.transform((data) => ({
298
+ ...data,
299
+ url: (0, url_helpers_1.getFullCommsURL)({
300
+ workspaceId: data.workspaceId,
301
+ channelId: data.channelId,
302
+ threadId: data.threadId,
303
+ commentId: data.id,
304
+ }, linkBaseUrl),
305
+ }));
306
+ }
307
+ exports.CommentSchema = createCommentSchema();
293
308
  exports.WorkspaceUserSchema = exports.BaseUserSchema.extend({
294
309
  email: zod_1.z.string().nullable().optional(),
295
310
  userType: zod_1.z.enum(enums_1.USER_TYPES),
@@ -302,8 +317,7 @@ exports.WorkspaceUserSchema = exports.BaseUserSchema.extend({
302
317
  // (coerced to a string post-parse) because the backend currently emits
303
318
  // either shape depending on the endpoint; the URL/reaction helpers accept
304
319
  // both.
305
- exports.ConversationMessageSchema = zod_1.z
306
- .object({
320
+ exports.ConversationMessageObjectSchema = zod_1.z.object({
307
321
  id: zod_1.z.union([zod_1.z.string(), zod_1.z.number()]).transform(String),
308
322
  content: zod_1.z.string(),
309
323
  creator: zod_1.z.number(),
@@ -320,65 +334,72 @@ exports.ConversationMessageSchema = zod_1.z
320
334
  directMentions: zod_1.z.array(zod_1.z.number()).nullable().optional(),
321
335
  version: zod_1.z.number().nullable().optional(),
322
336
  workspaceId: zod_1.z.number(),
323
- })
324
- .transform((data) => ({
325
- ...data,
326
- url: (0, url_helpers_1.getFullCommsURL)({
327
- workspaceId: data.workspaceId,
328
- conversationId: data.conversationId,
329
- messageId: data.id,
330
- }),
331
- }));
337
+ });
338
+ function createConversationMessageSchema(linkBaseUrl) {
339
+ return exports.ConversationMessageObjectSchema.transform((data) => ({
340
+ ...data,
341
+ url: (0, url_helpers_1.getFullCommsURL)({
342
+ workspaceId: data.workspaceId,
343
+ conversationId: data.conversationId,
344
+ messageId: data.id,
345
+ }, linkBaseUrl),
346
+ }));
347
+ }
348
+ exports.ConversationMessageSchema = createConversationMessageSchema();
332
349
  // InboxThread entity from API - returns full Thread objects with additional inbox metadata.
333
- exports.InboxThreadSchema = zod_1.z
334
- .object({
335
- id: zod_1.z.string(),
336
- title: zod_1.z.string(),
337
- content: zod_1.z.string(),
338
- creator: zod_1.z.number(),
339
- creatorName: zod_1.z.string().nullable().optional(),
340
- channelId: zod_1.z.string(),
341
- workspaceId: zod_1.z.number(),
342
- actions: zod_1.z.array(zod_1.z.unknown()).nullable().optional(),
343
- attachments: zod_1.z.array(exports.AttachmentSchema).nullable().optional(),
344
- commentCount: zod_1.z.number(),
345
- directGroupMentions: zod_1.z.array(zod_1.z.string()).nullable().optional(),
346
- directMentions: zod_1.z.array(zod_1.z.number()).nullable().optional(),
347
- groups: zod_1.z.array(zod_1.z.string()).nullable().optional(),
348
- lastEdited: zod_1.z.date().nullable().optional(),
349
- lastObjIndex: zod_1.z.number().nullable().optional(),
350
- lastUpdated: zod_1.z.date(),
351
- mutedUntil: zod_1.z.date().nullable().optional(),
352
- participants: zod_1.z.array(zod_1.z.number()).nullable().optional(),
353
- // Backend wire shape only includes `pinned_ts` (epoch ms or null);
354
- // derive `pinned` from `pinnedTs != null` if you need a bool.
355
- pinned: zod_1.z.boolean().optional(),
356
- pinnedTs: zod_1.z.number().int().nullable().optional(),
357
- posted: zod_1.z.date(),
358
- reactions: zod_1.z.record(zod_1.z.string(), zod_1.z.array(zod_1.z.number())).nullable().optional(),
359
- recipients: zod_1.z.array(zod_1.z.number()).nullable().optional(),
360
- snippet: zod_1.z.string(),
361
- snippetCreator: zod_1.z.number(),
362
- snippetMaskAvatarUrl: zod_1.z.string().nullable().optional(),
363
- snippetMaskPoster: zod_1.z.string().nullable().optional(),
364
- systemMessage: exports.SystemMessageSchema,
365
- isArchived: zod_1.z.boolean(),
366
- inInbox: zod_1.z.boolean(),
367
- isSaved: zod_1.z.boolean().nullable().optional(),
368
- closed: zod_1.z.boolean(),
369
- responders: zod_1.z.array(zod_1.z.number()).nullable().optional(),
370
- lastComment: exports.CommentSchema.nullable().optional(),
371
- toEmails: zod_1.z.array(zod_1.z.string()).nullable().optional(),
372
- version: zod_1.z.number().nullable().optional(),
373
- })
374
- .transform((data) => ({
375
- ...data,
376
- url: (0, url_helpers_1.getFullCommsURL)({
377
- workspaceId: data.workspaceId,
378
- channelId: data.channelId,
379
- threadId: data.id,
380
- }),
381
- }));
350
+ function createInboxThreadObjectSchema(linkBaseUrl) {
351
+ return zod_1.z.object({
352
+ id: zod_1.z.string(),
353
+ title: zod_1.z.string(),
354
+ content: zod_1.z.string(),
355
+ creator: zod_1.z.number(),
356
+ creatorName: zod_1.z.string().nullable().optional(),
357
+ channelId: zod_1.z.string(),
358
+ workspaceId: zod_1.z.number(),
359
+ actions: zod_1.z.array(zod_1.z.unknown()).nullable().optional(),
360
+ attachments: zod_1.z.array(exports.AttachmentSchema).nullable().optional(),
361
+ commentCount: zod_1.z.number(),
362
+ directGroupMentions: zod_1.z.array(zod_1.z.string()).nullable().optional(),
363
+ directMentions: zod_1.z.array(zod_1.z.number()).nullable().optional(),
364
+ groups: zod_1.z.array(zod_1.z.string()).nullable().optional(),
365
+ lastEdited: zod_1.z.date().nullable().optional(),
366
+ lastObjIndex: zod_1.z.number().nullable().optional(),
367
+ lastUpdated: zod_1.z.date(),
368
+ mutedUntil: zod_1.z.date().nullable().optional(),
369
+ participants: zod_1.z.array(zod_1.z.number()).nullable().optional(),
370
+ // Backend wire shape only includes `pinned_ts` (epoch ms or null);
371
+ // derive `pinned` from `pinnedTs != null` if you need a bool.
372
+ pinned: zod_1.z.boolean().optional(),
373
+ pinnedTs: zod_1.z.number().int().nullable().optional(),
374
+ posted: zod_1.z.date(),
375
+ reactions: zod_1.z.record(zod_1.z.string(), zod_1.z.array(zod_1.z.number())).nullable().optional(),
376
+ recipients: zod_1.z.array(zod_1.z.number()).nullable().optional(),
377
+ snippet: zod_1.z.string(),
378
+ snippetCreator: zod_1.z.number(),
379
+ snippetMaskAvatarUrl: zod_1.z.string().nullable().optional(),
380
+ snippetMaskPoster: zod_1.z.string().nullable().optional(),
381
+ systemMessage: exports.SystemMessageSchema,
382
+ isArchived: zod_1.z.boolean(),
383
+ inInbox: zod_1.z.boolean(),
384
+ isSaved: zod_1.z.boolean().nullable().optional(),
385
+ closed: zod_1.z.boolean(),
386
+ responders: zod_1.z.array(zod_1.z.number()).nullable().optional(),
387
+ lastComment: createCommentSchema(linkBaseUrl).nullable().optional(),
388
+ toEmails: zod_1.z.array(zod_1.z.string()).nullable().optional(),
389
+ version: zod_1.z.number().nullable().optional(),
390
+ });
391
+ }
392
+ function createInboxThreadSchema(linkBaseUrl) {
393
+ return createInboxThreadObjectSchema(linkBaseUrl).transform((data) => ({
394
+ ...data,
395
+ url: (0, url_helpers_1.getFullCommsURL)({
396
+ workspaceId: data.workspaceId,
397
+ channelId: data.channelId,
398
+ threadId: data.id,
399
+ }, linkBaseUrl),
400
+ }));
401
+ }
402
+ exports.InboxThreadSchema = createInboxThreadSchema();
382
403
  // UnreadThread entity from API - simplified thread reference.
383
404
  exports.UnreadThreadSchema = zod_1.z.object({
384
405
  threadId: zod_1.z.string(),
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./api-version"), exports);
17
18
  __exportStar(require("./entities"), exports);
18
19
  __exportStar(require("./enums"), exports);
19
20
  __exportStar(require("./errors"), exports);
@@ -97,7 +97,6 @@ exports.GetThreadsArgsSchema = zod_1.z.object({
97
97
  });
98
98
  exports.GetCommentsArgsSchema = zod_1.z.object({
99
99
  threadId: zod_1.z.string(),
100
- from: zod_1.z.date().nullable().optional(),
101
100
  newerThan: zod_1.z.date().nullable().optional(),
102
101
  olderThan: zod_1.z.date().nullable().optional(),
103
102
  limit: zod_1.z.number().nullable().optional(),
@@ -64,7 +64,9 @@ function getCommsURL(params) {
64
64
  * @param baseUrl - Optional base URL (defaults to 'https://comms.todoist.com')
65
65
  */
66
66
  function getFullCommsURL(params, baseUrl = COMMS_BASE_URL) {
67
- return `${baseUrl}${getCommsURL(params)}`;
67
+ // Strip a trailing slash so links don't double up — `getCommsURL` paths start with '/'.
68
+ const normalizedBase = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl;
69
+ return `${normalizedBase}${getCommsURL(params)}`;
68
70
  }
69
71
  /** Returns the URL for a thread in a channel. */
70
72
  function getThreadURL(params) {
@@ -1,6 +1,5 @@
1
1
  import { ENDPOINT_COMMENTS } from '../consts/endpoints.js';
2
2
  import { request } from '../transport/http-client.js';
3
- import { CommentSchema } from '../types/entities.js';
4
3
  import { NOTIFY_AUDIENCE_GROUP_IDS, NOTIFY_AUDIENCES } from '../types/enums.js';
5
4
  import { resolveCreateId } from '../utils/uuidv7.js';
6
5
  const SENTINEL_GROUP_IDS = new Set(Object.values(NOTIFY_AUDIENCE_GROUP_IDS));
@@ -33,6 +32,23 @@ function applyNotifyAudience(params) {
33
32
  const { notifyAudience: _stripped, groups, ...rest } = params;
34
33
  return { ...rest, groups: [...(groups ?? []), sentinel] };
35
34
  }
35
+ /**
36
+ * Internal helper that powers `comments.createComment`,
37
+ * `threads.closeThread`, and `threads.reopenThread`.
38
+ *
39
+ * Normalizes the `notifyAudience` flag into a sentinel `groups` entry,
40
+ * rejects sentinel IDs passed via `groups` / `directGroupMentions`, mints a
41
+ * UUIDv7 `id` when the caller omits one, and posts to `/comments/add`. When
42
+ * `threadAction` is set (`'close'` / `'reopen'`), it is forwarded on the
43
+ * wire so the same request both adds the comment and transitions the
44
+ * parent thread.
45
+ *
46
+ * @param context - Per-call client context (base URI, API token, optional `customFetch`).
47
+ * @param params - The comment payload (`{@link CreateCommentArgs}`).
48
+ * @param options - Optional configuration.
49
+ * @param options.threadAction - When set, also transitions the parent thread (`'close'` or `'reopen'`).
50
+ * @returns The parsed {@link Comment} returned by the API.
51
+ */
36
52
  export function addCommentRequest(context, params, options) {
37
53
  const normalized = applyNotifyAudience(params);
38
54
  const withId = { ...normalized, id: resolveCreateId(normalized.id) };
@@ -46,5 +62,5 @@ export function addCommentRequest(context, params, options) {
46
62
  apiToken: context.apiToken,
47
63
  payload,
48
64
  customFetch: context.customFetch,
49
- }).then((response) => CommentSchema.parse(response.data));
65
+ }).then((response) => context.schema.parse(response.data));
50
66
  }
@@ -1,4 +1,5 @@
1
1
  import { getCommsBaseUri } from '../consts/endpoints.js';
2
+ import { DEFAULT_API_VERSION } from '../types/api-version.js';
2
3
  /**
3
4
  * Base class for every Comms API client. Centralizes URL handling and
4
5
  * config so individual clients stay focused on their endpoints.
@@ -7,6 +8,7 @@ export class BaseClient {
7
8
  constructor(config) {
8
9
  this.apiToken = config.apiToken;
9
10
  this.baseUrl = config.baseUrl;
11
+ this.defaultVersion = config.version || DEFAULT_API_VERSION;
10
12
  this.customFetch = config.customFetch;
11
13
  }
12
14
  /**
@@ -14,10 +16,14 @@ export class BaseClient {
14
16
  * slash so relative paths resolve cleanly through `URL`.
15
17
  */
16
18
  getBaseUri() {
17
- if (this.baseUrl) {
18
- const normalizedBaseUrl = this.baseUrl.endsWith('/') ? this.baseUrl : `${this.baseUrl}/`;
19
- return `${normalizedBaseUrl}api/v1/`;
20
- }
21
- return getCommsBaseUri();
19
+ return getCommsBaseUri(this.defaultVersion, this.baseUrl);
20
+ }
21
+ /**
22
+ * Base URL for entity web links, or `undefined` to use getFullCommsURL's
23
+ * default web app. Trailing-slash normalization happens in
24
+ * `getFullCommsURL`, so the configured value is returned verbatim.
25
+ */
26
+ getLinkBaseUrl() {
27
+ return this.baseUrl;
22
28
  }
23
29
  }
@@ -1,7 +1,7 @@
1
1
  import { z } from 'zod';
2
2
  import { ENDPOINT_CHANNELS } from '../consts/endpoints.js';
3
3
  import { request } from '../transport/http-client.js';
4
- import { ChannelSchema, StatusOkSchema } from '../types/entities.js';
4
+ import { ChannelSchema, createChannelSchema, StatusOkSchema, } from '../types/entities.js';
5
5
  import { resolveCreateId } from '../utils/uuidv7.js';
6
6
  import { BaseClient } from './base-client.js';
7
7
  export const ChannelListSchema = z.array(ChannelSchema);
@@ -11,7 +11,31 @@ export const ChannelListSchema = z.array(ChannelSchema);
11
11
  * to keep an optimistic-UI ID stable through the round-trip.
12
12
  */
13
13
  export class ChannelsClient extends BaseClient {
14
- /** Lists channels in a workspace. */
14
+ constructor() {
15
+ super(...arguments);
16
+ this.linkBaseUrl = this.getLinkBaseUrl();
17
+ // Reuse the shared singletons when no custom base is configured.
18
+ this.channelSchema = this.linkBaseUrl
19
+ ? createChannelSchema(this.linkBaseUrl)
20
+ : ChannelSchema;
21
+ this.channelListSchema = this.linkBaseUrl
22
+ ? z.array(this.channelSchema)
23
+ : ChannelListSchema;
24
+ }
25
+ /**
26
+ * Gets all channels for a given workspace.
27
+ *
28
+ * @param args - The arguments for getting channels.
29
+ * @param args.workspaceId - The workspace ID.
30
+ * @param args.archived - Optional flag to include archived channels.
31
+ * @returns An array of channel objects.
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * const channels = await api.channels.getChannels({ workspaceId: 123 })
36
+ * channels.forEach(ch => console.log(ch.name))
37
+ * ```
38
+ */
15
39
  getChannels(args) {
16
40
  return request({
17
41
  httpMethod: 'GET',
@@ -20,51 +44,155 @@ export class ChannelsClient extends BaseClient {
20
44
  apiToken: this.apiToken,
21
45
  payload: args,
22
46
  customFetch: this.customFetch,
23
- }).then((response) => ChannelListSchema.parse(response.data));
47
+ }).then((response) => this.channelListSchema.parse(response.data));
24
48
  }
25
- /** Fetches a single channel by ID. */
49
+ /**
50
+ * Gets a single channel object by id.
51
+ *
52
+ * @param id - The channel ID.
53
+ * @returns The channel object.
54
+ */
26
55
  getChannel(id) {
27
- return this.simple('GET', 'getone', { id }, ChannelSchema);
56
+ return this.simple('GET', 'getone', { id }, this.channelSchema);
28
57
  }
29
- /** Creates a new channel. `id` is auto-generated if not supplied. */
58
+ /**
59
+ * Creates a new channel. `id` is auto-generated if not supplied — pass your
60
+ * own `id` to keep an optimistic-UI ID stable through the round-trip.
61
+ *
62
+ * @param args - The arguments for creating a channel.
63
+ * @param args.workspaceId - The workspace ID.
64
+ * @param args.name - The channel name.
65
+ * @param args.description - Optional channel description.
66
+ * @param args.color - Optional channel color.
67
+ * @param args.userIds - Optional array of user IDs to add to the channel.
68
+ * @param args.public - Optional flag to make the channel public.
69
+ * @returns The created channel object.
70
+ *
71
+ * @example
72
+ * ```typescript
73
+ * const channel = await api.channels.createChannel({
74
+ * workspaceId: 123,
75
+ * name: 'Engineering',
76
+ * description: 'Engineering team channel',
77
+ * })
78
+ * ```
79
+ */
30
80
  createChannel(args) {
31
- return this.simple('POST', 'add', { ...args, id: resolveCreateId(args.id) }, ChannelSchema);
81
+ return this.simple('POST', 'add', { ...args, id: resolveCreateId(args.id) }, this.channelSchema);
32
82
  }
33
- /** Partial update of an existing channel. */
83
+ /**
84
+ * Partial update of an existing channel.
85
+ *
86
+ * @param args - The arguments for updating a channel.
87
+ * @param args.id - The channel ID.
88
+ * @param args.name - Optional new channel name.
89
+ * @param args.description - Optional new channel description.
90
+ * @param args.color - Optional new channel color.
91
+ * @param args.public - Optional flag to change channel visibility.
92
+ * @returns The updated channel object.
93
+ */
34
94
  updateChannel(args) {
35
- return this.simple('POST', 'update', { ...args }, ChannelSchema);
95
+ return this.simple('POST', 'update', { ...args }, this.channelSchema);
36
96
  }
37
- /** Updates the channel's view filter (`only_open` / `all` / `only_closed`). */
97
+ /**
98
+ * Updates the channel's view filter (`only_open` / `all` / `only_closed`).
99
+ *
100
+ * @param args - The arguments for updating the channel filter.
101
+ * @param args.id - The channel ID.
102
+ * @param args.filterClosed - The new filter value.
103
+ */
38
104
  updateFilters(args) {
39
105
  return this.simple('POST', 'update_filters', { ...args }, StatusOkSchema);
40
106
  }
41
- /** Permanently deletes a channel. */
107
+ /**
108
+ * Permanently deletes a channel.
109
+ *
110
+ * @param id - The channel ID.
111
+ */
42
112
  deleteChannel(id) {
43
113
  return this.simple('POST', 'remove', { id }, StatusOkSchema);
44
114
  }
115
+ /**
116
+ * Archives a channel.
117
+ *
118
+ * @param id - The channel ID.
119
+ */
45
120
  archiveChannel(id) {
46
121
  return this.simple('POST', 'archive', { id }, StatusOkSchema);
47
122
  }
123
+ /**
124
+ * Unarchives a channel.
125
+ *
126
+ * @param id - The channel ID.
127
+ */
48
128
  unarchiveChannel(id) {
49
129
  return this.simple('POST', 'unarchive', { id }, StatusOkSchema);
50
130
  }
131
+ /**
132
+ * Favorites a channel.
133
+ *
134
+ * @param id - The channel ID.
135
+ */
51
136
  favoriteChannel(id) {
52
137
  return this.simple('POST', 'favorite', { id }, StatusOkSchema);
53
138
  }
139
+ /**
140
+ * Unfavorites a channel.
141
+ *
142
+ * @param id - The channel ID.
143
+ */
54
144
  unfavoriteChannel(id) {
55
145
  return this.simple('POST', 'unfavorite', { id }, StatusOkSchema);
56
146
  }
147
+ /**
148
+ * Adds a user to a channel.
149
+ *
150
+ * @param args - The arguments for adding a user.
151
+ * @param args.id - The channel ID.
152
+ * @param args.userId - The user ID to add.
153
+ *
154
+ * @example
155
+ * ```typescript
156
+ * await api.channels.addUser({ id: '7YpL3oZ4kZ9vP7Q1tR2sX44', userId: 101 })
157
+ * ```
158
+ */
57
159
  addUser(args) {
58
- return this.simple('POST', 'add_user', { ...args }, ChannelSchema);
160
+ return this.simple('POST', 'add_user', { ...args }, this.channelSchema);
59
161
  }
162
+ /**
163
+ * Adds multiple users to a channel.
164
+ *
165
+ * @param args - The arguments for adding users.
166
+ * @param args.id - The channel ID.
167
+ * @param args.userIds - Array of user IDs to add.
168
+ *
169
+ * @example
170
+ * ```typescript
171
+ * await api.channels.addUsers({ id: '7YpL3oZ4kZ9vP7Q1tR2sX44', userIds: [101, 202] })
172
+ * ```
173
+ */
60
174
  addUsers(args) {
61
- return this.simple('POST', 'add_users', { ...args }, ChannelSchema);
175
+ return this.simple('POST', 'add_users', { ...args }, this.channelSchema);
62
176
  }
177
+ /**
178
+ * Removes a user from a channel.
179
+ *
180
+ * @param args - The arguments for removing a user.
181
+ * @param args.id - The channel ID.
182
+ * @param args.userId - The user ID to remove.
183
+ */
63
184
  removeUser(args) {
64
- return this.simple('POST', 'remove_user', { ...args }, ChannelSchema);
185
+ return this.simple('POST', 'remove_user', { ...args }, this.channelSchema);
65
186
  }
187
+ /**
188
+ * Removes multiple users from a channel.
189
+ *
190
+ * @param args - The arguments for removing users.
191
+ * @param args.id - The channel ID.
192
+ * @param args.userIds - Array of user IDs to remove.
193
+ */
66
194
  removeUsers(args) {
67
- return this.simple('POST', 'remove_users', { ...args }, ChannelSchema);
195
+ return this.simple('POST', 'remove_users', { ...args }, this.channelSchema);
68
196
  }
69
197
  simple(httpMethod, suffix, params, schema) {
70
198
  return request({