refactor: use functional app implementation

This commit is contained in:
Ali BARIN
2022-10-06 23:27:37 +02:00
committed by Faruk AYDIN
parent 89d7359060
commit e7b47f5c98
9 changed files with 72 additions and 50 deletions

View File

@@ -1,6 +1,6 @@
import { Token } from 'oauth-1.0a';
import { IGlobalVariable, IJSONObject } from '@automatisch/types'; import { IGlobalVariable, IJSONObject } from '@automatisch/types';
import oauthClient from './oauth-client'; import oauthClient from './oauth-client';
import { Token } from 'oauth-1.0a';
type IGenereateRequestOptons = { type IGenereateRequestOptons = {
requestPath: string; requestPath: 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 $ = globalVariable(connection, app); const $ = await globalVariable(connection, app);
await authInstance.createAuthData($); await authInstance.createAuthData($);
try { try {

View File

@@ -21,11 +21,8 @@ const verifyConnection = async (
.throwIfNotFound(); .throwIfNotFound();
const app = await App.findOneByKey(connection.key); const app = await App.findOneByKey(connection.key);
const authInstance = (await import(`../../apps/${connection.key}2/auth`)) const $ = await globalVariable(connection, app);
.default; await app.auth.verifyCredentials($);
const $ = globalVariable(connection, app);
await authInstance.verifyCredentials($);
connection = await connection.$query().patchAndFetch({ connection = await connection.$query().patchAndFetch({
verified: true, verified: true,

View File

@@ -1,5 +1,7 @@
import { IJSONObject } from '@automatisch/types'; import { IJSONObject } from '@automatisch/types';
import Context from '../../types/express/context'; import Context from '../../types/express/context';
import App from '../../models/app';
import globalVariable from '../../helpers/global-variable';
type Params = { type Params = {
stepId: string; stepId: string;
@@ -22,11 +24,11 @@ const getData = async (_parent: unknown, params: Params, context: Context) => {
if (!connection || !step.appKey) return null; if (!connection || !step.appKey) return null;
const AppClass = (await import(`../../apps/${step.appKey}`)).default; const app = await App.findOneByKey(step.appKey);
const appInstance = new AppClass(connection, step.flow, step); const $ = await globalVariable(connection, app, step.flow, step)
const command = appInstance.data[params.key]; const command = app.data[params.key];
const fetchedData = await command.run(); const fetchedData = await command.run($);
return fetchedData; return fetchedData;
}; };

View File

@@ -1,4 +1,6 @@
import Context from '../../types/express/context'; import Context from '../../types/express/context';
import App from '../../models/app';
import globalVariable from '../../helpers/global-variable';
type Params = { type Params = {
id: string; id: string;
@@ -17,11 +19,11 @@ const testConnection = async (
}) })
.throwIfNotFound(); .throwIfNotFound();
const appClass = (await import(`../../apps/${connection.key}`)).default; const app = await App.findOneByKey(connection.key, false);
const appInstance = new appClass(connection); const $ = await globalVariable(connection, app);
const isStillVerified = const isStillVerified =
await appInstance.authenticationClient.isStillVerified(); await app.auth.isStillVerified($);
connection = await connection.$query().patchAndFetch({ connection = await connection.$query().patchAndFetch({
formattedData: connection.formattedData, formattedData: connection.formattedData,

View File

@@ -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 { try {
const rawTriggerData = await getDefaultExport(path); const fileContent = await getDefaultExport(path);
return stripFunctions(rawTriggerData); if (stripFuncs) {
return stripFunctions(fileContent);
}
return fileContent;
} catch (err) { } catch (err) {
return null; 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 appSubdirectory = join(appsPath, path);
const childrenContent = []; const childrenContent = [];
@@ -33,7 +37,7 @@ async function getChildrenContentInDirectory<C>(path: string): Promise<C[]> {
for (const filename of filesInSubdirectory) { for (const filename of filesInSubdirectory) {
const filePath = join(appSubdirectory, filename, 'index.ts'); const filePath = join(appSubdirectory, filename, 'index.ts');
const fileContent = await getStrippedFileContent<C>(filePath); const fileContent = await getFileContent<C>(filePath, stripFuncs);
childrenContent.push(fileContent); childrenContent.push(fileContent);
} }
@@ -44,12 +48,12 @@ async function getChildrenContentInDirectory<C>(path: string): Promise<C[]> {
return []; return [];
} }
const getApp = async (appKey: string) => { const getApp = async (appKey: string, stripFuncs = true) => {
const appData: IApp = await getDefaultExport(`../apps/${appKey}`); const appData: IApp = await getDefaultExport(`../apps/${appKey}`);
appData.auth = await getStrippedFileContent<IAuth>(`../apps/${appKey}/auth/index.ts`); appData.auth = await getFileContent<IAuth>(`../apps/${appKey}/auth/index.ts`, stripFuncs);
appData.triggers = await getChildrenContentInDirectory<ITrigger>(`${appKey}/triggers`); appData.triggers = await getChildrenContentInDirectory<ITrigger>(`${appKey}/triggers`, stripFuncs);
appData.actions = await getChildrenContentInDirectory<IAction>(`${appKey}/actions`); appData.actions = await getChildrenContentInDirectory<IAction>(`${appKey}/actions`, stripFuncs);
return appData; return appData;
}; };

View File

@@ -10,28 +10,38 @@ class App {
// Temporaryly restrict the apps we expose until // Temporaryly restrict the apps we expose until
// their actions/triggers are implemented! // 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) if (!name)
return Promise.all( 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( return Promise.all(
this.temporaryList this.temporaryList
.filter((app) => app.includes(name.toLowerCase())) .filter((app) => app.includes(name.toLowerCase()))
.map((name) => this.findOneByName(name)) .map((name) => this.findOneByName(name, stripFuncs))
); );
} }
static async findOneByName(name: string): Promise<IApp> { static async findOneByName(name: string, stripFuncs = false): Promise<IApp> {
const rawAppData = await getApp(name.toLocaleLowerCase()); const rawAppData = await getApp(name.toLocaleLowerCase(), stripFuncs);
if (!stripFuncs) return rawAppData;
return appInfoConverter(rawAppData); return appInfoConverter(rawAppData);
} }
static async findOneByKey(key: string): Promise<IApp> { static async findOneByKey(key: string, stripFuncs = false): Promise<IApp> {
const rawAppData = await getApp(key); const rawAppData = await getApp(key, stripFuncs);
if (!stripFuncs) return rawAppData;
return appInfoConverter(rawAppData); return appInfoConverter(rawAppData);
} }
} }

View File

@@ -97,12 +97,8 @@ class Step extends Base {
const { appKey, key } = this; const { appKey, key } = this;
const connection = await this.$relatedQuery('connection'); const app = await App.findOneByKey(appKey);
const flow = await this.$relatedQuery('flow'); const command = app.triggers.find((trigger) => trigger.key === key);
const AppClass = (await import(`../apps/${appKey}`)).default;
const appInstance = new AppClass(connection, flow, this);
const command = appInstance.triggers[key];
return command; return command;
} }

View File

@@ -1,9 +1,12 @@
import get from 'lodash.get'; import get from 'lodash.get';
import { IJSONObject } from '@automatisch/types';
import App from '../models/app';
import Flow from '../models/flow'; import Flow from '../models/flow';
import Step from '../models/step'; import Step from '../models/step';
import Execution from '../models/execution'; import Execution from '../models/execution';
import ExecutionStep from '../models/execution-step'; import ExecutionStep from '../models/execution-step';
import { IJSONObject } from '@automatisch/types'; import globalVariable from '../helpers/global-variable';
type ExecutionSteps = Record<string, ExecutionStep>; type ExecutionSteps = Record<string, ExecutionStep>;
@@ -70,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, internalId: data.id as string,
}); });
executions.push(execution); executions.push(execution);
@@ -92,7 +95,7 @@ class Processor {
const { appKey, key, type, parameters: rawParameters = {}, id } = step; const { appKey, key, type, parameters: rawParameters = {}, id } = step;
const isTrigger = type === 'trigger'; const isTrigger = type === 'trigger';
const AppClass = (await import(`../apps/${appKey}`)).default; const app = await App.findOneByKey(appKey);
const computedParameters = Processor.computeParameters( const computedParameters = Processor.computeParameters(
rawParameters, rawParameters,
@@ -101,11 +104,16 @@ class Processor {
step.parameters = computedParameters; step.parameters = computedParameters;
const appInstance = new AppClass(step.connection, this.flow, step); const $ = await globalVariable(
step.connection,
app,
this.flow,
step,
);
if (!isTrigger && key) { if (!isTrigger && key) {
const command = appInstance.actions[key]; const command = app.actions.find((action) => action.key === key);
fetchedActionData = await command.run(); fetchedActionData = await command.run($);
} }
if (!isTrigger && fetchedActionData.error) { if (!isTrigger && fetchedActionData.error) {
@@ -166,19 +174,22 @@ class Processor {
async getInitialTriggerData(step: Step) { async getInitialTriggerData(step: Step) {
if (!step.appKey || !step.key) return null; if (!step.appKey || !step.key) return null;
const AppClass = (await import(`../apps/${step.appKey}`)).default; const app = await App.findOneByKey(step.appKey);
const appInstance = new AppClass(step.connection, this.flow, step); 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; let fetchedData;
const lastInternalId = await this.flow.lastInternalId();
if (this.testRun) { if (this.testRun) {
fetchedData = await command.testRun(); fetchedData = await command.testRun($);
} else { } else {
fetchedData = await command.run(lastInternalId); fetchedData = await command.run($);
} }
return fetchedData; return fetchedData;