sync upstream changes

This commit is contained in:
Ricardo Espinoza
2020-01-02 17:36:24 -05:00
20 changed files with 914 additions and 82 deletions

View File

@@ -0,0 +1,24 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class TogglApi implements ICredentialType {
name = 'togglApi';
displayName = 'Toggl API';
properties = [
{
displayName: 'Username',
name: 'username',
type: 'string' as NodePropertyTypes,
default: '',
},
{
displayName: 'Password',
name: 'password',
type: 'string' as NodePropertyTypes,
default: '',
},
];
}

View File

@@ -55,27 +55,31 @@ export class Cron implements INodeType {
options: [
{
name: 'Every Minute',
value: 'everyMinute'
value: 'everyMinute',
},
{
name: 'Every Hour',
value: 'everyHour'
value: 'everyHour',
},
{
name: 'Every Day',
value: 'everyDay'
value: 'everyDay',
},
{
name: 'Every Week',
value: 'everyWeek'
value: 'everyWeek',
},
{
name: 'Every Month',
value: 'everyMonth'
value: 'everyMonth',
},
{
name: 'Every X',
value: 'everyX',
},
{
name: 'Custom',
value: 'custom'
value: 'custom',
},
],
default: 'everyDay',
@@ -94,7 +98,8 @@ export class Cron implements INodeType {
mode: [
'custom',
'everyHour',
'everyMinute'
'everyMinute',
'everyX',
],
},
},
@@ -113,7 +118,8 @@ export class Cron implements INodeType {
hide: {
mode: [
'custom',
'everyMinute'
'everyMinute',
'everyX',
],
},
},
@@ -196,6 +202,48 @@ export class Cron implements INodeType {
default: '* * * * * *',
description: 'Use custom cron expression. Values and ranges as follows:<ul><li>Seconds: 0-59</li><li>Minutes: 0 - 59</li><li>Hours: 0 - 23</li><li>Day of Month: 1 - 31</li><li>Months: 0 - 11 (Jan - Dec)</li><li>Day of Week: 0 - 6 (Sun - Sat)</li></ul>',
},
{
displayName: 'Value',
name: 'value',
type: 'number',
typeOptions: {
minValue: 0,
maxValue: 1000,
},
displayOptions: {
show: {
mode: [
'everyX',
],
},
},
default: 2,
description: 'All how many X minutes/hours it should trigger.',
},
{
displayName: 'Unit',
name: 'unit',
type: 'options',
displayOptions: {
show: {
mode: [
'everyX',
],
},
},
options: [
{
name: 'Minutes',
value: 'minutes'
},
{
name: 'Hours',
value: 'hours'
},
],
default: 'hours',
description: 'If it should trigger all X minutes or hours.',
},
]
},
],
@@ -236,6 +284,14 @@ export class Cron implements INodeType {
cronTimes.push(`${Math.floor(Math.random() * 60).toString()} * * * * *`);
continue;
}
if (item.mode === 'everyX') {
if (item.unit === 'minutes') {
cronTimes.push(`${Math.floor(Math.random() * 60).toString()} */${item.value} * * * *`);
} else if (item.unit === 'hours') {
cronTimes.push(`${Math.floor(Math.random() * 60).toString()} 0 */${item.value} * * *`);
}
continue;
}
for (parameterName of parameterOrder) {
if (item[parameterName] !== undefined) {

View File

@@ -0,0 +1,57 @@
import * as EventSource from 'eventsource';
import { ITriggerFunctions } from 'n8n-core';
import {
INodeType,
INodeTypeDescription,
ITriggerResponse,
} from 'n8n-workflow';
export class SseTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'SSE Trigger',
name: 'sseTrigger',
icon: 'fa:cloud-download-alt',
group: ['trigger'],
version: 1,
description: 'Triggers worklfow on a new Server-Sent Event',
defaults: {
name: 'SSE Trigger',
color: '#225577',
},
inputs: [],
outputs: ['main'],
properties: [
{
displayName: 'URL',
name: 'url',
type: 'string',
default: '',
placeholder: 'http://example.com',
description: 'The URL to receive the SSE from.',
required: true,
},
]
};
async trigger(this: ITriggerFunctions): Promise<ITriggerResponse> {
const url = this.getNodeParameter('url') as string;
const eventSource = new EventSource(url);
eventSource.onmessage = (event) => {
const eventData = JSON.parse(event.data);
this.emit([this.helpers.returnJsonArray([eventData])]);
};
async function closeFunction() {
eventSource.close();
}
return {
closeFunction,
};
}
}

View File

@@ -0,0 +1,49 @@
import { OptionsWithUri } from 'request';
import {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
IExecuteSingleFunctions,
IPollFunctions,
ITriggerFunctions,
} from 'n8n-core';
import {
IDataObject,
} from 'n8n-workflow';
export async function togglApiRequest(this: ITriggerFunctions | IPollFunctions | IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, query?: IDataObject, uri?: string): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('togglApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const headerWithAuthentication = Object.assign({},
{ Authorization: ` Basic ${Buffer.from(`${credentials.username}:${credentials.password}`).toString('base64')}` });
const options: OptionsWithUri = {
headers: headerWithAuthentication,
method,
qs: query,
uri: uri || `https://www.toggl.com/api/v8${resource}`,
body,
json: true
};
if (Object.keys(options.body).length === 0) {
delete options.body;
}
try {
return await this.helpers.request!(options);
} catch (error) {
if (error.statusCode === 403) {
throw new Error('The Toggle credentials are probably invalid!');
}
const errorMessage = error.response.body && (error.response.body.message || error.response.body.Message);
if (errorMessage !== undefined) {
throw new Error(errorMessage);
}
throw error;
}
}

