Todoist node enhancement (#823)

* Todoist node enhancement

* done

*  Improvements

* 💄 Remove comment

* 💄 remove unnecessary line

* 📚 Add breaking change message

*  Remove unnecessary line

Co-authored-by: lukigarazus <ywnwa96@gmail.com>
This commit is contained in:
Ricardo Espinoza
2020-08-26 03:09:07 -04:00
committed by GitHub
parent cc32418c4f
commit c3277df25b
6 changed files with 395 additions and 177 deletions

View File

@@ -1,66 +1,37 @@
import { OptionsWithUri } from 'request';
import {
OptionsWithUri,
} from 'request';
import {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
IExecuteSingleFunctions
} from 'n8n-core';
import * as _ from 'lodash';
export const filterAndExecuteForEachTask = async function(
this: IExecuteSingleFunctions,
taskCallback: (t: any) => any
) {
const expression = this.getNodeParameter('expression') as string;
const projectId = this.getNodeParameter('project') as number;
// Enable regular expressions
const reg = new RegExp(expression);
const tasks = await todoistApiRequest.call(this, '/tasks', 'GET');
const filteredTasks = tasks.filter(
// Make sure that project will match no matter what the type is. If project was not selected match all projects
(el: any) => (!projectId || el.project_id) && el.content.match(reg)
);
return {
affectedTasks: (
await Promise.all(filteredTasks.map((t: any) => taskCallback(t)))
)
// This makes it more clear and informative. We pass the ID as a convention and content to give the user confirmation that his/her expression works as expected
.map(
(el, i) =>
el || { id: filteredTasks[i].id, content: filteredTasks[i].content }
)
};
};
import {
IDataObject,
} from 'n8n-workflow';
export async function todoistApiRequest(
this:
| IHookFunctions
| IExecuteFunctions
| IExecuteSingleFunctions
| ILoadOptionsFunctions,
resource: string,
method: string,
resource: string,
body: any = {},
headers?: object
): Promise<any> {
// tslint:disable-line:no-any
const credentials = this.getCredentials('todoistApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const headerWithAuthentication = Object.assign({}, headers, { Authorization: `Bearer ${credentials.apiKey}` });
qs: IDataObject = {},
): Promise<any> { // tslint:disable-line:no-any
const authentication = this.getNodeParameter('authentication', 0, 'apiKey');
const endpoint = 'api.todoist.com/rest/v1';
const options: OptionsWithUri = {
headers: headerWithAuthentication,
headers: {},
method,
qs,
uri: `https://${endpoint}${resource}`,
json: true
json: true,
};
if (Object.keys(body).length !== 0) {
@@ -68,13 +39,25 @@ export async function todoistApiRequest(
}
try {
return this.helpers.request!(options);
if (authentication === 'apiKey') {
const credentials = this.getCredentials('todoistApi') as IDataObject;
//@ts-ignore
options.headers['Authorization'] = `Bearer ${credentials.apiKey}`;
return this.helpers.request!(options);
} else {
//@ts-ignore
return await this.helpers.requestOAuth2.call(this, 'todoistOAuth2Api', options);
}
} catch (error) {
const errorMessage = error.response.body.message || error.response.body.Message;
const errorMessage = error.response.body;
if (errorMessage !== undefined) {
throw errorMessage;
throw new Error(errorMessage);
}
throw error.response.body;
throw errorMessage;
}
}