zudello-integration-sdk 1.0.78 → 1.0.80

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zudello-integration-sdk",
3
- "version": "1.0.78",
3
+ "version": "1.0.80",
4
4
  "description": "Zudello Integrations SDK",
5
5
  "main": "./src/index.js",
6
6
  "repository": {
@@ -107,13 +107,13 @@ class UniversalModule {
107
107
  let response = await this.list({ url, method, qs: qsCopy, body });
108
108
  yield response;
109
109
 
110
- let nextRef = this.extractRefFromLinkHeader(response?.headers?.link);
110
+ let nextRef = this.extractRefFromLinkHeader(response?.data?.headers?.link);
111
111
 
112
112
  while (nextRef) {
113
113
  qsCopy.ref = nextRef;
114
114
  response = await this.list({ url, method, qs: qsCopy, body });
115
115
  yield response;
116
- nextRef = this.extractRefFromLinkHeader(response?.headers?.link);
116
+ nextRef = this.extractRefFromLinkHeader(response?.data?.headers?.link);
117
117
  }
118
118
  }
119
119
  }
@@ -76,7 +76,7 @@ class UniversalModule {
76
76
  }
77
77
 
78
78
  /**
79
- * Universal Auto Pagination Listing By URL and Method.
79
+ * Universal Auto Pagination Listing By URL and Method (Page-based).
80
80
  * @param {string} url URL of listed data.
81
81
  * @param {string} method Method of listed data.
82
82
  * @param {object} header Headers to pass to the block
@@ -90,17 +90,64 @@ class UniversalModule {
90
90
  }
91
91
 
92
92
  let response = await this.list({ url, method, header, qs, body });
93
- let currentPageCount = response?.data?.length;
93
+
94
+ const data = response?.data || {};
95
+ const firstArrayKey = Object.keys(data).find(key => Array.isArray(data[key]));
96
+
97
+ let currentPageCount = data[firstArrayKey]?.length || 0;
94
98
 
95
99
  yield response;
96
100
 
97
- if (currentPageCount && currentPageCount === 100) {
98
- while (currentPageCount === 100) {
99
- qs.page = qs.page + 1;
101
+ while (currentPageCount === 100) {
102
+ qs.page = qs.page + 1;
100
103
 
101
- response = await this.list({ url, method, header, qs, body });
102
- currentPageCount = response?.data?.length;
104
+ response = await this.list({ url, method, header, qs, body });
105
+ const newData = response?.data || {};
106
+ currentPageCount = newData[firstArrayKey]?.length || 0;
103
107
 
108
+ if (currentPageCount > 0) {
109
+ yield response;
110
+ }
111
+ }
112
+ }
113
+
114
+ /**
115
+ * Universal Auto Pagination Listing By URL and Method (Cursor-based).
116
+ * Used for endpoints like Journals that use offset cursors instead of page numbers.
117
+ * @param {string} url URL of listed data.
118
+ * @param {string} method Method of listed data.
119
+ * @param {object} header Headers to pass to the block
120
+ * @param {object} qs Some available filters inside: offset.
121
+ * @param {object} body Some available data inside.
122
+ * @param {string} cursorField Field name to use as cursor (e.g., 'JournalNumber').
123
+ * @returns {object} Auto Pagination responses.
124
+ */
125
+ async *autoPaginationCursorList({ url, method, header = {}, qs = {}, body = {} }) {
126
+ let response = await this.request({ url, method, header, qs, body });
127
+
128
+ const data = response?.data || {};
129
+ const firstArrayKey = Object.keys(data).find(key => Array.isArray(data[key]));
130
+
131
+ let records = response?.data?.[firstArrayKey] || [];
132
+ let currentPageCount = records.length;
133
+
134
+ yield response;
135
+
136
+ while (currentPageCount === 100 && records.length > 0) {
137
+ const lastRecord = records[records.length - 1];
138
+ const cursorValue = lastRecord?.[cursorField];
139
+
140
+ if (!cursorValue) {
141
+ break;
142
+ }
143
+
144
+ qs.offset = cursorValue;
145
+
146
+ response = await this.request({ url, method, header, qs, body });
147
+ records = response?.data?.[firstArrayKey] || [];
148
+ currentPageCount = records.length;
149
+
150
+ if (currentPageCount > 0) {
104
151
  yield response;
105
152
  }
106
153
  }