2023-04-24 03:18:24 -07:00
|
|
|
import type { IRestApiContext } from '@/Interface';
|
2023-11-28 03:15:08 -08:00
|
|
|
import { makeRestApiRequest } from '@/utils/apiUtils';
|
2023-04-24 03:18:24 -07:00
|
|
|
import type { IDataObject, MessageEventBusDestinationOptions } from 'n8n-workflow';
|
2023-01-20 03:08:40 -08:00
|
|
|
|
2024-05-31 00:48:09 -07:00
|
|
|
export type ApiMessageEventBusDestinationOptions = MessageEventBusDestinationOptions & {
|
|
|
|
id: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function hasDestinationId(
|
|
|
|
destination: MessageEventBusDestinationOptions,
|
|
|
|
): destination is ApiMessageEventBusDestinationOptions {
|
|
|
|
return destination.id !== undefined;
|
|
|
|
}
|
|
|
|
|
2023-01-20 03:08:40 -08:00
|
|
|
export async function saveDestinationToDb(
|
|
|
|
context: IRestApiContext,
|
2024-05-31 00:48:09 -07:00
|
|
|
destination: ApiMessageEventBusDestinationOptions,
|
2023-01-20 03:08:40 -08:00
|
|
|
subscribedEvents: string[] = [],
|
|
|
|
) {
|
2024-05-31 00:48:09 -07:00
|
|
|
const data: IDataObject = {
|
|
|
|
...destination,
|
|
|
|
subscribedEvents,
|
|
|
|
};
|
|
|
|
return await makeRestApiRequest(context, 'POST', '/eventbus/destination', data);
|
2023-01-20 03:08:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function deleteDestinationFromDb(context: IRestApiContext, destinationId: string) {
|
2024-01-18 01:28:01 -08:00
|
|
|
return await makeRestApiRequest(context, 'DELETE', `/eventbus/destination?id=${destinationId}`);
|
2023-01-20 03:08:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function sendTestMessageToDestination(
|
|
|
|
context: IRestApiContext,
|
2024-05-31 00:48:09 -07:00
|
|
|
destination: ApiMessageEventBusDestinationOptions,
|
2024-06-10 06:23:06 -07:00
|
|
|
): Promise<boolean> {
|
2024-05-31 00:48:09 -07:00
|
|
|
const data: IDataObject = {
|
|
|
|
...destination,
|
|
|
|
};
|
|
|
|
return await makeRestApiRequest(context, 'GET', '/eventbus/testmessage', data);
|
2023-01-20 03:08:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getEventNamesFromBackend(context: IRestApiContext): Promise<string[]> {
|
2024-01-18 01:28:01 -08:00
|
|
|
return await makeRestApiRequest(context, 'GET', '/eventbus/eventnames');
|
2023-01-20 03:08:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getDestinationsFromBackend(
|
|
|
|
context: IRestApiContext,
|
|
|
|
): Promise<MessageEventBusDestinationOptions[]> {
|
2024-01-18 01:28:01 -08:00
|
|
|
return await makeRestApiRequest(context, 'GET', '/eventbus/destination');
|
2023-01-20 03:08:40 -08:00
|
|
|
}
|