@marvalt/wadapter 2.3.11 → 2.3.13

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/index.js CHANGED
@@ -111,29 +111,102 @@ class WordPressClient {
111
111
  return this.config.apiUrl || '';
112
112
  }
113
113
  }
114
+ /**
115
+ * Fetch posts with automatic pagination support
116
+ * @param params Query parameters including per_page for total items to fetch
117
+ * @returns All posts up to the limit, fetched across multiple pages if needed
118
+ */
114
119
  async getPosts(params = {}) {
115
- return this.makeRequest('/wp/v2/posts', params);
120
+ return this.fetchPaginated('/wp/v2/posts', params);
116
121
  }
117
122
  async getPost(id) {
118
123
  return this.makeRequest(`/wp/v2/posts/${id}`);
119
124
  }
125
+ /**
126
+ * Fetch pages with automatic pagination support
127
+ * @param params Query parameters including per_page for total items to fetch
128
+ * @returns All pages up to the limit, fetched across multiple pages if needed
129
+ */
120
130
  async getPages(params = {}) {
121
- return this.makeRequest('/wp/v2/pages', params);
131
+ return this.fetchPaginated('/wp/v2/pages', params);
122
132
  }
123
133
  async getPage(id) {
124
134
  return this.makeRequest(`/wp/v2/pages/${id}`);
125
135
  }
136
+ /**
137
+ * Fetch media with automatic pagination support
138
+ * @param params Query parameters including per_page for total items to fetch
139
+ * @returns All media items up to the limit, fetched across multiple pages if needed
140
+ */
126
141
  async getMedia(params = {}) {
127
- return this.makeRequest('/wp/v2/media', params);
142
+ return this.fetchPaginated('/wp/v2/media', params);
128
143
  }
129
144
  async getMediaItem(id) {
130
145
  return this.makeRequest(`/wp/v2/media/${id}`);
131
146
  }
147
+ /**
148
+ * Fetch categories with automatic pagination support
149
+ * @param params Query parameters including per_page for total items to fetch
150
+ * @returns All categories up to the limit, fetched across multiple pages if needed
151
+ */
132
152
  async getCategories(params = {}) {
133
- return this.makeRequest('/wp/v2/categories', params);
153
+ return this.fetchPaginated('/wp/v2/categories', params);
134
154
  }
155
+ /**
156
+ * Fetch tags with automatic pagination support
157
+ * @param params Query parameters including per_page for total items to fetch
158
+ * @returns All tags up to the limit, fetched across multiple pages if needed
159
+ */
135
160
  async getTags(params = {}) {
136
- return this.makeRequest('/wp/v2/tags', params);
161
+ return this.fetchPaginated('/wp/v2/tags', params);
162
+ }
163
+ /**
164
+ * Fetch data with automatic pagination
165
+ * Handles fetching all items up to the requested limit by making multiple API calls if needed
166
+ * @param endpoint The API endpoint to fetch from
167
+ * @param params Query parameters including per_page for total items to fetch
168
+ * @returns Array of all items fetched across all pages
169
+ */
170
+ async fetchPaginated(endpoint, params = {}) {
171
+ const requestedTotal = params.per_page || 100;
172
+ const itemsPerPage = Math.min(requestedTotal, 100); // WordPress max is typically 100
173
+ const maxPages = Math.ceil(requestedTotal / itemsPerPage);
174
+ let allItems = [];
175
+ let currentPage = 1;
176
+ let hasMorePages = true;
177
+ console.log(`🔄 Fetching ${endpoint} with pagination: ${requestedTotal} items requested (${maxPages} pages of ${itemsPerPage})`);
178
+ while (hasMorePages && currentPage <= maxPages && allItems.length < requestedTotal) {
179
+ const pageParams = {
180
+ ...params,
181
+ per_page: itemsPerPage,
182
+ page: currentPage,
183
+ };
184
+ console.log(`📄 Fetching page ${currentPage}/${maxPages} for ${endpoint}...`);
185
+ try {
186
+ const pageData = await this.makeRequest(endpoint, pageParams);
187
+ if (!pageData || pageData.length === 0) {
188
+ console.log(`✓ No more data on page ${currentPage}, stopping pagination`);
189
+ hasMorePages = false;
190
+ break;
191
+ }
192
+ allItems = allItems.concat(pageData);
193
+ console.log(`✓ Page ${currentPage}: fetched ${pageData.length} items (total: ${allItems.length})`);
194
+ // Stop if we got fewer items than requested per page (last page)
195
+ if (pageData.length < itemsPerPage) {
196
+ console.log(`✓ Reached last page (partial results)`);
197
+ hasMorePages = false;
198
+ }
199
+ currentPage++;
200
+ }
201
+ catch (error) {
202
+ console.error(`❌ Error fetching page ${currentPage}:`, error);
203
+ // Stop pagination on error but return what we have so far
204
+ hasMorePages = false;
205
+ }
206
+ }
207
+ console.log(`✅ Pagination complete for ${endpoint}: ${allItems.length} total items fetched`);
208
+ // Trim to exact requested amount if we fetched more
209
+ return allItems.slice(0, requestedTotal);
137
210
  }
138
211
  // Gravity Forms endpoints
139
212
  async getGravityForms() {
@@ -152,6 +225,15 @@ class WordPressClient {
152
225
  async getStaticDataNonce() {
153
226
  return this.makeRequest('/static-data/v1/nonce');
154
227
  }
228
+ /**
229
+ * Fetch a custom post type with automatic pagination support
230
+ * @param postType The custom post type slug (e.g., 'chapter-member', 'events')
231
+ * @param params Query parameters including per_page for total items to fetch
232
+ * @returns All items of the custom post type up to the limit
233
+ */
234
+ async getCustomPostType(postType, params = {}) {
235
+ return this.fetchPaginated(`/wp/v2/${postType}`, params);
236
+ }
155
237
  async getAllData(params = {}) {
156
238
  const [posts, pages, media, categories, tags] = await Promise.all([
157
239
  this.getPosts(params),