refactor: Introduce IActionOutput and ITriggerOutput types

This commit is contained in:
Faruk AYDIN
2022-10-12 21:10:44 +02:00
committed by Ali BARIN
parent 6895378d33
commit d9192f6e6b
22 changed files with 327 additions and 279 deletions

View File

@@ -24,13 +24,13 @@ export default {
options: [ options: [
{ {
label: 'Yes', label: 'Yes',
value: true value: true,
}, },
{ {
label: 'No', label: 'No',
value: false value: false,
} },
] ],
}, },
{ {
label: 'Time of day', label: 'Time of day',
@@ -42,111 +42,111 @@ export default {
options: [ options: [
{ {
label: '00:00', label: '00:00',
value: 0 value: 0,
}, },
{ {
label: '01:00', label: '01:00',
value: 1 value: 1,
}, },
{ {
label: '02:00', label: '02:00',
value: 2 value: 2,
}, },
{ {
label: '03:00', label: '03:00',
value: 3 value: 3,
}, },
{ {
label: '04:00', label: '04:00',
value: 4 value: 4,
}, },
{ {
label: '05:00', label: '05:00',
value: 5 value: 5,
}, },
{ {
label: '06:00', label: '06:00',
value: 6 value: 6,
}, },
{ {
label: '07:00', label: '07:00',
value: 7 value: 7,
}, },
{ {
label: '08:00', label: '08:00',
value: 8 value: 8,
}, },
{ {
label: '09:00', label: '09:00',
value: 9 value: 9,
}, },
{ {
label: '10:00', label: '10:00',
value: 10 value: 10,
}, },
{ {
label: '11:00', label: '11:00',
value: 11 value: 11,
}, },
{ {
label: '12:00', label: '12:00',
value: 12 value: 12,
}, },
{ {
label: '13:00', label: '13:00',
value: 13 value: 13,
}, },
{ {
label: '14:00', label: '14:00',
value: 14 value: 14,
}, },
{ {
label: '15:00', label: '15:00',
value: 15 value: 15,
}, },
{ {
label: '16:00', label: '16:00',
value: 16 value: 16,
}, },
{ {
label: '17:00', label: '17:00',
value: 17 value: 17,
}, },
{ {
label: '18:00', label: '18:00',
value: 18 value: 18,
}, },
{ {
label: '19:00', label: '19:00',
value: 19 value: 19,
}, },
{ {
label: '20:00', label: '20:00',
value: 20 value: 20,
}, },
{ {
label: '21:00', label: '21:00',
value: 21 value: 21,
}, },
{ {
label: '22:00', label: '22:00',
value: 22 value: 22,
}, },
{ {
label: '23:00', label: '23:00',
value: 23 value: 23,
} },
] ],
} },
] ],
}, },
{ {
key: 'testStep', key: 'testStep',
name: 'Test trigger' name: 'Test trigger',
} },
], ],
getInterval(parameters: IGlobalVariable["db"]["step"]["parameters"]) { getInterval(parameters: IGlobalVariable['step']['parameters']) {
if (parameters.triggersOnWeekend as boolean) { if (parameters.triggersOnWeekend as boolean) {
return cronTimes.everyDayAt(parameters.hour as number); return cronTimes.everyDayAt(parameters.hour as number);
} }
@@ -156,14 +156,20 @@ export default {
async run($: IGlobalVariable, startDateTime: Date) { async run($: IGlobalVariable, startDateTime: Date) {
const dateTime = DateTime.fromJSDate(startDateTime); const dateTime = DateTime.fromJSDate(startDateTime);
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(dateTime) as IJSONValue; const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(
dateTime
) as IJSONValue;
return { data: [dateTimeObjectRepresentation] }; return { data: [dateTimeObjectRepresentation] };
}, },
async testRun($: IGlobalVariable) { async testRun($: IGlobalVariable) {
const nextCronDateTime = getNextCronDateTime(this.getInterval($.db.step.parameters)); const nextCronDateTime = getNextCronDateTime(
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(nextCronDateTime) as IJSONValue; this.getInterval($.step.parameters)
);
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(
nextCronDateTime
) as IJSONValue;
return { data: [dateTimeObjectRepresentation] }; return { data: [dateTimeObjectRepresentation] };
}, },

View File

@@ -24,25 +24,25 @@ export default {
options: [ options: [
{ {
label: 'Yes', label: 'Yes',
value: true value: true,
}, },
{ {
label: 'No', label: 'No',
value: false value: false,
} },
] ],
} },
] ],
}, },
{ {
key: 'testStep', key: 'testStep',
name: 'Test trigger' name: 'Test trigger',
} },
], ],
getInterval(parameters: IGlobalVariable["db"]["step"]["parameters"]) { getInterval(parameters: IGlobalVariable['step']['parameters']) {
if (parameters.triggersOnWeekend) { if (parameters.triggersOnWeekend) {
return cronTimes.everyHour return cronTimes.everyHour;
} }
return cronTimes.everyHourExcludingWeekends; return cronTimes.everyHourExcludingWeekends;
@@ -50,14 +50,20 @@ export default {
async run($: IGlobalVariable, startDateTime: Date) { async run($: IGlobalVariable, startDateTime: Date) {
const dateTime = DateTime.fromJSDate(startDateTime); const dateTime = DateTime.fromJSDate(startDateTime);
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(dateTime) as IJSONValue; const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(
dateTime
) as IJSONValue;
return { data: [dateTimeObjectRepresentation] }; return { data: [dateTimeObjectRepresentation] };
}, },
async testRun($: IGlobalVariable) { async testRun($: IGlobalVariable) {
const nextCronDateTime = getNextCronDateTime(this.getInterval($.db.step.parameters)); const nextCronDateTime = getNextCronDateTime(
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(nextCronDateTime) as IJSONValue; this.getInterval($.step.parameters)
);
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(
nextCronDateTime
) as IJSONValue;
return { data: [dateTimeObjectRepresentation] }; return { data: [dateTimeObjectRepresentation] };
}, },

View File

@@ -23,129 +23,129 @@ export default {
options: [ options: [
{ {
label: 1, label: 1,
value: 1 value: 1,
}, },
{ {
label: 2, label: 2,
value: 2 value: 2,
}, },
{ {
label: 3, label: 3,
value: 3 value: 3,
}, },
{ {
label: 4, label: 4,
value: 4 value: 4,
}, },
{ {
label: 5, label: 5,
value: 5 value: 5,
}, },
{ {
label: 6, label: 6,
value: 6 value: 6,
}, },
{ {
label: 7, label: 7,
value: 7 value: 7,
}, },
{ {
label: 8, label: 8,
value: 8 value: 8,
}, },
{ {
label: 9, label: 9,
value: 9 value: 9,
}, },
{ {
label: 10, label: 10,
value: 10 value: 10,
}, },
{ {
label: 11, label: 11,
value: 11 value: 11,
}, },
{ {
label: 12, label: 12,
value: 12 value: 12,
}, },
{ {
label: 13, label: 13,
value: 13 value: 13,
}, },
{ {
label: 14, label: 14,
value: 14 value: 14,
}, },
{ {
label: 15, label: 15,
value: 15 value: 15,
}, },
{ {
label: 16, label: 16,
value: 16 value: 16,
}, },
{ {
label: 17, label: 17,
value: 17 value: 17,
}, },
{ {
label: 18, label: 18,
value: 18 value: 18,
}, },
{ {
label: 19, label: 19,
value: 19 value: 19,
}, },
{ {
label: 20, label: 20,
value: 20 value: 20,
}, },
{ {
label: 21, label: 21,
value: 21 value: 21,
}, },
{ {
label: 22, label: 22,
value: 22 value: 22,
}, },
{ {
label: 23, label: 23,
value: 23 value: 23,
}, },
{ {
label: 24, label: 24,
value: 24 value: 24,
}, },
{ {
label: 25, label: 25,
value: 25 value: 25,
}, },
{ {
label: 26, label: 26,
value: 26 value: 26,
}, },
{ {
label: 27, label: 27,
value: 27 value: 27,
}, },
{ {
label: 28, label: 28,
value: 28 value: 28,
}, },
{ {
label: 29, label: 29,
value: 29 value: 29,
}, },
{ {
label: 30, label: 30,
value: 30 value: 30,
}, },
{ {
label: 31, label: 31,
value: 31 value: 31,
} },
] ],
}, },
{ {
label: 'Time of day', label: 'Time of day',
@@ -157,126 +157,135 @@ export default {
options: [ options: [
{ {
label: '00:00', label: '00:00',
value: 0 value: 0,
}, },
{ {
label: '01:00', label: '01:00',
value: 1 value: 1,
}, },
{ {
label: '02:00', label: '02:00',
value: 2 value: 2,
}, },
{ {
label: '03:00', label: '03:00',
value: 3 value: 3,
}, },
{ {
label: '04:00', label: '04:00',
value: 4 value: 4,
}, },
{ {
label: '05:00', label: '05:00',
value: 5 value: 5,
}, },
{ {
label: '06:00', label: '06:00',
value: 6 value: 6,
}, },
{ {
label: '07:00', label: '07:00',
value: 7 value: 7,
}, },
{ {
label: '08:00', label: '08:00',
value: 8 value: 8,
}, },
{ {
label: '09:00', label: '09:00',
value: 9 value: 9,
}, },
{ {
label: '10:00', label: '10:00',
value: 10 value: 10,
}, },
{ {
label: '11:00', label: '11:00',
value: 11 value: 11,
}, },
{ {
label: '12:00', label: '12:00',
value: 12 value: 12,
}, },
{ {
label: '13:00', label: '13:00',
value: 13 value: 13,
}, },
{ {
label: '14:00', label: '14:00',
value: 14 value: 14,
}, },
{ {
label: '15:00', label: '15:00',
value: 15 value: 15,
}, },
{ {
label: '16:00', label: '16:00',
value: 16 value: 16,
}, },
{ {
label: '17:00', label: '17:00',
value: 17 value: 17,
}, },
{ {
label: '18:00', label: '18:00',
value: 18 value: 18,
}, },
{ {
label: '19:00', label: '19:00',
value: 19 value: 19,
}, },
{ {
label: '20:00', label: '20:00',
value: 20 value: 20,
}, },
{ {
label: '21:00', label: '21:00',
value: 21 value: 21,
}, },
{ {
label: '22:00', label: '22:00',
value: 22 value: 22,
}, },
{ {
label: '23:00', label: '23:00',
value: 23 value: 23,
} },
] ],
} },
] ],
}, },
{ {
key: 'testStep', key: 'testStep',
name: 'Test trigger' name: 'Test trigger',
} },
], ],
getInterval(parameters: IGlobalVariable["db"]["step"]["parameters"]) { getInterval(parameters: IGlobalVariable['step']['parameters']) {
const interval = cronTimes.everyMonthOnAndAt(parameters.day as number, parameters.hour as number); const interval = cronTimes.everyMonthOnAndAt(
parameters.day as number,
parameters.hour as number
);
return interval; return interval;
}, },
async run($: IGlobalVariable, startDateTime: Date) { async run($: IGlobalVariable, startDateTime: Date) {
const dateTime = DateTime.fromJSDate(startDateTime); const dateTime = DateTime.fromJSDate(startDateTime);
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(dateTime) as IJSONValue; const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(
dateTime
) as IJSONValue;
return { data: [dateTimeObjectRepresentation] }; return { data: [dateTimeObjectRepresentation] };
}, },
async testRun($: IGlobalVariable) { async testRun($: IGlobalVariable) {
const nextCronDateTime = getNextCronDateTime(this.getInterval($.db.step.parameters)); const nextCronDateTime = getNextCronDateTime(
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(nextCronDateTime) as IJSONValue; this.getInterval($.step.parameters)
);
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(
nextCronDateTime
) as IJSONValue;
return { data: [dateTimeObjectRepresentation] }; return { data: [dateTimeObjectRepresentation] };
}, },

