Improvements to Google Tasks node

This commit is contained in:
ricardo 2020-06-17 17:15:54 -04:00
parent 8116b5f970
commit d261eb5a12
4 changed files with 77 additions and 87 deletions

View file

@ -1,6 +1,11 @@
import { ICredentialType, NodePropertyTypes } from 'n8n-workflow';
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
const scopes = ['https://www.googleapis.com/auth/tasks'];
const scopes = [
'https://www.googleapis.com/auth/tasks',
];
export class GoogleTasksOAuth2Api implements ICredentialType {
name = 'googleTasksOAuth2Api';
@ -12,6 +17,6 @@ export class GoogleTasksOAuth2Api implements ICredentialType {
name: 'scope',
type: 'hidden' as NodePropertyTypes,
default: scopes.join(' ')
}
},
];
}

View file

@ -1,12 +1,16 @@
import { OptionsWithUri } from 'request';
import {
OptionsWithUri,
} from 'request';
import {
IExecuteFunctions,
IExecuteSingleFunctions,
ILoadOptionsFunctions
ILoadOptionsFunctions,
} from 'n8n-core';
import { IDataObject } from 'n8n-workflow';
import {
IDataObject,
} from 'n8n-workflow';
export async function googleApiRequest(
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
@ -27,6 +31,7 @@ export async function googleApiRequest(
uri: uri || `https://www.googleapis.com${resource}`,
json: true
};
try {
if (Object.keys(headers).length !== 0) {
options.headers = Object.assign({}, options.headers, headers);
@ -41,10 +46,14 @@ export async function googleApiRequest(
options
);
} catch (error) {
if (error.response && error.response.body && error.response.body.message) {
if (error.response && error.response.body && error.response.body.error) {
let errors = error.response.body.error.errors;
errors = errors.map((e: IDataObject) => e.message);
// Try to return the error prettier
throw new Error(
`Google Tasks error response [${error.statusCode}]: ${error.response.body.message}`
`Google Tasks error response [${error.statusCode}]: ${errors.join('|')}`
);
}
throw error;

View file

@ -1,16 +1,25 @@
import { IExecuteFunctions } from 'n8n-core';
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeTypeDescription,
INodeType,
ILoadOptionsFunctions,
INodePropertyOptions
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import { googleApiRequest, googleApiRequestAllItems } from './GenericFunctions';
import { taskOperations, taskFields } from './TaskDescription';
import {
googleApiRequest,
googleApiRequestAllItems,
} from './GenericFunctions';
import {
taskOperations,
taskFields,
} from './TaskDescription';
export class GoogleTasks implements INodeType {
description: INodeTypeDescription = {
@ -129,10 +138,6 @@ export class GoogleTasks implements INodeType {
body.deleted = additionalFields.deleted as boolean;
}
if (additionalFields.selfLink) {
body.selfLink = additionalFields.selfLink as string;
}
responseData = await googleApiRequest.call(
this,
'POST',
@ -186,9 +191,6 @@ export class GoogleTasks implements INodeType {
if (options.dueMax) {
qs.dueMax = options.dueMax as string;
}
if (options.pageToken) {
qs.pageToken = options.pageToken as string;
}
if (options.showCompleted) {
qs.showCompleted = options.showCompleted as boolean;
}
@ -198,10 +200,10 @@ export class GoogleTasks implements INodeType {
if (options.showHidden) {
qs.showHidden = options.showHidden as boolean;
}
if (options.updatedMin) {
qs.updatedMin = options.updatedMin as string;
}
if (returnAll) {
responseData = await googleApiRequestAllItems.call(
this,
@ -261,10 +263,6 @@ export class GoogleTasks implements INodeType {
body.deleted = updateFields.deleted as boolean;
}
if (updateFields.selfLink) {
body.selfLink = updateFields.selfLink as string;
}
responseData = await googleApiRequest.call(
this,
'PATCH',
@ -274,12 +272,11 @@ export class GoogleTasks implements INodeType {
);
}
}
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else if (responseData !== undefined) {
returnData.push(responseData as IDataObject);
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else if (responseData !== undefined) {
returnData.push(responseData as IDataObject);
}
}
return [this.helpers.returnJsonArray(returnData)];
}

View file

@ -1,4 +1,6 @@
import { INodeProperties } from 'n8n-workflow';
import {
INodeProperties,
} from 'n8n-workflow';
export const taskOperations = [
{
@ -46,7 +48,7 @@ export const taskOperations = [
export const taskFields = [
/* -------------------------------------------------------------------------- */
/* task:create */
/* task:create */
/* -------------------------------------------------------------------------- */
{
displayName: 'TaskList',
@ -64,7 +66,7 @@ export const taskFields = [
resource: [
'task',
],
}
},
},
default: '',
},
@ -86,14 +88,14 @@ export const taskFields = [
},
options: [
{
displayName: 'Completion date',
displayName: 'Completion Date',
name: 'completed',
type: 'dateTime',
default: '',
description: `Completion date of the task (as a RFC 3339 timestamp). This field is omitted if the task has not been completed.`,
},
{
displayName: 'Deleted status',
displayName: 'Deleted',
name: 'deleted',
type: 'boolean',
default: false,
@ -127,24 +129,17 @@ export const taskFields = [
default: '',
description: 'Previous sibling task identifier. If the task is created at the first position among its siblings, this parameter is omitted.',
},
{
displayName: 'Self Link',
name: 'selfLink',
type: 'string',
default: '',
description: 'URL pointing to this task. Used to retrieve, update, or delete this task.',
},
{
displayName: 'Status',
name: 'status',
type: 'options',
options: [
{
name: 'needs Action',
name: 'Needs Action',
value: 'needsAction',
},
{
name: 'completed',
name: 'Completed',
value: 'completed',
}
],
@ -158,11 +153,10 @@ export const taskFields = [
default: '',
description: 'Title of the task.',
},
]
],
},
/* -------------------------------------------------------------------------- */
/* task:delete */
/* task:delete */
/* -------------------------------------------------------------------------- */
{
displayName: 'TaskList',
@ -180,7 +174,7 @@ export const taskFields = [
resource: [
'task',
],
}
},
},
default: '',
},
@ -197,12 +191,12 @@ export const taskFields = [
resource: [
'task',
],
}
},
},
default: '',
},
/* -------------------------------------------------------------------------- */
/* task:get */
/* task:get */
/* -------------------------------------------------------------------------- */
{
displayName: 'TaskList',
@ -237,12 +231,12 @@ export const taskFields = [
resource: [
'task',
],
}
},
},
default: '',
},
/* -------------------------------------------------------------------------- */
/* task:getAll */
/* task:getAll */
/* -------------------------------------------------------------------------- */
{
displayName: 'TaskList',
@ -260,7 +254,7 @@ export const taskFields = [
resource: [
'task',
],
}
},
},
default: '',
},
@ -276,7 +270,7 @@ export const taskFields = [
resource: [
'task',
],
}
},
},
default: false,
description: 'If all results should be returned or only up to a given limit.',
@ -296,7 +290,7 @@ export const taskFields = [
returnAll: [
false,
],
}
},
},
typeOptions: {
minValue: 1,
@ -319,9 +313,8 @@ export const taskFields = [
resource: [
'task',
],
}
},
},
options: [
{
displayName: 'Completed Max',
@ -351,14 +344,6 @@ export const taskFields = [
default: '',
description: 'Upper bound for a task due date (as a RFC 3339 timestamp) to filter by.',
},
{
displayName: 'Page Token',
name: 'pageToken',
type: 'string',
default: '',
description: 'Token specifying the result page to return.',
},
{
displayName: 'Show Completed',
name: 'showCompleted',
@ -383,14 +368,14 @@ export const taskFields = [
{
displayName: 'Updated Min',
name: 'updatedMin',
type: 'string',
type: 'dateTime',
default: '',
description: 'Lower bound for a task last modification time (as a RFC 3339 timestamp) to filter by.',
},
]
},
/* -------------------------------------------------------------------------- */
/* task:update */
/* task:update */
/* -------------------------------------------------------------------------- */
{
displayName: 'TaskList',
@ -408,7 +393,7 @@ export const taskFields = [
resource: [
'task',
],
}
},
},
default: '',
},
@ -425,7 +410,7 @@ export const taskFields = [
resource: [
'task',
],
}
},
},
default: '',
},
@ -447,7 +432,7 @@ export const taskFields = [
},
options: [
{
displayName: 'Completion date',
displayName: 'Completion Date',
name: 'completed',
type: 'dateTime',
default: '',
@ -455,7 +440,7 @@ export const taskFields = [
},
{
displayName: 'Deleted status',
displayName: 'Deleted',
name: 'deleted',
type: 'boolean',
default: false,
@ -465,6 +450,9 @@ export const taskFields = [
displayName: 'Notes',
name: 'notes',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
description: 'Additional Notes.',
},
@ -475,24 +463,17 @@ export const taskFields = [
default: '',
description: 'Previous sibling task identifier. If the task is created at the first position among its siblings, this parameter is omitted.',
},
{
displayName: 'Self Link',
name: 'selfLink',
type: 'string',
default: '',
description: 'URL pointing to this task. Used to retrieve, update, or delete this task.',
},
{
displayName: 'Status',
name: 'status',
type: 'options',
options: [
{
name: 'needs Update',
name: 'Needs Update',
value: 'needsAction',
},
{
name: 'completed',
name: 'Completed',
value: 'completed',
}
],
@ -506,8 +487,6 @@ export const taskFields = [
default: '',
description: 'Title of the task.',
},
]
}
],
},
] as INodeProperties[];