From 1760c6e454eda02e276749b3c07901b4d25c1f7d Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Mon, 17 Oct 2022 23:29:20 +0200 Subject: [PATCH] feat: add defineApp and beforeRequest --- .../src/apps/github/common/add-auth-header.ts | 11 ++++++++ packages/backend/src/apps/github/index.ts | 8 ++++-- packages/backend/src/helpers/define-app.ts | 5 ++++ .../backend/src/helpers/global-variable.ts | 13 ++++++--- .../backend/src/helpers/http-client/index.ts | 10 +++++-- packages/types/index.d.ts | 27 ++++++++++++------- .../src/components/AddAppConnection/index.tsx | 7 ++--- 7 files changed, 60 insertions(+), 21 deletions(-) create mode 100644 packages/backend/src/apps/github/common/add-auth-header.ts create mode 100644 packages/backend/src/helpers/define-app.ts diff --git a/packages/backend/src/apps/github/common/add-auth-header.ts b/packages/backend/src/apps/github/common/add-auth-header.ts new file mode 100644 index 00000000..ab286783 --- /dev/null +++ b/packages/backend/src/apps/github/common/add-auth-header.ts @@ -0,0 +1,11 @@ +import { TBeforeRequest } from "@automatisch/types"; + +const addAuthHeader: TBeforeRequest = ($, requestConfig) => { + if (requestConfig.headers && $.auth.data?.accessToken) { + requestConfig.headers.Authorization = `Bearer ${$.auth.data.accessToken}` + } + + return requestConfig; +} + +export default addAuthHeader; diff --git a/packages/backend/src/apps/github/index.ts b/packages/backend/src/apps/github/index.ts index 0e4bbf48..7cf3b2c5 100644 --- a/packages/backend/src/apps/github/index.ts +++ b/packages/backend/src/apps/github/index.ts @@ -1,4 +1,7 @@ -export default { +import defineApp from '../../helpers/define-app'; +import addAuthHeader from './common/add-auth-header'; + +export default defineApp({ name: 'Github', key: 'github', baseUrl: 'https://github.com', @@ -7,4 +10,5 @@ export default { authDocUrl: 'https://automatisch.io/docs/connections/github', primaryColor: '000000', supportsConnections: true, -}; + beforeRequest: [addAuthHeader], +}); diff --git a/packages/backend/src/helpers/define-app.ts b/packages/backend/src/helpers/define-app.ts new file mode 100644 index 00000000..a8ca6b44 --- /dev/null +++ b/packages/backend/src/helpers/define-app.ts @@ -0,0 +1,5 @@ +import { IApp } from '@automatisch/types'; + +export default function defineApp(appDefinition: IApp): IApp { + return appDefinition; +} diff --git a/packages/backend/src/helpers/global-variable.ts b/packages/backend/src/helpers/global-variable.ts index 7d93644c..e2c08e74 100644 --- a/packages/backend/src/helpers/global-variable.ts +++ b/packages/backend/src/helpers/global-variable.ts @@ -24,7 +24,7 @@ const globalVariable = async ( const trigger = await step?.getTriggerCommand(); const nextStep = await step?.getNextStep(); - const variable: IGlobalVariable = { + const $: IGlobalVariable = { auth: { set: async (args: IJSONObject) => { if (connection) { @@ -41,7 +41,6 @@ const globalVariable = async ( data: connection?.formattedData, }, app: app, - http: createHttpClient({ baseURL: app.apiBaseUrl }), flow: { id: flow?.id, lastInternalId, @@ -62,6 +61,12 @@ const globalVariable = async ( }, }; + $.http = createHttpClient({ + $, + baseURL: app.apiBaseUrl, + beforeRequest: app.beforeRequest, + }); + if (trigger && trigger.dedupeStrategy === 'unique') { const lastInternalIds = await flow?.lastInternalIds(); @@ -69,10 +74,10 @@ const globalVariable = async ( return lastInternalIds?.includes(internalId); }; - variable.flow.isAlreadyProcessed = isAlreadyProcessed; + $.flow.isAlreadyProcessed = isAlreadyProcessed; } - return variable; + return $; }; export default globalVariable; diff --git a/packages/backend/src/helpers/http-client/index.ts b/packages/backend/src/helpers/http-client/index.ts index 4e3d2539..2924383f 100644 --- a/packages/backend/src/helpers/http-client/index.ts +++ b/packages/backend/src/helpers/http-client/index.ts @@ -1,12 +1,18 @@ -import axios from 'axios'; +import axios, { AxiosRequestConfig } from 'axios'; export { AxiosInstance as IHttpClient } from 'axios'; import { IHttpClientParams } from '@automatisch/types'; -export default function createHttpClient({ baseURL }: IHttpClientParams) { +export default function createHttpClient({ $, baseURL, beforeRequest = [] }: IHttpClientParams) { const instance = axios.create({ baseURL, }); + instance.interceptors.request.use((requestConfig: AxiosRequestConfig): AxiosRequestConfig => { + return beforeRequest.reduce((newConfig, beforeRequestFunc) => { + return beforeRequestFunc($, newConfig); + }, requestConfig); + }); + instance.interceptors.response.use( (response) => response, (error) => { diff --git a/packages/types/index.d.ts b/packages/types/index.d.ts index 4c771e6d..487557dc 100644 --- a/packages/types/index.d.ts +++ b/packages/types/index.d.ts @@ -1,4 +1,4 @@ -import type { AxiosInstance } from 'axios'; +import type { AxiosInstance, AxiosRequestConfig } from 'axios'; export type IHttpClient = AxiosInstance; // Type definitions for automatisch @@ -153,19 +153,24 @@ export interface IApp { name: string; key: string; iconUrl: string; - docUrl: string; + docUrl?: string; authDocUrl: string; primaryColor: string; supportsConnections: boolean; apiBaseUrl: string; baseUrl: string; - auth: IAuth; - connectionCount: number; - flowCount: number; - data: IData; - triggers: ITrigger[]; - actions: IAction[]; - connections: IConnection[]; + auth?: IAuth; + connectionCount?: number; + flowCount?: number; + beforeRequest: TBeforeRequest[]; + data?: IData; + triggers?: ITrigger[]; + actions?: IAction[]; + connections?: IConnection[]; +} + +export type TBeforeRequest = { + ($: IGlobalVariable, requestConfig: AxiosRequestConfig): AxiosRequestConfig; } export interface IData { @@ -243,7 +248,9 @@ export interface ISubstep { } export type IHttpClientParams = { + $: IGlobalVariable; baseURL?: string; + beforeRequest?: TBeforeRequest[]; }; export type IGlobalVariable = { @@ -252,7 +259,7 @@ export type IGlobalVariable = { data: IJSONObject; }; app: IApp; - http: IHttpClient; + http?: IHttpClient; flow?: { id: string; lastInternalId: string; diff --git a/packages/web/src/components/AddAppConnection/index.tsx b/packages/web/src/components/AddAppConnection/index.tsx index 726d555e..4f4f9bf7 100644 --- a/packages/web/src/components/AddAppConnection/index.tsx +++ b/packages/web/src/components/AddAppConnection/index.tsx @@ -31,12 +31,11 @@ type Response = { export default function AddAppConnection(props: AddAppConnectionProps): React.ReactElement { const { application, connectionId, onClose } = props; const { name, authDocUrl, key, auth } = application; - const { fields, authenticationSteps, reconnectionSteps } = auth; const formatMessage = useFormatMessage(); const [errorMessage, setErrorMessage] = React.useState(null); const [inProgress, setInProgress] = React.useState(false); const hasConnection = Boolean(connectionId); - const steps = hasConnection ? reconnectionSteps : authenticationSteps; + const steps = hasConnection ? auth?.reconnectionSteps : auth?.authenticationSteps; React.useEffect(() => { if (window.opener) { @@ -46,6 +45,8 @@ export default function AddAppConnection(props: AddAppConnectionProps): React.Re }, []); const submitHandler: SubmitHandler = React.useCallback(async (data) => { + if (!steps) return; + setInProgress(true); setErrorMessage(null); @@ -106,7 +107,7 @@ export default function AddAppConnection(props: AddAppConnectionProps): React.Re
- {fields?.map((field: IField) => ())} + {auth?.fields?.map((field: IField) => ())}