@doist/comms-sdk 0.1.0-alpha.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.
- package/LICENSE +21 -0
- package/README.md +143 -0
- package/dist/cjs/authentication.js +211 -0
- package/dist/cjs/clients/add-comment-helper.js +53 -0
- package/dist/cjs/clients/base-client.js +27 -0
- package/dist/cjs/clients/channels-client.js +83 -0
- package/dist/cjs/clients/comments-client.js +93 -0
- package/dist/cjs/clients/conversation-messages-client.js +87 -0
- package/dist/cjs/clients/conversations-client.js +103 -0
- package/dist/cjs/clients/groups-client.js +71 -0
- package/dist/cjs/clients/inbox-client.js +98 -0
- package/dist/cjs/clients/reactions-client.js +59 -0
- package/dist/cjs/clients/search-client.js +88 -0
- package/dist/cjs/clients/threads-client.js +135 -0
- package/dist/cjs/clients/users-client.js +199 -0
- package/dist/cjs/clients/workspace-users-client.js +140 -0
- package/dist/cjs/clients/workspaces-client.js +93 -0
- package/dist/cjs/comms-api.js +65 -0
- package/dist/cjs/consts/endpoints.js +27 -0
- package/dist/cjs/index.js +48 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/testUtils/msw-handlers.js +51 -0
- package/dist/cjs/testUtils/msw-setup.js +21 -0
- package/dist/cjs/testUtils/obsidian-fetch-adapter.js +53 -0
- package/dist/cjs/testUtils/test-defaults.js +102 -0
- package/dist/cjs/transport/fetch-with-retry.js +136 -0
- package/dist/cjs/transport/http-client.js +56 -0
- package/dist/cjs/transport/http-dispatcher.js +143 -0
- package/dist/cjs/types/entities.js +411 -0
- package/dist/cjs/types/enums.js +37 -0
- package/dist/cjs/types/errors.js +12 -0
- package/dist/cjs/types/http.js +4 -0
- package/dist/cjs/types/index.js +21 -0
- package/dist/cjs/types/requests.js +117 -0
- package/dist/cjs/utils/case-conversion.js +54 -0
- package/dist/cjs/utils/index.js +19 -0
- package/dist/cjs/utils/timestamp-conversion.js +49 -0
- package/dist/cjs/utils/url-helpers.js +131 -0
- package/dist/cjs/utils/uuidv7.js +174 -0
- package/dist/esm/authentication.js +203 -0
- package/dist/esm/clients/add-comment-helper.js +50 -0
- package/dist/esm/clients/base-client.js +23 -0
- package/dist/esm/clients/channels-client.js +79 -0
- package/dist/esm/clients/comments-client.js +89 -0
- package/dist/esm/clients/conversation-messages-client.js +83 -0
- package/dist/esm/clients/conversations-client.js +99 -0
- package/dist/esm/clients/groups-client.js +67 -0
- package/dist/esm/clients/inbox-client.js +94 -0
- package/dist/esm/clients/reactions-client.js +55 -0
- package/dist/esm/clients/search-client.js +84 -0
- package/dist/esm/clients/threads-client.js +131 -0
- package/dist/esm/clients/users-client.js +195 -0
- package/dist/esm/clients/workspace-users-client.js +136 -0
- package/dist/esm/clients/workspaces-client.js +89 -0
- package/dist/esm/comms-api.js +61 -0
- package/dist/esm/consts/endpoints.js +23 -0
- package/dist/esm/index.js +17 -0
- package/dist/esm/testUtils/msw-handlers.js +45 -0
- package/dist/esm/testUtils/msw-setup.js +18 -0
- package/dist/esm/testUtils/obsidian-fetch-adapter.js +50 -0
- package/dist/esm/testUtils/test-defaults.js +99 -0
- package/dist/esm/transport/fetch-with-retry.js +133 -0
- package/dist/esm/transport/http-client.js +51 -0
- package/dist/esm/transport/http-dispatcher.js +104 -0
- package/dist/esm/types/entities.js +408 -0
- package/dist/esm/types/enums.js +34 -0
- package/dist/esm/types/errors.js +8 -0
- package/dist/esm/types/http.js +1 -0
- package/dist/esm/types/index.js +5 -0
- package/dist/esm/types/requests.js +114 -0
- package/dist/esm/utils/case-conversion.js +47 -0
- package/dist/esm/utils/index.js +3 -0
- package/dist/esm/utils/timestamp-conversion.js +45 -0
- package/dist/esm/utils/url-helpers.js +112 -0
- package/dist/esm/utils/uuidv7.js +163 -0
- package/dist/types/authentication.d.ts +160 -0
- package/dist/types/clients/add-comment-helper.d.ts +12 -0
- package/dist/types/clients/base-client.d.ts +24 -0
- package/dist/types/clients/channels-client.d.ts +91 -0
- package/dist/types/clients/comments-client.d.ts +157 -0
- package/dist/types/clients/conversation-messages-client.d.ts +127 -0
- package/dist/types/clients/conversations-client.d.ts +206 -0
- package/dist/types/clients/groups-client.d.ts +55 -0
- package/dist/types/clients/inbox-client.d.ts +20 -0
- package/dist/types/clients/reactions-client.d.ts +19 -0
- package/dist/types/clients/search-client.d.ts +20 -0
- package/dist/types/clients/threads-client.d.ts +344 -0
- package/dist/types/clients/users-client.d.ts +123 -0
- package/dist/types/clients/workspace-users-client.d.ts +47 -0
- package/dist/types/clients/workspaces-client.d.ts +79 -0
- package/dist/types/comms-api.d.ts +59 -0
- package/dist/types/consts/endpoints.d.ts +19 -0
- package/dist/types/index.d.ts +18 -0
- package/dist/types/testUtils/msw-handlers.d.ts +28 -0
- package/dist/types/testUtils/msw-setup.d.ts +1 -0
- package/dist/types/testUtils/obsidian-fetch-adapter.d.ts +29 -0
- package/dist/types/testUtils/test-defaults.d.ts +16 -0
- package/dist/types/transport/fetch-with-retry.d.ts +4 -0
- package/dist/types/transport/http-client.d.ts +13 -0
- package/dist/types/transport/http-dispatcher.d.ts +10 -0
- package/dist/types/types/entities.d.ts +1288 -0
- package/dist/types/types/enums.d.ts +55 -0
- package/dist/types/types/errors.d.ts +6 -0
- package/dist/types/types/http.d.ts +54 -0
- package/dist/types/types/index.d.ts +5 -0
- package/dist/types/types/requests.d.ts +385 -0
- package/dist/types/utils/case-conversion.d.ts +8 -0
- package/dist/types/utils/index.d.ts +3 -0
- package/dist/types/utils/timestamp-conversion.d.ts +13 -0
- package/dist/types/utils/url-helpers.d.ts +88 -0
- package/dist/types/utils/uuidv7.d.ts +40 -0
- package/package.json +93 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { ENDPOINT_THREADS } from '../consts/endpoints.js';
|
|
3
|
+
import { request } from '../transport/http-client.js';
|
|
4
|
+
import { StatusOkSchema, ThreadSchema, UnreadThreadSchema, } from '../types/entities.js';
|
|
5
|
+
import { resolveCreateId } from '../utils/uuidv7.js';
|
|
6
|
+
import { addCommentRequest } from './add-comment-helper.js';
|
|
7
|
+
import { BaseClient } from './base-client.js';
|
|
8
|
+
export const ThreadListSchema = z.array(ThreadSchema);
|
|
9
|
+
const GetUnreadResponseSchema = z.object({
|
|
10
|
+
data: z.array(UnreadThreadSchema),
|
|
11
|
+
version: z.number().int(),
|
|
12
|
+
inboxUnread: z.number().int().nullable().optional(),
|
|
13
|
+
});
|
|
14
|
+
/**
|
|
15
|
+
* Client for `/api/v1/threads/`. The SDK auto-generates the thread `id` on
|
|
16
|
+
* `createThread` when the caller doesn't supply one.
|
|
17
|
+
*/
|
|
18
|
+
export class ThreadsClient extends BaseClient {
|
|
19
|
+
/**
|
|
20
|
+
* Lists threads. At least one of `channelId` / `workspaceId` is required.
|
|
21
|
+
* `newerThan` / `olderThan` (`Date`) are converted to the
|
|
22
|
+
* `newer_than_ts` / `older_than_ts` epoch-second params on the wire.
|
|
23
|
+
*/
|
|
24
|
+
getThreads(args) {
|
|
25
|
+
const { newerThan, olderThan, newer_than_ts, older_than_ts, ...rest } = args;
|
|
26
|
+
const resolvedNewerThan = newerThan ? Math.floor(newerThan.getTime() / 1000) : newer_than_ts;
|
|
27
|
+
const resolvedOlderThan = olderThan ? Math.floor(olderThan.getTime() / 1000) : older_than_ts;
|
|
28
|
+
const params = {
|
|
29
|
+
...rest,
|
|
30
|
+
...(resolvedNewerThan != null ? { newer_than_ts: resolvedNewerThan } : {}),
|
|
31
|
+
...(resolvedOlderThan != null ? { older_than_ts: resolvedOlderThan } : {}),
|
|
32
|
+
};
|
|
33
|
+
return request({
|
|
34
|
+
httpMethod: 'GET',
|
|
35
|
+
baseUri: this.getBaseUri(),
|
|
36
|
+
relativePath: `${ENDPOINT_THREADS}/get`,
|
|
37
|
+
apiToken: this.apiToken,
|
|
38
|
+
payload: params,
|
|
39
|
+
customFetch: this.customFetch,
|
|
40
|
+
}).then((response) => ThreadListSchema.parse(response.data));
|
|
41
|
+
}
|
|
42
|
+
/** Fetches a single thread by ID. */
|
|
43
|
+
getThread(id) {
|
|
44
|
+
return this.simple('GET', 'getone', { id }, ThreadSchema);
|
|
45
|
+
}
|
|
46
|
+
/** Creates a new thread. `id` is auto-generated if not supplied. */
|
|
47
|
+
createThread(args) {
|
|
48
|
+
return this.simple('POST', 'add', { ...args, id: resolveCreateId(args.id) }, ThreadSchema);
|
|
49
|
+
}
|
|
50
|
+
/** Partial update of an existing thread. */
|
|
51
|
+
updateThread(args) {
|
|
52
|
+
return this.simple('POST', 'update', { ...args }, ThreadSchema);
|
|
53
|
+
}
|
|
54
|
+
/** Permanently deletes a thread. */
|
|
55
|
+
deleteThread(id) {
|
|
56
|
+
return this.simple('POST', 'remove', { id }, StatusOkSchema);
|
|
57
|
+
}
|
|
58
|
+
/** Saves a thread (formerly "star"). */
|
|
59
|
+
saveThread(id) {
|
|
60
|
+
return this.simple('GET', 'save', { id }, StatusOkSchema);
|
|
61
|
+
}
|
|
62
|
+
/** Unsaves a thread (formerly "unstar"). */
|
|
63
|
+
unsaveThread(id) {
|
|
64
|
+
return this.simple('GET', 'unsave', { id }, StatusOkSchema);
|
|
65
|
+
}
|
|
66
|
+
pinThread(id) {
|
|
67
|
+
return this.simple('GET', 'pin', { id }, StatusOkSchema);
|
|
68
|
+
}
|
|
69
|
+
unpinThread(id) {
|
|
70
|
+
return this.simple('GET', 'unpin', { id }, StatusOkSchema);
|
|
71
|
+
}
|
|
72
|
+
/** Moves a thread to another channel. */
|
|
73
|
+
moveToChannel(args) {
|
|
74
|
+
return this.simple('GET', 'move_to_channel', { ...args }, ThreadSchema);
|
|
75
|
+
}
|
|
76
|
+
markRead(args) {
|
|
77
|
+
return this.simple('POST', 'mark_read', { ...args }, StatusOkSchema);
|
|
78
|
+
}
|
|
79
|
+
markUnread(args) {
|
|
80
|
+
return this.simple('POST', 'mark_unread', { ...args }, StatusOkSchema);
|
|
81
|
+
}
|
|
82
|
+
markUnreadForOthers(args) {
|
|
83
|
+
return this.simple('POST', 'mark_unread_for_others', { ...args }, StatusOkSchema);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Marks every thread in a workspace or channel as read. Exactly one of
|
|
87
|
+
* `workspaceId` / `channelId` should be set.
|
|
88
|
+
*/
|
|
89
|
+
markAllRead(args) {
|
|
90
|
+
if (!args.workspaceId && !args.channelId) {
|
|
91
|
+
throw new Error('Either workspaceId or channelId is required');
|
|
92
|
+
}
|
|
93
|
+
return this.simple('POST', 'mark_all_read', { ...args }, StatusOkSchema);
|
|
94
|
+
}
|
|
95
|
+
clearUnread(workspaceId) {
|
|
96
|
+
return this.simple('GET', 'clear_unread', { workspaceId }, StatusOkSchema);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Returns unread threads for a workspace, paired with the unread version
|
|
100
|
+
* counter and (optionally) the inbox unread count.
|
|
101
|
+
*/
|
|
102
|
+
getUnread(workspaceId) {
|
|
103
|
+
return this.simple('GET', 'get_unread', { workspaceId }, GetUnreadResponseSchema);
|
|
104
|
+
}
|
|
105
|
+
muteThread(args) {
|
|
106
|
+
return this.simple('GET', 'mute', { ...args }, ThreadSchema);
|
|
107
|
+
}
|
|
108
|
+
unmuteThread(id) {
|
|
109
|
+
return this.simple('GET', 'unmute', { id }, ThreadSchema);
|
|
110
|
+
}
|
|
111
|
+
closeThread(args) {
|
|
112
|
+
return this.addCommentWithAction(args, 'close');
|
|
113
|
+
}
|
|
114
|
+
reopenThread(args) {
|
|
115
|
+
return this.addCommentWithAction(args, 'reopen');
|
|
116
|
+
}
|
|
117
|
+
addCommentWithAction(args, threadAction) {
|
|
118
|
+
const { id, ...rest } = args;
|
|
119
|
+
return addCommentRequest({ baseUri: this.getBaseUri(), apiToken: this.apiToken, customFetch: this.customFetch }, { threadId: id, ...rest }, { threadAction });
|
|
120
|
+
}
|
|
121
|
+
simple(httpMethod, suffix, params, schema) {
|
|
122
|
+
return request({
|
|
123
|
+
httpMethod,
|
|
124
|
+
baseUri: this.getBaseUri(),
|
|
125
|
+
relativePath: `${ENDPOINT_THREADS}/${suffix}`,
|
|
126
|
+
apiToken: this.apiToken,
|
|
127
|
+
payload: params,
|
|
128
|
+
customFetch: this.customFetch,
|
|
129
|
+
}).then((response) => schema.parse(response.data));
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { ENDPOINT_USERS } from '../consts/endpoints.js';
|
|
2
|
+
import { request } from '../transport/http-client.js';
|
|
3
|
+
import { UserSchema } from '../types/entities.js';
|
|
4
|
+
import { BaseClient } from './base-client.js';
|
|
5
|
+
/**
|
|
6
|
+
* Client for the `/api/v1/users/` endpoints. Authentication flows through
|
|
7
|
+
* Todoist-ID; `register` / `login` / `loginWithGoogle` / `loginWithToken` /
|
|
8
|
+
* `loginWithTodoist` are the available entry points.
|
|
9
|
+
*/
|
|
10
|
+
export class UsersClient extends BaseClient {
|
|
11
|
+
/** Registers a new user via the Todoist-ID bridge. */
|
|
12
|
+
register(args) {
|
|
13
|
+
return this.post(`${ENDPOINT_USERS}/register`, args, UserSchema, { authed: false });
|
|
14
|
+
}
|
|
15
|
+
/** Logs in an existing user. */
|
|
16
|
+
login(args) {
|
|
17
|
+
return this.post(`${ENDPOINT_USERS}/login`, args, UserSchema, { authed: false });
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Logs in using a valid token (sent via Authorization header). The SDK
|
|
21
|
+
* client is already configured with the token, so no args are needed.
|
|
22
|
+
*/
|
|
23
|
+
loginWithToken() {
|
|
24
|
+
return this.post(`${ENDPOINT_USERS}/login_with_token`, undefined, UserSchema);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Exchanges the browser's Todoist web-session cookie for a Comms session.
|
|
28
|
+
* Only useful when running in a browser context on the shared Todoist
|
|
29
|
+
* registrable domain — the cookie is sent automatically by the browser.
|
|
30
|
+
*/
|
|
31
|
+
loginWithTodoist() {
|
|
32
|
+
return this.post(`${ENDPOINT_USERS}/login_with_todoist`, {}, UserSchema, { authed: false });
|
|
33
|
+
}
|
|
34
|
+
/** Logs in (and auto-signs-up) via a Google ID token. */
|
|
35
|
+
loginWithGoogle(args) {
|
|
36
|
+
return this.post(`${ENDPOINT_USERS}/login_with_google`, args, UserSchema, { authed: false });
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Completes an MFA challenge issued by `loginWithGoogle` (returns an MFA
|
|
40
|
+
* token to pass back to `loginWithGoogle.mfaToken`).
|
|
41
|
+
*/
|
|
42
|
+
mfaChallenge(args) {
|
|
43
|
+
return request({
|
|
44
|
+
httpMethod: 'POST',
|
|
45
|
+
baseUri: this.getBaseUri(),
|
|
46
|
+
relativePath: `${ENDPOINT_USERS}/mfa/challenge`,
|
|
47
|
+
apiToken: undefined,
|
|
48
|
+
payload: args,
|
|
49
|
+
customFetch: this.customFetch,
|
|
50
|
+
}).then((response) => response.data);
|
|
51
|
+
}
|
|
52
|
+
/** Logs out the current user and clears the session cookie. */
|
|
53
|
+
logout() {
|
|
54
|
+
return request({
|
|
55
|
+
httpMethod: 'POST',
|
|
56
|
+
baseUri: this.getBaseUri(),
|
|
57
|
+
relativePath: `${ENDPOINT_USERS}/logout`,
|
|
58
|
+
apiToken: this.apiToken,
|
|
59
|
+
payload: undefined,
|
|
60
|
+
customFetch: this.customFetch,
|
|
61
|
+
}).then(() => undefined);
|
|
62
|
+
}
|
|
63
|
+
/** Returns the user associated with the current access token. */
|
|
64
|
+
getSessionUser() {
|
|
65
|
+
return this.get(`${ENDPOINT_USERS}/get_session_user`, undefined, UserSchema);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Fetches a single user. Defaults to the session user when no `id` is
|
|
69
|
+
* passed. Cross-workspace lookups require that the caller and the target
|
|
70
|
+
* share a workspace.
|
|
71
|
+
*/
|
|
72
|
+
getUser(args) {
|
|
73
|
+
return this.get(`${ENDPOINT_USERS}/getone`, args ?? {}, UserSchema);
|
|
74
|
+
}
|
|
75
|
+
/** Looks up a user by their email address. */
|
|
76
|
+
getUserByEmail(email) {
|
|
77
|
+
return this.get(`${ENDPOINT_USERS}/get_by_email`, { email }, UserSchema);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Updates the logged-in user's profile. Most fields are proxied to
|
|
81
|
+
* Todoist (full name, password, language, timezone, etc.).
|
|
82
|
+
*/
|
|
83
|
+
update(args) {
|
|
84
|
+
return this.post(`${ENDPOINT_USERS}/update`, args, UserSchema);
|
|
85
|
+
}
|
|
86
|
+
/** Updates the user's password. Requires `currentPassword`. */
|
|
87
|
+
updatePassword(args) {
|
|
88
|
+
return this.post(`${ENDPOINT_USERS}/update_password`, args, UserSchema);
|
|
89
|
+
}
|
|
90
|
+
/** Removes the user's avatar. */
|
|
91
|
+
removeAvatar() {
|
|
92
|
+
return this.post(`${ENDPOINT_USERS}/remove_avatar`, undefined, UserSchema);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Invalidates the current API token and returns the user with a fresh
|
|
96
|
+
* token.
|
|
97
|
+
*/
|
|
98
|
+
invalidateToken() {
|
|
99
|
+
return this.post(`${ENDPOINT_USERS}/invalidate_token`, undefined, UserSchema);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Validates that an arbitrary token is still active. Note this is sent
|
|
103
|
+
* as a GET — the token is read from the query string, not the
|
|
104
|
+
* Authorization header.
|
|
105
|
+
*/
|
|
106
|
+
validateToken(token) {
|
|
107
|
+
return request({
|
|
108
|
+
httpMethod: 'GET',
|
|
109
|
+
baseUri: this.getBaseUri(),
|
|
110
|
+
relativePath: `${ENDPOINT_USERS}/validate_token`,
|
|
111
|
+
apiToken: undefined,
|
|
112
|
+
payload: { token },
|
|
113
|
+
customFetch: this.customFetch,
|
|
114
|
+
}).then(() => undefined);
|
|
115
|
+
}
|
|
116
|
+
/** Marks the user as active on a workspace (presence beacon). */
|
|
117
|
+
heartbeat(args) {
|
|
118
|
+
return request({
|
|
119
|
+
httpMethod: 'GET',
|
|
120
|
+
baseUri: this.getBaseUri(),
|
|
121
|
+
relativePath: `${ENDPOINT_USERS}/heartbeat`,
|
|
122
|
+
apiToken: this.apiToken,
|
|
123
|
+
payload: args,
|
|
124
|
+
customFetch: this.customFetch,
|
|
125
|
+
}).then(() => undefined);
|
|
126
|
+
}
|
|
127
|
+
/** Resets the user's presence for a workspace. */
|
|
128
|
+
resetPresence(workspaceId) {
|
|
129
|
+
return request({
|
|
130
|
+
httpMethod: 'POST',
|
|
131
|
+
baseUri: this.getBaseUri(),
|
|
132
|
+
relativePath: `${ENDPOINT_USERS}/reset_presence`,
|
|
133
|
+
apiToken: this.apiToken,
|
|
134
|
+
payload: { workspaceId },
|
|
135
|
+
customFetch: this.customFetch,
|
|
136
|
+
}).then(() => undefined);
|
|
137
|
+
}
|
|
138
|
+
/** Checks whether an email address is registered (and verified). */
|
|
139
|
+
checkEmail(email) {
|
|
140
|
+
return request({
|
|
141
|
+
httpMethod: 'POST',
|
|
142
|
+
baseUri: this.getBaseUri(),
|
|
143
|
+
relativePath: `${ENDPOINT_USERS}/check_email`,
|
|
144
|
+
apiToken: undefined,
|
|
145
|
+
payload: { email },
|
|
146
|
+
customFetch: this.customFetch,
|
|
147
|
+
}).then((response) => response.data);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Returns the current per-channel mail unsubscribe settings for the
|
|
151
|
+
* caller's primary email.
|
|
152
|
+
*/
|
|
153
|
+
getUnsubscribeSettings() {
|
|
154
|
+
return request({
|
|
155
|
+
httpMethod: 'GET',
|
|
156
|
+
baseUri: this.getBaseUri(),
|
|
157
|
+
relativePath: `${ENDPOINT_USERS}/get_unsubscribe_settings`,
|
|
158
|
+
apiToken: this.apiToken,
|
|
159
|
+
payload: undefined,
|
|
160
|
+
customFetch: this.customFetch,
|
|
161
|
+
}).then((response) => response.data);
|
|
162
|
+
}
|
|
163
|
+
/** Toggles per-email-type opt-out settings. */
|
|
164
|
+
updateUnsubscribeSettings(settings) {
|
|
165
|
+
return request({
|
|
166
|
+
httpMethod: 'POST',
|
|
167
|
+
baseUri: this.getBaseUri(),
|
|
168
|
+
relativePath: `${ENDPOINT_USERS}/update_unsubscribe_settings`,
|
|
169
|
+
apiToken: this.apiToken,
|
|
170
|
+
payload: settings,
|
|
171
|
+
customFetch: this.customFetch,
|
|
172
|
+
}).then((response) => response.data);
|
|
173
|
+
}
|
|
174
|
+
get(url, params, schema) {
|
|
175
|
+
return request({
|
|
176
|
+
httpMethod: 'GET',
|
|
177
|
+
baseUri: this.getBaseUri(),
|
|
178
|
+
relativePath: url,
|
|
179
|
+
apiToken: this.apiToken,
|
|
180
|
+
payload: params,
|
|
181
|
+
customFetch: this.customFetch,
|
|
182
|
+
}).then((response) => schema.parse(response.data));
|
|
183
|
+
}
|
|
184
|
+
post(url, params, schema, options = {}) {
|
|
185
|
+
const authed = options.authed ?? true;
|
|
186
|
+
return request({
|
|
187
|
+
httpMethod: 'POST',
|
|
188
|
+
baseUri: this.getBaseUri(),
|
|
189
|
+
relativePath: url,
|
|
190
|
+
apiToken: authed ? this.apiToken : undefined,
|
|
191
|
+
payload: params,
|
|
192
|
+
customFetch: this.customFetch,
|
|
193
|
+
}).then((response) => schema.parse(response.data));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { request } from '../transport/http-client.js';
|
|
2
|
+
import { WorkspaceUserSchema } from '../types/entities.js';
|
|
3
|
+
import { BaseClient } from './base-client.js';
|
|
4
|
+
/**
|
|
5
|
+
* Client for `/api/v1/workspace_users/`. The backend's `add` endpoint
|
|
6
|
+
* rejects non-empty `name` and `channelIds` — set neither.
|
|
7
|
+
*/
|
|
8
|
+
export class WorkspaceUsersClient extends BaseClient {
|
|
9
|
+
/** Returns workspace user objects for the given workspace id. */
|
|
10
|
+
getWorkspaceUsers(args) {
|
|
11
|
+
return request({
|
|
12
|
+
httpMethod: 'GET',
|
|
13
|
+
baseUri: this.getBaseUri(),
|
|
14
|
+
relativePath: 'workspace_users/get',
|
|
15
|
+
apiToken: this.apiToken,
|
|
16
|
+
payload: { id: args.workspaceId, archived: args.archived },
|
|
17
|
+
customFetch: this.customFetch,
|
|
18
|
+
}).then((response) => response.data.map((user) => WorkspaceUserSchema.parse(user)));
|
|
19
|
+
}
|
|
20
|
+
/** Returns workspace user IDs for the given workspace id. */
|
|
21
|
+
getWorkspaceUserIds(workspaceId) {
|
|
22
|
+
return request({
|
|
23
|
+
httpMethod: 'GET',
|
|
24
|
+
baseUri: this.getBaseUri(),
|
|
25
|
+
relativePath: 'workspace_users/get_ids',
|
|
26
|
+
apiToken: this.apiToken,
|
|
27
|
+
payload: { id: workspaceId },
|
|
28
|
+
customFetch: this.customFetch,
|
|
29
|
+
}).then((response) => response.data);
|
|
30
|
+
}
|
|
31
|
+
/** Gets a user by id. */
|
|
32
|
+
getUserById(args) {
|
|
33
|
+
return request({
|
|
34
|
+
httpMethod: 'GET',
|
|
35
|
+
baseUri: this.getBaseUri(),
|
|
36
|
+
relativePath: 'workspace_users/getone',
|
|
37
|
+
apiToken: this.apiToken,
|
|
38
|
+
payload: { id: args.workspaceId, user_id: args.userId },
|
|
39
|
+
customFetch: this.customFetch,
|
|
40
|
+
}).then((response) => WorkspaceUserSchema.parse(response.data));
|
|
41
|
+
}
|
|
42
|
+
/** Gets a user by email. */
|
|
43
|
+
getUserByEmail(args) {
|
|
44
|
+
return request({
|
|
45
|
+
httpMethod: 'GET',
|
|
46
|
+
baseUri: this.getBaseUri(),
|
|
47
|
+
relativePath: 'workspace_users/get_by_email',
|
|
48
|
+
apiToken: this.apiToken,
|
|
49
|
+
payload: { id: args.workspaceId, email: args.email },
|
|
50
|
+
customFetch: this.customFetch,
|
|
51
|
+
}).then((response) => WorkspaceUserSchema.parse(response.data));
|
|
52
|
+
}
|
|
53
|
+
/** Gets the user's info in the context of the workspace. */
|
|
54
|
+
getUserInfo(args) {
|
|
55
|
+
return request({
|
|
56
|
+
httpMethod: 'GET',
|
|
57
|
+
baseUri: this.getBaseUri(),
|
|
58
|
+
relativePath: 'workspace_users/get_info',
|
|
59
|
+
apiToken: this.apiToken,
|
|
60
|
+
payload: { id: args.workspaceId, user_id: args.userId },
|
|
61
|
+
customFetch: this.customFetch,
|
|
62
|
+
}).then((response) => response.data);
|
|
63
|
+
}
|
|
64
|
+
/** Gets the user's local time (e.g., "2017-05-10 07:55:40"). */
|
|
65
|
+
getUserLocalTime(args) {
|
|
66
|
+
return request({
|
|
67
|
+
httpMethod: 'GET',
|
|
68
|
+
baseUri: this.getBaseUri(),
|
|
69
|
+
relativePath: 'workspace_users/get_local_time',
|
|
70
|
+
apiToken: this.apiToken,
|
|
71
|
+
payload: { id: args.workspaceId, user_id: args.userId },
|
|
72
|
+
customFetch: this.customFetch,
|
|
73
|
+
}).then((response) => response.data);
|
|
74
|
+
}
|
|
75
|
+
/** Adds a person to a workspace. */
|
|
76
|
+
addUser(args) {
|
|
77
|
+
return request({
|
|
78
|
+
httpMethod: 'POST',
|
|
79
|
+
baseUri: this.getBaseUri(),
|
|
80
|
+
relativePath: 'workspace_users/add',
|
|
81
|
+
apiToken: this.apiToken,
|
|
82
|
+
payload: {
|
|
83
|
+
id: args.workspaceId,
|
|
84
|
+
email: args.email,
|
|
85
|
+
userType: args.userType,
|
|
86
|
+
},
|
|
87
|
+
customFetch: this.customFetch,
|
|
88
|
+
}).then((response) => WorkspaceUserSchema.parse(response.data));
|
|
89
|
+
}
|
|
90
|
+
/** Updates a person in a workspace. */
|
|
91
|
+
updateUser(args) {
|
|
92
|
+
return request({
|
|
93
|
+
httpMethod: 'POST',
|
|
94
|
+
baseUri: this.getBaseUri(),
|
|
95
|
+
relativePath: 'workspace_users/update',
|
|
96
|
+
apiToken: this.apiToken,
|
|
97
|
+
payload: {
|
|
98
|
+
id: args.workspaceId,
|
|
99
|
+
userType: args.userType,
|
|
100
|
+
email: args.email,
|
|
101
|
+
userId: args.userId,
|
|
102
|
+
},
|
|
103
|
+
customFetch: this.customFetch,
|
|
104
|
+
}).then((response) => WorkspaceUserSchema.parse(response.data));
|
|
105
|
+
}
|
|
106
|
+
/** Removes a person from a workspace. */
|
|
107
|
+
removeUser(args) {
|
|
108
|
+
return request({
|
|
109
|
+
httpMethod: 'POST',
|
|
110
|
+
baseUri: this.getBaseUri(),
|
|
111
|
+
relativePath: 'workspace_users/remove',
|
|
112
|
+
apiToken: this.apiToken,
|
|
113
|
+
payload: {
|
|
114
|
+
id: args.workspaceId,
|
|
115
|
+
email: args.email,
|
|
116
|
+
userId: args.userId,
|
|
117
|
+
},
|
|
118
|
+
customFetch: this.customFetch,
|
|
119
|
+
}).then(() => undefined);
|
|
120
|
+
}
|
|
121
|
+
/** Sends a new workspace invitation to the selected user. */
|
|
122
|
+
resendInvite(args) {
|
|
123
|
+
return request({
|
|
124
|
+
httpMethod: 'POST',
|
|
125
|
+
baseUri: this.getBaseUri(),
|
|
126
|
+
relativePath: 'workspace_users/resend_invite',
|
|
127
|
+
apiToken: this.apiToken,
|
|
128
|
+
payload: {
|
|
129
|
+
id: args.workspaceId,
|
|
130
|
+
email: args.email,
|
|
131
|
+
userId: args.userId,
|
|
132
|
+
},
|
|
133
|
+
customFetch: this.customFetch,
|
|
134
|
+
}).then(() => undefined);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { ENDPOINT_WORKSPACES } from '../consts/endpoints.js';
|
|
3
|
+
import { request } from '../transport/http-client.js';
|
|
4
|
+
import { ChannelSchema, WorkspaceSchema } from '../types/entities.js';
|
|
5
|
+
import { BaseClient } from './base-client.js';
|
|
6
|
+
export const ChannelListSchema = z.array(ChannelSchema);
|
|
7
|
+
/**
|
|
8
|
+
* Client for `/api/v1/workspaces/`. Workspace IDs are integers. The backend
|
|
9
|
+
* currently rejects any `color` other than `1` on add/update.
|
|
10
|
+
*/
|
|
11
|
+
export class WorkspacesClient extends BaseClient {
|
|
12
|
+
/** Gets all the user's workspaces. */
|
|
13
|
+
getWorkspaces() {
|
|
14
|
+
return request({
|
|
15
|
+
httpMethod: 'GET',
|
|
16
|
+
baseUri: this.getBaseUri(),
|
|
17
|
+
relativePath: `${ENDPOINT_WORKSPACES}/get`,
|
|
18
|
+
apiToken: this.apiToken,
|
|
19
|
+
payload: undefined,
|
|
20
|
+
customFetch: this.customFetch,
|
|
21
|
+
}).then((response) => response.data.map((workspace) => WorkspaceSchema.parse(workspace)));
|
|
22
|
+
}
|
|
23
|
+
/** Gets a single workspace object by id. */
|
|
24
|
+
getWorkspace(id) {
|
|
25
|
+
return request({
|
|
26
|
+
httpMethod: 'GET',
|
|
27
|
+
baseUri: this.getBaseUri(),
|
|
28
|
+
relativePath: `${ENDPOINT_WORKSPACES}/getone`,
|
|
29
|
+
apiToken: this.apiToken,
|
|
30
|
+
payload: { id },
|
|
31
|
+
customFetch: this.customFetch,
|
|
32
|
+
}).then((response) => WorkspaceSchema.parse(response.data));
|
|
33
|
+
}
|
|
34
|
+
/** Gets the user's default workspace. */
|
|
35
|
+
getDefaultWorkspace() {
|
|
36
|
+
return request({
|
|
37
|
+
httpMethod: 'GET',
|
|
38
|
+
baseUri: this.getBaseUri(),
|
|
39
|
+
relativePath: `${ENDPOINT_WORKSPACES}/get_default`,
|
|
40
|
+
apiToken: this.apiToken,
|
|
41
|
+
payload: undefined,
|
|
42
|
+
customFetch: this.customFetch,
|
|
43
|
+
}).then((response) => WorkspaceSchema.parse(response.data));
|
|
44
|
+
}
|
|
45
|
+
/** Creates a new workspace. */
|
|
46
|
+
createWorkspace(name) {
|
|
47
|
+
return request({
|
|
48
|
+
httpMethod: 'POST',
|
|
49
|
+
baseUri: this.getBaseUri(),
|
|
50
|
+
relativePath: `${ENDPOINT_WORKSPACES}/add`,
|
|
51
|
+
apiToken: this.apiToken,
|
|
52
|
+
payload: { name },
|
|
53
|
+
customFetch: this.customFetch,
|
|
54
|
+
}).then((response) => WorkspaceSchema.parse(response.data));
|
|
55
|
+
}
|
|
56
|
+
/** Updates an existing workspace. */
|
|
57
|
+
updateWorkspace(id, name) {
|
|
58
|
+
return request({
|
|
59
|
+
httpMethod: 'POST',
|
|
60
|
+
baseUri: this.getBaseUri(),
|
|
61
|
+
relativePath: `${ENDPOINT_WORKSPACES}/update`,
|
|
62
|
+
apiToken: this.apiToken,
|
|
63
|
+
payload: { id, name },
|
|
64
|
+
customFetch: this.customFetch,
|
|
65
|
+
}).then((response) => WorkspaceSchema.parse(response.data));
|
|
66
|
+
}
|
|
67
|
+
/** Removes a workspace and all its data (not recoverable). */
|
|
68
|
+
removeWorkspace(id) {
|
|
69
|
+
return request({
|
|
70
|
+
httpMethod: 'POST',
|
|
71
|
+
baseUri: this.getBaseUri(),
|
|
72
|
+
relativePath: `${ENDPOINT_WORKSPACES}/remove`,
|
|
73
|
+
apiToken: this.apiToken,
|
|
74
|
+
payload: { id },
|
|
75
|
+
customFetch: this.customFetch,
|
|
76
|
+
}).then(() => undefined);
|
|
77
|
+
}
|
|
78
|
+
/** Gets the public channels of a workspace. */
|
|
79
|
+
getPublicChannels(id) {
|
|
80
|
+
return request({
|
|
81
|
+
httpMethod: 'GET',
|
|
82
|
+
baseUri: this.getBaseUri(),
|
|
83
|
+
relativePath: `${ENDPOINT_WORKSPACES}/get_public_channels`,
|
|
84
|
+
apiToken: this.apiToken,
|
|
85
|
+
payload: { id },
|
|
86
|
+
customFetch: this.customFetch,
|
|
87
|
+
}).then((response) => ChannelListSchema.parse(response.data));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { ChannelsClient } from './clients/channels-client.js';
|
|
2
|
+
import { CommentsClient } from './clients/comments-client.js';
|
|
3
|
+
import { ConversationMessagesClient } from './clients/conversation-messages-client.js';
|
|
4
|
+
import { ConversationsClient } from './clients/conversations-client.js';
|
|
5
|
+
import { GroupsClient } from './clients/groups-client.js';
|
|
6
|
+
import { InboxClient } from './clients/inbox-client.js';
|
|
7
|
+
import { ReactionsClient } from './clients/reactions-client.js';
|
|
8
|
+
import { SearchClient } from './clients/search-client.js';
|
|
9
|
+
import { ThreadsClient } from './clients/threads-client.js';
|
|
10
|
+
import { UsersClient } from './clients/users-client.js';
|
|
11
|
+
import { WorkspaceUsersClient } from './clients/workspace-users-client.js';
|
|
12
|
+
import { WorkspacesClient } from './clients/workspaces-client.js';
|
|
13
|
+
import { closeDefaultDispatcher } from './transport/http-dispatcher.js';
|
|
14
|
+
/**
|
|
15
|
+
* The main API client for interacting with the Comms REST API.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { CommsApi } from '@doist/comms-sdk'
|
|
20
|
+
*
|
|
21
|
+
* const api = new CommsApi('your-api-token')
|
|
22
|
+
* const user = await api.users.getSessionUser()
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export class CommsApi {
|
|
26
|
+
/**
|
|
27
|
+
* Creates a new Comms API client.
|
|
28
|
+
*
|
|
29
|
+
* @param authToken - Your Comms API token.
|
|
30
|
+
* @param options - Optional configuration options.
|
|
31
|
+
*/
|
|
32
|
+
constructor(authToken, options) {
|
|
33
|
+
const clientConfig = {
|
|
34
|
+
apiToken: authToken,
|
|
35
|
+
baseUrl: options?.baseUrl,
|
|
36
|
+
customFetch: options?.customFetch,
|
|
37
|
+
};
|
|
38
|
+
this.users = new UsersClient(clientConfig);
|
|
39
|
+
this.workspaces = new WorkspacesClient(clientConfig);
|
|
40
|
+
this.workspaceUsers = new WorkspaceUsersClient(clientConfig);
|
|
41
|
+
this.channels = new ChannelsClient(clientConfig);
|
|
42
|
+
this.threads = new ThreadsClient(clientConfig);
|
|
43
|
+
this.groups = new GroupsClient(clientConfig);
|
|
44
|
+
this.conversations = new ConversationsClient(clientConfig);
|
|
45
|
+
this.comments = new CommentsClient(clientConfig);
|
|
46
|
+
this.conversationMessages = new ConversationMessagesClient(clientConfig);
|
|
47
|
+
this.inbox = new InboxClient(clientConfig);
|
|
48
|
+
this.reactions = new ReactionsClient(clientConfig);
|
|
49
|
+
this.search = new SearchClient(clientConfig);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Drains the SDK's process-global connection pool. CLIs and scripts
|
|
53
|
+
* should `await api.close()` before exit so Node's event loop empties
|
|
54
|
+
* immediately instead of waiting ~4s on keep-alive. Affects every
|
|
55
|
+
* `CommsApi` and OAuth helper in the same process — it's a
|
|
56
|
+
* process-shutdown gesture, not an instance teardown. Browser-safe.
|
|
57
|
+
*/
|
|
58
|
+
async close() {
|
|
59
|
+
await closeDefaultDispatcher();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const BASE_URI = 'https://comms.todoist.com';
|
|
2
|
+
const API_VERSION = 'v1';
|
|
3
|
+
/**
|
|
4
|
+
* Gets the base URI for Comms API requests.
|
|
5
|
+
*
|
|
6
|
+
* @param domainBase - Custom domain base URL. Defaults to Comms' API domain.
|
|
7
|
+
* @returns Complete base URI with trailing slash (e.g., 'https://comms.todoist.com/api/v1/')
|
|
8
|
+
*/
|
|
9
|
+
export function getCommsBaseUri(domainBase = BASE_URI) {
|
|
10
|
+
return new URL(`/api/${API_VERSION}/`, domainBase).toString();
|
|
11
|
+
}
|
|
12
|
+
export const ENDPOINT_USERS = 'users';
|
|
13
|
+
export const ENDPOINT_WORKSPACES = 'workspaces';
|
|
14
|
+
export const ENDPOINT_CHANNELS = 'channels';
|
|
15
|
+
export const ENDPOINT_THREADS = 'threads';
|
|
16
|
+
export const ENDPOINT_GROUPS = 'groups';
|
|
17
|
+
export const ENDPOINT_CONVERSATIONS = 'conversations';
|
|
18
|
+
export const ENDPOINT_COMMENTS = 'comments';
|
|
19
|
+
export const ENDPOINT_NOTIFICATIONS = 'notifications';
|
|
20
|
+
export const ENDPOINT_INBOX = 'inbox';
|
|
21
|
+
export const ENDPOINT_REACTIONS = 'reactions';
|
|
22
|
+
export const ENDPOINT_SEARCH = 'search';
|
|
23
|
+
export const ENDPOINT_CONVERSATION_MESSAGES = 'conversation_messages';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export * from './authentication.js';
|
|
2
|
+
export { ChannelsClient } from './clients/channels-client.js';
|
|
3
|
+
export { CommentsClient } from './clients/comments-client.js';
|
|
4
|
+
export { ConversationMessagesClient } from './clients/conversation-messages-client.js';
|
|
5
|
+
export { ConversationsClient } from './clients/conversations-client.js';
|
|
6
|
+
export { GroupsClient } from './clients/groups-client.js';
|
|
7
|
+
export { InboxClient } from './clients/inbox-client.js';
|
|
8
|
+
export { ReactionsClient } from './clients/reactions-client.js';
|
|
9
|
+
export { SearchClient } from './clients/search-client.js';
|
|
10
|
+
export { ThreadsClient } from './clients/threads-client.js';
|
|
11
|
+
export { UsersClient } from './clients/users-client.js';
|
|
12
|
+
export { WorkspaceUsersClient } from './clients/workspace-users-client.js';
|
|
13
|
+
export { WorkspacesClient } from './clients/workspaces-client.js';
|
|
14
|
+
export { CommsApi } from './comms-api.js';
|
|
15
|
+
export { closeDefaultDispatcher } from './transport/http-dispatcher.js';
|
|
16
|
+
export * from './types/index.js';
|
|
17
|
+
export * from './utils/index.js';
|