2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2019-11-05 07:17:06 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions, IHookFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
2019-11-05 07:17:06 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IDataObject, NodeApiError } from 'n8n-workflow';
|
2020-08-17 13:41:05 -07:00
|
|
|
|
2022-06-20 16:42:08 -07:00
|
|
|
export type Context = IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions;
|
|
|
|
|
2021-12-19 05:38:51 -08:00
|
|
|
export function FormatDueDatetime(isoString: string): string {
|
2021-12-19 03:41:19 -08:00
|
|
|
// Assuming that the problem with incorrect date format was caused by milliseconds
|
|
|
|
// Replacing the last 5 characters of ISO-formatted string with just Z char
|
2021-12-19 05:38:51 -08:00
|
|
|
return isoString.replace(new RegExp('.000Z$'), 'Z');
|
2021-12-19 03:41:19 -08:00
|
|
|
}
|
|
|
|
|
2020-08-17 13:41:05 -07:00
|
|
|
export async function todoistApiRequest(
|
2022-06-20 16:42:08 -07:00
|
|
|
this: Context,
|
2020-08-17 13:41:05 -07:00
|
|
|
method: string,
|
2020-08-26 00:09:07 -07:00
|
|
|
resource: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
|
|
|
body: any = {},
|
2020-10-22 09:00:28 -07:00
|
|
|
qs: IDataObject = {},
|
2022-08-17 08:50:24 -07:00
|
|
|
): Promise<any> {
|
2022-05-27 09:15:05 -07:00
|
|
|
const authentication = this.getNodeParameter('authentication', 0) as string;
|
2019-11-05 07:17:06 -08:00
|
|
|
|
2022-11-29 03:37:37 -08:00
|
|
|
const endpoint = 'api.todoist.com/rest/v2';
|
2019-11-05 07:17:06 -08:00
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
method,
|
2020-08-26 00:09:07 -07:00
|
|
|
qs,
|
2019-11-05 12:56:10 -08:00
|
|
|
uri: `https://${endpoint}${resource}`,
|
2020-08-26 00:09:07 -07:00
|
|
|
json: true,
|
2019-11-05 07:17:06 -08:00
|
|
|
};
|
|
|
|
|
2019-11-06 23:40:12 -08:00
|
|
|
if (Object.keys(body).length !== 0) {
|
|
|
|
options.body = body;
|
2019-11-05 12:56:10 -08:00
|
|
|
}
|
2019-11-05 07:17:06 -08:00
|
|
|
|
|
|
|
try {
|
2022-05-27 09:15:05 -07:00
|
|
|
const credentialType = authentication === 'apiKey' ? 'todoistApi' : 'todoistOAuth2Api';
|
|
|
|
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
|
2019-11-05 07:17:06 -08:00
|
|
|
} catch (error) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2022-06-20 16:42:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function todoistSyncRequest(
|
|
|
|
this: Context,
|
2022-12-02 06:25:21 -08:00
|
|
|
body: any = {},
|
2022-06-20 16:42:08 -07:00
|
|
|
qs: IDataObject = {},
|
2022-08-17 08:50:24 -07:00
|
|
|
): Promise<any> {
|
2022-06-20 16:42:08 -07:00
|
|
|
const authentication = this.getNodeParameter('authentication', 0, 'oAuth2');
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {},
|
|
|
|
method: 'POST',
|
|
|
|
qs,
|
2022-11-29 03:37:37 -08:00
|
|
|
uri: `https://api.todoist.com/sync/v9/sync`,
|
2022-06-20 16:42:08 -07:00
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (Object.keys(body).length !== 0) {
|
|
|
|
options.body = body;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const credentialType = authentication === 'oAuth2' ? 'todoistOAuth2Api' : 'todoistApi';
|
|
|
|
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
|
|
|
|
} catch (error) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2019-11-05 07:17:06 -08:00
|
|
|
}
|
|
|
|
}
|