Skip to content

Commit 72657d8

Browse files
committed
Refactor common parsing logic
1 parent 40c130f commit 72657d8

11 files changed

+51
-151
lines changed

src/crashlytics/addNote.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Client } from "../apiv2";
22
import { logger } from "../logger";
33
import { FirebaseError } from "../error";
44
import { crashlyticsApiOrigin } from "../api";
5+
import { parseProjectNumber } from "./utils";
56

67
const TIMEOUT = 10000;
78

@@ -15,12 +16,8 @@ type NoteRequest = {
1516
};
1617

1718
export async function addNote(appId: string, issueId: string, note: string): Promise<string> {
19+
const requestProjectId = parseProjectNumber(appId);
1820
try {
19-
const requestProjectId = parseProjectId(appId);
20-
if (requestProjectId === undefined) {
21-
throw new FirebaseError("Unable to get the projectId from the AppId.");
22-
}
23-
2421
const response = await apiClient.request<NoteRequest, string>({
2522
method: "POST",
2623
headers: {
@@ -40,11 +37,3 @@ export async function addNote(appId: string, issueId: string, note: string): Pro
4037
);
4138
}
4239
}
43-
44-
function parseProjectId(appId: string): string | undefined {
45-
const appIdParts = appId.split(":");
46-
if (appIdParts.length > 1) {
47-
return appIdParts[1];
48-
}
49-
return undefined;
50-
}

src/crashlytics/deleteNote.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Client } from "../apiv2";
22
import { logger } from "../logger";
33
import { FirebaseError } from "../error";
44
import { crashlyticsApiOrigin } from "../api";
5+
import { parseProjectNumber } from "./utils";
56

67
const TIMEOUT = 10000;
78

@@ -11,12 +12,8 @@ const apiClient = new Client({
1112
});
1213

1314
export async function deleteNote(appId: string, issueId: string, noteId: string): Promise<void> {
15+
const requestProjectId = parseProjectNumber(appId);
1416
try {
15-
const requestProjectId = parseProjectId(appId);
16-
if (requestProjectId === undefined) {
17-
throw new FirebaseError("Unable to get the projectId from the AppId.");
18-
}
19-
2017
await apiClient.request<void, void>({
2118
method: "DELETE",
2219
path: `/projects/${requestProjectId}/apps/${appId}/issues/${issueId}/notes/${noteId}`,
@@ -30,11 +27,3 @@ export async function deleteNote(appId: string, issueId: string, noteId: string)
3027
);
3128
}
3229
}
33-
34-
function parseProjectId(appId: string): string | undefined {
35-
const appIdParts = appId.split(":");
36-
if (appIdParts.length > 1) {
37-
return appIdParts[1];
38-
}
39-
return undefined;
40-
}

src/crashlytics/getIssueDetails.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Client } from "../apiv2";
22
import { logger } from "../logger";
33
import { FirebaseError } from "../error";
44
import { crashlyticsApiOrigin } from "../api";
5+
import { parseProjectNumber } from "./utils";
56

67
const TIMEOUT = 10000;
78

@@ -11,18 +12,14 @@ const apiClient = new Client({
1112
});
1213

1314
export async function getIssueDetails(appId: string, issueId: string): Promise<string> {
15+
const requestProjectId = parseProjectNumber(appId);
1416
try {
15-
const requestProjectNumber = parseProjectNumber(appId);
16-
if (requestProjectNumber === undefined) {
17-
throw new FirebaseError("Unable to get the projectId from the AppId.");
18-
}
19-
2017
const response = await apiClient.request<void, string>({
2118
method: "GET",
2219
headers: {
2320
"Content-Type": "application/json",
2421
},
25-
path: `/projects/${requestProjectNumber}/apps/${appId}/issues/${issueId}`,
22+
path: `/projects/${requestProjectId}/apps/${appId}/issues/${issueId}`,
2623
timeout: TIMEOUT,
2724
});
2825

@@ -35,11 +32,3 @@ export async function getIssueDetails(appId: string, issueId: string): Promise<s
3532
);
3633
}
3734
}
38-
39-
function parseProjectNumber(appId: string): string | undefined {
40-
const appIdParts = appId.split(":");
41-
if (appIdParts.length > 1) {
42-
return appIdParts[1];
43-
}
44-
return undefined;
45-
}

src/crashlytics/getSampleCrash.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Client } from "../apiv2";
22
import { logger } from "../logger";
33
import { FirebaseError } from "../error";
44
import { crashlyticsApiOrigin } from "../api";
5+
import { parseProjectNumber } from "./utils";
56

67
const TIMEOUT = 10000;
78

