n8n/packages/nodes-base/nodes/Todoist/GenericFunctions.ts
agobrech 049e4544d9
feat(Todoist Node): Make it possible to move tasks between sections (#3074)
* refactor todoist implementation and break code into separate classes

* add move item between sections functionality

* add todoist sync integration

* rebase with master

* Fixed get task returning only true

* Fix empty response bug

* Fixed bug which prevented sections from being loaded

* Removed crendentials from node and injected directly in credentials file

* Remove console.logs

* Changed parameter name for coherence

* Fixed error

* 🐛 Fix merge issues

*  Improvements

* 🔥 Remove unnecessary code

* 👕 Fix linting issue

* 👕 Fix linting issue

* 🐛 Fix ts issue

* 👕 Fix linting issue

Co-authored-by: Rahim Rahimli <ragim95@gmail.com>
Co-authored-by: Ricardo Espinoza <ricardo@n8n.io>
Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
2022-06-20 16:42:08 -07:00

81 lines
2 KiB
TypeScript

import {
OptionsWithUri,
} from 'request';
import {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import {
IDataObject, NodeApiError,
} from 'n8n-workflow';
export type Context = IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions;
export function FormatDueDatetime(isoString: string): string {
// 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
return isoString.replace(new RegExp('.000Z$'), 'Z');
}
export async function todoistApiRequest(
this: Context,
method: string,
resource: string,
body: any = {}, // tslint:disable-line:no-any
qs: IDataObject = {},
): Promise<any> { // tslint:disable-line:no-any
const authentication = this.getNodeParameter('authentication', 0) as string;
const endpoint = 'api.todoist.com/rest/v1';
const options: OptionsWithUri = {
method,
qs,
uri: `https://${endpoint}${resource}`,
json: true,
};
if (Object.keys(body).length !== 0) {
options.body = body;
}
try {
const credentialType = authentication === 'apiKey' ? 'todoistApi' : 'todoistOAuth2Api';
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
} catch (error) {
throw new NodeApiError(this.getNode(), (error));
}
}
export async function todoistSyncRequest(
this: Context,
body: any = {}, // tslint:disable-line:no-any
qs: IDataObject = {},
): Promise<any> { // tslint:disable-line:no-any
const authentication = this.getNodeParameter('authentication', 0, 'oAuth2');
const options: OptionsWithUri = {
headers: {},
method: 'POST',
qs,
uri: `https://api.todoist.com/sync/v8/sync`,
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) {
throw new NodeApiError(this.getNode(), (error));
}
}