|
| 1 | +import { z } from "zod"; |
| 2 | + |
| 3 | +export const ApplicationIdSchema = z |
| 4 | + .string() |
| 5 | + .describe( |
| 6 | + "Firebase app id. For an Android application, read the " + |
| 7 | + "mobilesdk_app_id value specified in the google-services.json file for " + |
| 8 | + "the current package name. For an iOS Application, read the GOOGLE_APP_ID " + |
| 9 | + "from GoogleService-Info.plist. If neither is available, ask the user to " + |
| 10 | + "provide the app id.", |
| 11 | + ); |
| 12 | + |
| 13 | +export const IssueIdSchema = z.string().describe("Crashlytics issue id, as hexidecimal uuid"); |
| 14 | + |
| 15 | +export const EventFilterSchema = z |
| 16 | + .object({ |
| 17 | + intervalStartTime: z.string().optional().describe(`A timestamp in ISO 8601 string format`), |
| 18 | + intervalEndTime: z.string().optional().describe(`A timestamp in ISO 8601 string format.`), |
| 19 | + versionDisplayNames: z |
| 20 | + .array(z.string()) |
| 21 | + .optional() |
| 22 | + .describe(`The version display names should be obtained from an API response.`), |
| 23 | + issueId: z.string().optional().describe(`Count events for the given issue`), |
| 24 | + issueVariantId: z.string().optional().describe(`Count events for the given issue variant`), |
| 25 | + issueErrorTypes: z |
| 26 | + .array(z.enum(["FATAL", "NON_FATAL", "ANR"])) |
| 27 | + .optional() |
| 28 | + .describe( |
| 29 | + `Count FATAL events (crashes), NON_FATAL events (exceptions) or ANR events (application not responding)`, |
| 30 | + ), |
| 31 | + issueSignals: z |
| 32 | + .array(z.enum(["SIGNAL_EARLY", "SIGNAL_FRESH", "SIGNAL_REGRESSED", "SIGNAL_REPETITIVE"])) |
| 33 | + .optional() |
| 34 | + .describe(`Count events matching the given signals`), |
| 35 | + operatingSystemDisplayNames: z |
| 36 | + .array(z.string()) |
| 37 | + .optional() |
| 38 | + .describe(`The operating system displayNames should be obtained from an API response`), |
| 39 | + deviceDisplayNames: z |
| 40 | + .array(z.string()) |
| 41 | + .optional() |
| 42 | + .describe(`The operating system displayNames should be obtained from an API response`), |
| 43 | + deviceFormFactors: z |
| 44 | + .array(z.enum(["PHONE", "TABLET", "DESKTOP", "TV", "WATCH"])) |
| 45 | + .optional() |
| 46 | + .describe(`Count events originating from the given device form factors`), |
| 47 | + }) |
| 48 | + .optional() |
| 49 | + .describe(`Only events matching the given filters will be counted. All filters are optional. |
| 50 | + If setting a time interval, set both intervalStartTime and intervalEndTime.`); |
| 51 | + |
| 52 | +export type EventFilter = z.infer<typeof EventFilterSchema>; |
| 53 | + |
| 54 | +// Most models seem to understand the flattened, camelCase representation better. |
| 55 | +// This maps those strings to the filter params the API expects. |
| 56 | + |
| 57 | +const toolToParamMap: Record<string, string> = { |
| 58 | + intervalStartTime: "filter.interval.start_time", |
| 59 | + intervalEndTime: "filter.interval.end_time", |
| 60 | + versionDisplayNames: "filter.version.display_names", |
| 61 | + issueId: "filter.issue.id", |
| 62 | + issueVariantId: "filter.issue.variant_id", |
| 63 | + issueErrorTypes: "filter.issue.error_types", |
| 64 | + issueSignals: "filter.issue.signals", |
| 65 | + operatingSystemDisplayNames: "filter.operating_system.display_names", |
| 66 | + deviceDisplayNames: "filter.device.display_names", |
| 67 | + deviceFormFactors: "filter.device.form_factors", |
| 68 | +}; |
| 69 | + |
| 70 | +/** |
| 71 | + * Converts the model-friendly, flattened camelCase tool parameters into the |
| 72 | + * AIP-160 style url parameters for all of the filtering options. |
| 73 | + * @param filter an EventFilter |
| 74 | + * @return URLSearchParams for a request to the GetReport endpoint |
| 75 | + */ |
| 76 | +export function filterToUrlSearchParams(filter: EventFilter): URLSearchParams { |
| 77 | + const params = new URLSearchParams(); |
| 78 | + for (const [key, value] of Object.entries(filter || {})) { |
| 79 | + if (value === undefined) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + const paramKey: string = toolToParamMap[key]; |
| 83 | + if (Array.isArray(value)) { |
| 84 | + for (const v of value) { |
| 85 | + params.append(paramKey, v); |
| 86 | + } |
| 87 | + } else if (value) { |
| 88 | + params.set(paramKey, value); |
| 89 | + } |
| 90 | + } |
| 91 | + return params; |
| 92 | +} |
0 commit comments