@@ -16,6 +17,7 @@ export async function getSampleCrash(
1617
sampleCount: number,
1718
variantId?: string,
1819
): Promise<string> {
20+
const requestProjectId = parseProjectNumber(appId);
1921
try {
2022
const queryParams = new URLSearchParams();
2123
queryParams.set("filter.issue.id", issueId);
@@ -24,18 +26,13 @@ export async function getSampleCrash(
2426
queryParams.set("filter.issue.variant_id", variantId);
2527
}
2628

27-
const requestProjectNumber = parseProjectNumber(appId);
28-
if (requestProjectNumber === undefined) {
29-
throw new FirebaseError("Unable to get the projectId from the AppId.");
30-
}
31-
3229
logger.debug(`[mcp][crashlytics] getSampleCrash query paramaters: ${queryParams}`);
3330
const response = await apiClient.request<void, string>({
3431
method: "GET",
3532
headers: {
3633
"Content-Type": "application/json",
3734
},
38-
path: `/projects/${requestProjectNumber}/apps/${appId}/events`,
35+
path: `/projects/${requestProjectId}/apps/${appId}/events`,
3936
queryParams: queryParams,
4037
timeout: TIMEOUT,
4138
});
@@ -49,11 +46,3 @@ export async function getSampleCrash(
4946
);
5047
}
5148
}
52-
53-
function parseProjectNumber(appId: string): string | undefined {
54-
const appIdParts = appId.split(":");
55-
if (appIdParts.length > 1) {
56-
return appIdParts[1];
57-
}
58-
return undefined;
59-
}

src/crashlytics/listNotes.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Client } from "../apiv2";
22
import { logger } from "../logger";
33
import { FirebaseError } from "../error";
44
import { crashlyticsApiOrigin } from "../api";
5+
import { parseProjectNumber } from "./utils";
56

67
const TIMEOUT = 10000;
78

