@middag-io/n8n-nodes-middag 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/credentials/MiddagConnectorApi.credentials.d.ts +16 -0
- package/dist/credentials/MiddagConnectorApi.credentials.js +57 -0
- package/dist/nodes/MiddagHubspot/MiddagHubspot.node.d.ts +23 -0
- package/dist/nodes/MiddagHubspot/MiddagHubspot.node.js +299 -0
- package/dist/nodes/MiddagTrigger/MiddagTrigger.node.d.ts +29 -0
- package/dist/nodes/MiddagTrigger/MiddagTrigger.node.js +112 -0
- package/dist/nodes/shared/deliveryDedupe.d.ts +14 -0
- package/dist/nodes/shared/deliveryDedupe.js +24 -0
- package/dist/nodes/shared/middagSignature.d.ts +16 -0
- package/dist/nodes/shared/middagSignature.js +48 -0
- package/package.json +46 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ICredentialType, INodeProperties } from 'n8n-workflow';
|
|
2
|
+
/**
|
|
3
|
+
* Credential for the MIDDAG Connector Worker (`worker-ts-middag-connector-hubspot`):
|
|
4
|
+
* - `workerUrl`: base URL, e.g. `https://connector-hubspot.middag.io`.
|
|
5
|
+
* - `signingSecret`: the account's destination signing secret, used by MiddagTrigger to verify
|
|
6
|
+
* `X-Middag-Signature` on deliveries (docs/05-contratos-worker.md, "Entregas do Worker ao n8n").
|
|
7
|
+
* - `accessClientId` / `accessClientSecret`: Cloudflare Access service token, used by
|
|
8
|
+
* MiddagHubspot to call the Worker's internal routes (`/internal/hubspot/*`, `/internal/events`)
|
|
9
|
+
* as `CF-Access-Client-Id` / `CF-Access-Client-Secret` headers.
|
|
10
|
+
*/
|
|
11
|
+
export declare class MiddagConnectorApi implements ICredentialType {
|
|
12
|
+
name: string;
|
|
13
|
+
displayName: string;
|
|
14
|
+
documentationUrl: string;
|
|
15
|
+
properties: INodeProperties[];
|
|
16
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MiddagConnectorApi = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Credential for the MIDDAG Connector Worker (`worker-ts-middag-connector-hubspot`):
|
|
6
|
+
* - `workerUrl`: base URL, e.g. `https://connector-hubspot.middag.io`.
|
|
7
|
+
* - `signingSecret`: the account's destination signing secret, used by MiddagTrigger to verify
|
|
8
|
+
* `X-Middag-Signature` on deliveries (docs/05-contratos-worker.md, "Entregas do Worker ao n8n").
|
|
9
|
+
* - `accessClientId` / `accessClientSecret`: Cloudflare Access service token, used by
|
|
10
|
+
* MiddagHubspot to call the Worker's internal routes (`/internal/hubspot/*`, `/internal/events`)
|
|
11
|
+
* as `CF-Access-Client-Id` / `CF-Access-Client-Secret` headers.
|
|
12
|
+
*/
|
|
13
|
+
class MiddagConnectorApi {
|
|
14
|
+
constructor() {
|
|
15
|
+
this.name = 'middagConnectorApi';
|
|
16
|
+
this.displayName = 'MIDDAG Connector';
|
|
17
|
+
this.documentationUrl = 'https://github.com/middag-io/worker-ts-middag-connector-hubspot';
|
|
18
|
+
this.properties = [
|
|
19
|
+
{
|
|
20
|
+
displayName: 'Worker URL',
|
|
21
|
+
name: 'workerUrl',
|
|
22
|
+
type: 'string',
|
|
23
|
+
default: 'https://connector-hubspot.middag.io',
|
|
24
|
+
placeholder: 'https://connector-hubspot.middag.io',
|
|
25
|
+
description: 'Base URL of the MIDDAG Connector Worker.',
|
|
26
|
+
required: true,
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
displayName: 'Signing Secret',
|
|
30
|
+
name: 'signingSecret',
|
|
31
|
+
type: 'string',
|
|
32
|
+
typeOptions: { password: true },
|
|
33
|
+
default: '',
|
|
34
|
+
description: "The account's destination signing secret, used to verify X-Middag-Signature on deliveries from the Worker.",
|
|
35
|
+
required: true,
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
displayName: 'Access Client ID',
|
|
39
|
+
name: 'accessClientId',
|
|
40
|
+
type: 'string',
|
|
41
|
+
default: '',
|
|
42
|
+
description: 'Cloudflare Access service token client ID, sent as CF-Access-Client-Id.',
|
|
43
|
+
required: true,
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
displayName: 'Access Client Secret',
|
|
47
|
+
name: 'accessClientSecret',
|
|
48
|
+
type: 'string',
|
|
49
|
+
typeOptions: { password: true },
|
|
50
|
+
default: '',
|
|
51
|
+
description: 'Cloudflare Access service token client secret, sent as CF-Access-Client-Secret.',
|
|
52
|
+
required: true,
|
|
53
|
+
},
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.MiddagConnectorApi = MiddagConnectorApi;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { IExecuteFunctions, INodeExecutionData, INodeType, INodeTypeDescription } from 'n8n-workflow';
|
|
2
|
+
/**
|
|
3
|
+
* Calls the Worker's HubSpot proxy (`ANY /internal/hubspot/{portalId}/{path}`, docs/05,
|
|
4
|
+
* "Proxy da API HubSpot") using the Cloudflare Access service token from the credential.
|
|
5
|
+
*
|
|
6
|
+
* `verify-and-plan.js` (the Code node it replaces) does not itself call the Worker — it only
|
|
7
|
+
* verifies the signature and prepares a WooCommerce order plan, leaving the HubSpot side to other
|
|
8
|
+
* nodes downstream in that workflow. The operations below are not "replicated calls" from that
|
|
9
|
+
* file; they are the HubSpot actions the task asks this node to cover ("nota em contato e
|
|
10
|
+
* criar/atualizar negócio"), scoped to the paths docs/05 lists as allowed by the proxy:
|
|
11
|
+
* contacts/deals (create/update/search), associations, and notes.
|
|
12
|
+
*/
|
|
13
|
+
export declare class MiddagHubspot implements INodeType {
|
|
14
|
+
description: INodeTypeDescription;
|
|
15
|
+
execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]>;
|
|
16
|
+
}
|
|
17
|
+
type ProxyRequest = {
|
|
18
|
+
method: 'GET' | 'POST' | 'PATCH' | 'PUT';
|
|
19
|
+
path: string;
|
|
20
|
+
body?: unknown;
|
|
21
|
+
};
|
|
22
|
+
export declare function requestFor(ctx: IExecuteFunctions, resource: string, operation: string, i: number): ProxyRequest;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MiddagHubspot = void 0;
|
|
4
|
+
exports.requestFor = requestFor;
|
|
5
|
+
const n8n_workflow_1 = require("n8n-workflow");
|
|
6
|
+
/**
|
|
7
|
+
* Calls the Worker's HubSpot proxy (`ANY /internal/hubspot/{portalId}/{path}`, docs/05,
|
|
8
|
+
* "Proxy da API HubSpot") using the Cloudflare Access service token from the credential.
|
|
9
|
+
*
|
|
10
|
+
* `verify-and-plan.js` (the Code node it replaces) does not itself call the Worker — it only
|
|
11
|
+
* verifies the signature and prepares a WooCommerce order plan, leaving the HubSpot side to other
|
|
12
|
+
* nodes downstream in that workflow. The operations below are not "replicated calls" from that
|
|
13
|
+
* file; they are the HubSpot actions the task asks this node to cover ("nota em contato e
|
|
14
|
+
* criar/atualizar negócio"), scoped to the paths docs/05 lists as allowed by the proxy:
|
|
15
|
+
* contacts/deals (create/update/search), associations, and notes.
|
|
16
|
+
*/
|
|
17
|
+
class MiddagHubspot {
|
|
18
|
+
constructor() {
|
|
19
|
+
this.description = {
|
|
20
|
+
displayName: 'MIDDAG HubSpot',
|
|
21
|
+
name: 'middagHubspot',
|
|
22
|
+
icon: 'fa:link',
|
|
23
|
+
iconColor: 'orange-red',
|
|
24
|
+
group: ['transform'],
|
|
25
|
+
version: 1,
|
|
26
|
+
subtitle: '={{$parameter["resource"] + ": " + $parameter["operation"]}}',
|
|
27
|
+
description: "Calls the MIDDAG Connector Worker's HubSpot proxy (/internal/hubspot/*)",
|
|
28
|
+
defaults: { name: 'MIDDAG HubSpot' },
|
|
29
|
+
inputs: [n8n_workflow_1.NodeConnectionTypes.Main],
|
|
30
|
+
outputs: [n8n_workflow_1.NodeConnectionTypes.Main],
|
|
31
|
+
credentials: [{ name: 'middagConnectorApi', required: true }],
|
|
32
|
+
properties: [
|
|
33
|
+
{
|
|
34
|
+
displayName: 'Portal ID',
|
|
35
|
+
name: 'portalId',
|
|
36
|
+
type: 'number',
|
|
37
|
+
default: 0,
|
|
38
|
+
required: true,
|
|
39
|
+
description: 'HubSpot portal (account) ID to act on.',
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
displayName: 'Resource',
|
|
43
|
+
name: 'resource',
|
|
44
|
+
type: 'options',
|
|
45
|
+
noDataExpression: true,
|
|
46
|
+
options: [
|
|
47
|
+
{ name: 'Contact', value: 'contact' },
|
|
48
|
+
{ name: 'Deal', value: 'deal' },
|
|
49
|
+
{ name: 'Note', value: 'note' },
|
|
50
|
+
{ name: 'Association', value: 'association' },
|
|
51
|
+
],
|
|
52
|
+
default: 'contact',
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
displayName: 'Operation',
|
|
56
|
+
name: 'operation',
|
|
57
|
+
type: 'options',
|
|
58
|
+
noDataExpression: true,
|
|
59
|
+
displayOptions: { show: { resource: ['contact'] } },
|
|
60
|
+
options: [
|
|
61
|
+
{ name: 'Create', value: 'create', action: 'Create a contact' },
|
|
62
|
+
{ name: 'Update', value: 'update', action: 'Update a contact' },
|
|
63
|
+
{ name: 'Search', value: 'search', action: 'Search contacts by email' },
|
|
64
|
+
],
|
|
65
|
+
default: 'create',
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
displayName: 'Operation',
|
|
69
|
+
name: 'operation',
|
|
70
|
+
type: 'options',
|
|
71
|
+
noDataExpression: true,
|
|
72
|
+
displayOptions: { show: { resource: ['deal'] } },
|
|
73
|
+
options: [
|
|
74
|
+
{ name: 'Create', value: 'create', action: 'Create a deal' },
|
|
75
|
+
{ name: 'Update', value: 'update', action: 'Update a deal' },
|
|
76
|
+
],
|
|
77
|
+
default: 'create',
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
displayName: 'Operation',
|
|
81
|
+
name: 'operation',
|
|
82
|
+
type: 'options',
|
|
83
|
+
noDataExpression: true,
|
|
84
|
+
displayOptions: { show: { resource: ['note'] } },
|
|
85
|
+
options: [{ name: 'Create', value: 'create', action: 'Create a note on an object' }],
|
|
86
|
+
default: 'create',
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
displayName: 'Operation',
|
|
90
|
+
name: 'operation',
|
|
91
|
+
type: 'options',
|
|
92
|
+
noDataExpression: true,
|
|
93
|
+
displayOptions: { show: { resource: ['association'] } },
|
|
94
|
+
options: [{ name: 'Create', value: 'create', action: 'Associate two objects' }],
|
|
95
|
+
default: 'create',
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
displayName: 'Object ID',
|
|
99
|
+
name: 'objectId',
|
|
100
|
+
type: 'string',
|
|
101
|
+
default: '',
|
|
102
|
+
required: true,
|
|
103
|
+
displayOptions: { show: { operation: ['update'] } },
|
|
104
|
+
description: 'HubSpot object ID (contact or deal) to update.',
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
displayName: 'Email',
|
|
108
|
+
name: 'email',
|
|
109
|
+
type: 'string',
|
|
110
|
+
default: '',
|
|
111
|
+
displayOptions: { show: { resource: ['contact'], operation: ['create', 'search'] } },
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
displayName: 'Properties (JSON)',
|
|
115
|
+
name: 'properties',
|
|
116
|
+
type: 'json',
|
|
117
|
+
default: '{}',
|
|
118
|
+
displayOptions: { show: { operation: ['create', 'update'] } },
|
|
119
|
+
description: 'HubSpot object properties, as a JSON object of property name to value.',
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
displayName: 'Object Type',
|
|
123
|
+
name: 'noteObjectType',
|
|
124
|
+
type: 'options',
|
|
125
|
+
options: [
|
|
126
|
+
{ name: 'Contact', value: 'contacts' },
|
|
127
|
+
{ name: 'Deal', value: 'deals' },
|
|
128
|
+
],
|
|
129
|
+
default: 'contacts',
|
|
130
|
+
displayOptions: { show: { resource: ['note'] } },
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
displayName: 'Object ID',
|
|
134
|
+
name: 'noteObjectId',
|
|
135
|
+
type: 'string',
|
|
136
|
+
default: '',
|
|
137
|
+
required: true,
|
|
138
|
+
displayOptions: { show: { resource: ['note'] } },
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
displayName: 'Note Body',
|
|
142
|
+
name: 'noteBody',
|
|
143
|
+
type: 'string',
|
|
144
|
+
typeOptions: { rows: 4 },
|
|
145
|
+
default: '',
|
|
146
|
+
required: true,
|
|
147
|
+
displayOptions: { show: { resource: ['note'] } },
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
displayName: 'From Object Type',
|
|
151
|
+
name: 'fromObjectType',
|
|
152
|
+
type: 'options',
|
|
153
|
+
options: [
|
|
154
|
+
{ name: 'Contact', value: 'contacts' },
|
|
155
|
+
{ name: 'Deal', value: 'deals' },
|
|
156
|
+
],
|
|
157
|
+
default: 'contacts',
|
|
158
|
+
displayOptions: { show: { resource: ['association'] } },
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
displayName: 'From Object ID',
|
|
162
|
+
name: 'fromObjectId',
|
|
163
|
+
type: 'string',
|
|
164
|
+
default: '',
|
|
165
|
+
required: true,
|
|
166
|
+
displayOptions: { show: { resource: ['association'] } },
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
displayName: 'To Object Type',
|
|
170
|
+
name: 'toObjectType',
|
|
171
|
+
type: 'options',
|
|
172
|
+
options: [
|
|
173
|
+
{ name: 'Contact', value: 'contacts' },
|
|
174
|
+
{ name: 'Deal', value: 'deals' },
|
|
175
|
+
],
|
|
176
|
+
default: 'deals',
|
|
177
|
+
displayOptions: { show: { resource: ['association'] } },
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
displayName: 'To Object ID',
|
|
181
|
+
name: 'toObjectId',
|
|
182
|
+
type: 'string',
|
|
183
|
+
default: '',
|
|
184
|
+
required: true,
|
|
185
|
+
displayOptions: { show: { resource: ['association'] } },
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
displayName: 'Association Type ID',
|
|
189
|
+
name: 'associationTypeId',
|
|
190
|
+
type: 'number',
|
|
191
|
+
default: 1,
|
|
192
|
+
displayOptions: { show: { resource: ['association'] } },
|
|
193
|
+
description: 'HubSpot association type ID (default 1 = contact_to_deal / primary).',
|
|
194
|
+
},
|
|
195
|
+
],
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
async execute() {
|
|
199
|
+
const items = this.getInputData();
|
|
200
|
+
const returnData = [];
|
|
201
|
+
const credentials = await this.getCredentials('middagConnectorApi');
|
|
202
|
+
for (let i = 0; i < items.length; i++) {
|
|
203
|
+
const resource = this.getNodeParameter('resource', i);
|
|
204
|
+
const operation = this.getNodeParameter('operation', i);
|
|
205
|
+
const portalId = this.getNodeParameter('portalId', i);
|
|
206
|
+
const request = requestFor(this, resource, operation, i);
|
|
207
|
+
const options = {
|
|
208
|
+
method: request.method,
|
|
209
|
+
url: `${credentials.workerUrl.replace(/\/$/, '')}/internal/hubspot/${portalId}/${request.path}`,
|
|
210
|
+
headers: {
|
|
211
|
+
'CF-Access-Client-Id': credentials.accessClientId,
|
|
212
|
+
'CF-Access-Client-Secret': credentials.accessClientSecret,
|
|
213
|
+
'content-type': 'application/json',
|
|
214
|
+
},
|
|
215
|
+
body: request.body,
|
|
216
|
+
json: true,
|
|
217
|
+
};
|
|
218
|
+
const response = await this.helpers.httpRequest(options);
|
|
219
|
+
returnData.push({ json: response, pairedItem: { item: i } });
|
|
220
|
+
}
|
|
221
|
+
return [returnData];
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
exports.MiddagHubspot = MiddagHubspot;
|
|
225
|
+
function requestFor(ctx, resource, operation, i) {
|
|
226
|
+
if (resource === 'contact')
|
|
227
|
+
return contactRequest(ctx, operation, i);
|
|
228
|
+
if (resource === 'deal')
|
|
229
|
+
return dealRequest(ctx, operation, i);
|
|
230
|
+
if (resource === 'note')
|
|
231
|
+
return noteRequest(ctx, i);
|
|
232
|
+
if (resource === 'association')
|
|
233
|
+
return associationRequest(ctx, i);
|
|
234
|
+
throw new n8n_workflow_1.NodeOperationError(ctx.getNode(), `Unsupported resource: ${resource}`);
|
|
235
|
+
}
|
|
236
|
+
function contactRequest(ctx, operation, i) {
|
|
237
|
+
if (operation === 'search') {
|
|
238
|
+
const email = ctx.getNodeParameter('email', i);
|
|
239
|
+
return {
|
|
240
|
+
method: 'POST',
|
|
241
|
+
path: 'crm/v3/objects/contacts/search',
|
|
242
|
+
body: { filterGroups: [{ filters: [{ propertyName: 'email', operator: 'EQ', value: email }] }] },
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
const properties = JSON.parse(ctx.getNodeParameter('properties', i));
|
|
246
|
+
if (operation === 'create') {
|
|
247
|
+
const email = ctx.getNodeParameter('email', i);
|
|
248
|
+
return {
|
|
249
|
+
method: 'POST',
|
|
250
|
+
path: 'crm/v3/objects/contacts',
|
|
251
|
+
body: { properties: { email, ...properties } },
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
const objectId = ctx.getNodeParameter('objectId', i);
|
|
255
|
+
return { method: 'PATCH', path: `crm/v3/objects/contacts/${objectId}`, body: { properties } };
|
|
256
|
+
}
|
|
257
|
+
function dealRequest(ctx, operation, i) {
|
|
258
|
+
const properties = JSON.parse(ctx.getNodeParameter('properties', i));
|
|
259
|
+
if (operation === 'create') {
|
|
260
|
+
return { method: 'POST', path: 'crm/v3/objects/deals', body: { properties } };
|
|
261
|
+
}
|
|
262
|
+
const objectId = ctx.getNodeParameter('objectId', i);
|
|
263
|
+
return { method: 'PATCH', path: `crm/v3/objects/deals/${objectId}`, body: { properties } };
|
|
264
|
+
}
|
|
265
|
+
function noteRequest(ctx, i) {
|
|
266
|
+
const objectType = ctx.getNodeParameter('noteObjectType', i);
|
|
267
|
+
const objectId = ctx.getNodeParameter('noteObjectId', i);
|
|
268
|
+
const body = ctx.getNodeParameter('noteBody', i);
|
|
269
|
+
return {
|
|
270
|
+
method: 'POST',
|
|
271
|
+
path: 'crm/v3/objects/notes',
|
|
272
|
+
body: {
|
|
273
|
+
properties: { hs_note_body: body, hs_timestamp: Date.now() },
|
|
274
|
+
associations: [
|
|
275
|
+
{
|
|
276
|
+
to: { id: objectId },
|
|
277
|
+
types: [
|
|
278
|
+
{
|
|
279
|
+
associationCategory: 'HUBSPOT_DEFINED',
|
|
280
|
+
associationTypeId: objectType === 'deals' ? 214 : 202,
|
|
281
|
+
},
|
|
282
|
+
],
|
|
283
|
+
},
|
|
284
|
+
],
|
|
285
|
+
},
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
function associationRequest(ctx, i) {
|
|
289
|
+
const fromObjectType = ctx.getNodeParameter('fromObjectType', i);
|
|
290
|
+
const fromObjectId = ctx.getNodeParameter('fromObjectId', i);
|
|
291
|
+
const toObjectType = ctx.getNodeParameter('toObjectType', i);
|
|
292
|
+
const toObjectId = ctx.getNodeParameter('toObjectId', i);
|
|
293
|
+
const associationTypeId = ctx.getNodeParameter('associationTypeId', i);
|
|
294
|
+
return {
|
|
295
|
+
method: 'PUT',
|
|
296
|
+
path: `crm/v4/objects/${fromObjectType}/${fromObjectId}/associations/${toObjectType}/${toObjectId}`,
|
|
297
|
+
body: [{ associationCategory: 'HUBSPOT_DEFINED', associationTypeId }],
|
|
298
|
+
};
|
|
299
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { IWebhookFunctions, IWebhookResponseData, INodeType, INodeTypeDescription, IDataObject } from 'n8n-workflow';
|
|
2
|
+
/**
|
|
3
|
+
* Native webhook trigger for deliveries from the Worker (`docs/05-contratos-worker.md`,
|
|
4
|
+
* "Entregas do Worker ao n8n"), replacing the manual-verification Code node
|
|
5
|
+
* (`n8n-workflows-helico/workflows/woo-order.code/verify-and-plan.js`).
|
|
6
|
+
*
|
|
7
|
+
* Output shape decision: ONE output, with `kind` included in the emitted JSON — not one named
|
|
8
|
+
* output per `kind`. Rationale: `DELIVERY_KINDS` in the Worker
|
|
9
|
+
* (`worker-ts-middag-connector-hubspot/src/delivery/n8n.ts`) is a living list (currently
|
|
10
|
+
* `form_submission`, `hubspot_change`, `hubspot_merge`, `hubspot_deletion`, `alert`,
|
|
11
|
+
* `card_request`; docs/05 also names `card_action`) that changes as the product grows. Hard-coding
|
|
12
|
+
* n8n output ports per kind would mean editing this node (and every workflow's Switch/IF wiring)
|
|
13
|
+
* every time a kind is added or renamed. A single output lets each workflow branch on `kind` with
|
|
14
|
+
* an ordinary Switch/IF node instead, and new kinds show up without a node update.
|
|
15
|
+
*/
|
|
16
|
+
export declare class MiddagTrigger implements INodeType {
|
|
17
|
+
description: INodeTypeDescription;
|
|
18
|
+
webhook(this: IWebhookFunctions): Promise<IWebhookResponseData>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Shapes the single output item for a verified, non-duplicate delivery. Kept as a plain function
|
|
22
|
+
* (not inlined in `webhook`) so the `kind`-routing decision described above is unit-testable
|
|
23
|
+
* without an `IWebhookFunctions` mock.
|
|
24
|
+
*/
|
|
25
|
+
export declare function outputItemFor(envelope: {
|
|
26
|
+
kind?: string;
|
|
27
|
+
portalId?: number;
|
|
28
|
+
payload?: unknown;
|
|
29
|
+
}, deliveryId: string | undefined): IDataObject;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MiddagTrigger = void 0;
|
|
4
|
+
exports.outputItemFor = outputItemFor;
|
|
5
|
+
const n8n_workflow_1 = require("n8n-workflow");
|
|
6
|
+
const middagSignature_1 = require("../shared/middagSignature");
|
|
7
|
+
const deliveryDedupe_1 = require("../shared/deliveryDedupe");
|
|
8
|
+
const DELIVERY_ID_HEADER = 'x-middag-delivery-id';
|
|
9
|
+
/**
|
|
10
|
+
* Native webhook trigger for deliveries from the Worker (`docs/05-contratos-worker.md`,
|
|
11
|
+
* "Entregas do Worker ao n8n"), replacing the manual-verification Code node
|
|
12
|
+
* (`n8n-workflows-helico/workflows/woo-order.code/verify-and-plan.js`).
|
|
13
|
+
*
|
|
14
|
+
* Output shape decision: ONE output, with `kind` included in the emitted JSON — not one named
|
|
15
|
+
* output per `kind`. Rationale: `DELIVERY_KINDS` in the Worker
|
|
16
|
+
* (`worker-ts-middag-connector-hubspot/src/delivery/n8n.ts`) is a living list (currently
|
|
17
|
+
* `form_submission`, `hubspot_change`, `hubspot_merge`, `hubspot_deletion`, `alert`,
|
|
18
|
+
* `card_request`; docs/05 also names `card_action`) that changes as the product grows. Hard-coding
|
|
19
|
+
* n8n output ports per kind would mean editing this node (and every workflow's Switch/IF wiring)
|
|
20
|
+
* every time a kind is added or renamed. A single output lets each workflow branch on `kind` with
|
|
21
|
+
* an ordinary Switch/IF node instead, and new kinds show up without a node update.
|
|
22
|
+
*/
|
|
23
|
+
class MiddagTrigger {
|
|
24
|
+
constructor() {
|
|
25
|
+
this.description = {
|
|
26
|
+
displayName: 'MIDDAG Trigger',
|
|
27
|
+
name: 'middagTrigger',
|
|
28
|
+
icon: 'fa:bolt',
|
|
29
|
+
iconColor: 'orange-red',
|
|
30
|
+
group: ['trigger'],
|
|
31
|
+
version: 1,
|
|
32
|
+
subtitle: '={{$parameter["path"]}}',
|
|
33
|
+
description: 'Starts the workflow on a signed delivery from the MIDDAG Connector Worker',
|
|
34
|
+
defaults: { name: 'MIDDAG Trigger' },
|
|
35
|
+
inputs: [],
|
|
36
|
+
outputs: [n8n_workflow_1.NodeConnectionTypes.Main],
|
|
37
|
+
credentials: [{ name: 'middagConnectorApi', required: true }],
|
|
38
|
+
webhooks: [
|
|
39
|
+
{
|
|
40
|
+
name: 'default',
|
|
41
|
+
httpMethod: 'POST',
|
|
42
|
+
responseMode: 'onReceived',
|
|
43
|
+
path: '={{$parameter["path"]}}',
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
properties: [
|
|
47
|
+
{
|
|
48
|
+
displayName: 'Path',
|
|
49
|
+
name: 'path',
|
|
50
|
+
type: 'string',
|
|
51
|
+
default: 'middag',
|
|
52
|
+
required: true,
|
|
53
|
+
description: 'Webhook path. Configure this exact URL as the account destination.n8nUrl.',
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
async webhook() {
|
|
59
|
+
const credentials = await this.getCredentials('middagConnectorApi');
|
|
60
|
+
const secret = credentials.signingSecret;
|
|
61
|
+
const req = this.getRequestObject();
|
|
62
|
+
const headers = this.getHeaderData();
|
|
63
|
+
const signatureHeader = firstValue(headers[middagSignature_1.MIDDAG_SIGNATURE_HEADER]);
|
|
64
|
+
const deliveryId = firstValue(headers[DELIVERY_ID_HEADER]);
|
|
65
|
+
// n8n's JSON body parser already ran; re-serializing it is what the signature was computed
|
|
66
|
+
// over as long as the Worker sends compact JSON with stable key order, which it does
|
|
67
|
+
// (`JSON.stringify(delivery)` in src/delivery/n8n.ts). Prefer the raw body when the node's
|
|
68
|
+
// webhook config exposes it, falling back to a re-stringify.
|
|
69
|
+
const rawBody = typeof req.rawBody !== 'undefined'
|
|
70
|
+
? req.rawBody.toString('utf8')
|
|
71
|
+
: JSON.stringify(this.getBodyData());
|
|
72
|
+
const verification = (0, middagSignature_1.verifyMiddagSignature)(secret, signatureHeader, rawBody);
|
|
73
|
+
if (!verification.valid) {
|
|
74
|
+
return {
|
|
75
|
+
webhookResponse: { status: 401, body: { success: false, error: verification.reason } },
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
const staticData = this.getWorkflowStaticData('node');
|
|
79
|
+
if (deliveryId) {
|
|
80
|
+
if ((0, deliveryDedupe_1.isDuplicateDelivery)(staticData, deliveryId)) {
|
|
81
|
+
return {
|
|
82
|
+
webhookResponse: { status: 200, body: { success: true, duplicate: true } },
|
|
83
|
+
noWebhookResponse: false,
|
|
84
|
+
workflowData: [],
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
(0, deliveryDedupe_1.rememberDelivery)(staticData, deliveryId);
|
|
88
|
+
}
|
|
89
|
+
const envelope = this.getBodyData();
|
|
90
|
+
return {
|
|
91
|
+
webhookResponse: { status: 200, body: { success: true } },
|
|
92
|
+
workflowData: [[{ json: outputItemFor(envelope, deliveryId) }]],
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
exports.MiddagTrigger = MiddagTrigger;
|
|
97
|
+
function firstValue(value) {
|
|
98
|
+
return Array.isArray(value) ? value[0] : value;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Shapes the single output item for a verified, non-duplicate delivery. Kept as a plain function
|
|
102
|
+
* (not inlined in `webhook`) so the `kind`-routing decision described above is unit-testable
|
|
103
|
+
* without an `IWebhookFunctions` mock.
|
|
104
|
+
*/
|
|
105
|
+
function outputItemFor(envelope, deliveryId) {
|
|
106
|
+
return {
|
|
107
|
+
kind: envelope.kind ?? null,
|
|
108
|
+
portalId: envelope.portalId ?? null,
|
|
109
|
+
payload: (envelope.payload ?? null),
|
|
110
|
+
deliveryId: deliveryId ?? null,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Best-effort dedupe of `X-Middag-Delivery-Id` redeliveries, backed by n8n's workflow static
|
|
3
|
+
* data (`this.getWorkflowStaticData('node')`). Limitation, documented per the task: this store
|
|
4
|
+
* is NOT durable across workflow re-imports, ID changes, or an operator clearing static data —
|
|
5
|
+
* it is a convenience against ordinary retries (Worker timeout/5xx), not a correctness
|
|
6
|
+
* guarantee. Real idempotency for anything written downstream (e.g. HubSpot notes/deals) must
|
|
7
|
+
* still be keyed by the domain id (submissionId, delivery id) on the receiving side.
|
|
8
|
+
*/
|
|
9
|
+
export type DedupeStaticData = {
|
|
10
|
+
seenDeliveryIds?: string[];
|
|
11
|
+
};
|
|
12
|
+
export declare function isDuplicateDelivery(staticData: DedupeStaticData, deliveryId: string): boolean;
|
|
13
|
+
/** Records the id, trimming the tracked list to the most recent `MAX_TRACKED_IDS` entries. */
|
|
14
|
+
export declare function rememberDelivery(staticData: DedupeStaticData, deliveryId: string): void;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Best-effort dedupe of `X-Middag-Delivery-Id` redeliveries, backed by n8n's workflow static
|
|
4
|
+
* data (`this.getWorkflowStaticData('node')`). Limitation, documented per the task: this store
|
|
5
|
+
* is NOT durable across workflow re-imports, ID changes, or an operator clearing static data —
|
|
6
|
+
* it is a convenience against ordinary retries (Worker timeout/5xx), not a correctness
|
|
7
|
+
* guarantee. Real idempotency for anything written downstream (e.g. HubSpot notes/deals) must
|
|
8
|
+
* still be keyed by the domain id (submissionId, delivery id) on the receiving side.
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.isDuplicateDelivery = isDuplicateDelivery;
|
|
12
|
+
exports.rememberDelivery = rememberDelivery;
|
|
13
|
+
const MAX_TRACKED_IDS = 500;
|
|
14
|
+
function isDuplicateDelivery(staticData, deliveryId) {
|
|
15
|
+
const seen = staticData.seenDeliveryIds ?? [];
|
|
16
|
+
return seen.includes(deliveryId);
|
|
17
|
+
}
|
|
18
|
+
/** Records the id, trimming the tracked list to the most recent `MAX_TRACKED_IDS` entries. */
|
|
19
|
+
function rememberDelivery(staticData, deliveryId) {
|
|
20
|
+
const seen = staticData.seenDeliveryIds ?? [];
|
|
21
|
+
const next = [...seen, deliveryId];
|
|
22
|
+
staticData.seenDeliveryIds =
|
|
23
|
+
next.length > MAX_TRACKED_IDS ? next.slice(next.length - MAX_TRACKED_IDS) : next;
|
|
24
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `X-Middag-Signature: t=<unix seconds>,v1=<hex HMAC-SHA256 of "<t>.<raw body>">`.
|
|
3
|
+
* Mirrors the Worker's implementation (`worker-ts-middag-connector-hubspot/src/lib/middag-signature.ts`):
|
|
4
|
+
* HMAC-SHA256 over `"<timestamp>.<rawBody>"`, compared in constant time, timestamp within a
|
|
5
|
+
* 5-minute window of "now".
|
|
6
|
+
*/
|
|
7
|
+
export declare const MIDDAG_SIGNATURE_HEADER = "x-middag-signature";
|
|
8
|
+
export declare const MIDDAG_SIGNATURE_MAX_AGE_S: number;
|
|
9
|
+
export type MiddagSignatureResult = {
|
|
10
|
+
readonly valid: true;
|
|
11
|
+
} | {
|
|
12
|
+
readonly valid: false;
|
|
13
|
+
readonly reason: 'malformed' | 'expired' | 'mismatch';
|
|
14
|
+
};
|
|
15
|
+
export declare function signMiddagPayload(secret: string, body: string, nowMs?: number): string;
|
|
16
|
+
export declare function verifyMiddagSignature(secret: string, header: string | undefined, rawBody: string, nowMs?: number): MiddagSignatureResult;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MIDDAG_SIGNATURE_MAX_AGE_S = exports.MIDDAG_SIGNATURE_HEADER = void 0;
|
|
4
|
+
exports.signMiddagPayload = signMiddagPayload;
|
|
5
|
+
exports.verifyMiddagSignature = verifyMiddagSignature;
|
|
6
|
+
const crypto_1 = require("crypto");
|
|
7
|
+
/**
|
|
8
|
+
* `X-Middag-Signature: t=<unix seconds>,v1=<hex HMAC-SHA256 of "<t>.<raw body>">`.
|
|
9
|
+
* Mirrors the Worker's implementation (`worker-ts-middag-connector-hubspot/src/lib/middag-signature.ts`):
|
|
10
|
+
* HMAC-SHA256 over `"<timestamp>.<rawBody>"`, compared in constant time, timestamp within a
|
|
11
|
+
* 5-minute window of "now".
|
|
12
|
+
*/
|
|
13
|
+
exports.MIDDAG_SIGNATURE_HEADER = 'x-middag-signature';
|
|
14
|
+
exports.MIDDAG_SIGNATURE_MAX_AGE_S = 5 * 60;
|
|
15
|
+
const TIMESTAMP_PATTERN = /^\d{1,12}$/;
|
|
16
|
+
const HEX_SHA256_PATTERN = /^[0-9a-f]{64}$/;
|
|
17
|
+
function hmacHex(secret, message) {
|
|
18
|
+
return (0, crypto_1.createHmac)('sha256', secret).update(message, 'utf8').digest('hex');
|
|
19
|
+
}
|
|
20
|
+
function parseHeader(header) {
|
|
21
|
+
const parts = new Map(header.split(',').map((part) => {
|
|
22
|
+
const [name = '', ...rest] = part.trim().split('=');
|
|
23
|
+
return [name, rest.join('=')];
|
|
24
|
+
}));
|
|
25
|
+
const t = parts.get('t') ?? '';
|
|
26
|
+
const v1 = (parts.get('v1') ?? '').toLowerCase();
|
|
27
|
+
return TIMESTAMP_PATTERN.test(t) && HEX_SHA256_PATTERN.test(v1) ? { t, v1 } : null;
|
|
28
|
+
}
|
|
29
|
+
function signMiddagPayload(secret, body, nowMs = Date.now()) {
|
|
30
|
+
const timestamp = String(Math.floor(nowMs / 1000));
|
|
31
|
+
return `t=${timestamp},v1=${hmacHex(secret, `${timestamp}.${body}`)}`;
|
|
32
|
+
}
|
|
33
|
+
/** Constant-time comparison of two equal-length hex strings; unequal lengths are always invalid. */
|
|
34
|
+
function safeEqualHex(a, b) {
|
|
35
|
+
if (a.length !== b.length)
|
|
36
|
+
return false;
|
|
37
|
+
return (0, crypto_1.timingSafeEqual)(Buffer.from(a, 'utf8'), Buffer.from(b, 'utf8'));
|
|
38
|
+
}
|
|
39
|
+
function verifyMiddagSignature(secret, header, rawBody, nowMs = Date.now()) {
|
|
40
|
+
const parsed = header ? parseHeader(header) : null;
|
|
41
|
+
if (!parsed)
|
|
42
|
+
return { valid: false, reason: 'malformed' };
|
|
43
|
+
if (Math.abs(nowMs / 1000 - Number(parsed.t)) > exports.MIDDAG_SIGNATURE_MAX_AGE_S) {
|
|
44
|
+
return { valid: false, reason: 'expired' };
|
|
45
|
+
}
|
|
46
|
+
const expected = hmacHex(secret, `${parsed.t}.${rawBody}`);
|
|
47
|
+
return safeEqualHex(expected, parsed.v1) ? { valid: true } : { valid: false, reason: 'mismatch' };
|
|
48
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@middag-io/n8n-nodes-middag",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "n8n community nodes for the MIDDAG Connector for HubSpot: a native trigger for signed deliveries from the Worker and an action node for the HubSpot proxy.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"n8n-community-node-package"
|
|
7
|
+
],
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"author": "MIDDAG",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/middag-io/n8n-nodes-middag.git"
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public",
|
|
16
|
+
"registry": "https://registry.npmjs.org/"
|
|
17
|
+
},
|
|
18
|
+
"main": "dist/index.js",
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsc",
|
|
24
|
+
"dev": "tsc --watch",
|
|
25
|
+
"test": "jest",
|
|
26
|
+
"lint": "tsc --noEmit"
|
|
27
|
+
},
|
|
28
|
+
"n8n": {
|
|
29
|
+
"n8nNodesApiVersion": 1,
|
|
30
|
+
"credentials": [
|
|
31
|
+
"dist/credentials/MiddagConnectorApi.credentials.js"
|
|
32
|
+
],
|
|
33
|
+
"nodes": [
|
|
34
|
+
"dist/nodes/MiddagTrigger/MiddagTrigger.node.js",
|
|
35
|
+
"dist/nodes/MiddagHubspot/MiddagHubspot.node.js"
|
|
36
|
+
]
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/jest": "^29.5.12",
|
|
40
|
+
"@types/node": "^20.14.0",
|
|
41
|
+
"jest": "^29.7.0",
|
|
42
|
+
"n8n-workflow": "^1.63.0",
|
|
43
|
+
"ts-jest": "^29.1.5",
|
|
44
|
+
"typescript": "^5.5.4"
|
|
45
|
+
}
|
|
46
|
+
}
|