* Fetch all pages for a paginated endpoint when you already know totalCount. * Builds page calls from totalCount and pageSize and aggregates all results. * - endpoint: path like "/api/channels/channels/" * - params: URLSearchParams for filters (will not be mutated) * - totalCount: total
(endpoint, params, totalCount, pageSize = 200)
| 118 | * Returns a flat array of results. Supports both array and {results, next} responses. |
| 119 | */ |
| 120 | static async fetchAllByCount(endpoint, params, totalCount, pageSize = 200) { |
| 121 | const total = Number(totalCount) || 0; |
| 122 | const size = Number(pageSize) || 200; |
| 123 | const totalPages = Math.max(1, Math.ceil(total / size)); |
| 124 | |
| 125 | const requests = []; |
| 126 | for (let page = 1; page <= totalPages; page++) { |
| 127 | const q = new URLSearchParams(params || new URLSearchParams()); |
| 128 | q.set('page', String(page)); |
| 129 | q.set('page_size', String(size)); |
| 130 | const url = `${host}${endpoint}?${q.toString()}`; |
| 131 | requests.push(request(url)); |
| 132 | } |
| 133 | |
| 134 | const responses = await Promise.all(requests); |
| 135 | const all = []; |
| 136 | for (const data of responses) { |
| 137 | if (Array.isArray(data)) { |
| 138 | all.push(...data); |
| 139 | } else if (Array.isArray(data?.results)) { |
| 140 | all.push(...data.results); |
| 141 | } |
| 142 | } |
| 143 | return all; |
| 144 | } |
| 145 | |
| 146 | static async fetchSuperUser() { |
| 147 | try { |