Fixed some lint and cosmetic issues

This commit is contained in:
Jan Oberhauser 2019-11-07 08:40:12 +01:00
parent 98ef334ab0
commit 47856f236e
2 changed files with 74 additions and 82 deletions

View file

@ -8,7 +8,6 @@ import {
} from 'n8n-core'; } from 'n8n-core';
import * as _ from 'lodash'; import * as _ from 'lodash';
import { IDataObject } from 'n8n-workflow';
export async function todoistApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resource: string, method: string, body: any = {}, headers?: object): Promise<any> { // tslint:disable-line:no-any export async function todoistApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resource: string, method: string, body: any = {}, headers?: object): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('todoistApi'); const credentials = this.getCredentials('todoistApi');
@ -24,20 +23,17 @@ export async function todoistApiRequest(this: IHookFunctions | IExecuteFunctions
const options: OptionsWithUri = { const options: OptionsWithUri = {
headers: headerWithAuthentication, headers: headerWithAuthentication,
method, method,
body,
uri: `https://${endpoint}${resource}`, uri: `https://${endpoint}${resource}`,
json: true json: true
}; };
if (_.isEmpty(options.body)) { if (Object.keys(body).length !== 0) {
delete options.body options.body = body;
} }
try { try {
return await this.helpers.request!(options); return await this.helpers.request!(options);
} catch (error) { } catch (error) {
//console.error(error);
const errorMessage = error.response.body.message || error.response.body.Message; const errorMessage = error.response.body.message || error.response.body.Message;
if (errorMessage !== undefined) { if (errorMessage !== undefined) {

View file

@ -10,11 +10,22 @@ import {
INodePropertyOptions, INodePropertyOptions,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { import {
todoistApiRequest todoistApiRequest,
} from './GenericFunctions'; } from './GenericFunctions';
import moment = require('moment');
import _ = require('lodash') interface IBodyCreateTask {
content: string;
project_id?: number;
parent?: number;
order?: number;
label_ids?: number[];
priority?: number;
due_string?: string;
due_datetime?: string;
due_date?: string;
due_lang?: string;
}
export class Todoist implements INodeType { export class Todoist implements INodeType {
@ -37,9 +48,9 @@ export class Todoist implements INodeType {
name: 'todoistApi', name: 'todoistApi',
required: true, required: true,
} }
], ],
properties: [ properties: [
{ {
displayName: 'Resource', displayName: 'Resource',
name: 'resource', name: 'resource',
type: 'options', type: 'options',
@ -50,7 +61,7 @@ export class Todoist implements INodeType {
description: 'Task resource.', description: 'Task resource.',
}, },
], ],
default: '', default: 'task',
required: true, required: true,
description: 'Resource to consume.', description: 'Resource to consume.',
}, },
@ -75,22 +86,22 @@ export class Todoist implements INodeType {
], ],
default: 'create', default: 'create',
description: 'The operation to perform.', description: 'The operation to perform.',
}, },
{ {
displayName: 'Project', displayName: 'Project',
name: 'project', name: 'project',
type: 'options', type: 'options',
typeOptions: { typeOptions: {
loadOptionsMethod: 'getProjects', loadOptionsMethod: 'getProjects',
}, },
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'task', 'task',
], ],
operation: [ operation: [
'create' 'create',
] ]
}, },
}, },
default: [], default: [],
@ -107,38 +118,38 @@ export class Todoist implements INodeType {
show: { show: {
resource: [ resource: [
'task', 'task',
], ],
operation: [ operation: [
'create' 'create',
] ]
}, },
}, },
default: [], default: [],
required: false, required: false,
description: 'Labels', description: 'Labels',
}, },
{ {
displayName: 'Content', displayName: 'Content',
name: 'content', name: 'content',
type: 'string', type: 'string',
typeOptions: { typeOptions: {
rows: 5, rows: 5,
}, },
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'task', 'task',
], ],
operation: [ operation: [
'create' 'create',
] ]
}, },
}, },
default: [], default: [],
required: true, required: true,
description: 'Task content', description: 'Task content',
}, },
{ {
displayName: 'Options', displayName: 'Options',
name: 'options', name: 'options',
type: 'collection', type: 'collection',
@ -148,46 +159,46 @@ export class Todoist implements INodeType {
show: { show: {
resource: [ resource: [
'task', 'task',
], ],
operation: [ operation: [
'create' 'create',
] ]
}, },
}, },
options: [ options: [
{ {
displayName: 'Priority', displayName: 'Priority',
name: 'priority', name: 'priority',
type: 'number', type: 'number',
typeOptions: { typeOptions: {
numberStepSize: 1, numberStepSize: 1,
maxValue: 4, maxValue: 4,
minValue: 1 minValue: 1,
}, },
default: 1, default: 1,
description: 'Task priority from 1 (normal) to 4 (urgent).', description: 'Task priority from 1 (normal) to 4 (urgent).',
}, },
{ {
displayName: 'Due Date Time', displayName: 'Due Date Time',
name: 'dueDateTime', name: 'dueDateTime',
type: 'dateTime', type: 'dateTime',
default: '', default: '',
description: 'Specific date and time in RFC3339 format in UTC.', description: 'Specific date and time in RFC3339 format in UTC.',
}, },
{ {
displayName: 'Due String', displayName: 'Due String',
name: 'dueString', name: 'dueString',
type: 'string', type: 'string',
default: '', default: '',
description: 'Human defined task due date (ex.: “next Monday”, “Tomorrow”). Value is set using local (not UTC) time.', description: 'Human defined task due date (ex.: “next Monday”, “Tomorrow”). Value is set using local (not UTC) time.',
}, },
] ]
} }
] ]
}; };
methods = {
methods = {
loadOptions: { loadOptions: {
// Get all the available projects to display them to user so that he can // Get all the available projects to display them to user so that he can
// select them easily // select them easily
@ -209,7 +220,7 @@ export class Todoist implements INodeType {
}); });
} }
return returnData return returnData;
}, },
// Get all the available labels to display them to user so that he can // Get all the available labels to display them to user so that he can
@ -232,39 +243,24 @@ export class Todoist implements INodeType {
}); });
} }
return returnData return returnData;
} }
} }
}; };
async executeSingle(this: IExecuteSingleFunctions): Promise<INodeExecutionData> { async executeSingle(this: IExecuteSingleFunctions): Promise<INodeExecutionData> {
const resource = this.getNodeParameter('resource') as string; const resource = this.getNodeParameter('resource') as string;
const opeation = this.getNodeParameter('operation') as string; const opeation = this.getNodeParameter('operation') as string;
let response; let response;
if (resource === 'task' && opeation === 'create') { if (resource === 'task' && opeation === 'create') {
//https://developer.todoist.com/rest/v1/#create-a-new-task
//https://developer.todoist.com/rest/v1/#create-a-new-task
const content = this.getNodeParameter('content') as string; const content = this.getNodeParameter('content') as string;
const projectId = this.getNodeParameter('project') as number; const projectId = this.getNodeParameter('project') as number;
const labels = this.getNodeParameter('labels') as [number]; const labels = this.getNodeParameter('labels') as number[];
const options = this.getNodeParameter('options') as IDataObject; const options = this.getNodeParameter('options') as IDataObject;
interface IBodyCreateTask {
content: string;
project_id?: number;
parent?: number;
order?: number;
label_ids?: [number];
priority?: number;
due_string?: string;
due_datetime?: string;
due_date?: string;
due_lang?: string;
}
const body: IBodyCreateTask = { const body: IBodyCreateTask = {
content, content,
project_id: projectId, project_id: projectId,
@ -272,15 +268,15 @@ export class Todoist implements INodeType {
}; };
if (options.dueDateTime) { if (options.dueDateTime) {
body.due_datetime = moment(options.dueDateTime as string).utc().format(); body.due_datetime = options.dueDateTime as string;
} }
if (options.dueString) { if (options.dueString) {
body.due_string = options.dueString as string; body.due_string = options.dueString as string;
} }
if (!_.isEmpty(labels)) { if (labels !== undefined && labels.length !== 0) {
body.label_ids = labels body.label_ids = labels;
} }
try { try {