View File

@@ -23,33 +23,33 @@ export default {
options: [ options: [
{ {
label: 'Monday', label: 'Monday',
value: 1 value: 1,
}, },
{ {
label: 'Tuesday', label: 'Tuesday',
value: 2 value: 2,
}, },
{ {
label: 'Wednesday', label: 'Wednesday',
value: 3 value: 3,
}, },
{ {
label: 'Thursday', label: 'Thursday',
value: 4 value: 4,
}, },
{ {
label: 'Friday', label: 'Friday',
value: 5 value: 5,
}, },
{ {
label: 'Saturday', label: 'Saturday',
value: 6 value: 6,
}, },
{ {
label: 'Sunday', label: 'Sunday',
value: 0 value: 0,
} },
] ],
}, },
{ {
label: 'Time of day', label: 'Time of day',
@@ -61,126 +61,135 @@ export default {
options: [ options: [
{ {
label: '00:00', label: '00:00',
value: 0 value: 0,
}, },
{ {
label: '01:00', label: '01:00',
value: 1 value: 1,
}, },
{ {
label: '02:00', label: '02:00',
value: 2 value: 2,
}, },
{ {
label: '03:00', label: '03:00',
value: 3 value: 3,
}, },
{ {
label: '04:00', label: '04:00',
value: 4 value: 4,
}, },
{ {
label: '05:00', label: '05:00',
value: 5 value: 5,
}, },
{ {
label: '06:00', label: '06:00',
value: 6 value: 6,
}, },
{ {
label: '07:00', label: '07:00',
value: 7 value: 7,
}, },
{ {
label: '08:00', label: '08:00',
value: 8 value: 8,
}, },
{ {
label: '09:00', label: '09:00',
value: 9 value: 9,
}, },
{ {
label: '10:00', label: '10:00',
value: 10 value: 10,
}, },
{ {
label: '11:00', label: '11:00',
value: 11 value: 11,
}, },
{ {
label: '12:00', label: '12:00',
value: 12 value: 12,
}, },
{ {
label: '13:00', label: '13:00',
value: 13 value: 13,
}, },
{ {
label: '14:00', label: '14:00',
value: 14 value: 14,
}, },
{ {
label: '15:00', label: '15:00',
value: 15 value: 15,
}, },
{ {
label: '16:00', label: '16:00',
value: 16 value: 16,
}, },
{ {
label: '17:00', label: '17:00',
value: 17 value: 17,
}, },
{ {
label: '18:00', label: '18:00',
value: 18 value: 18,
}, },
{ {
label: '19:00', label: '19:00',
value: 19 value: 19,
}, },
{ {
label: '20:00', label: '20:00',
value: 20 value: 20,
}, },
{ {
label: '21:00', label: '21:00',
value: 21 value: 21,
}, },
{ {
label: '22:00', label: '22:00',
value: 22 value: 22,
}, },
{ {
label: '23:00', label: '23:00',
value: 23 value: 23,
} },
] ],
} },
] ],
}, },
{ {
key: 'testStep', key: 'testStep',
name: 'Test trigger' name: 'Test trigger',
} },
], ],
getInterval(parameters: IGlobalVariable["db"]["step"]["parameters"]) { getInterval(parameters: IGlobalVariable['step']['parameters']) {
const interval = cronTimes.everyWeekOnAndAt(parameters.weekday as number, parameters.hour as number); const interval = cronTimes.everyWeekOnAndAt(
parameters.weekday as number,
parameters.hour as number
);
return interval; return interval;
}, },
async run($: IGlobalVariable, startDateTime: Date) { async run($: IGlobalVariable, startDateTime: Date) {
const dateTime = DateTime.fromJSDate(startDateTime); const dateTime = DateTime.fromJSDate(startDateTime);
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(dateTime) as IJSONValue; const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(
dateTime
) as IJSONValue;
return { data: [dateTimeObjectRepresentation] }; return { data: [dateTimeObjectRepresentation] };
}, },
async testRun($: IGlobalVariable) { async testRun($: IGlobalVariable) {
const nextCronDateTime = getNextCronDateTime(this.getInterval($.db.step.parameters)); const nextCronDateTime = getNextCronDateTime(
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(nextCronDateTime) as IJSONValue; this.getInterval($.step.parameters)
);
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(
nextCronDateTime
) as IJSONValue;
return { data: [dateTimeObjectRepresentation] }; return { data: [dateTimeObjectRepresentation] };
}, },

