n8n-nodes-mautic-advanced 0.2.0 → 0.2.1

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.
@@ -43,26 +43,44 @@ exports.mauticApiRequest = mauticApiRequest;
43
43
  * Make an API request to paginated mautic endpoint
44
44
  * and return all results
45
45
  */
46
+ // Optional: Slightly improved error handling
46
47
  async function mauticApiRequestAllItems(propertyName, method, endpoint, body = {}, query = {}, maxResults) {
47
48
  const returnData = [];
48
49
  let responseData;
49
50
  query.limit = 30;
50
51
  query.start = 0;
51
- // Removed unused 'page' variable
52
- do {
53
- responseData = await mauticApiRequest.call(this, method, endpoint, body, query);
54
- const values = Object.values(responseData[propertyName]);
55
- if (maxResults !== undefined && returnData.length + values.length > maxResults) {
56
- const needed = maxResults - returnData.length;
57
- returnData.push(...values.slice(0, needed));
58
- break;
52
+ while (true) {
53
+ try {
54
+ responseData = await mauticApiRequest.call(this, method, endpoint, body, query);
55
+ if (responseData.errors) {
56
+ throw new n8n_workflow_1.NodeApiError(this.getNode(), responseData);
57
+ }
58
+ const pageItems = responseData[propertyName] ? Object.values(responseData[propertyName]) : [];
59
+ if (!pageItems.length) {
60
+ break;
61
+ }
62
+ if (maxResults !== undefined && returnData.length + pageItems.length > maxResults) {
63
+ const needed = maxResults - returnData.length;
64
+ returnData.push(...pageItems.slice(0, needed));
65
+ break;
66
+ }
67
+ else {
68
+ returnData.push(...pageItems);
69
+ }
70
+ query.start = Number(query.start) + pageItems.length;
71
+ // If less than limit returned, no more data
72
+ if (pageItems.length < Number(query.limit)) {
73
+ break;
74
+ }
59
75
  }
60
- else {
61
- returnData.push(...values);
76
+ catch (error) {
77
+ // Optional: Only wrap non-NodeApiError errors
78
+ if (error instanceof n8n_workflow_1.NodeApiError) {
79
+ throw error;
80
+ }
81
+ throw new n8n_workflow_1.NodeApiError(this.getNode(), error);
62
82
  }
63
- query.start += query.limit;
64
- } while ((responseData.total !== undefined && returnData.length - parseInt(responseData.total, 10) < 0) &&
65
- (maxResults === undefined || returnData.length < maxResults));
83
+ }
66
84
  return returnData;
67
85
  }
68
86
  exports.mauticApiRequestAllItems = mauticApiRequestAllItems;
@@ -498,19 +498,11 @@ class MauticAdvanced {
498
498
  const simple = this.getNodeParameter('simple', i);
499
499
  const additionalFields = this.getNodeParameter('additionalFields', i);
500
500
  qs = Object.assign(qs, additionalFields);
501
- if (returnAll) {
502
- responseData = await GenericFunctions_1.mauticApiRequestAllItems.call(this, 'companies', 'GET', '/companies', {}, qs);
503
- }
504
- else {
505
- qs.limit = this.getNodeParameter('limit', i);
506
- qs.start = 0;
507
- responseData = await GenericFunctions_1.mauticApiRequest.call(this, 'GET', '/companies', {}, qs);
508
- if (responseData.errors) {
509
- throw new n8n_workflow_1.NodeApiError(this.getNode(), responseData);
510
- }
511
- responseData = responseData.companies;
512
- responseData = Object.values(responseData);
501
+ let limit = undefined;
502
+ if (!returnAll) {
503
+ limit = this.getNodeParameter('limit', i);
513
504
  }
505
+ responseData = await GenericFunctions_1.mauticApiRequestAllItems.call(this, 'companies', 'GET', '/companies', {}, qs, limit);
514
506
  if (simple) {
515
507
  //@ts-ignore
516
508
  responseData = responseData.map((item) => item.fields.all);
@@ -555,14 +547,11 @@ class MauticAdvanced {
555
547
  const returnAll = this.getNodeParameter('returnAll', i);
556
548
  const options = this.getNodeParameter('options', i);
557
549
  Object.assign(qs, options);
558
- if (returnAll) {
559
- responseData = await GenericFunctions_1.mauticApiRequestAllItems.call(this, 'tags', 'GET', '/tags', {}, qs);
560
- }
561
- else {
562
- qs.limit = this.getNodeParameter('limit', i);
563
- responseData = await GenericFunctions_1.mauticApiRequest.call(this, 'GET', '/tags', {}, qs);
564
- responseData = responseData.tags;
550
+ let limit = undefined;
551
+ if (!returnAll) {
552
+ limit = this.getNodeParameter('limit', i);
565
553
  }
554
+ responseData = await GenericFunctions_1.mauticApiRequestAllItems.call(this, 'tags', 'GET', '/tags', {}, qs, limit);
566
555
  }
567
556
  if (operation === 'delete') {
568
557
  const tagId = this.getNodeParameter('tagId', i);
@@ -802,28 +791,21 @@ class MauticAdvanced {
802
791
  const returnAll = this.getNodeParameter('returnAll', i);
803
792
  const options = this.getNodeParameter('options', i);
804
793
  qs = Object.assign(qs, options);
794
+ // Patch: enforce default sort order by id ascending if not set by user
795
+ if (!qs.orderBy) {
796
+ qs.orderBy = 'id';
797
+ }
798
+ if (!qs.orderByDir) {
799
+ qs.orderByDir = 'asc';
800
+ }
805
801
  if (qs.orderBy) {
806
802
  qs.orderBy = (0, change_case_1.snakeCase)(qs.orderBy);
807
803
  }
808
- if (returnAll) {
809
- responseData = await GenericFunctions_1.mauticApiRequestAllItems.call(this, 'contacts', 'GET', '/contacts', {}, qs);
810
- }
811
- else {
812
- const limit = this.getNodeParameter('limit', i);
813
- if (limit > 30) {
814
- responseData = await GenericFunctions_1.mauticApiRequestAllItems.call(this, 'contacts', 'GET', '/contacts', {}, qs, limit);
815
- }
816
- else {
817
- qs.limit = limit;
818
- qs.start = 0;
819
- responseData = await GenericFunctions_1.mauticApiRequest.call(this, 'GET', '/contacts', {}, qs);
820
- if (responseData.errors) {
821
- throw new n8n_workflow_1.NodeApiError(this.getNode(), responseData);
822
- }
823
- responseData = responseData.contacts;
824
- responseData = Object.values(responseData);
825
- }
804
+ let limit = undefined;
805
+ if (!returnAll) {
806
+ limit = this.getNodeParameter('limit', i);
826
807
  }
808
+ responseData = await GenericFunctions_1.mauticApiRequestAllItems.call(this, 'contacts', 'GET', '/contacts', {}, qs, limit);
827
809
  if (options.rawData === false) {
828
810
  //@ts-ignore
829
811
  responseData = responseData.map((item) => item.fields.all);
@@ -1063,14 +1045,11 @@ class MauticAdvanced {
1063
1045
  const returnAll = this.getNodeParameter('returnAll', i);
1064
1046
  const options = this.getNodeParameter('options', i);
1065
1047
  Object.assign(qs, options);
1066
- if (returnAll) {
1067
- responseData = await GenericFunctions_1.mauticApiRequestAllItems.call(this, 'categories', 'GET', '/categories', {}, qs);
1068
- }
1069
- else {
1070
- qs.limit = this.getNodeParameter('limit', i);
1071
- responseData = await GenericFunctions_1.mauticApiRequest.call(this, 'GET', '/categories', {}, qs);
1072
- responseData = responseData.categories;
1048
+ let limit = undefined;
1049
+ if (!returnAll) {
1050
+ limit = this.getNodeParameter('limit', i);
1073
1051
  }
1052
+ responseData = await GenericFunctions_1.mauticApiRequestAllItems.call(this, 'categories', 'GET', '/categories', {}, qs, limit);
1074
1053
  }
1075
1054
  if (operation === 'delete') {
1076
1055
  const categoryId = this.getNodeParameter('categoryId', i);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n8n-nodes-mautic-advanced",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Enhanced n8n node for Mautic with comprehensive API coverage including tags, campaigns, categories, and advanced contact management",
5
5
  "keywords": [
6
6
  "n8n",