Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

2.2 KB
Raw Blame
import {
  AppConnectionValueForAuthProperty,
  PieceAuth,
  Property,
} from '@activepieces/pieces-framework';
import { getLists } from './api';
import * as z from 'zod/mini'
import { propsValidation } from '@activepieces/pieces-common';
import { AppConnectionType } from '@activepieces/pieces-framework';

export type SendyAuthType = AppConnectionValueForAuthProperty<typeof sendyAuth>;

const markdownDescription = `
Your sendy domain should be the base URL of your Sendy installation. Example: https://sendy.example.com

Follow these instructions to get your Sendy API Key:

1. Visit the Settings page of your Sendy domain: _https://sendy-domain.com_/settings
2. Once on the website, locate and click on the API Key and copy it.

Get your Brand ID from the main Brands page. The ID is the first column in the table.
`;

export const sendyAuth = PieceAuth.CustomAuth({
  description: markdownDescription,
  props: {
    domain: Property.ShortText({
      displayName: 'Sendy Domain',
      description: 'The domain of your Sendy account',
      required: true,
    }),
    apiKey: PieceAuth.SecretText({
      displayName: 'API Key',
      description: 'The API key for your Sendy account',
      required: true,
    }),
    brandId: Property.ShortText({
      displayName: 'Brand ID',
      description:
        'The brand ID that will be associated to this connection. Brand IDs can be found on the main Brands page.',
      required: true,
    }),
  },
  validate: async ({ auth }) => {
    try {
      await propsValidation.validateZod(auth, {
        domain: z.string().check(z.url()),
        apiKey: z.string().check(z.minLength(1), z.regex(/^\S+$/)),
        brandId: z.string().check(z.regex(/^[0-9]+$/)),
      });
      await validateAuth({
        type: AppConnectionType.CUSTOM_AUTH,
        props: auth,
      });
      return {
        valid: true,
      };
    } catch (e) {
      return {
        valid: false,
        error: (e as Error)?.message,
      };
    }
  },
  required: true,
});

const validateAuth = async (auth: SendyAuthType) => {
  const response = await getLists(auth);
  if (response.success !== true) {
    throw new Error(
      `Authentication failed. Please check your domain and API key and try again. ${response.text}`
    );
  }
};