(request: (page: number) => Promise<HttpResponse<T[], any>>)
| 239 | |
| 240 | // @see : https://docs.gitea.com/development/api-usage#pagination |
| 241 | const paginate = async <T>(request: (page: number) => Promise<HttpResponse<T[], any>>) => { |
| 242 | let page = 1; |
| 243 | const result = await request(page); |
| 244 | const output: T[] = result.data; |
| 245 | |
| 246 | const totalCountString = result.headers.get('x-total-count'); |
| 247 | if (!totalCountString) { |
| 248 | const e = new Error("Header 'x-total-count' not found"); |
| 249 | Sentry.captureException(e); |
| 250 | throw e; |
| 251 | } |
| 252 | const totalCount = parseInt(totalCountString); |
| 253 | |
| 254 | while (output.length < totalCount) { |
| 255 | page++; |
| 256 | const result = await request(page); |
| 257 | if (result.data.length === 0) { |
| 258 | break; |
| 259 | } |
| 260 | output.push(...result.data); |
| 261 | } |
| 262 | |
| 263 | return output; |
| 264 | } |
no test coverage detected