diff --git a/packages/backend/src/apps/twitter/common/generate-request.ts b/packages/backend/src/apps/twitter/common/generate-request.ts index 76667636..c53c8a95 100644 --- a/packages/backend/src/apps/twitter/common/generate-request.ts +++ b/packages/backend/src/apps/twitter/common/generate-request.ts @@ -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; diff --git a/packages/backend/src/graphql/mutations/create-auth-data.ts b/packages/backend/src/graphql/mutations/create-auth-data.ts index c29444d3..8dac9f28 100644 --- a/packages/backend/src/graphql/mutations/create-auth-data.ts +++ b/packages/backend/src/graphql/mutations/create-auth-data.ts @@ -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 { diff --git a/packages/backend/src/graphql/mutations/verify-connection.ts b/packages/backend/src/graphql/mutations/verify-connection.ts index 8cc02c78..081752b9 100644 --- a/packages/backend/src/graphql/mutations/verify-connection.ts +++ b/packages/backend/src/graphql/mutations/verify-connection.ts @@ -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, diff --git a/packages/backend/src/graphql/queries/get-data.ts b/packages/backend/src/graphql/queries/get-data.ts index f4fd4959..d30acbe0 100644 --- a/packages/backend/src/graphql/queries/get-data.ts +++ b/packages/backend/src/graphql/queries/get-data.ts @@ -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; }; diff --git a/packages/backend/src/graphql/queries/test-connection.ts b/packages/backend/src/graphql/queries/test-connection.ts index 1b9a01f5..5aa90fd1 100644 --- a/packages/backend/src/graphql/queries/test-connection.ts +++ b/packages/backend/src/graphql/queries/test-connection.ts @@ -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, diff --git a/packages/backend/src/helpers/get-app.ts b/packages/backend/src/helpers/get-app.ts index 54a15357..38a72577 100644 --- a/packages/backend/src/helpers/get-app.ts +++ b/packages/backend/src/helpers/get-app.ts @@ -14,17 +14,21 @@ function stripFunctions(data: C): C { ); } -async function getStrippedFileContent(path: string): Promise { +async function getFileContent(path: string, stripFuncs: boolean): Promise { 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(path: string): Promise { +async function getChildrenContentInDirectory(path: string, stripFuncs: boolean): Promise { const appSubdirectory = join(appsPath, path); const childrenContent = []; @@ -33,7 +37,7 @@ async function getChildrenContentInDirectory(path: string): Promise { for (const filename of filesInSubdirectory) { const filePath = join(appSubdirectory, filename, 'index.ts'); - const fileContent = await getStrippedFileContent(filePath); + const fileContent = await getFileContent(filePath, stripFuncs); childrenContent.push(fileContent); } @@ -44,12 +48,12 @@ async function getChildrenContentInDirectory(path: string): Promise { return []; } -const getApp = async (appKey: string) => { +const getApp = async (appKey: string, stripFuncs = true) => { const appData: IApp = await getDefaultExport(`../apps/${appKey}`); - appData.auth = await getStrippedFileContent(`../apps/${appKey}/auth/index.ts`); - appData.triggers = await getChildrenContentInDirectory(`${appKey}/triggers`); - appData.actions = await getChildrenContentInDirectory(`${appKey}/actions`); + appData.auth = await getFileContent(`../apps/${appKey}/auth/index.ts`, stripFuncs); + appData.triggers = await getChildrenContentInDirectory(`${appKey}/triggers`, stripFuncs); + appData.actions = await getChildrenContentInDirectory(`${appKey}/actions`, stripFuncs); return appData; }; diff --git a/packages/backend/src/models/app.ts b/packages/backend/src/models/app.ts index 948cdfac..7101e092 100644 --- a/packages/backend/src/models/app.ts +++ b/packages/backend/src/models/app.ts @@ -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 { + static async findAll(name?: string, stripFuncs = true): Promise { 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 { - const rawAppData = await getApp(name.toLocaleLowerCase()); + static async findOneByName(name: string, stripFuncs = false): Promise { + const rawAppData = await getApp(name.toLocaleLowerCase(), stripFuncs); + + if (!stripFuncs) return rawAppData; + return appInfoConverter(rawAppData); } - static async findOneByKey(key: string): Promise { - const rawAppData = await getApp(key); + static async findOneByKey(key: string, stripFuncs = false): Promise { + const rawAppData = await getApp(key, stripFuncs); + + if (!stripFuncs) return rawAppData; + return appInfoConverter(rawAppData); } } diff --git a/packages/backend/src/models/step.ts b/packages/backend/src/models/step.ts index 7f121bb0..b69f3d20 100644 --- a/packages/backend/src/models/step.ts +++ b/packages/backend/src/models/step.ts @@ -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; } diff --git a/packages/backend/src/services/processor.ts b/packages/backend/src/services/processor.ts index 6d2efeca..224fcceb 100644 --- a/packages/backend/src/services/processor.ts +++ b/packages/backend/src/services/processor.ts @@ -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; @@ -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;