-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathOpenPixTrigger.node.ts
204 lines (177 loc) · 5.03 KB
/
OpenPixTrigger.node.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import {
IHookFunctions,
ILoadOptionsFunctions,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
IWebhookFunctions,
IWebhookResponseData,
NodeApiError,
} from 'n8n-workflow';
import { apiRequest } from './transport';
export class OpenPixTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'OpenPix Trigger',
name: 'openpixTrigger',
icon: 'file:openpix.svg',
group: ['trigger'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Handle OpenPix Events via Webhook',
defaults: {
name: 'OpenPix Trigger',
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'openpixApi',
required: true,
},
],
requestDefaults: {
baseURL: 'https://api.openpix.com.br/api',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'User-Agent': 'n8n',
},
},
webhooks: [
{
name: 'default',
httpMethod: 'POST',
reponseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'Event Names or Name or ID',
name: 'events',
type: 'options',
required: true,
default: '',
description:
'The event to listen to. Choose from the list, or specify IDs using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
typeOptions: {
loadOptionsMethod: 'getEvents',
},
options: [],
},
],
};
methods = {
loadOptions: {
// Get all the events types to display them to user so that he can
// select them easily
async getEvents(
this: ILoadOptionsFunctions,
): Promise<INodePropertyOptions[]> {
// TODO: Check if make sense the * event
const returnData: INodePropertyOptions[] = [];
let response;
try {
const endpoint = '/webhook/events';
response = await apiRequest.call(this, 'GET', endpoint);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
for (const event of response.events) {
const eventName = event.name;
const eventId = event.name;
// TODO: Add description
const eventDescription = event.name;
returnData.push({
name: eventName,
value: eventId,
description: eventDescription,
});
}
return returnData;
},
},
};
webhookMethods = {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
const webhookUrl = this.getNodeWebhookUrl('default');
let result;
try {
result = await apiRequest.call(
this,
'GET',
'/webhook',
{},
{
url: webhookUrl,
},
);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
const { webhooks } = result;
if (webhooks.length) {
const [webhook] = webhooks;
const webhookData = this.getWorkflowStaticData('node');
webhookData.webhookId = webhook.id;
return true;
}
return false;
},
async create(this: IHookFunctions): Promise<boolean> {
const webhookUrl = this.getNodeWebhookUrl('default');
const events = this.getNodeParameter('events', []) as string[];
const body = {
name: 'N8N Webhook',
url: webhookUrl,
event: events,
isActive: true,
};
let result;
try {
result = await apiRequest.call(
this,
'POST',
'/webhook?validate=false',
{
webhook: body,
},
);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
const { webhook } = result;
if (webhook.id === undefined) {
return false;
}
const webhookData = this.getWorkflowStaticData('node');
webhookData.webhookId = webhook.id as string;
return true;
},
async delete(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
if (webhookData.webhookId !== undefined) {
try {
await apiRequest.call(
this,
'DELETE',
`/webhook/${webhookData.webhookId}`,
{},
);
} catch (error) {
return false;
}
delete webhookData.webhookId;
}
return true;
},
},
};
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const req = this.getRequestObject();
return {
workflowData: [this.helpers.returnJsonArray(req.body)],
};
}
}