Skip to content

Commit eeed751

Browse files
committed
feat: add trigger node
1 parent fa7b360 commit eeed751

File tree

4 files changed

+186
-3
lines changed

4 files changed

+186
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
import {
2+
IDataObject,
3+
IExecuteFunctions,
4+
IHookFunctions,
5+
ILoadOptionsFunctions,
6+
IWebhookFunctions,
7+
IWebhookResponseData,
8+
NodeConnectionType,
9+
type INodeType,
10+
type INodeTypeDescription,
11+
} from 'n8n-workflow';
12+
import axios, { AxiosRequestConfig } from 'axios';
13+
14+
export class ShopwareAdminTriggerNode implements INodeType {
15+
description: INodeTypeDescription = {
16+
name: 'shopwareAdminTriggerNode',
17+
displayName: 'Shopware Trigger',
18+
icon: 'file:shopware.svg',
19+
group: ['trigger'],
20+
version: 1,
21+
description: 'Shopware Trigger',
22+
defaults: {
23+
name: 'Shopware Trigger',
24+
},
25+
// eslint-disable-next-line n8n-nodes-base/node-class-description-inputs-wrong-regular-node
26+
inputs: [],
27+
// eslint-disable-next-line n8n-nodes-base/node-class-description-outputs-wrong
28+
outputs: [NodeConnectionType.Main],
29+
credentials: [
30+
{
31+
name: 'shopwareAdminCredentialsApi',
32+
required: true,
33+
},
34+
],
35+
requestDefaults: {
36+
headers: {
37+
Accept: 'application/json',
38+
'Content-Type': 'application/json',
39+
},
40+
baseURL: '={{$credentials.url}}',
41+
},
42+
webhooks: [
43+
{
44+
name: 'default',
45+
httpMethod: 'POST',
46+
responseMode: 'onReceived',
47+
path: 'webhook',
48+
},
49+
],
50+
properties: [
51+
{
52+
displayName: 'Event Name',
53+
name: 'eventName',
54+
type: 'string',
55+
default: '',
56+
required: true,
57+
},
58+
{
59+
displayName: 'Only Live Version',
60+
name: 'onlyLiveVersion',
61+
type: 'boolean',
62+
default: true,
63+
},
64+
],
65+
};
66+
67+
webhookMethods = {
68+
default: {
69+
async checkExists(this: IHookFunctions): Promise<boolean> {
70+
const webhookData = this.getWorkflowStaticData('node');
71+
72+
if (webhookData.webhookId === undefined) {
73+
return false;
74+
}
75+
76+
try {
77+
await axios({
78+
...(await authenticate.call(this)),
79+
url: `/webhook/${webhookData.webhookId}`,
80+
method: 'GET',
81+
});
82+
} catch (error) {
83+
return false;
84+
}
85+
86+
return true;
87+
},
88+
async create(this: IHookFunctions): Promise<boolean> {
89+
const webhookUrl = this.getNodeWebhookUrl('default');
90+
const webhookData = this.getWorkflowStaticData('node');
91+
92+
const eventName = this.getNodeParameter('eventName') as string;
93+
const onlyLiveVersion = this.getNodeParameter('onlyLiveVersion') as boolean;
94+
95+
try {
96+
const result = await axios({
97+
...(await authenticate.call(this)),
98+
url: '/webhook',
99+
method: 'POST',
100+
data: {
101+
name: `n8n-${this.getNode().id}`,
102+
url: webhookUrl,
103+
eventName,
104+
onlyLiveVersion,
105+
},
106+
});
107+
108+
webhookData.webhookId = result.data.data.id;
109+
} catch (error) {
110+
return false;
111+
}
112+
113+
return true;
114+
},
115+
async delete(this: IHookFunctions): Promise<boolean> {
116+
const webhookData = this.getWorkflowStaticData('node');
117+
118+
try {
119+
await axios({
120+
...(await authenticate.call(this)),
121+
url: `/webhook/${webhookData.webhookId}`,
122+
method: 'DELETE',
123+
});
124+
} catch (error) {
125+
return false;
126+
}
127+
128+
delete webhookData.webhookId;
129+
130+
return true;
131+
},
132+
},
133+
};
134+
135+
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
136+
const req = this.getRequestObject();
137+
138+
return {
139+
workflowData: [this.helpers.returnJsonArray(req.body as IDataObject)],
140+
};
141+
}
142+
}
143+
144+
async function authenticate(
145+
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
146+
): Promise<AxiosRequestConfig> {
147+
const credentials = await this.getCredentials('shopwareAdminCredentialsApi');
148+
149+
const axiosRequestConfig: AxiosRequestConfig = {
150+
headers: {
151+
'Content-Type': 'application/json',
152+
},
153+
method: 'POST',
154+
url: credentials.url + '/oauth/token',
155+
data: {
156+
grant_type: 'client_credentials',
157+
client_id: credentials.clientId,
158+
client_secret: credentials.clientSecret,
159+
},
160+
};
161+
162+
const result = await axios(axiosRequestConfig);
163+
164+
const { access_token } = result.data;
165+
166+
return {
167+
headers: {
168+
'Content-Type': 'application/json',
169+
Authorization: `Bearer ${access_token}`,
170+
},
171+
baseURL: credentials.url as string,
172+
};
173+
}

Diff for: nodes/ShopwareAdminTriggerNode/shopware.svg

+10
Loading

Diff for: package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@c0d3bl1ck/n8n-nodes-shopware",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"description": "Community nodes to connect Shopware 6 with n8n.",
55
"keywords": [
66
"n8n-community-node-package"

0 commit comments

Comments
 (0)