@@ -15,15 +16,11 @@ export async function listNotes(
1516
issueId: string,
1617
noteCount: number,
1718
): Promise<string> {
19+
const requestProjectId = parseProjectNumber(appId);
1820
try {
1921
const queryParams = new URLSearchParams();
2022
queryParams.set("page_size", `${noteCount}`);
2123

22-
const requestProjectId = parseProjectId(appId);
23-
if (requestProjectId === undefined) {
24-
throw new FirebaseError("Unable to get the projectId from the AppId.");
25-
}
26-
2724
logger.debug(`[mcp][crashlytics] listNotes query paramaters: ${queryParams}`);
2825
const response = await apiClient.request<void, string>({
2926
method: "GET",
@@ -44,11 +41,3 @@ export async function listNotes(
4441
);
4542
}
4643
}
47-
48-
function parseProjectId(appId: string): string | undefined {
49-
const appIdParts = appId.split(":");
50-
if (appIdParts.length > 1) {
51-
return appIdParts[1];
52-
}
53-
return undefined;
54-
}

src/crashlytics/listTopDevices.ts

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Client } from "../apiv2";
22
import { logger } from "../logger";
33
import { FirebaseError } from "../error";
44
import { crashlyticsApiOrigin } from "../api";
5+
import { parsePlatform, parseProjectNumber } from "./utils";
56

67
const TIMEOUT = 10000;
78

@@ -10,30 +11,20 @@ const apiClient = new Client({
1011
apiVersion: "v1alpha",
1112
});
1213

13-
enum PLATFORM_PATH {
14-
ANDROID = "topAndroidDevices",
15-
IOS = "topAppleDevices",
16-
}
17-
1814
export async function listTopDevices(
1915
appId: string,
2016
deviceCount: number,
2117
issueId?: string,
2218
): Promise<string> {
19+
const requestProjectId = parseProjectNumber(appId);
20+
const platformPath = parsePlatform(appId);
2321
try {
2422
const queryParams = new URLSearchParams();
2523
queryParams.set("page_size", `${deviceCount}`);
2624
if (issueId) {
2725
queryParams.set("filter.issue.id", issueId);
2826
}
2927

30-
const requestProjectId = parseProjectId(appId);
31-
if (requestProjectId === undefined) {
32-
throw new FirebaseError("Unable to get the projectId from the AppId.");
33-
}
34-
35-
const platformPath = parsePlatform(appId);
36-
3728
logger.debug(`[mcp][crashlytics] listTopDevices query paramaters: ${queryParams}`);
3829
const response = await apiClient.request<void, string>({
3930
method: "GET",
@@ -54,23 +45,3 @@ export async function listTopDevices(
5445
);
5546
}
5647
}
57-
58-
function parseProjectId(appId: string): string | undefined {
59-
const appIdParts = appId.split(":");
60-
if (appIdParts.length > 1) {
61-
return appIdParts[1];
62-
}
63-
return undefined;
64-
}
65-
66-
function parsePlatform(appId: string): PLATFORM_PATH {
67-
const appIdParts = appId.split(":");
68-
if (appIdParts.length < 3) {
69-
throw new FirebaseError("Unable to get the platform from the AppId.");
70-
}
71-
72-
if (appIdParts[2] === "android") {
73-
return PLATFORM_PATH.ANDROID;
74-
} else if (appIdParts[2] === "ios") return PLATFORM_PATH.IOS;
75-
throw new FirebaseError(`Only android or ios apps are supported.`);
76-
}

src/crashlytics/listTopIssues.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Client } from "../apiv2";
22
import { logger } from "../logger";
33
import { FirebaseError } from "../error";
44
import { crashlyticsApiOrigin } from "../api";
5+
import { parseProjectNumber } from "./utils";
56

67
const TIMEOUT = 10000;
78

@@ -15,16 +16,12 @@ export async function listTopIssues(
1516
issueType: string,
1617
issueCount: number,
1718
): Promise<string> {
19+
const requestProjectId = parseProjectNumber(appId);
1820
try {
1921
const queryParams = new URLSearchParams();
2022
queryParams.set("page_size", `${issueCount}`);
2123
queryParams.set("filter.issue.error_types", `${issueType}`);
2224

23-
const requestProjectId = parseProjectId(appId);
24-
if (requestProjectId === undefined) {
25-
throw new FirebaseError("Unable to get the projectId from the AppId.");
26-
}
27-
2825
logger.debug(`[mcp][crashlytics] listTopIssues query paramaters: ${queryParams}`);
2926
const response = await apiClient.request<void, string>({
3027
method: "GET",
@@ -45,11 +42,3 @@ export async function listTopIssues(
4542
);
4643
}
4744
}
48-
49-
function parseProjectId(appId: string): string | undefined {
50-
const appIdParts = appId.split(":");
51-
if (appIdParts.length > 1) {
52-
return appIdParts[1];
53-
}
54-
return undefined;
55-
}

src/crashlytics/listTopOperatingSystems.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Client } from "../apiv2";
22
import { logger } from "../logger";
33
import { FirebaseError } from "../error";
44
import { crashlyticsApiOrigin } from "../api";
5+
import { parseProjectNumber } from "./utils";
56

67
const TIMEOUT = 10000;
78

@@ -15,18 +16,14 @@ export async function listTopOperatingSystems(
1516
osCount: number,
1617
issueId?: string,
1718
): Promise<string> {
19+
const requestProjectId = parseProjectNumber(appId);
1820
try {
1921
const queryParams = new URLSearchParams();
2022
queryParams.set("page_size", `${osCount}`);
2123
if (issueId) {
2224
queryParams.set("filter.issue.id", issueId);
2325
}
2426

25-
const requestProjectId = parseProjectId(appId);
26-
if (requestProjectId === undefined) {
27-
throw new FirebaseError("Unable to get the projectId from the AppId.");
28-
}
29-
3027
logger.debug(`[mcp][crashlytics] listTopOperatingSystems query paramaters: ${queryParams}`);
3128
const response = await apiClient.request<void, string>({
3229
method: "GET",
@@ -47,11 +44,3 @@ export async function listTopOperatingSystems(
4744
);
4845
}
4946
}
50-
51-
function parseProjectId(appId: string): string | undefined {
52-
const appIdParts = appId.split(":");
53-
if (appIdParts.length > 1) {
54-
return appIdParts[1];
55-
}
56-
return undefined;
57-
}

src/crashlytics/listTopVersions.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Client } from "../apiv2";
22
import { logger } from "../logger";
33
import { FirebaseError } from "../error";
44
import { crashlyticsApiOrigin } from "../api";
5+
import { parseProjectNumber } from "./utils";
56

67
const TIMEOUT = 10000;
78

@@ -15,18 +16,14 @@ export async function listTopVersions(
1516
versionCount: number,
1617
issueId?: string,
1718
): Promise<string> {
19+
const requestProjectId = parseProjectNumber(appId);
1820
try {
1921
const queryParams = new URLSearchParams();
2022
queryParams.set("page_size", `${versionCount}`);
2123
if (issueId) {
2224
queryParams.set("filter.issue.id", issueId);
2325
}
2426

25-
const requestProjectId = parseProjectId(appId);
26-
if (requestProjectId === undefined) {
27-
throw new FirebaseError("Unable to get the projectId from the AppId.");
28-
}
29-
3027
logger.debug(`[mcp][crashlytics] listTopVersions query paramaters: ${queryParams}`);
3128
const response = await apiClient.request<void, string>({
3229
method: "GET",
@@ -47,11 +44,3 @@ export async function listTopVersions(
4744
);
4845
}
4946
}
50-
51-
function parseProjectId(appId: string): string | undefined {
52-
const appIdParts = appId.split(":");
53-
if (appIdParts.length > 1) {
54-
return appIdParts[1];
55-
}
56-
return undefined;
57-
}

0 commit comments

Comments
 (0)