2024-01-17 01:15:20 -08:00
|
|
|
/* eslint-disable @typescript-eslint/no-loop-func */
|
2023-03-09 09:13:15 -08:00
|
|
|
import type {
|
|
|
|
ITriggerFunctions,
|
|
|
|
IDataObject,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
ITriggerResponse,
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeOperationError } from 'n8n-workflow';
|
2022-03-12 03:14:39 -08:00
|
|
|
|
2024-01-17 01:15:20 -08:00
|
|
|
import { redisConnectionTest, setupRedisClient } from './utils';
|
2022-03-12 03:14:39 -08:00
|
|
|
|
|
|
|
export class RedisTrigger implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Redis Trigger',
|
|
|
|
name: 'redisTrigger',
|
|
|
|
icon: 'file:redis.svg',
|
|
|
|
group: ['trigger'],
|
|
|
|
version: 1,
|
|
|
|
description: 'Subscribe to redis channel',
|
|
|
|
defaults: {
|
|
|
|
name: 'Redis Trigger',
|
|
|
|
},
|
|
|
|
inputs: [],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'redis',
|
|
|
|
required: true,
|
2024-01-17 01:15:20 -08:00
|
|
|
testedBy: 'redisConnectionTest',
|
2022-03-12 03:14:39 -08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Channels',
|
|
|
|
name: 'channels',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
required: true,
|
2022-08-17 08:50:24 -07:00
|
|
|
description:
|
|
|
|
'Channels to subscribe to, multiple channels be defined with comma. Wildcard character(*) is supported.',
|
2022-03-12 03:14:39 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Options',
|
|
|
|
name: 'options',
|
|
|
|
type: 'collection',
|
|
|
|
placeholder: 'Add Option',
|
|
|
|
default: {},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
displayName: 'JSON Parse Body',
|
|
|
|
name: 'jsonParseBody',
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
2022-06-20 07:54:01 -07:00
|
|
|
description: 'Whether to try to parse the message to an object',
|
2022-03-12 03:14:39 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Only Message',
|
|
|
|
name: 'onlyMessage',
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
2022-06-20 07:54:01 -07:00
|
|
|
description: 'Whether to return only the message property',
|
2022-03-12 03:14:39 -08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2024-01-17 01:15:20 -08:00
|
|
|
methods = {
|
|
|
|
credentialTest: { redisConnectionTest },
|
|
|
|
};
|
|
|
|
|
2022-03-12 03:14:39 -08:00
|
|
|
async trigger(this: ITriggerFunctions): Promise<ITriggerResponse> {
|
|
|
|
const credentials = await this.getCredentials('redis');
|
|
|
|
|
|
|
|
const channels = (this.getNodeParameter('channels') as string).split(',');
|
|
|
|
const options = this.getNodeParameter('options') as IDataObject;
|
|
|
|
|
|
|
|
if (!channels) {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Channels are mandatory!');
|
|
|
|
}
|
|
|
|
|
2024-01-17 01:15:20 -08:00
|
|
|
const client = setupRedisClient(credentials);
|
2022-03-12 03:14:39 -08:00
|
|
|
|
2023-01-13 09:11:56 -08:00
|
|
|
const manualTriggerFunction = async () => {
|
2024-01-17 01:15:20 -08:00
|
|
|
await client.connect();
|
|
|
|
await client.ping();
|
|
|
|
|
|
|
|
try {
|
|
|
|
for (const channel of channels) {
|
|
|
|
await client.pSubscribe(channel, (message) => {
|
2022-03-12 03:14:39 -08:00
|
|
|
if (options.jsonParseBody) {
|
|
|
|
try {
|
|
|
|
message = JSON.parse(message);
|
2022-08-17 08:50:24 -07:00
|
|
|
} catch (error) {}
|
2022-03-12 03:14:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.onlyMessage) {
|
2023-01-13 09:11:56 -08:00
|
|
|
this.emit([this.helpers.returnJsonArray({ message })]);
|
2022-03-12 03:14:39 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-13 09:11:56 -08:00
|
|
|
this.emit([this.helpers.returnJsonArray({ channel, message })]);
|
2022-03-12 03:14:39 -08:00
|
|
|
});
|
2024-01-17 01:15:20 -08:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
throw new NodeOperationError(this.getNode(), error);
|
|
|
|
}
|
2023-01-13 09:11:56 -08:00
|
|
|
};
|
2022-03-12 03:14:39 -08:00
|
|
|
|
|
|
|
if (this.getMode() === 'trigger') {
|
2023-12-21 09:29:26 -08:00
|
|
|
void manualTriggerFunction();
|
2022-03-12 03:14:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function closeFunction() {
|
2024-01-17 01:15:20 -08:00
|
|
|
await client.quit();
|
2022-03-12 03:14:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
closeFunction,
|
|
|
|
manualTriggerFunction,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|