feat(mailchimp): add mailchimp integration
This commit is contained in:
1
packages/backend/src/apps/mailchimp/assets/favicon.svg
Normal file
1
packages/backend/src/apps/mailchimp/assets/favicon.svg
Normal file
File diff suppressed because one or more lines are too long
After Width: | Height: | Size: 5.0 KiB |
@@ -0,0 +1,19 @@
|
||||
import { URLSearchParams } from 'url';
|
||||
|
||||
export default async function generateAuthUrl($) {
|
||||
const oauthRedirectUrlField = $.app.auth.fields.find(
|
||||
(field) => field.key == 'oAuthRedirectUrl'
|
||||
);
|
||||
const redirectUri = oauthRedirectUrlField.value;
|
||||
const searchParams = new URLSearchParams({
|
||||
response_type: 'code',
|
||||
client_id: $.auth.data.clientId,
|
||||
redirect_uri: redirectUri,
|
||||
});
|
||||
|
||||
const url = `https://login.mailchimp.com/oauth2/authorize?${searchParams.toString()}`;
|
||||
|
||||
await $.auth.set({
|
||||
url,
|
||||
});
|
||||
}
|
46
packages/backend/src/apps/mailchimp/auth/index.js
Normal file
46
packages/backend/src/apps/mailchimp/auth/index.js
Normal file
@@ -0,0 +1,46 @@
|
||||
import generateAuthUrl from './generate-auth-url.js';
|
||||
import verifyCredentials from './verify-credentials.js';
|
||||
import isStillVerified from './is-still-verified.js';
|
||||
|
||||
export default {
|
||||
fields: [
|
||||
{
|
||||
key: 'oAuthRedirectUrl',
|
||||
label: 'OAuth Redirect URL',
|
||||
type: 'string',
|
||||
required: true,
|
||||
readOnly: true,
|
||||
value: '{WEB_APP_URL}/app/mailchimp/connections/add',
|
||||
placeholder: null,
|
||||
description:
|
||||
'When asked to input a redirect URL in Mailchimp, enter the URL above.',
|
||||
clickToCopy: true,
|
||||
},
|
||||
{
|
||||
key: 'clientId',
|
||||
label: 'Client ID',
|
||||
type: 'string',
|
||||
required: true,
|
||||
readOnly: false,
|
||||
value: null,
|
||||
placeholder: null,
|
||||
description: null,
|
||||
clickToCopy: false,
|
||||
},
|
||||
{
|
||||
key: 'clientSecret',
|
||||
label: 'Client Secret',
|
||||
type: 'string',
|
||||
required: true,
|
||||
readOnly: false,
|
||||
value: null,
|
||||
placeholder: null,
|
||||
description: null,
|
||||
clickToCopy: false,
|
||||
},
|
||||
],
|
||||
|
||||
generateAuthUrl,
|
||||
verifyCredentials,
|
||||
isStillVerified,
|
||||
};
|
@@ -0,0 +1,8 @@
|
||||
import getCurrentUser from '../common/get-current-user.js';
|
||||
|
||||
const isStillVerified = async ($) => {
|
||||
const currentUser = await getCurrentUser($);
|
||||
return !!currentUser.user_id;
|
||||
};
|
||||
|
||||
export default isStillVerified;
|
@@ -0,0 +1,40 @@
|
||||
import getCurrentUser from '../common/get-current-user.js';
|
||||
|
||||
const verifyCredentials = async ($) => {
|
||||
const oauthRedirectUrlField = $.app.auth.fields.find(
|
||||
(field) => field.key == 'oAuthRedirectUrl'
|
||||
);
|
||||
const redirectUri = oauthRedirectUrlField.value;
|
||||
const params = new URLSearchParams({
|
||||
grant_type: 'authorization_code',
|
||||
client_id: $.auth.data.clientId,
|
||||
client_secret: $.auth.data.clientSecret,
|
||||
redirect_uri: redirectUri,
|
||||
code: $.auth.data.code,
|
||||
});
|
||||
|
||||
const { data } = await $.http.post(
|
||||
'https://login.mailchimp.com/oauth2/token',
|
||||
params.toString()
|
||||
);
|
||||
|
||||
await $.auth.set({
|
||||
accessToken: data.access_token,
|
||||
tokenType: data.token_type,
|
||||
});
|
||||
|
||||
const currentUser = await getCurrentUser($);
|
||||
|
||||
await $.auth.set({
|
||||
clientId: $.auth.data.clientId,
|
||||
clientSecret: $.auth.data.clientSecret,
|
||||
scope: $.auth.data.scope,
|
||||
idToken: data.id_token,
|
||||
expiresIn: data.expires_in,
|
||||
refreshToken: data.refresh_token,
|
||||
serverPrefix: currentUser.dc,
|
||||
screenName: currentUser.login.login_name,
|
||||
});
|
||||
};
|
||||
|
||||
export default verifyCredentials;
|
@@ -0,0 +1,12 @@
|
||||
const addAuthHeader = ($, requestConfig) => {
|
||||
if (
|
||||
!requestConfig.additionalProperties?.skipAddingAuthHeader &&
|
||||
$.auth.data?.accessToken
|
||||
) {
|
||||
requestConfig.headers.Authorization = `Bearer ${$.auth.data.accessToken}`;
|
||||
}
|
||||
|
||||
return requestConfig;
|
||||
};
|
||||
|
||||
export default addAuthHeader;
|
@@ -0,0 +1,18 @@
|
||||
const getCurrentUser = async ($) => {
|
||||
const { data: currentUser } = await $.http.get(
|
||||
'https://login.mailchimp.com/oauth2/metadata',
|
||||
{
|
||||
headers: {
|
||||
Authorization: `OAuth ${$.auth.data.accessToken}`,
|
||||
},
|
||||
additionalProperties: {
|
||||
skipAddingAuthHeader: true,
|
||||
skipAddingBaseUrl: true,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
return currentUser;
|
||||
};
|
||||
|
||||
export default getCurrentUser;
|
10
packages/backend/src/apps/mailchimp/common/set-base-url.js
Normal file
10
packages/backend/src/apps/mailchimp/common/set-base-url.js
Normal file
@@ -0,0 +1,10 @@
|
||||
const setBaseUrl = ($, requestConfig) => {
|
||||
const serverPrefix = $.auth.data.serverPrefix;
|
||||
if (!requestConfig.additionalProperties?.skipAddingBaseUrl && serverPrefix) {
|
||||
requestConfig.baseURL = `https://${serverPrefix}.api.mailchimp.com`;
|
||||
}
|
||||
|
||||
return requestConfig;
|
||||
};
|
||||
|
||||
export default setBaseUrl;
|
17
packages/backend/src/apps/mailchimp/index.js
Normal file
17
packages/backend/src/apps/mailchimp/index.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import defineApp from '../../helpers/define-app.js';
|
||||
import addAuthHeader from './common/add-auth-header.js';
|
||||
import setBaseUrl from './common/set-base-url.js';
|
||||
import auth from './auth/index.js';
|
||||
|
||||
export default defineApp({
|
||||
name: 'Mailchimp',
|
||||
key: 'mailchimp',
|
||||
baseUrl: 'https://mailchimp.com',
|
||||
apiBaseUrl: '',
|
||||
iconUrl: '{BASE_URL}/apps/mailchimp/assets/favicon.svg',
|
||||
authDocUrl: 'https://automatisch.io/docs/apps/mailchimp/connection',
|
||||
primaryColor: '000000',
|
||||
supportsConnections: true,
|
||||
beforeRequest: [setBaseUrl, addAuthHeader],
|
||||
auth,
|
||||
});
|
@@ -261,6 +261,12 @@ export default defineConfig({
|
||||
{ text: 'Connection', link: '/apps/mailerlite/connection' },
|
||||
],
|
||||
},
|
||||
{
|
||||
text: 'Mailchimp',
|
||||
collapsible: true,
|
||||
collapsed: true,
|
||||
items: [{ text: 'Connection', link: '/apps/mailchimp/connection' }],
|
||||
},
|
||||
{
|
||||
text: 'Mattermost',
|
||||
collapsible: true,
|
||||
|
17
packages/docs/pages/apps/mailchimp/connection.md
Normal file
17
packages/docs/pages/apps/mailchimp/connection.md
Normal file
@@ -0,0 +1,17 @@
|
||||
# Mailchimp
|
||||
|
||||
:::info
|
||||
This page explains the steps you need to follow to set up the Mailchimp
|
||||
connection in Automatisch. If any of the steps are outdated, please let us know!
|
||||
:::
|
||||
|
||||
1. Login to your [Mailchimp account](https://mailchimp.com/) to create an app.
|
||||
2. Click on the account image and go to your **profile** page.
|
||||
3. Click on the **Extras** tab and choose the **Registered apps**.
|
||||
4. Click on the **Register An App** button.
|
||||
5. Fill the registration form.
|
||||
6. Copy **OAuth Redirect URL** from Automatisch to **Redirect URI** field, and click on the **Create** button.
|
||||
7. Copy the **Your Client ID** value to the `Client ID` field on Automatisch.
|
||||
8. Copy the **Your Client Secret** value to the `Client Secret` field on Automatisch.
|
||||
9. Click **Submit** button on Automatisch.
|
||||
10. Congrats! Start using your new Mailchimp connection within the flows.
|
1
packages/docs/pages/public/favicons/mailchimp.svg
Normal file
1
packages/docs/pages/public/favicons/mailchimp.svg
Normal file
File diff suppressed because one or more lines are too long
After Width: | Height: | Size: 5.0 KiB |
Reference in New Issue
Block a user