refactor: use functional app implementation
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { Token } from 'oauth-1.0a';
|
||||
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
|
||||
import oauthClient from './oauth-client';
|
||||
import { Token } from 'oauth-1.0a';
|
||||
|
||||
type IGenereateRequestOptons = {
|
||||
requestPath: string;
|
||||
|
@@ -29,7 +29,7 @@ const createAuthData = async (
|
||||
.default;
|
||||
const app = await App.findOneByKey(connection.key);
|
||||
|
||||
const $ = globalVariable(connection, app);
|
||||
const $ = await globalVariable(connection, app);
|
||||
await authInstance.createAuthData($);
|
||||
|
||||
try {
|
||||
|
@@ -21,11 +21,8 @@ const verifyConnection = async (
|
||||
.throwIfNotFound();
|
||||
|
||||
const app = await App.findOneByKey(connection.key);
|
||||
const authInstance = (await import(`../../apps/${connection.key}2/auth`))
|
||||
.default;
|
||||
|
||||
const $ = globalVariable(connection, app);
|
||||
await authInstance.verifyCredentials($);
|
||||
const $ = await globalVariable(connection, app);
|
||||
await app.auth.verifyCredentials($);
|
||||
|
||||
connection = await connection.$query().patchAndFetch({
|
||||
verified: true,
|
||||
|
@@ -1,5 +1,7 @@
|
||||
import { IJSONObject } from '@automatisch/types';
|
||||
import Context from '../../types/express/context';
|
||||
import App from '../../models/app';
|
||||
import globalVariable from '../../helpers/global-variable';
|
||||
|
||||
type Params = {
|
||||
stepId: string;
|
||||
@@ -22,11 +24,11 @@ const getData = async (_parent: unknown, params: Params, context: Context) => {
|
||||
|
||||
if (!connection || !step.appKey) return null;
|
||||
|
||||
const AppClass = (await import(`../../apps/${step.appKey}`)).default;
|
||||
const appInstance = new AppClass(connection, step.flow, step);
|
||||
const app = await App.findOneByKey(step.appKey);
|
||||
const $ = await globalVariable(connection, app, step.flow, step)
|
||||
|
||||
const command = appInstance.data[params.key];
|
||||
const fetchedData = await command.run();
|
||||
const command = app.data[params.key];
|
||||
const fetchedData = await command.run($);
|
||||
|
||||
return fetchedData;
|
||||
};
|
||||
|
@@ -1,4 +1,6 @@
|
||||
import Context from '../../types/express/context';
|
||||
import App from '../../models/app';
|
||||
import globalVariable from '../../helpers/global-variable';
|
||||
|
||||
type Params = {
|
||||
id: string;
|
||||
@@ -17,11 +19,11 @@ const testConnection = async (
|
||||
})
|
||||
.throwIfNotFound();
|
||||
|
||||
const appClass = (await import(`../../apps/${connection.key}`)).default;
|
||||
const appInstance = new appClass(connection);
|
||||
const app = await App.findOneByKey(connection.key, false);
|
||||
const $ = await globalVariable(connection, app);
|
||||
|
||||
const isStillVerified =
|
||||
await appInstance.authenticationClient.isStillVerified();
|
||||
await app.auth.isStillVerified($);
|
||||
|
||||
connection = await connection.$query().patchAndFetch({
|
||||
formattedData: connection.formattedData,
|
||||
|
@@ -14,17 +14,21 @@ function stripFunctions<C>(data: C): C {
|
||||
);
|
||||
}
|
||||
|
||||
async function getStrippedFileContent<C>(path: string): Promise<C> {
|
||||
async function getFileContent<C>(path: string, stripFuncs: boolean): Promise<C> {
|
||||
try {
|
||||
const rawTriggerData = await getDefaultExport(path);
|
||||
const fileContent = await getDefaultExport(path);
|
||||
|
||||
return stripFunctions(rawTriggerData);
|
||||
if (stripFuncs) {
|
||||
return stripFunctions(fileContent);
|
||||
}
|
||||
|
||||
return fileContent;
|
||||
} catch (err) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function getChildrenContentInDirectory<C>(path: string): Promise<C[]> {
|
||||
async function getChildrenContentInDirectory<C>(path: string, stripFuncs: boolean): Promise<C[]> {
|
||||
const appSubdirectory = join(appsPath, path);
|
||||
const childrenContent = [];
|
||||
|
||||
@@ -33,7 +37,7 @@ async function getChildrenContentInDirectory<C>(path: string): Promise<C[]> {
|
||||
|
||||
for (const filename of filesInSubdirectory) {
|
||||
const filePath = join(appSubdirectory, filename, 'index.ts');
|
||||
const fileContent = await getStrippedFileContent<C>(filePath);
|
||||
const fileContent = await getFileContent<C>(filePath, stripFuncs);
|
||||
|
||||
childrenContent.push(fileContent);
|
||||
}
|
||||
@@ -44,12 +48,12 @@ async function getChildrenContentInDirectory<C>(path: string): Promise<C[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
const getApp = async (appKey: string) => {
|
||||
const getApp = async (appKey: string, stripFuncs = true) => {
|
||||
const appData: IApp = await getDefaultExport(`../apps/${appKey}`);
|
||||
|
||||
appData.auth = await getStrippedFileContent<IAuth>(`../apps/${appKey}/auth/index.ts`);
|
||||
appData.triggers = await getChildrenContentInDirectory<ITrigger>(`${appKey}/triggers`);
|
||||
appData.actions = await getChildrenContentInDirectory<IAction>(`${appKey}/actions`);
|
||||
appData.auth = await getFileContent<IAuth>(`../apps/${appKey}/auth/index.ts`, stripFuncs);
|
||||
appData.triggers = await getChildrenContentInDirectory<ITrigger>(`${appKey}/triggers`, stripFuncs);
|
||||
appData.actions = await getChildrenContentInDirectory<IAction>(`${appKey}/actions`, stripFuncs);
|
||||
|
||||
return appData;
|
||||
};
|
||||
|
@@ -10,28 +10,38 @@ class App {
|
||||
|
||||
// Temporaryly restrict the apps we expose until
|
||||
// their actions/triggers are implemented!
|
||||
static temporaryList = ['slack', 'twitter'];
|
||||
static temporaryList = [
|
||||
'slack',
|
||||
'twitter',
|
||||
'scheduler'
|
||||
];
|
||||
|
||||
static async findAll(name?: string): Promise<IApp[]> {
|
||||
static async findAll(name?: string, stripFuncs = true): Promise<IApp[]> {
|
||||
if (!name)
|
||||
return Promise.all(
|
||||
this.temporaryList.map(async (name) => await this.findOneByName(name))
|
||||
this.temporaryList.map(async (name) => await this.findOneByName(name, stripFuncs))
|
||||
);
|
||||
|
||||
return Promise.all(
|
||||
this.temporaryList
|
||||
.filter((app) => app.includes(name.toLowerCase()))
|
||||
.map((name) => this.findOneByName(name))
|
||||
.map((name) => this.findOneByName(name, stripFuncs))
|
||||
);
|
||||
}
|
||||
|
||||
static async findOneByName(name: string): Promise<IApp> {
|
||||
const rawAppData = await getApp(name.toLocaleLowerCase());
|
||||
static async findOneByName(name: string, stripFuncs = false): Promise<IApp> {
|
||||
const rawAppData = await getApp(name.toLocaleLowerCase(), stripFuncs);
|
||||
|
||||
if (!stripFuncs) return rawAppData;
|
||||
|
||||
return appInfoConverter(rawAppData);
|
||||
}
|
||||
|
||||
static async findOneByKey(key: string): Promise<IApp> {
|
||||
const rawAppData = await getApp(key);
|
||||
static async findOneByKey(key: string, stripFuncs = false): Promise<IApp> {
|
||||
const rawAppData = await getApp(key, stripFuncs);
|
||||
|
||||
if (!stripFuncs) return rawAppData;
|
||||
|
||||
return appInfoConverter(rawAppData);
|
||||
}
|
||||
}
|
||||
|
@@ -97,12 +97,8 @@ class Step extends Base {
|
||||
|
||||
const { appKey, key } = this;
|
||||
|
||||
const connection = await this.$relatedQuery('connection');
|
||||
const flow = await this.$relatedQuery('flow');
|
||||
|
||||
const AppClass = (await import(`../apps/${appKey}`)).default;
|
||||
const appInstance = new AppClass(connection, flow, this);
|
||||
const command = appInstance.triggers[key];
|
||||
const app = await App.findOneByKey(appKey);
|
||||
const command = app.triggers.find((trigger) => trigger.key === key);
|
||||
|
||||
return command;
|
||||
}
|
||||
|
@@ -1,9 +1,12 @@
|
||||
import get from 'lodash.get';
|
||||
import { IJSONObject } from '@automatisch/types';
|
||||
|
||||
import App from '../models/app';
|
||||
import Flow from '../models/flow';
|
||||
import Step from '../models/step';
|
||||
import Execution from '../models/execution';
|
||||
import ExecutionStep from '../models/execution-step';
|
||||
import { IJSONObject } from '@automatisch/types';
|
||||
import globalVariable from '../helpers/global-variable';
|
||||
|
||||
type ExecutionSteps = Record<string, ExecutionStep>;
|
||||
|
||||
@@ -70,7 +73,7 @@ class Processor {
|
||||
const execution = await Execution.query().insert({
|
||||
flowId: this.flow.id,
|
||||
testRun: this.testRun,
|
||||
internalId: data.id,
|
||||
internalId: data.id as string,
|
||||
});
|
||||
|
||||
executions.push(execution);
|
||||
@@ -92,7 +95,7 @@ class Processor {
|
||||
const { appKey, key, type, parameters: rawParameters = {}, id } = step;
|
||||
|
||||
const isTrigger = type === 'trigger';
|
||||
const AppClass = (await import(`../apps/${appKey}`)).default;
|
||||
const app = await App.findOneByKey(appKey);
|
||||
|
||||
const computedParameters = Processor.computeParameters(
|
||||
rawParameters,
|
||||
@@ -101,11 +104,16 @@ class Processor {
|
||||
|
||||
step.parameters = computedParameters;
|
||||
|
||||
const appInstance = new AppClass(step.connection, this.flow, step);
|
||||
const $ = await globalVariable(
|
||||
step.connection,
|
||||
app,
|
||||
this.flow,
|
||||
step,
|
||||
);
|
||||
|
||||
if (!isTrigger && key) {
|
||||
const command = appInstance.actions[key];
|
||||
fetchedActionData = await command.run();
|
||||
const command = app.actions.find((action) => action.key === key);
|
||||
fetchedActionData = await command.run($);
|
||||
}
|
||||
|
||||
if (!isTrigger && fetchedActionData.error) {
|
||||
@@ -166,19 +174,22 @@ class Processor {
|
||||
async getInitialTriggerData(step: Step) {
|
||||
if (!step.appKey || !step.key) return null;
|
||||
|
||||
const AppClass = (await import(`../apps/${step.appKey}`)).default;
|
||||
const appInstance = new AppClass(step.connection, this.flow, step);
|
||||
const app = await App.findOneByKey(step.appKey);
|
||||
const $ = await globalVariable(
|
||||
step.connection,
|
||||
app,
|
||||
this.flow,
|
||||
step,
|
||||
)
|
||||
|
||||
const command = appInstance.triggers[step.key];
|
||||
const command = app.triggers.find((trigger) => trigger.key === step.key);
|
||||
|
||||
let fetchedData;
|
||||
|
||||
const lastInternalId = await this.flow.lastInternalId();
|
||||
|
||||
if (this.testRun) {
|
||||
fetchedData = await command.testRun();
|
||||
fetchedData = await command.testRun($);
|
||||
} else {
|
||||
fetchedData = await command.run(lastInternalId);
|
||||
fetchedData = await command.run($);
|
||||
}
|
||||
|
||||
return fetchedData;
|
||||
|
Reference in New Issue
Block a user