@doist/todoist-api-typescript 4.0.0-alpha.2 → 4.0.0-alpha.4

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.
@@ -1,19 +1,93 @@
1
+ /**
2
+ * Permission scopes that can be requested during OAuth2 authorization.
3
+ * @see {@link https://developer.todoist.com/guides/#step-1-authorization-request}
4
+ */
1
5
  export type Permission = 'task:add' | 'data:read' | 'data:read_write' | 'data:delete' | 'project:delete';
2
- export type AuthTokenResponse = {
3
- accessToken: string;
4
- tokenType: string;
5
- };
6
+ /**
7
+ * Parameters required to exchange an authorization code for an access token.
8
+ * @see https://developer.todoist.com/guides/#step-3-token-exchange
9
+ */
6
10
  export type AuthTokenRequestArgs = {
7
11
  clientId: string;
8
12
  clientSecret: string;
9
13
  code: string;
10
14
  };
15
+ /**
16
+ * Response from a successful OAuth2 token exchange.
17
+ * @see https://developer.todoist.com/guides/#step-3-token-exchange
18
+ */
19
+ export type AuthTokenResponse = {
20
+ accessToken: string;
21
+ tokenType: string;
22
+ };
23
+ /**
24
+ * Parameters required to revoke an access token.
25
+ * @see https://developer.todoist.com/rest/v2/#authorization
26
+ */
11
27
  export type RevokeAuthTokenRequestArgs = {
12
28
  clientId: string;
13
29
  clientSecret: string;
14
30
  accessToken: string;
15
31
  };
32
+ /**
33
+ * Generates a random state parameter for OAuth2 authorization.
34
+ * The state parameter helps prevent CSRF attacks.
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * const state = getAuthStateParameter()
39
+ * // Store state in session
40
+ * const authUrl = getAuthorizationUrl(clientId, ['data:read'], state)
41
+ * ```
42
+ *
43
+ * @returns A random UUID v4 string
44
+ */
16
45
  export declare function getAuthStateParameter(): string;
46
+ /**
47
+ * Generates the authorization URL for the OAuth2 flow.
48
+ *
49
+ * @example
50
+ * ```typescript
51
+ * const url = getAuthorizationUrl(
52
+ * 'your-client-id',
53
+ * ['data:read', 'task:add'],
54
+ * state
55
+ * )
56
+ * // Redirect user to url
57
+ * ```
58
+ *
59
+ * @returns The full authorization URL to redirect users to
60
+ * @see https://developer.todoist.com/guides/#step-1-authorization-request
61
+ */
17
62
  export declare function getAuthorizationUrl(clientId: string, permissions: Permission[], state: string, baseUrl?: string): string;
63
+ /**
64
+ * Exchanges an authorization code for an access token.
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * const { accessToken } = await getAuthToken({
69
+ * clientId: 'your-client-id',
70
+ * clientSecret: 'your-client-secret',
71
+ * code: authCode
72
+ * })
73
+ * ```
74
+ *
75
+ * @returns The access token response
76
+ * @throws {@link TodoistRequestError} If the token exchange fails
77
+ */
18
78
  export declare function getAuthToken(args: AuthTokenRequestArgs, baseUrl?: string): Promise<AuthTokenResponse>;
79
+ /**
80
+ * Revokes an access token, making it invalid for future use.
81
+ *
82
+ * @example
83
+ * ```typescript
84
+ * await revokeAuthToken({
85
+ * clientId: 'your-client-id',
86
+ * clientSecret: 'your-client-secret',
87
+ * accessToken: token
88
+ * })
89
+ * ```
90
+ *
91
+ * @returns True if revocation was successful
92
+ */
19
93
  export declare function revokeAuthToken(args: RevokeAuthTokenRequestArgs, baseUrl?: string): Promise<boolean>;
@@ -41,10 +41,39 @@ var restClient_1 = require("./restClient");
41
41
  var uuid_1 = require("uuid");
42
42
  var types_1 = require("./types");
43
43
  var endpoints_1 = require("./consts/endpoints");
44
+ /**
45
+ * Generates a random state parameter for OAuth2 authorization.
46
+ * The state parameter helps prevent CSRF attacks.
47
+ *
48
+ * @example
49
+ * ```typescript
50
+ * const state = getAuthStateParameter()
51
+ * // Store state in session
52
+ * const authUrl = getAuthorizationUrl(clientId, ['data:read'], state)
53
+ * ```
54
+ *
55
+ * @returns A random UUID v4 string
56
+ */
44
57
  function getAuthStateParameter() {
45
58
  return (0, uuid_1.v4)();
46
59
  }