View File

@@ -1,4 +1,4 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types'; import { IGlobalVariable, IActionOutput } from '@automatisch/types';
type FindMessageOptions = { type FindMessageOptions = {
query: string; query: string;
@@ -8,11 +8,6 @@ type FindMessageOptions = {
}; };
const findMessage = async ($: IGlobalVariable, options: FindMessageOptions) => { const findMessage = async ($: IGlobalVariable, options: FindMessageOptions) => {
const message: {
data?: IJSONObject;
error?: IJSONObject;
} = {};
const headers = { const headers = {
Authorization: `Bearer ${$.auth.data.accessToken}`, Authorization: `Bearer ${$.auth.data.accessToken}`,
}; };
@@ -29,20 +24,14 @@ const findMessage = async ($: IGlobalVariable, options: FindMessageOptions) => {
params, params,
}); });
if (response.integrationError) {
message.error = response.integrationError;
return message;
}
const data = response.data; const data = response.data;
if (!data.ok) { const message: IActionOutput = {
message.error = data; data: {
return message; raw: data?.data?.messages.matches[0],
} },
error: response?.integrationError || (!data.ok && data),
const messages = data.messages.matches; };
message.data = messages?.[0];
return message; return message;
}; };

View File

@@ -72,7 +72,7 @@ export default {
], ],
async run($: IGlobalVariable) { async run($: IGlobalVariable) {
const parameters = $.db.step.parameters; const parameters = $.step.parameters;
const query = parameters.query as string; const query = parameters.query as string;
const sortBy = parameters.sortBy as string; const sortBy = parameters.sortBy as string;
const sortDirection = parameters.sortDirection as string; const sortDirection = parameters.sortDirection as string;

View File

@@ -49,8 +49,8 @@ export default {
], ],
async run($: IGlobalVariable) { async run($: IGlobalVariable) {
const channelId = $.db.step.parameters.channel as string; const channelId = $.step.parameters.channel as string;
const text = $.db.step.parameters.message as string; const text = $.step.parameters.message as string;
const message = await postMessage($, channelId, text); const message = await postMessage($, channelId, text);

View File

@@ -1,18 +1,10 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types'; import { IGlobalVariable, IActionOutput } from '@automatisch/types';
const postMessage = async ( const postMessage = async (
$: IGlobalVariable, $: IGlobalVariable,
channelId: string, channelId: string,
text: string text: string
) => { ) => {
const message: {
data: IJSONObject | null | undefined;
error: IJSONObject | null | undefined;
} = {
data: null,
error: null,
};
const headers = { const headers = {
Authorization: `Bearer ${$.auth.data.accessToken}`, Authorization: `Bearer ${$.auth.data.accessToken}`,
}; };
@@ -24,8 +16,12 @@ const postMessage = async (
const response = await $.http.post('/chat.postMessage', params, { headers }); const response = await $.http.post('/chat.postMessage', params, { headers });
message.error = response?.integrationError; const message: IActionOutput = {
message.data = response?.data?.message; data: {
raw: response?.data?.message,
},
error: response?.integrationError,
};
if (response.data.ok === false) { if (response.data.ok === false) {
message.error = response.data; message.error = response.data;

View File

@@ -1,4 +1,8 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types'; import {
IGlobalVariable,
IJSONObject,
ITriggerOutput,
} from '@automatisch/types';
import { URLSearchParams } from 'url'; import { URLSearchParams } from 'url';
import { omitBy, isEmpty } from 'lodash'; import { omitBy, isEmpty } from 'lodash';
import generateRequest from './generate-request'; import generateRequest from './generate-request';
@@ -14,12 +18,8 @@ const getUserFollowers = async (
) => { ) => {
let response; let response;
const followers: { const followers: ITriggerOutput = {
data: IJSONObject[];
error: IJSONObject | null;
} = {
data: [], data: [],
error: null,
}; };
do { do {
@@ -54,7 +54,10 @@ const getUserFollowers = async (
!options.lastInternalId || !options.lastInternalId ||
Number(tweet.id) > Number(options.lastInternalId) Number(tweet.id) > Number(options.lastInternalId)
) { ) {
followers.data.push(tweet); followers.data.push({
raw: tweet,
meta: { internalId: tweet.id as string },
});
} else { } else {
return; return;
} }

View File

@@ -1,4 +1,8 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types'; import {
IGlobalVariable,
IJSONObject,
ITriggerOutput,
} from '@automatisch/types';
import { URLSearchParams } from 'url'; import { URLSearchParams } from 'url';
import omitBy from 'lodash/omitBy'; import omitBy from 'lodash/omitBy';
import isEmpty from 'lodash/isEmpty'; import isEmpty from 'lodash/isEmpty';
@@ -22,19 +26,15 @@ const getUserTweets = async (
const currentUser = await getCurrentUser($); const currentUser = await getCurrentUser($);
username = currentUser.username as string; username = currentUser.username as string;
} else { } else {
username = $.db.step.parameters.username as string; username = $.step.parameters.username as string;
} }
const user = await getUserByUsername($, username); const user = await getUserByUsername($, username);
let response; let response;
const tweets: { const tweets: ITriggerOutput = {
data: IJSONObject[];
error: IJSONObject | null;
} = {
data: [], data: [],
error: null,
}; };
do { do {
@@ -65,7 +65,10 @@ const getUserTweets = async (
!options.lastInternalId || !options.lastInternalId ||
Number(tweet.id) > Number(options.lastInternalId) Number(tweet.id) > Number(options.lastInternalId)
) { ) {
tweets.data.push(tweet); tweets.data.push({
raw: tweet,
meta: { internalId: tweet.id as string },
});
} else { } else {
return; return;
} }

View File

@@ -20,7 +20,7 @@ export default {
async run($: IGlobalVariable) { async run($: IGlobalVariable) {
return await getUserTweets($, { return await getUserTweets($, {
currentUser: true, currentUser: true,
lastInternalId: $.db.flow.lastInternalId, lastInternalId: $.flow.lastInternalId,
}); });
}, },

View File

@@ -18,7 +18,7 @@ export default {
], ],
async run($: IGlobalVariable) { async run($: IGlobalVariable) {
return await myFollowers($, $.db.flow.lastInternalId); return await myFollowers($, $.flow.lastInternalId);
}, },
async testRun($: IGlobalVariable) { async testRun($: IGlobalVariable) {

View File

@@ -32,14 +32,14 @@ export default {
async run($: IGlobalVariable) { async run($: IGlobalVariable) {
return await searchTweets($, { return await searchTweets($, {
searchTerm: $.db.step.parameters.searchTerm as string, searchTerm: $.step.parameters.searchTerm as string,
lastInternalId: $.db.flow.lastInternalId, lastInternalId: $.flow.lastInternalId,
}); });
}, },
async testRun($: IGlobalVariable) { async testRun($: IGlobalVariable) {
return await searchTweets($, { return await searchTweets($, {
searchTerm: $.db.step.parameters.searchTerm as string, searchTerm: $.step.parameters.searchTerm as string,
}); });
}, },
}; };

View File

@@ -1,4 +1,8 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types'; import {
IGlobalVariable,
IJSONObject,
ITriggerOutput,
} from '@automatisch/types';
import qs from 'qs'; import qs from 'qs';
import generateRequest from '../../common/generate-request'; import generateRequest from '../../common/generate-request';
import { omitBy, isEmpty } from 'lodash'; import { omitBy, isEmpty } from 'lodash';
@@ -14,12 +18,8 @@ const searchTweets = async (
) => { ) => {
let response; let response;
const tweets: { const tweets: ITriggerOutput = {
data: IJSONObject[];
error: IJSONObject | null;
} = {
data: [], data: [],
error: null,
}; };
do { do {
@@ -56,7 +56,14 @@ const searchTweets = async (
!options.lastInternalId || !options.lastInternalId ||
Number(tweet.id) > Number(options.lastInternalId) Number(tweet.id) > Number(options.lastInternalId)
) { ) {
tweets.data.push(tweet); const dataItem = {
raw: tweet,
meta: {
internalId: tweet.id as string,
},
};
tweets.data.push(dataItem);
} else { } else {
return; return;
} }

View File

@@ -32,15 +32,15 @@ export default {
async run($: IGlobalVariable) { async run($: IGlobalVariable) {
return await getUserTweets($, { return await getUserTweets($, {
currentUser: false, currentUser: false,
userId: $.db.step.parameters.username as string, userId: $.step.parameters.username as string,
lastInternalId: $.db.flow.lastInternalId, lastInternalId: $.flow.lastInternalId,
}); });
}, },
async testRun($: IGlobalVariable) { async testRun($: IGlobalVariable) {
return await getUserTweets($, { return await getUserTweets($, {
currentUser: false, currentUser: false,
userId: $.db.step.parameters.username as string, userId: $.step.parameters.username as string,
}); });
}, },
}; };

View File

@@ -29,7 +29,7 @@ const createAuthData = async (
.default; .default;
const app = await App.findOneByKey(connection.key); const app = await App.findOneByKey(connection.key);
const $ = await globalVariable(connection, app); const $ = await globalVariable({ connection, app });
await authInstance.createAuthData($); await authInstance.createAuthData($);
try { try {

View File

@@ -21,7 +21,7 @@ const verifyConnection = async (
.throwIfNotFound(); .throwIfNotFound();
const app = await App.findOneByKey(connection.key); const app = await App.findOneByKey(connection.key);
const $ = await globalVariable(connection, app); const $ = await globalVariable({ connection, app });
await app.auth.verifyCredentials($); await app.auth.verifyCredentials($);
connection = await connection.$query().patchAndFetch({ connection = await connection.$query().patchAndFetch({

View File

@@ -25,7 +25,7 @@ const getData = async (_parent: unknown, params: Params, context: Context) => {
if (!connection || !step.appKey) return null; if (!connection || !step.appKey) return null;
const app = await App.findOneByKey(step.appKey); const app = await App.findOneByKey(step.appKey);
const $ = await globalVariable(connection, app, step.flow, step); const $ = await globalVariable({ connection, app, flow: step.flow, step });
const command = app.data.find((data: IData) => data.key === params.key); const command = app.data.find((data: IData) => data.key === params.key);

View File

@@ -20,10 +20,9 @@ const testConnection = async (
.throwIfNotFound(); .throwIfNotFound();
const app = await App.findOneByKey(connection.key, false); const app = await App.findOneByKey(connection.key, false);
const $ = await globalVariable(connection, app); const $ = await globalVariable({ connection, app });
const isStillVerified = const isStillVerified = await app.auth.isStillVerified($);
await app.auth.isStillVerified($);
connection = await connection.$query().patchAndFetch({ connection = await connection.$query().patchAndFetch({
formattedData: connection.formattedData, formattedData: connection.formattedData,

View File

@@ -4,12 +4,18 @@ import Flow from '../models/flow';
import Step from '../models/step'; import Step from '../models/step';
import { IJSONObject, IApp, IGlobalVariable } from '@automatisch/types'; import { IJSONObject, IApp, IGlobalVariable } from '@automatisch/types';
type GlobalVariableOptions = {
connection?: Connection;
app: IApp;
flow?: Flow;
step?: Step;
};
const globalVariable = async ( const globalVariable = async (
connection: Connection, options: GlobalVariableOptions
appData: IApp,
flow?: Flow,
currentStep?: Step
): Promise<IGlobalVariable> => { ): Promise<IGlobalVariable> => {
const { connection, app, flow, step } = options;
const lastInternalId = await flow?.lastInternalId(); const lastInternalId = await flow?.lastInternalId();
return { return {
@@ -28,15 +34,13 @@ const globalVariable = async (
}, },
data: connection?.formattedData, data: connection?.formattedData,
}, },
app: appData, app: app,
http: createHttpClient({ baseURL: appData.baseUrl }), http: createHttpClient({ baseURL: app.baseUrl }),
db: { flow: {
flow: { lastInternalId,
lastInternalId, },
}, step: {
step: { parameters: step?.parameters || {},
parameters: currentStep?.parameters || {},
},
}, },
}; };
}; };

View File

@@ -1,5 +1,5 @@
import get from 'lodash.get'; import get from 'lodash.get';
import { IJSONObject } from '@automatisch/types'; import { IActionOutput } from '@automatisch/types';
import App from '../models/app'; import App from '../models/app';
import Flow from '../models/flow'; import Flow from '../models/flow';
@@ -61,8 +61,8 @@ class Processor {
if (initialTriggerData.data.length > 1) { if (initialTriggerData.data.length > 1) {
initialTriggerData.data = initialTriggerData.data.sort( initialTriggerData.data = initialTriggerData.data.sort(
(item: IJSONObject, nextItem: IJSONObject) => { (item, nextItem) => {
return (item.id as number) - (nextItem.id as number); return (item.raw.id as number) - (nextItem.raw.id as number);
} }
); );
} }
@@ -73,7 +73,7 @@ class Processor {
const execution = await Execution.query().insert({ const execution = await Execution.query().insert({
flowId: this.flow.id, flowId: this.flow.id,
testRun: this.testRun, testRun: this.testRun,
internalId: data.id as string, internalId: data.meta.internalId as string,
}); });
executions.push(execution); executions.push(execution);
@@ -81,12 +81,8 @@ class Processor {
let previousExecutionStep: ExecutionStep; let previousExecutionStep: ExecutionStep;
const priorExecutionSteps: ExecutionSteps = {}; const priorExecutionSteps: ExecutionSteps = {};
let fetchedActionData: { let fetchedActionData: IActionOutput = {
data: IJSONObject | null;
error: IJSONObject | null;
} = {
data: null, data: null,
error: null,
}; };
for await (const step of steps) { for await (const step of steps) {
@@ -105,12 +101,12 @@ class Processor {
const clonedStep = Object.assign({}, step); const clonedStep = Object.assign({}, step);
clonedStep.parameters = computedParameters; clonedStep.parameters = computedParameters;
const $ = await globalVariable( const $ = await globalVariable({
step.connection, connection: step.connection,
app, app,
this.flow, flow: this.flow,
clonedStep step: clonedStep,
); });
if (!isTrigger && key) { if (!isTrigger && key) {
const command = app.actions.find((action) => action.key === key); const command = app.actions.find((action) => action.key === key);
@@ -135,7 +131,7 @@ class Processor {
stepId: id, stepId: id,
status: 'success', status: 'success',
dataIn: isTrigger ? rawParameters : computedParameters, dataIn: isTrigger ? rawParameters : computedParameters,
dataOut: isTrigger ? data : fetchedActionData.data, dataOut: isTrigger ? data.raw : fetchedActionData.data.raw,
}); });
priorExecutionSteps[id] = previousExecutionStep; priorExecutionSteps[id] = previousExecutionStep;
@@ -176,12 +172,12 @@ class Processor {
if (!step.appKey || !step.key) return null; if (!step.appKey || !step.key) return null;
const app = await App.findOneByKey(step.appKey); const app = await App.findOneByKey(step.appKey);
const $ = await globalVariable( const $ = await globalVariable({
step.connection, connection: step.connection,
app, app,
this.flow, flow: this.flow,
step, step,
) });
const command = app.triggers.find((trigger) => trigger.key === step.key); const command = app.triggers.find((trigger) => trigger.key === step.key);

View File

@@ -172,9 +172,9 @@ export interface IData {
} }
export interface IAuth { export interface IAuth {
createAuthData($: IGlobalVariable): Promise<void>, createAuthData($: IGlobalVariable): Promise<void>;
verifyCredentials($: IGlobalVariable): Promise<any>, verifyCredentials($: IGlobalVariable): Promise<any>;
isStillVerified($: IGlobalVariable): Promise<boolean>, isStillVerified($: IGlobalVariable): Promise<boolean>;
fields: IField[]; fields: IField[];
authenticationSteps: IAuthenticationStep[]; authenticationSteps: IAuthenticationStep[];
reconnectionSteps: IAuthenticationStep[]; reconnectionSteps: IAuthenticationStep[];
@@ -187,23 +187,46 @@ export interface IService {
data?: any; data?: any;
} }
export interface ITriggerOutput {
data: ITriggerDataItem[];
error?: IJSONObject;
}
export interface ITriggerDataItem {
raw: IJSONObject;
meta: {
internalId: string;
};
}
export interface ITrigger { export interface ITrigger {
name: string; name: string;
key: string, key: string;
pollInterval: number; pollInterval: number;
description: string; description: string;
substeps: ISubstep[]; substeps: ISubstep[];
getInterval(parameters: IGlobalVariable["db"]["step"]["parameters"]): string; getInterval(parameters: IGlobalVariable['step']['parameters']): string;
run($: IGlobalVariable): Promise<{ data: IJSONObject[], error: IJSONObject | null }>; run($: IGlobalVariable): Promise<ITriggerOutput>;
testRun($: IGlobalVariable, startTime?: Date): Promise<{ data: IJSONObject[], error: IJSONObject | null }>; testRun($: IGlobalVariable): Promise<ITriggerOutput>;
}
export interface IActionOutput {
data: IActionDataItem;
error?: IJSONObject;
}
export interface IActionDataItem {
raw: {
data?: IJSONObject;
};
} }
export interface IAction { export interface IAction {
name: string; name: string;
key: string, key: string;
description: string; description: string;
substeps: ISubstep[]; substeps: ISubstep[];
run($: IGlobalVariable): Promise<{ data: IJSONObject, error: IJSONObject | null }>; run($: IGlobalVariable): Promise<IActionOutput>;
} }
export interface IAuthentication { export interface IAuthentication {
@@ -229,13 +252,11 @@ export type IGlobalVariable = {
}; };
app: IApp; app: IApp;
http: IHttpClient; http: IHttpClient;
db: { flow: {
flow: { lastInternalId: string;
lastInternalId: string; };
}; step: {
step: { parameters: IJSONObject;
parameters: IJSONObject;
}
}; };
}; };