View File

@@ -0,0 +1,79 @@
import { IPollFunctions } from 'n8n-core';
import {
INodeExecutionData,
INodeType,
INodeTypeDescription,
IDataObject,
} from 'n8n-workflow';
import * as moment from 'moment';
import { togglApiRequest } from './GenericFunctions';
export class TogglTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Toggl',
name: 'toggl',
icon: 'file:toggl.png',
group: ['trigger'],
version: 1,
description: 'Starts the workflow when Toggl events occure',
defaults: {
name: 'Toggl',
color: '#00FF00',
},
credentials: [
{
name: 'togglApi',
required: true,
}
],
polling: true,
inputs: [],
outputs: ['main'],
properties: [
{
displayName: 'Event',
name: 'event',
type: 'options',
options: [
{
name: 'New Time Entry',
value: 'newTimeEntry',
}
],
required: true,
default: 'newTimeEntry',
},
]
};
async poll(this: IPollFunctions): Promise<INodeExecutionData[][] | null> {
const webhookData = this.getWorkflowStaticData('node');
const event = this.getNodeParameter('event') as string;
let endpoint: string;
if (event === 'newTimeEntry') {
endpoint = '/time_entries';
} else {
throw new Error(`The defined event "${event}" is not supported`);
}
const qs: IDataObject = {};
let timeEntries = [];
qs.start_date = webhookData.lastTimeChecked;
qs.end_date = moment().format();
try {
timeEntries = await togglApiRequest.call(this, 'GET', endpoint, {}, qs);
webhookData.lastTimeChecked = qs.end_date;
} catch (err) {
throw new Error(`Toggl Trigger Error: ${err}`);
}
if (Array.isArray(timeEntries) && timeEntries.length !== 0) {
return [this.helpers.returnJsonArray(timeEntries)];
}
return null;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

View File

@@ -1,6 +1,6 @@
{
"name": "n8n-nodes-base",
"version": "0.36.0",
"version": "0.37.0",
"description": "Base nodes of n8n",
"license": "SEE LICENSE IN LICENSE.md",
"homepage": "https://n8n.io",
@@ -75,6 +75,7 @@
"dist/credentials/TypeformApi.credentials.js",
"dist/credentials/VeroApi.credentials.js",
"dist/credentials/WordpressApi.credentials.js"
"dist/credentials/TogglApi.credentials.js",
],
"nodes": [
"dist/nodes/ActiveCampaign/ActiveCampaign.node.js",
@@ -144,6 +145,7 @@
"dist/nodes/RenameKeys.node.js",
"dist/nodes/RssFeedRead.node.js",
"dist/nodes/Set.node.js",
"dist/nodes/SseTrigger.node.js",
"dist/nodes/SplitInBatches.node.js",
"dist/nodes/Slack/Slack.node.js",
"dist/nodes/SpreadsheetFile.node.js",
@@ -158,6 +160,7 @@
"dist/nodes/Trello/TrelloTrigger.node.js",
"dist/nodes/Twilio/Twilio.node.js",
"dist/nodes/Typeform/TypeformTrigger.node.js",
"dist/nodes/Toggl/TogglTrigger.node.js",
"dist/nodes/Vero/Vero.node.js",
"dist/nodes/WriteBinaryFile.node.js",
"dist/nodes/Webhook.node.js",
@@ -170,6 +173,7 @@
"@types/basic-auth": "^1.1.2",
"@types/cheerio": "^0.22.15",
"@types/cron": "^1.6.1",
"@types/eventsource": "^1.1.2",
"@types/express": "^4.16.1",
"@types/gm": "^1.18.2",
"@types/imap-simple": "^4.2.0",
@@ -183,7 +187,7 @@
"@types/xml2js": "^0.4.3",
"gulp": "^4.0.0",
"jest": "^24.9.0",
"n8n-workflow": "~0.18.0",
"n8n-workflow": "~0.19.0",
"ts-jest": "^24.0.2",
"tslint": "^5.17.0",
"typescript": "~3.7.4"
@@ -192,7 +196,8 @@
"aws4": "^1.8.0",
"basic-auth": "^2.0.1",
"cheerio": "^1.0.0-rc.3",
"cron": "^1.6.0",
"cron": "^1.7.2",
"eventsource": "^1.0.7",
"glob-promise": "^3.4.0",
"gm": "^1.23.1",
"googleapis": "^46.0.0",
@@ -202,7 +207,7 @@
"lodash.unset": "^4.5.2",
"mongodb": "^3.3.2",
"mysql2": "^2.0.1",
"n8n-core": "~0.18.0",
"n8n-core": "~0.19.0",
"nodemailer": "^5.1.1",
"pdf-parse": "^1.1.1",
"pg-promise": "^9.0.3",