@doist/todoist-api-typescript 5.8.0 → 6.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.
- package/README.md +1 -1
- package/dist/cjs/authentication.js +158 -0
- package/dist/cjs/consts/endpoints.js +74 -0
- package/dist/{index.js → cjs/index.js} +1 -1
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/rest-client.js +124 -0
- package/dist/{testUtils → cjs/test-utils}/asserts.js +1 -1
- package/dist/{testUtils → cjs/test-utils}/mocks.js +8 -4
- package/dist/cjs/test-utils/msw-setup.js +27 -0
- package/dist/{testUtils/testDefaults.js → cjs/test-utils/test-defaults.js} +41 -52
- package/dist/cjs/todoist-api.js +1235 -0
- package/dist/{types → cjs/types}/entities.js +78 -31
- package/dist/cjs/types/errors.js +22 -0
- package/dist/cjs/types/http.js +22 -0
- package/dist/cjs/utils/case-conversion.js +69 -0
- package/dist/{utils → cjs/utils}/colors.js +3 -3
- package/dist/cjs/utils/fetch-with-retry.js +150 -0
- package/dist/{utils → cjs/utils}/index.js +4 -4
- package/dist/cjs/utils/multipart-upload.js +126 -0
- package/dist/{utils → cjs/utils}/processing-helpers.js +3 -3
- package/dist/{utils → cjs/utils}/sanitization.js +17 -28
- package/dist/{utils/urlHelpers.js → cjs/utils/url-helpers.js} +15 -15
- package/dist/{utils → cjs/utils}/validators.js +28 -1
- package/dist/esm/authentication.js +151 -0
- package/dist/esm/consts/endpoints.js +65 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/rest-client.js +119 -0
- package/dist/esm/test-utils/asserts.js +8 -0
- package/dist/esm/test-utils/mocks.js +10 -0
- package/dist/esm/test-utils/msw-setup.js +22 -0
- package/dist/esm/test-utils/test-defaults.js +198 -0
- package/dist/esm/todoist-api.js +1231 -0
- package/dist/esm/types/entities.js +366 -0
- package/dist/esm/types/errors.js +18 -0
- package/dist/esm/types/http.js +18 -0
- package/dist/esm/types/index.js +3 -0
- package/dist/esm/types/requests.js +1 -0
- package/dist/esm/types/sync.js +1 -0
- package/dist/esm/utils/activity-helpers.js +36 -0
- package/dist/esm/utils/case-conversion.js +61 -0
- package/dist/esm/utils/colors.js +215 -0
- package/dist/esm/utils/fetch-with-retry.js +147 -0
- package/dist/esm/utils/index.js +3 -0
- package/dist/esm/utils/multipart-upload.js +120 -0
- package/dist/esm/utils/processing-helpers.js +12 -0
- package/dist/esm/utils/sanitization.js +112 -0
- package/dist/esm/utils/url-helpers.js +68 -0
- package/dist/esm/utils/validators.js +97 -0
- package/dist/{authentication.d.ts → types/authentication.d.ts} +6 -1
- package/dist/{consts → types/consts}/endpoints.d.ts +11 -0
- package/dist/types/index.d.ts +4 -3
- package/dist/types/rest-client.d.ts +15 -0
- package/dist/types/test-utils/msw-setup.d.ts +3 -0
- package/dist/{TodoistApi.d.ts → types/todoist-api.d.ts} +91 -2
- package/dist/types/{entities.d.ts → types/entities.d.ts} +119 -0
- package/dist/types/types/http.d.ts +68 -0
- package/dist/types/types/index.d.ts +3 -0
- package/dist/types/{requests.d.ts → types/requests.d.ts} +137 -0
- package/dist/types/utils/case-conversion.d.ts +12 -0
- package/dist/types/utils/fetch-with-retry.d.ts +11 -0
- package/dist/types/utils/index.d.ts +3 -0
- package/dist/types/utils/multipart-upload.d.ts +50 -0
- package/dist/{utils → types/utils}/validators.d.ts +7 -1
- package/package.json +24 -8
- package/dist/TodoistApi.js +0 -1209
- package/dist/authentication.js +0 -199
- package/dist/consts/endpoints.js +0 -50
- package/dist/index.d.ts +0 -4
- package/dist/restClient.d.ts +0 -5
- package/dist/restClient.js +0 -170
- package/dist/types/errors.js +0 -39
- package/dist/types/http.d.ts +0 -1
- package/dist/types/http.js +0 -2
- package/dist/utils/index.d.ts +0 -3
- /package/dist/{types → cjs/types}/index.js +0 -0
- /package/dist/{types → cjs/types}/requests.js +0 -0
- /package/dist/{types → cjs/types}/sync.js +0 -0
- /package/dist/{utils → cjs/utils}/activity-helpers.js +0 -0
- /package/dist/{testUtils → types/test-utils}/asserts.d.ts +0 -0
- /package/dist/{testUtils → types/test-utils}/mocks.d.ts +0 -0
- /package/dist/{testUtils/testDefaults.d.ts → types/test-utils/test-defaults.d.ts} +0 -0
- /package/dist/types/{errors.d.ts → types/errors.d.ts} +0 -0
- /package/dist/types/{sync.d.ts → types/sync.d.ts} +0 -0
- /package/dist/{utils → types/utils}/activity-helpers.d.ts +0 -0
- /package/dist/{utils → types/utils}/colors.d.ts +0 -0
- /package/dist/{utils → types/utils}/processing-helpers.d.ts +0 -0
- /package/dist/{utils → types/utils}/sanitization.d.ts +0 -0
- /package/dist/{utils/urlHelpers.d.ts → types/utils/url-helpers.d.ts} +0 -0
|
@@ -1,36 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
3
|
exports.getSanitizedContent = getSanitizedContent;
|
|
15
4
|
exports.getSanitizedTasks = getSanitizedTasks;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
5
|
+
const BOLD_FORMAT = /(^|[\s!?,;>:]+)(?:\*\*|__|!!)(.+?)(\*\*|__|!!)(?=$|[\s!?,;><:]+)/gi;
|
|
6
|
+
const ITALIC_FORMAT = /(^|[\s!?,;>:]+)(?:\*|_|!)(.+?)(\*|_|!)(?=$|[\s!?,;><:]+)/gi;
|
|
7
|
+
const BOLD_ITALIC_FORMAT = /(^|[\s!?,;>:]+)(?:\*\*\*|___|!!!)(.+?)(\*\*\*|___|!!!)(?=$|[\s!?,;><:]+)/gi;
|
|
8
|
+
const CODE_BLOCK_FORMAT = /```([\s\S]*?)```/gi;
|
|
9
|
+
const CODE_INLINE_FORMAT = /`([^`]+)`/gi;
|
|
10
|
+
const TODOIST_LINK = /((?:(?:onenote:)?[\w-]+):\/\/[^\s]+)\s+[[(]([^)]+)[\])]/gi;
|
|
11
|
+
const MARKDOWN_LINK = /\[(.+?)\]\((.+?)\)/gi;
|
|
12
|
+
const GMAIL_LINK = /\[\[gmail=(.+?),\s*(.+?)\]\]/gi;
|
|
13
|
+
const OUTLOOK_LINK = /\[\[outlook=(.+?),\s*(.+?)\]\]/gi;
|
|
14
|
+
const THUNDERBIRD_LINK = /\[\[thunderbird\n(.+)\n(.+)\n\s*\]\]/gi;
|
|
15
|
+
const FAKE_SECTION_PREFIX = '* ';
|
|
16
|
+
const FAKE_SECTION_SUFFIX = ':';
|
|
28
17
|
function removeStyleFormatting(input) {
|
|
29
18
|
if (!input.includes('!') && !input.includes('*') && !input.includes('_')) {
|
|
30
19
|
return input;
|
|
31
20
|
}
|
|
32
21
|
function removeMarkdown(match, prefix, text) {
|
|
33
|
-
return
|
|
22
|
+
return `${prefix}${text}`;
|
|
34
23
|
}
|
|
35
24
|
input = input.replace(BOLD_ITALIC_FORMAT, removeMarkdown);
|
|
36
25
|
input = input.replace(BOLD_FORMAT, removeMarkdown);
|
|
@@ -74,13 +63,13 @@ function removeTodoistLinks(input) {
|
|
|
74
63
|
}
|
|
75
64
|
function removeAppLinks(input) {
|
|
76
65
|
if (input.includes('gmail')) {
|
|
77
|
-
input = input.replace(GMAIL_LINK,
|
|
66
|
+
input = input.replace(GMAIL_LINK, (match, id, text) => text);
|
|
78
67
|
}
|
|
79
68
|
if (input.includes('outlook')) {
|
|
80
|
-
input = input.replace(OUTLOOK_LINK,
|
|
69
|
+
input = input.replace(OUTLOOK_LINK, (match, id, text) => text);
|
|
81
70
|
}
|
|
82
71
|
if (input.includes('thunderbird')) {
|
|
83
|
-
input = input.replace(THUNDERBIRD_LINK,
|
|
72
|
+
input = input.replace(THUNDERBIRD_LINK, (match, text) => text);
|
|
84
73
|
}
|
|
85
74
|
return input;
|
|
86
75
|
}
|
|
@@ -123,5 +112,5 @@ function getSanitizedContent(input) {
|
|
|
123
112
|
* @returns Array of tasks with added sanitizedContent property
|
|
124
113
|
*/
|
|
125
114
|
function getSanitizedTasks(tasks) {
|
|
126
|
-
return tasks.map(
|
|
115
|
+
return tasks.map((task) => (Object.assign(Object.assign({}, task), { sanitizedContent: getSanitizedContent(task.content) })));
|
|
127
116
|
}
|
|
@@ -4,7 +4,7 @@ exports.formatDateToYYYYMMDD = formatDateToYYYYMMDD;
|
|
|
4
4
|
exports.getTaskUrl = getTaskUrl;
|
|
5
5
|
exports.getProjectUrl = getProjectUrl;
|
|
6
6
|
exports.getSectionUrl = getSectionUrl;
|
|
7
|
-
|
|
7
|
+
const endpoints_1 = require("../consts/endpoints");
|
|
8
8
|
/**
|
|
9
9
|
* Formats a Date object to YYYY-MM-DD string format.
|
|
10
10
|
*
|
|
@@ -13,10 +13,10 @@ var endpoints_1 = require("../consts/endpoints");
|
|
|
13
13
|
* @returns The formatted date string in YYYY-MM-DD format.
|
|
14
14
|
*/
|
|
15
15
|
function formatDateToYYYYMMDD(date) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
return
|
|
16
|
+
const year = date.getFullYear();
|
|
17
|
+
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
18
|
+
const day = String(date.getDate()).padStart(2, '0');
|
|
19
|
+
return `${year}-${month}-${day}`;
|
|
20
20
|
}
|
|
21
21
|
/**
|
|
22
22
|
* Generate the URL for a given task.
|
|
@@ -26,9 +26,9 @@ function formatDateToYYYYMMDD(date) {
|
|
|
26
26
|
* @returns The URL string for the task view.
|
|
27
27
|
*/
|
|
28
28
|
function getTaskUrl(taskId, content) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return
|
|
29
|
+
const slug = content ? slugify(content) : undefined;
|
|
30
|
+
const path = slug ? `${slug}-${taskId}` : taskId;
|
|
31
|
+
return `${endpoints_1.TODOIST_WEB_URI}/task/${path}`;
|
|
32
32
|
}
|
|
33
33
|
/**
|
|
34
34
|
* Generate the URL for a given project.
|
|
@@ -38,9 +38,9 @@ function getTaskUrl(taskId, content) {
|
|
|
38
38
|
* @returns The URL string for the project view.
|
|
39
39
|
*/
|
|
40
40
|
function getProjectUrl(projectId, name) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
return
|
|
41
|
+
const slug = name ? slugify(name) : undefined;
|
|
42
|
+
const path = slug ? `${slug}-${projectId}` : projectId;
|
|
43
|
+
return `${endpoints_1.TODOIST_WEB_URI}/project/${path}`;
|
|
44
44
|
}
|
|
45
45
|
/**
|
|
46
46
|
* Generate the URL for a given section.
|
|
@@ -50,9 +50,9 @@ function getProjectUrl(projectId, name) {
|
|
|
50
50
|
* @returns The URL string for the section view.
|
|
51
51
|
*/
|
|
52
52
|
function getSectionUrl(sectionId, name) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return
|
|
53
|
+
const slug = name ? slugify(name) : undefined;
|
|
54
|
+
const path = slug ? `${slug}-${sectionId}` : sectionId;
|
|
55
|
+
return `${endpoints_1.TODOIST_WEB_URI}/section/${path}`;
|
|
56
56
|
}
|
|
57
57
|
/**
|
|
58
58
|
* Slugify function borrowed from Django.
|
|
@@ -62,7 +62,7 @@ function getSectionUrl(sectionId, name) {
|
|
|
62
62
|
*/
|
|
63
63
|
function slugify(value) {
|
|
64
64
|
// Convert to ASCII
|
|
65
|
-
|
|
65
|
+
let result = value.normalize('NFKD').replace(/[\u0300-\u036f]/g, '');
|
|
66
66
|
// Remove non-ASCII characters
|
|
67
67
|
result = result.replace(/[^\x20-\x7E]/g, '');
|
|
68
68
|
// Convert to lowercase and replace non-alphanumeric characters with dashes
|
|
@@ -19,7 +19,13 @@ exports.validateCurrentUser = validateCurrentUser;
|
|
|
19
19
|
exports.validateActivityEvent = validateActivityEvent;
|
|
20
20
|
exports.validateActivityEventArray = validateActivityEventArray;
|
|
21
21
|
exports.validateAttachment = validateAttachment;
|
|
22
|
-
|
|
22
|
+
exports.validateWorkspaceUser = validateWorkspaceUser;
|
|
23
|
+
exports.validateWorkspaceUserArray = validateWorkspaceUserArray;
|
|
24
|
+
exports.validateWorkspaceInvitation = validateWorkspaceInvitation;
|
|
25
|
+
exports.validateWorkspaceInvitationArray = validateWorkspaceInvitationArray;
|
|
26
|
+
exports.validateWorkspacePlanDetails = validateWorkspacePlanDetails;
|
|
27
|
+
exports.validateJoinWorkspaceResult = validateJoinWorkspaceResult;
|
|
28
|
+
const entities_1 = require("../types/entities");
|
|
23
29
|
function validateTask(input) {
|
|
24
30
|
return entities_1.TaskSchema.parse(input);
|
|
25
31
|
}
|
|
@@ -95,3 +101,24 @@ function validateActivityEventArray(input) {
|
|
|
95
101
|
function validateAttachment(input) {
|
|
96
102
|
return entities_1.AttachmentSchema.parse(input);
|
|
97
103
|
}
|
|
104
|
+
function validateWorkspaceUser(input) {
|
|
105
|
+
return entities_1.WorkspaceUserSchema.parse(input);
|
|
106
|
+
}
|
|
107
|
+
function validateWorkspaceUserArray(input) {
|
|
108
|
+
if (!Array.isArray(input)) {
|
|
109
|
+
throw new Error(`Expected array for workspace users, got ${typeof input}`);
|
|
110
|
+
}
|
|
111
|
+
return input.map(validateWorkspaceUser);
|
|
112
|
+
}
|
|
113
|
+
function validateWorkspaceInvitation(input) {
|
|
114
|
+
return entities_1.WorkspaceInvitationSchema.parse(input);
|
|
115
|
+
}
|
|
116
|
+
function validateWorkspaceInvitationArray(input) {
|
|
117
|
+
return input.map(validateWorkspaceInvitation);
|
|
118
|
+
}
|
|
119
|
+
function validateWorkspacePlanDetails(input) {
|
|
120
|
+
return entities_1.WorkspacePlanDetailsSchema.parse(input);
|
|
121
|
+
}
|
|
122
|
+
function validateJoinWorkspaceResult(input) {
|
|
123
|
+
return entities_1.JoinWorkspaceResultSchema.parse(input);
|
|
124
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { request, isSuccess } from './rest-client.js';
|
|
2
|
+
import { v4 as uuid } from 'uuid';
|
|
3
|
+
import { TodoistRequestError } from './types/index.js';
|
|
4
|
+
import { getAuthBaseUri, getSyncBaseUri, ENDPOINT_AUTHORIZATION, ENDPOINT_GET_TOKEN, ENDPOINT_REVOKE_TOKEN, ENDPOINT_REVOKE, } from './consts/endpoints.js';
|
|
5
|
+
/**
|
|
6
|
+
* Creates a Basic Authentication header value from client credentials.
|
|
7
|
+
* @param clientId - The OAuth client ID
|
|
8
|
+
* @param clientSecret - The OAuth client secret
|
|
9
|
+
* @returns The Basic Auth header value (without the 'Basic ' prefix)
|
|
10
|
+
*/
|
|
11
|
+
function createBasicAuthHeader(clientId, clientSecret) {
|
|
12
|
+
const credentials = `${clientId}:${clientSecret}`;
|
|
13
|
+
return Buffer.from(credentials).toString('base64');
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Generates a random state parameter for OAuth2 authorization.
|
|
17
|
+
* The state parameter helps prevent CSRF attacks.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* const state = getAuthStateParameter()
|
|
22
|
+
* // Store state in session
|
|
23
|
+
* const authUrl = getAuthorizationUrl(clientId, ['data:read'], state)
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @returns A random UUID v4 string
|
|
27
|
+
*/
|
|
28
|
+
export function getAuthStateParameter() {
|
|
29
|
+
return uuid();
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Generates the authorization URL for the OAuth2 flow.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* const url = getAuthorizationUrl(
|
|
37
|
+
* 'your-client-id',
|
|
38
|
+
* ['data:read', 'task:add'],
|
|
39
|
+
* state
|
|
40
|
+
* )
|
|
41
|
+
* // Redirect user to url
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @returns The full authorization URL to redirect users to
|
|
45
|
+
* @see https://todoist.com/api/v1/docs#tag/Authorization/OAuth
|
|
46
|
+
*/
|
|
47
|
+
export function getAuthorizationUrl({ clientId, permissions, state, baseUrl, }) {
|
|
48
|
+
if (!(permissions === null || permissions === void 0 ? void 0 : permissions.length)) {
|
|
49
|
+
throw new Error('At least one scope value should be passed for permissions.');
|
|
50
|
+
}
|
|
51
|
+
const scope = permissions.join(',');
|
|
52
|
+
return `${getAuthBaseUri(baseUrl)}${ENDPOINT_AUTHORIZATION}?client_id=${clientId}&scope=${scope}&state=${state}`;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Exchanges an authorization code for an access token.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* const { accessToken } = await getAuthToken({
|
|
60
|
+
* clientId: 'your-client-id',
|
|
61
|
+
* clientSecret: 'your-client-secret',
|
|
62
|
+
* code: authCode
|
|
63
|
+
* })
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* @returns The access token response
|
|
67
|
+
* @throws {@link TodoistRequestError} If the token exchange fails
|
|
68
|
+
*/
|
|
69
|
+
export async function getAuthToken(args, baseUrl) {
|
|
70
|
+
var _a;
|
|
71
|
+
const response = await request({
|
|
72
|
+
httpMethod: 'POST',
|
|
73
|
+
baseUri: getAuthBaseUri(baseUrl),
|
|
74
|
+
relativePath: ENDPOINT_GET_TOKEN,
|
|
75
|
+
apiToken: undefined,
|
|
76
|
+
payload: args,
|
|
77
|
+
});
|
|
78
|
+
if (response.status !== 200 || !((_a = response.data) === null || _a === void 0 ? void 0 : _a.accessToken)) {
|
|
79
|
+
throw new TodoistRequestError('Authentication token exchange failed.', response.status, response.data);
|
|
80
|
+
}
|
|
81
|
+
return response.data;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Revokes an access token, making it invalid for future use.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* await revokeAuthToken({
|
|
89
|
+
* clientId: 'your-client-id',
|
|
90
|
+
* clientSecret: 'your-client-secret',
|
|
91
|
+
* accessToken: token
|
|
92
|
+
* })
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* @deprecated Use {@link revokeToken} instead. This function uses a legacy endpoint that will be removed in a future version. The new function uses the RFC 7009 compliant endpoint.
|
|
96
|
+
* @returns True if revocation was successful
|
|
97
|
+
* @see https://todoist.com/api/v1/docs#tag/Authorization/operation/revoke_access_token_api_api_v1_access_tokens_delete
|
|
98
|
+
*/
|
|
99
|
+
export async function revokeAuthToken(args, baseUrl) {
|
|
100
|
+
const response = await request({
|
|
101
|
+
httpMethod: 'POST',
|
|
102
|
+
baseUri: getSyncBaseUri(baseUrl),
|
|
103
|
+
relativePath: ENDPOINT_REVOKE_TOKEN,
|
|
104
|
+
apiToken: undefined,
|
|
105
|
+
payload: args,
|
|
106
|
+
});
|
|
107
|
+
return isSuccess(response);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Revokes a token using the RFC 7009 OAuth 2.0 Token Revocation standard.
|
|
111
|
+
*
|
|
112
|
+
* This function uses HTTP Basic Authentication with client credentials and follows
|
|
113
|
+
* the RFC 7009 specification for token revocation.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```typescript
|
|
117
|
+
* await revokeToken({
|
|
118
|
+
* clientId: 'your-client-id',
|
|
119
|
+
* clientSecret: 'your-client-secret',
|
|
120
|
+
* token: 'access-token-to-revoke'
|
|
121
|
+
* })
|
|
122
|
+
* ```
|
|
123
|
+
*
|
|
124
|
+
* @returns True if revocation was successful
|
|
125
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7009
|
|
126
|
+
* @see https://todoist.com/api/v1/docs#tag/Authorization
|
|
127
|
+
*/
|
|
128
|
+
export async function revokeToken(args, baseUrl) {
|
|
129
|
+
const { clientId, clientSecret, token } = args;
|
|
130
|
+
// Create Basic Auth header as per RFC 7009
|
|
131
|
+
const basicAuth = createBasicAuthHeader(clientId, clientSecret);
|
|
132
|
+
const customHeaders = {
|
|
133
|
+
Authorization: `Basic ${basicAuth}`,
|
|
134
|
+
};
|
|
135
|
+
// Request body only contains the token and optional token_type_hint
|
|
136
|
+
const requestBody = {
|
|
137
|
+
token,
|
|
138
|
+
token_type_hint: 'access_token',
|
|
139
|
+
};
|
|
140
|
+
const response = await request({
|
|
141
|
+
httpMethod: 'POST',
|
|
142
|
+
baseUri: getSyncBaseUri(baseUrl),
|
|
143
|
+
relativePath: ENDPOINT_REVOKE,
|
|
144
|
+
apiToken: undefined,
|
|
145
|
+
payload: requestBody,
|
|
146
|
+
requestId: undefined,
|
|
147
|
+
hasSyncCommands: false,
|
|
148
|
+
customHeaders: customHeaders,
|
|
149
|
+
});
|
|
150
|
+
return isSuccess(response);
|
|
151
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
const BASE_URI = 'https://api.todoist.com';
|
|
2
|
+
const TODOIST_URI = 'https://todoist.com';
|
|
3
|
+
export const TODOIST_WEB_URI = 'https://app.todoist.com/app';
|
|
4
|
+
// The API version is not configurable, to ensure
|
|
5
|
+
// compatibility between the API and the client.
|
|
6
|
+
export const API_VERSION = 'v1';
|
|
7
|
+
export const API_BASE_URI = `/api/${API_VERSION}/`;
|
|
8
|
+
const API_AUTHORIZATION_BASE_URI = '/oauth/';
|
|
9
|
+
export function getSyncBaseUri(domainBase = BASE_URI) {
|
|
10
|
+
return new URL(API_BASE_URI, domainBase).toString();
|
|
11
|
+
}
|
|
12
|
+
export function getAuthBaseUri(domainBase = TODOIST_URI) {
|
|
13
|
+
return new URL(API_AUTHORIZATION_BASE_URI, domainBase).toString();
|
|
14
|
+
}
|
|
15
|
+
export const ENDPOINT_REST_TASKS = 'tasks';
|
|
16
|
+
export const ENDPOINT_REST_TASKS_FILTER = ENDPOINT_REST_TASKS + '/filter';
|
|
17
|
+
export const ENDPOINT_REST_TASKS_COMPLETED_BY_COMPLETION_DATE = ENDPOINT_REST_TASKS + '/completed/by_completion_date';
|
|
18
|
+
export const ENDPOINT_REST_TASKS_COMPLETED_BY_DUE_DATE = ENDPOINT_REST_TASKS + '/completed/by_due_date';
|
|
19
|
+
export const ENDPOINT_REST_TASKS_COMPLETED_SEARCH = 'completed/search';
|
|
20
|
+
export const ENDPOINT_REST_SECTIONS = 'sections';
|
|
21
|
+
export const ENDPOINT_REST_LABELS = 'labels';
|
|
22
|
+
export const ENDPOINT_REST_LABELS_SHARED = ENDPOINT_REST_LABELS + '/shared';
|
|
23
|
+
export const ENDPOINT_REST_LABELS_SHARED_RENAME = ENDPOINT_REST_LABELS_SHARED + '/rename';
|
|
24
|
+
export const ENDPOINT_REST_LABELS_SHARED_REMOVE = ENDPOINT_REST_LABELS_SHARED + '/remove';
|
|
25
|
+
export const ENDPOINT_REST_COMMENTS = 'comments';
|
|
26
|
+
export const ENDPOINT_REST_TASK_CLOSE = 'close';
|
|
27
|
+
export const ENDPOINT_REST_TASK_REOPEN = 'reopen';
|
|
28
|
+
export const ENDPOINT_REST_TASK_MOVE = 'move';
|
|
29
|
+
export const ENDPOINT_REST_PROJECTS = 'projects';
|
|
30
|
+
export const ENDPOINT_REST_PROJECTS_ARCHIVED = ENDPOINT_REST_PROJECTS + '/archived';
|
|
31
|
+
export const ENDPOINT_REST_PROJECT_COLLABORATORS = 'collaborators';
|
|
32
|
+
export const ENDPOINT_REST_USER = 'user';
|
|
33
|
+
export const ENDPOINT_REST_PRODUCTIVITY = ENDPOINT_REST_TASKS + '/completed/stats';
|
|
34
|
+
export const ENDPOINT_REST_ACTIVITIES = 'activities';
|
|
35
|
+
export const ENDPOINT_REST_UPLOADS = 'uploads';
|
|
36
|
+
export const PROJECT_ARCHIVE = 'archive';
|
|
37
|
+
export const PROJECT_UNARCHIVE = 'unarchive';
|
|
38
|
+
export const ENDPOINT_SYNC_QUICK_ADD = ENDPOINT_REST_TASKS + '/quick';
|
|
39
|
+
export const ENDPOINT_SYNC = 'sync';
|
|
40
|
+
export const ENDPOINT_AUTHORIZATION = 'authorize';
|
|
41
|
+
export const ENDPOINT_GET_TOKEN = 'access_token';
|
|
42
|
+
export const ENDPOINT_REVOKE_TOKEN = 'access_tokens/revoke';
|
|
43
|
+
export const ENDPOINT_REVOKE = 'revoke';
|
|
44
|
+
// Workspace endpoints
|
|
45
|
+
export const ENDPOINT_WORKSPACE_INVITATIONS = 'workspaces/invitations';
|
|
46
|
+
export const ENDPOINT_WORKSPACE_INVITATIONS_ALL = 'workspaces/invitations/all';
|
|
47
|
+
export const ENDPOINT_WORKSPACE_INVITATIONS_DELETE = 'workspaces/invitations/delete';
|
|
48
|
+
export const ENDPOINT_WORKSPACE_JOIN = 'workspaces/join';
|
|
49
|
+
export const ENDPOINT_WORKSPACE_LOGO = 'workspaces/logo';
|
|
50
|
+
export const ENDPOINT_WORKSPACE_PLAN_DETAILS = 'workspaces/plan_details';
|
|
51
|
+
export const ENDPOINT_WORKSPACE_USERS = 'workspaces/users';
|
|
52
|
+
// Workspace invitation actions (require invite_code parameter)
|
|
53
|
+
export function getWorkspaceInvitationAcceptEndpoint(inviteCode) {
|
|
54
|
+
return `workspaces/invitations/${inviteCode}/accept`;
|
|
55
|
+
}
|
|
56
|
+
export function getWorkspaceInvitationRejectEndpoint(inviteCode) {
|
|
57
|
+
return `workspaces/invitations/${inviteCode}/reject`;
|
|
58
|
+
}
|
|
59
|
+
// Workspace projects (require workspace_id parameter)
|
|
60
|
+
export function getWorkspaceActiveProjectsEndpoint(workspaceId) {
|
|
61
|
+
return `workspaces/${workspaceId}/projects/active`;
|
|
62
|
+
}
|
|
63
|
+
export function getWorkspaceArchivedProjectsEndpoint(workspaceId) {
|
|
64
|
+
return `workspaces/${workspaceId}/projects/archived`;
|
|
65
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { TodoistRequestError } from './types/errors.js';
|
|
2
|
+
import { isNetworkError, isHttpError } from './types/http.js';
|
|
3
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
4
|
+
import { API_BASE_URI } from './consts/endpoints.js';
|
|
5
|
+
import { camelCaseKeys, snakeCaseKeys } from './utils/case-conversion.js';
|
|
6
|
+
import { fetchWithRetry } from './utils/fetch-with-retry.js';
|
|
7
|
+
export function paramsSerializer(params) {
|
|
8
|
+
const qs = new URLSearchParams();
|
|
9
|
+
Object.keys(params).forEach((key) => {
|
|
10
|
+
const value = params[key];
|
|
11
|
+
if (value != null) {
|
|
12
|
+
if (Array.isArray(value)) {
|
|
13
|
+
qs.append(key, value.join(','));
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
qs.append(key, String(value));
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
return qs.toString();
|
|
21
|
+
}
|
|
22
|
+
const defaultHeaders = {
|
|
23
|
+
'Content-Type': 'application/json',
|
|
24
|
+
};
|
|
25
|
+
function getAuthHeader(apiKey) {
|
|
26
|
+
return `Bearer ${apiKey}`;
|
|
27
|
+
}
|
|
28
|
+
function getRetryDelay(retryCount) {
|
|
29
|
+
return retryCount === 1 ? 0 : 500;
|
|
30
|
+
}
|
|
31
|
+
function getTodoistRequestError(args) {
|
|
32
|
+
const { error, originalStack } = args;
|
|
33
|
+
const requestError = new TodoistRequestError(error.message);
|
|
34
|
+
requestError.stack = originalStack ? originalStack.stack : error.stack;
|
|
35
|
+
if (isHttpError(error)) {
|
|
36
|
+
requestError.httpStatusCode = error.status;
|
|
37
|
+
requestError.responseData = error.data;
|
|
38
|
+
}
|
|
39
|
+
return requestError;
|
|
40
|
+
}
|
|
41
|
+
function getRequestConfiguration(args) {
|
|
42
|
+
const { baseURL, apiToken, requestId, customHeaders } = args;
|
|
43
|
+
const authHeader = apiToken ? { Authorization: getAuthHeader(apiToken) } : undefined;
|
|
44
|
+
const requestIdHeader = requestId ? { 'X-Request-Id': requestId } : undefined;
|
|
45
|
+
const headers = Object.assign(Object.assign(Object.assign(Object.assign({}, defaultHeaders), authHeader), requestIdHeader), customHeaders);
|
|
46
|
+
return { baseURL, headers };
|
|
47
|
+
}
|
|
48
|
+
function getHttpClientConfig(args) {
|
|
49
|
+
const { baseURL, apiToken, requestId, customHeaders } = args;
|
|
50
|
+
const configuration = getRequestConfiguration({ baseURL, apiToken, requestId, customHeaders });
|
|
51
|
+
return Object.assign(Object.assign({}, configuration), { timeout: 30000, retry: {
|
|
52
|
+
retries: 3,
|
|
53
|
+
retryCondition: isNetworkError,
|
|
54
|
+
retryDelay: getRetryDelay,
|
|
55
|
+
} });
|
|
56
|
+
}
|
|
57
|
+
export function isSuccess(response) {
|
|
58
|
+
return response.status >= 200 && response.status < 300;
|
|
59
|
+
}
|
|
60
|
+
export async function request(args) {
|
|
61
|
+
const { httpMethod, baseUri, relativePath, apiToken, payload, requestId: initialRequestId, hasSyncCommands, customHeaders, } = args;
|
|
62
|
+
// Capture original stack for better error reporting
|
|
63
|
+
const originalStack = new Error();
|
|
64
|
+
try {
|
|
65
|
+
let requestId = initialRequestId;
|
|
66
|
+
// Sync api don't allow a request id in the CORS
|
|
67
|
+
if (httpMethod === 'POST' && !requestId && !baseUri.includes(API_BASE_URI)) {
|
|
68
|
+
requestId = uuidv4();
|
|
69
|
+
}
|
|
70
|
+
const config = getHttpClientConfig({ baseURL: baseUri, apiToken, requestId, customHeaders });
|
|
71
|
+
const url = `${baseUri}${relativePath}`;
|
|
72
|
+
const fetchOptions = {
|
|
73
|
+
method: httpMethod,
|
|
74
|
+
headers: config.headers,
|
|
75
|
+
timeout: config.timeout,
|
|
76
|
+
};
|
|
77
|
+
let finalUrl = url;
|
|
78
|
+
switch (httpMethod) {
|
|
79
|
+
case 'GET':
|
|
80
|
+
// For GET requests, add query parameters to URL
|
|
81
|
+
if (payload) {
|
|
82
|
+
const queryString = paramsSerializer(payload);
|
|
83
|
+
if (queryString) {
|
|
84
|
+
const separator = url.includes('?') ? '&' : '?';
|
|
85
|
+
finalUrl = `${url}${separator}${queryString}`;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
break;
|
|
89
|
+
case 'POST':
|
|
90
|
+
case 'PUT': {
|
|
91
|
+
// Convert payload from camelCase to snake_case
|
|
92
|
+
const convertedPayload = payload ? snakeCaseKeys(payload) : payload;
|
|
93
|
+
const body = hasSyncCommands
|
|
94
|
+
? JSON.stringify(convertedPayload)
|
|
95
|
+
: JSON.stringify(convertedPayload);
|
|
96
|
+
fetchOptions.body = body;
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
case 'DELETE':
|
|
100
|
+
// DELETE requests don't have a body
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
// Make the request
|
|
104
|
+
const response = await fetchWithRetry({
|
|
105
|
+
url: finalUrl,
|
|
106
|
+
options: fetchOptions,
|
|
107
|
+
retryConfig: config.retry,
|
|
108
|
+
});
|
|
109
|
+
// Convert snake_case response to camelCase
|
|
110
|
+
const convertedData = camelCaseKeys(response.data);
|
|
111
|
+
return Object.assign(Object.assign({}, response), { data: convertedData });
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
if (!(error instanceof Error)) {
|
|
115
|
+
throw new Error('An unknown error occurred during the request');
|
|
116
|
+
}
|
|
117
|
+
throw getTodoistRequestError({ error, originalStack });
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Has to use 'any' to express constructor type
|
|
2
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3
|
+
export function assertInstance(value, type) {
|
|
4
|
+
if (value instanceof type) {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
throw new TypeError(`Unexpected type ${typeof value}`);
|
|
8
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import * as restClient from '../rest-client.js';
|
|
2
|
+
export function setupRestClientMock(responseData, status = 200) {
|
|
3
|
+
const response = {
|
|
4
|
+
status,
|
|
5
|
+
statusText: status === 200 ? 'OK' : 'Error',
|
|
6
|
+
headers: {},
|
|
7
|
+
data: responseData,
|
|
8
|
+
};
|
|
9
|
+
return jest.spyOn(restClient, 'request').mockResolvedValue(response);
|
|
10
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { setupServer } from 'msw/node';
|
|
2
|
+
// Default handlers for common API responses
|
|
3
|
+
export const handlers = [
|
|
4
|
+
// Default handlers can be added here for common endpoints
|
|
5
|
+
// Individual test files will add their own specific handlers
|
|
6
|
+
];
|
|
7
|
+
// Create MSW server instance
|
|
8
|
+
export const server = setupServer(...handlers);
|
|
9
|
+
// Setup MSW for tests
|
|
10
|
+
beforeAll(() => {
|
|
11
|
+
server.listen({
|
|
12
|
+
onUnhandledRequest: 'warn', // Log warnings for unhandled requests during development
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
server.resetHandlers(); // Reset handlers between tests
|
|
17
|
+
});
|
|
18
|
+
afterAll(() => {
|
|
19
|
+
server.close(); // Clean up after all tests
|
|
20
|
+
});
|
|
21
|
+
// Export MSW utilities for use in tests
|
|
22
|
+
export { http, HttpResponse } from 'msw';
|