Merge pull request #616 from automatisch/add-define-app-and-before-request

feat: add defineApp and beforeRequest
This commit is contained in:
Ömer Faruk Aydın
2022-10-18 00:34:56 +02:00
committed by GitHub
7 changed files with 60 additions and 21 deletions

View File

@@ -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;

View File

@@ -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],
});

View File

@@ -0,0 +1,5 @@
import { IApp } from '@automatisch/types';
export default function defineApp(appDefinition: IApp): IApp {
return appDefinition;
}

View File

@@ -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;

View File

@@ -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) => {