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

1.9 KB
Raw Blame
import {
	HttpMessageBody,
	HttpMethod,
	QueryParams,
	httpClient,
} from '@activepieces/pieces-common';
import { isNil } from '@activepieces/pieces-framework';

async function apiCall<T extends HttpMessageBody>({
	apiKey,
	method,
	resourceUri,
	query,
	body,
}: BrevoApiCallParams): Promise<T> {
	const queryParams: QueryParams = {};
	if (query) {
		for (const [key, value] of Object.entries(query)) {
			if (!isNil(value)) {
				queryParams[key] = String(value);
			}
		}
	}

	const response = await httpClient.sendRequest<T>({
		method,
		url: `${BREVO_API_URL}${resourceUri}`,
		headers: {
			'api-key': apiKey,
			accept: 'application/json',
		},
		queryParams,
		body: compactBody(body),
	});

	return response.body;
}

function compactBody(body: unknown): unknown {
	if (!isPlainObject(body)) {
		return body;
	}
	return Object.fromEntries(
		Object.entries(body).filter(([, value]) => !isNil(value)),
	);
}

function isPlainObject(value: unknown): value is Record<string, unknown> {
	return typeof value === 'object' && value !== null && !Array.isArray(value);
}

function isEmptyObject(value: Record<string, unknown> | undefined): boolean {
	return isNil(value) || Object.keys(value).length === 0;
}

function optionalBooleanValue(value: unknown): boolean | undefined {
	const normalized = typeof value === 'string' ? value.trim().toLowerCase() : value;
	if (normalized === true || normalized === 'yes' || normalized === 'true') {
		return true;
	}
	if (normalized === false || normalized === 'no' || normalized === 'false') {
		return false;
	}
	return undefined;
}

export const brevoCommon = { apiCall, isEmptyObject, optionalBooleanValue };

export const BREVO_API_URL = 'https://api.brevo.com/v3';

export type BrevoApiCallParams = {
	apiKey: string;
	method: HttpMethod;
	resourceUri: string;
	query?: Record<string, string | number | boolean | undefined | null>;
	body?: unknown;
};