47
60
  exports.getAuthStateParameter = getAuthStateParameter;
61
+ /**
62
+ * Generates the authorization URL for the OAuth2 flow.
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * const url = getAuthorizationUrl(
67
+ * 'your-client-id',
68
+ * ['data:read', 'task:add'],
69
+ * state
70
+ * )
71
+ * // Redirect user to url
72
+ * ```
73
+ *
74
+ * @returns The full authorization URL to redirect users to
75
+ * @see https://developer.todoist.com/guides/#step-1-authorization-request
76
+ */
48
77
  function getAuthorizationUrl(clientId, permissions, state, baseUrl) {
49
78
  if (!(permissions === null || permissions === void 0 ? void 0 : permissions.length)) {
50
79
  throw new Error('At least one scope value should be passed for permissions.');
@@ -53,6 +82,21 @@ function getAuthorizationUrl(clientId, permissions, state, baseUrl) {
53
82
  return "".concat((0, endpoints_1.getAuthBaseUri)(baseUrl)).concat(endpoints_1.ENDPOINT_AUTHORIZATION, "?client_id=").concat(clientId, "&scope=").concat(scope, "&state=").concat(state);
54
83
  }
55
84
  exports.getAuthorizationUrl = getAuthorizationUrl;
85
+ /**
86
+ * Exchanges an authorization code for an access token.
87
+ *
88
+ * @example
89
+ * ```typescript
90
+ * const { accessToken } = await getAuthToken({
91
+ * clientId: 'your-client-id',
92
+ * clientSecret: 'your-client-secret',
93
+ * code: authCode
94
+ * })
95
+ * ```
96
+ *
97
+ * @returns The access token response
98
+ * @throws {@link TodoistRequestError} If the token exchange fails
99
+ */
56
100
  function getAuthToken(args, baseUrl) {
57
101
  var _a;
58
102
  return __awaiter(this, void 0, void 0, function () {
@@ -71,6 +115,20 @@ function getAuthToken(args, baseUrl) {
71
115
  });
72
116
  }
73
117
  exports.getAuthToken = getAuthToken;
118
+ /**
119
+ * Revokes an access token, making it invalid for future use.
120
+ *
121
+ * @example
122
+ * ```typescript
123
+ * await revokeAuthToken({
124
+ * clientId: 'your-client-id',
125
+ * clientSecret: 'your-client-secret',
126
+ * accessToken: token
127
+ * })
128
+ * ```
129
+ *
130
+ * @returns True if revocation was successful
131
+ */
74
132
  function revokeAuthToken(args, baseUrl) {
75
133
  return __awaiter(this, void 0, void 0, function () {
76
134
  var response;
@@ -1,5 +1,5 @@
1
1
  export declare const API_VERSION = "v9.208";
2
- export declare const API_BASE_URI: string;
2
+ export declare const API_BASE_URI = "/api/v9.208/";
3
3
  export declare function getSyncBaseUri(domainBase?: string): string;
4
4
  export declare function getAuthBaseUri(domainBase?: string): string;
5
5
  export declare const ENDPOINT_REST_TASKS = "tasks";
@@ -13,7 +13,8 @@ export declare const ENDPOINT_REST_COMMENTS = "comments";
13
13
  export declare const ENDPOINT_REST_TASK_CLOSE = "close";
14
14
  export declare const ENDPOINT_REST_TASK_REOPEN = "reopen";
15
15
  export declare const ENDPOINT_REST_PROJECT_COLLABORATORS = "collaborators";
16
- export declare const ENDPOINT_SYNC_QUICK_ADD = "quick";
16
+ export declare const ENDPOINT_SYNC_QUICK_ADD: string;
17
+ export declare const ENDPOINT_SYNC = "sync";
17
18
  export declare const ENDPOINT_AUTHORIZATION = "authorize";
18
19
  export declare const ENDPOINT_GET_TOKEN = "access_token";
19
20
  export declare const ENDPOINT_REVOKE_TOKEN = "access_tokens/revoke";
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ENDPOINT_REVOKE_TOKEN = exports.ENDPOINT_GET_TOKEN = exports.ENDPOINT_AUTHORIZATION = exports.ENDPOINT_SYNC_QUICK_ADD = exports.ENDPOINT_REST_PROJECT_COLLABORATORS = exports.ENDPOINT_REST_TASK_REOPEN = exports.ENDPOINT_REST_TASK_CLOSE = exports.ENDPOINT_REST_COMMENTS = exports.ENDPOINT_REST_LABELS_SHARED_REMOVE = exports.ENDPOINT_REST_LABELS_SHARED_RENAME = exports.ENDPOINT_REST_LABELS_SHARED = exports.ENDPOINT_REST_LABELS = exports.ENDPOINT_REST_SECTIONS = exports.ENDPOINT_REST_PROJECTS = exports.ENDPOINT_REST_TASKS = exports.getAuthBaseUri = exports.getSyncBaseUri = exports.API_BASE_URI = exports.API_VERSION = void 0;
3
+ exports.ENDPOINT_REVOKE_TOKEN = exports.ENDPOINT_GET_TOKEN = exports.ENDPOINT_AUTHORIZATION = exports.ENDPOINT_SYNC = exports.ENDPOINT_SYNC_QUICK_ADD = exports.ENDPOINT_REST_PROJECT_COLLABORATORS = exports.ENDPOINT_REST_TASK_REOPEN = exports.ENDPOINT_REST_TASK_CLOSE = exports.ENDPOINT_REST_COMMENTS = exports.ENDPOINT_REST_LABELS_SHARED_REMOVE = exports.ENDPOINT_REST_LABELS_SHARED_RENAME = exports.ENDPOINT_REST_LABELS_SHARED = exports.ENDPOINT_REST_LABELS = exports.ENDPOINT_REST_SECTIONS = exports.ENDPOINT_REST_PROJECTS = exports.ENDPOINT_REST_TASKS = exports.getAuthBaseUri = exports.getSyncBaseUri = exports.API_BASE_URI = exports.API_VERSION = void 0;
4
4
  var BASE_URI = 'https://api.todoist.com';
5
5
  var TODOIST_URI = 'https://todoist.com';
6
6
  // The API version is not configurable, to ensure
@@ -29,7 +29,8 @@ exports.ENDPOINT_REST_COMMENTS = 'comments';
29
29
  exports.ENDPOINT_REST_TASK_CLOSE = 'close';
30
30
  exports.ENDPOINT_REST_TASK_REOPEN = 'reopen';
31
31
  exports.ENDPOINT_REST_PROJECT_COLLABORATORS = 'collaborators';
32
- exports.ENDPOINT_SYNC_QUICK_ADD = 'quick';
32
+ exports.ENDPOINT_SYNC_QUICK_ADD = exports.ENDPOINT_REST_TASKS + '/quick';
33
+ exports.ENDPOINT_SYNC = 'sync';
33
34
  exports.ENDPOINT_AUTHORIZATION = 'authorize';
34
35
  exports.ENDPOINT_GET_TOKEN = 'access_token';
35
36
  exports.ENDPOINT_REVOKE_TOKEN = 'access_tokens/revoke';
package/dist/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export * from './TodoistApi';
2
- export * from './restClient';
3
2
  export * from './authentication';
4
3
  export * from './types';
5
4
  export * from './utils';
package/dist/index.js CHANGED
@@ -15,7 +15,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./TodoistApi"), exports);
18
- __exportStar(require("./restClient"), exports);
19
18
  __exportStar(require("./authentication"), exports);
20
19
  __exportStar(require("./types"), exports);
21
20
  __exportStar(require("./utils"), exports);
@@ -2,4 +2,4 @@ import { AxiosResponse } from 'axios';
2
2
  import { HttpMethod } from './types/http';
3
3
  export declare function paramsSerializer(params: Record<string, unknown>): string;
4
4
  export declare function isSuccess(response: AxiosResponse): boolean;
5
- export declare function request<T>(httpMethod: HttpMethod, baseUri: string, relativePath: string, apiToken?: string, payload?: Record<string, unknown>, requestId?: string): Promise<AxiosResponse<T>>;
5
+ export declare function request<T>(httpMethod: HttpMethod, baseUri: string, relativePath: string, apiToken?: string, payload?: Record<string, unknown>, requestId?: string, hasSyncCommands?: boolean): Promise<AxiosResponse<T>>;
@@ -116,7 +116,7 @@ function isSuccess(response) {
116
116
  return response.status >= 200 && response.status < 300;
117
117
  }
118
118
  exports.isSuccess = isSuccess;
119
- function request(httpMethod, baseUri, relativePath, apiToken, payload, requestId) {
119
+ function request(httpMethod, baseUri, relativePath, apiToken, payload, requestId, hasSyncCommands) {
120
120
  return __awaiter(this, void 0, void 0, function () {
121
121
  var originalStack, axiosClient, _a, error_1;
122
122
  return __generator(this, function (_b) {
@@ -145,7 +145,7 @@ function request(httpMethod, baseUri, relativePath, apiToken, payload, requestId
145
145
  },
146
146
  })];
147
147
  case 3: return [2 /*return*/, _b.sent()];
148
- case 4: return [4 /*yield*/, axiosClient.post(relativePath, payload)];
148
+ case 4: return [4 /*yield*/, axiosClient.post(relativePath, hasSyncCommands ? JSON.stringify(payload) : payload)];
149
149
  case 5: return [2 /*return*/, _b.sent()];
150
150
  case 6: return [4 /*yield*/, axiosClient.delete(relativePath)];
151
151
  case 7: return [2 /*return*/, _b.sent()];
@@ -1,2 +1 @@
1
1
  export declare function assertInstance<T extends new (...args: any) => any>(value: unknown, type: T): asserts value is InstanceType<T>;
2
- export declare function assertInputValidationError(apiCall: () => Promise<unknown>): Promise<void>;
@@ -1,44 +1,6 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
2
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.assertInputValidationError = exports.assertInstance = void 0;
40
- var mocks_1 = require("./mocks");
41
- var runtypes_1 = require("runtypes");
3
+ exports.assertInstance = void 0;
42
4
  // Has to use 'any' to express constructor type
43
5
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
44
6
  function assertInstance(value, type) {
@@ -48,29 +10,3 @@ function assertInstance(value, type) {
48
10
  throw new TypeError("Unexpected type ".concat(typeof value));
49
11
  }
50
12
  exports.assertInstance = assertInstance;
51
- function assertInputValidationError(apiCall) {
52
- return __awaiter(this, void 0, void 0, function () {
53
- var requestMock, e_1;
54
- return __generator(this, function (_a) {
55
- switch (_a.label) {
56
- case 0:
57
- requestMock = (0, mocks_1.setupRestClientMock)(undefined);
58
- expect.assertions(1);
59
- _a.label = 1;
60
- case 1:
61
- _a.trys.push([1, 3, , 4]);
62
- return [4 /*yield*/, apiCall()];
63
- case 2:
64
- _a.sent();
65
- return [3 /*break*/, 4];
66
- case 3:
67
- e_1 = _a.sent();
68
- assertInstance(e_1, runtypes_1.ValidationError);
69
- expect(requestMock).not.toBeCalled();
70
- return [3 /*break*/, 4];
71
- case 4: return [2 /*return*/];
72
- }
73
- });
74
- });
75
- }
76
- exports.assertInputValidationError = assertInputValidationError;
@@ -82,6 +82,7 @@ export declare const DEFAULT_ATTACHMENT: Attachment;
82
82
  export declare const INVALID_ATTACHMENT: {
83
83
  uploadState: string;
84
84
  resourceType: string;
85
+ url?: string | null | undefined;
85
86
  fileName?: string | null | undefined;
86
87
  fileSize?: number | null | undefined;
87
88
  fileType?: string | null | undefined;
@@ -90,7 +91,6 @@ export declare const INVALID_ATTACHMENT: {
90
91
  image?: string | null | undefined;
91
92
  imageWidth?: number | null | undefined;
92
93
  imageHeight?: number | null | undefined;
93
- url?: string | null | undefined;
94
94
  title?: string | null | undefined;
95
95
  };
96
96
  export declare const DEFAULT_COMMENT: Comment;
@@ -98,6 +98,7 @@ export declare const INVALID_COMMENT: {
98
98
  attachment: {
99
99
  uploadState: string;
100
100
  resourceType: string;
101
+ url?: string | null | undefined;
101
102
  fileName?: string | null | undefined;
102
103
  fileSize?: number | null | undefined;
103
104
  fileType?: string | null | undefined;
@@ -106,7 +107,6 @@ export declare const INVALID_COMMENT: {
106
107
  image?: string | null | undefined;
107
108
  imageWidth?: number | null | undefined;
108
109
  imageHeight?: number | null | undefined;
109
- url?: string | null | undefined;
110
110
  title?: string | null | undefined;
111
111
  };
112
112
  id: string;