|
| 1 | +import { Client } from "../apiv2"; |
| 2 | +import { logger } from "../logger"; |
| 3 | +import { FirebaseError } from "../error"; |
| 4 | +import { crashlyticsApiOrigin } from "../api"; |
| 5 | + |
| 6 | +const TIMEOUT = 10000; |
| 7 | + |
| 8 | +const apiClient = new Client({ |
| 9 | + urlPrefix: crashlyticsApiOrigin(), |
| 10 | + apiVersion: "v1alpha", |
| 11 | +}); |
| 12 | + |
| 13 | +export async function listTopIssues( |
| 14 | + projectId: string, |
| 15 | + appId: string, |
| 16 | + issueCount: number, |
| 17 | +): Promise<string> { |
| 18 | + try { |
| 19 | + const queryParams = new URLSearchParams(); |
| 20 | + queryParams.set("page_size", `${issueCount}`); |
| 21 | + |
| 22 | + const requestProjectId = parseProjectId(appId); |
| 23 | + if (requestProjectId === undefined) { |
| 24 | + throw new FirebaseError("Unable to get the projectId from the AppId."); |
| 25 | + } |
| 26 | + |
| 27 | + const response = await apiClient.request<void, string>({ |
| 28 | + method: "GET", |
| 29 | + headers: { |
| 30 | + "Content-Type": "application/json", |
| 31 | + }, |
| 32 | + path: `/projects/${requestProjectId}/apps/${appId}/reports/topIssues`, |
| 33 | + queryParams: queryParams, |
| 34 | + timeout: TIMEOUT, |
| 35 | + }); |
| 36 | + |
| 37 | + return response.body; |
| 38 | + } catch (err: any) { |
| 39 | + logger.debug(err.message); |
| 40 | + throw new FirebaseError( |
| 41 | + `Failed to fetch the top issues for the Firebase Project ${projectId}, AppId ${appId}. Error: ${err}.`, |
| 42 | + { original: err }, |
| 43 | + ); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +function parseProjectId(appId: string): string | undefined { |
| 48 | + const appIdParts = appId.split(":"); |
| 49 | + if (appIdParts.length > 1) { |
| 50 | + return appIdParts[1]; |
| 51 | + } |
| 52 | + return undefined; |
| 53 | +} |
0 commit comments