-
Notifications
You must be signed in to change notification settings - Fork 200
/
crashlytics.ts
394 lines (371 loc) · 12.8 KB
/
crashlytics.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import { FirebaseAlertData, getEndpointAnnotation } from '.';
import { CloudEvent, CloudFunction } from '../../core';
import * as options from '../../options';
/** Generic crashlytics issue interface */
interface Issue {
id: string;
title: string;
subtitle: string;
appVersion: string;
}
/**
* The internal payload object for a new fatal issue.
* Payload is wrapped inside a FirebaseAlertData object.
*/
export interface NewFatalIssuePayload {
['@type']: 'type.googleapis.com/google.events.firebase.firebasealerts.v1.CrashlyticsNewFatalIssuePayload';
/** Basic information of the Crashlytics issue */
issue: Issue;
}
/**
* The internal payload object for a new non-fatal issue.
* Payload is wrapped inside a FirebaseAlertData object.
*/
export interface NewNonfatalIssuePayload {
['@type']: 'type.googleapis.com/google.events.firebase.firebasealerts.v1.CrashlyticsNewNonfatalIssuePayload';
/** Basic information of the Crashlytics issue */
issue: Issue;
}
/**
* The internal payload object for a regression alert.
* Payload is wrapped inside a FirebaseAlertData object.
*/
export interface RegressionAlertPayload {
['@type']: 'type.googleapis.com/google.events.firebase.firebasealerts.v1.CrashlyticsRegressionAlertPayload';
/** The type of the Crashlytics issue, e.g. new fatal, new nonfatal, ANR */
type: string;
/** Basic information of the Crashlytics issue */
issue: Issue;
/**
* The time that the Crashlytics issues was most recently resolved before it
* began to reoccur.
*/
resolveTime: string;
}
/** Generic crashlytics trending issue interface */
interface TrendingIssueDetails {
/** The type of the Crashlytics issue, e.g. new fatal, new nonfatal, ANR */
type: string;
/** Basic information of the Crashlytics issue */
issue: Issue;
/** The number of crashes that occurred with the issue */
eventCount: number;
/** The number of distinct users that were affected by the issue */
userCount: number;
}
/**
* The internal payload object for a stability digest.
* Payload is wrapped inside a FirebaseAlertData object.
*/
export interface StabilityDigestPayload {
['@type']: 'type.googleapis.com/google.events.firebase.firebasealerts.v1.CrashlyticsStabilityDigestPayload';
/**
* The date that the digest gets created. Issues in the digest should have the
* same date as the digest date
*/
digestDate: string;
/** A stability digest containing several trending Crashlytics issues */
trendingIssues: TrendingIssueDetails[];
}
/**
* The internal payload object for a velocity alert.
* Payload is wrapped inside a FirebaseAlertData object.
*/
export interface VelocityAlertPayload {
['@type']: 'type.googleapis.com/google.events.firebase.firebasealerts.v1.CrashlyticsVelocityAlertPayload';
/** Basic information of the Crashlytics issue */
issue: Issue;
/** The time that the Crashlytics issue gets created */
createTime: string;
/**
* The number of user sessions for the given app version that had this
* specific crash issue in the time period used to trigger the velocity alert.
*/
crashCount: number;
/**
* The percentage of user sessions for the given app version that had this
* specific crash issue in the time period used to trigger the velocity alert.
*/
crashPercentage: number;
/**
* The first app version where this issue was seen, and not necessarily the
* version that has triggered the alert.
*/
firstVersion: string;
}
/**
* The internal payload object for a new Application Not Responding issue.
* Payload is wrapped inside a FirebaseAlertData object.
*/
export interface NewAnrIssuePayload {
['@type']: 'type.googleapis.com/google.events.firebase.firebasealerts.v1.CrashlyticsNewAnrIssuePayload';
/** Basic information of the Crashlytics issue */
issue: Issue;
}
interface WithAlertTypeAndApp {
/** The type of the alerts that got triggered. */
alertType: string;
/** The Firebase App ID that’s associated with the alert. */
appId: string;
}
/**
* A custom CloudEvent for Firebase Alerts (with custom extension attributes).
*/
export type CrashlyticsEvent<T> = CloudEvent<
FirebaseAlertData<T>,
WithAlertTypeAndApp
>;
/** @internal */
export const newFatalIssueAlert = 'crashlytics.newFatalIssue';
/** @internal */
export const newNonfatalIssueAlert = 'crashlytics.newNonfatalIssue';
/** @internal */
export const regressionAlert = 'crashlytics.regression';
/** @internal */
export const stabilityDigestAlert = 'crashlytics.stabilityDigest';
/** @internal */
export const velocityAlert = 'crashlytics.velocity';
/** @internal */
export const newAnrIssueAlert = 'crashlytics.newAnrIssue';
/**
* Configuration for crashlytics functions.
*/
export interface CrashlyticsOptions extends options.EventHandlerOptions {
appId?: string;
}
/**
* Declares a function that can handle a new fatal issue published to crashlytics.
*/
export function onNewFatalIssuePublished(
handler: (event: CrashlyticsEvent<NewFatalIssuePayload>) => any | Promise<any>
): CloudFunction<FirebaseAlertData<NewFatalIssuePayload>>;
export function onNewFatalIssuePublished(
appId: string,
handler: (event: CrashlyticsEvent<NewFatalIssuePayload>) => any | Promise<any>
): CloudFunction<FirebaseAlertData<NewFatalIssuePayload>>;
export function onNewFatalIssuePublished(
opts: CrashlyticsOptions,
handler: (event: CrashlyticsEvent<NewFatalIssuePayload>) => any | Promise<any>
): CloudFunction<FirebaseAlertData<NewFatalIssuePayload>>;
export function onNewFatalIssuePublished(
appIdOrOptsOrHandler:
| string
| CrashlyticsOptions
| ((event: CrashlyticsEvent<NewFatalIssuePayload>) => any | Promise<any>),
handler?: (
event: CrashlyticsEvent<NewFatalIssuePayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<NewFatalIssuePayload>> {
return onOperation<NewFatalIssuePayload>(
newFatalIssueAlert,
appIdOrOptsOrHandler,
handler
);
}
/**
* Declares a function that can handle aa new non-fatal issue published to crashlytics.
*/
export function onNewNonfatalIssuePublished(
handler: (
event: CrashlyticsEvent<NewNonfatalIssuePayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<NewNonfatalIssuePayload>>;
export function onNewNonfatalIssuePublished(
appId: string,
handler: (
event: CrashlyticsEvent<NewNonfatalIssuePayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<NewNonfatalIssuePayload>>;
export function onNewNonfatalIssuePublished(
opts: CrashlyticsOptions,
handler: (
event: CrashlyticsEvent<NewNonfatalIssuePayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<NewNonfatalIssuePayload>>;
export function onNewNonfatalIssuePublished(
appIdOrOptsOrHandler:
| string
| CrashlyticsOptions
| ((
event: CrashlyticsEvent<NewNonfatalIssuePayload>
) => any | Promise<any>),
handler?: (
event: CrashlyticsEvent<NewNonfatalIssuePayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<NewNonfatalIssuePayload>> {
return onOperation<NewNonfatalIssuePayload>(
newNonfatalIssueAlert,
appIdOrOptsOrHandler,
handler
);
}
/**
* Declares a function that can handle a regression alert published to crashlytics.
*/
export function onRegressionAlertPublished(
handler: (
event: CrashlyticsEvent<RegressionAlertPayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<RegressionAlertPayload>>;
export function onRegressionAlertPublished(
appId: string,
handler: (
event: CrashlyticsEvent<RegressionAlertPayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<RegressionAlertPayload>>;
export function onRegressionAlertPublished(
opts: CrashlyticsOptions,
handler: (
event: CrashlyticsEvent<RegressionAlertPayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<RegressionAlertPayload>>;
export function onRegressionAlertPublished(
appIdOrOptsOrHandler:
| string
| CrashlyticsOptions
| ((event: CrashlyticsEvent<RegressionAlertPayload>) => any | Promise<any>),
handler?: (
event: CrashlyticsEvent<RegressionAlertPayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<RegressionAlertPayload>> {
return onOperation<RegressionAlertPayload>(
regressionAlert,
appIdOrOptsOrHandler,
handler
);
}
/**
* Declares a function that can handle a stability digest published to crashlytics.
*/
export function onStabilityDigestPublished(
handler: (
event: CrashlyticsEvent<StabilityDigestPayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<StabilityDigestPayload>>;
export function onStabilityDigestPublished(
appId: string,
handler: (
event: CrashlyticsEvent<StabilityDigestPayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<StabilityDigestPayload>>;
export function onStabilityDigestPublished(
opts: CrashlyticsOptions,
handler: (
event: CrashlyticsEvent<StabilityDigestPayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<StabilityDigestPayload>>;
export function onStabilityDigestPublished(
appIdOrOptsOrHandler:
| string
| CrashlyticsOptions
| ((event: CrashlyticsEvent<StabilityDigestPayload>) => any | Promise<any>),
handler?: (
event: CrashlyticsEvent<StabilityDigestPayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<StabilityDigestPayload>> {
return onOperation<StabilityDigestPayload>(
stabilityDigestAlert,
appIdOrOptsOrHandler,
handler
);
}
/**
* Declares a function that can handle a velocity alert published to crashlytics.
*/
export function onVelocityAlertPublished(
handler: (event: CrashlyticsEvent<VelocityAlertPayload>) => any | Promise<any>
): CloudFunction<FirebaseAlertData<VelocityAlertPayload>>;
export function onVelocityAlertPublished(
appId: string,
handler: (event: CrashlyticsEvent<VelocityAlertPayload>) => any | Promise<any>
): CloudFunction<FirebaseAlertData<VelocityAlertPayload>>;
export function onVelocityAlertPublished(
opts: CrashlyticsOptions,
handler: (event: CrashlyticsEvent<VelocityAlertPayload>) => any | Promise<any>
): CloudFunction<FirebaseAlertData<VelocityAlertPayload>>;
export function onVelocityAlertPublished(
appIdOrOptsOrHandler:
| string
| CrashlyticsOptions
| ((event: CrashlyticsEvent<VelocityAlertPayload>) => any | Promise<any>),
handler?: (
event: CrashlyticsEvent<VelocityAlertPayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<VelocityAlertPayload>> {
return onOperation<VelocityAlertPayload>(
velocityAlert,
appIdOrOptsOrHandler,
handler
);
}
/**
* Declares a function that can handle a new Application Not Responding issue published to crashlytics.
*/
export function onNewAnrIssuePublished(
handler: (event: CrashlyticsEvent<NewAnrIssuePayload>) => any | Promise<any>
): CloudFunction<FirebaseAlertData<NewAnrIssuePayload>>;
export function onNewAnrIssuePublished(
appId: string,
handler: (event: CrashlyticsEvent<NewAnrIssuePayload>) => any | Promise<any>
): CloudFunction<FirebaseAlertData<NewAnrIssuePayload>>;
export function onNewAnrIssuePublished(
opts: CrashlyticsOptions,
handler: (event: CrashlyticsEvent<NewAnrIssuePayload>) => any | Promise<any>
): CloudFunction<FirebaseAlertData<NewAnrIssuePayload>>;
export function onNewAnrIssuePublished(
appIdOrOptsOrHandler:
| string
| CrashlyticsOptions
| ((event: CrashlyticsEvent<NewAnrIssuePayload>) => any | Promise<any>),
handler?: (event: CrashlyticsEvent<NewAnrIssuePayload>) => any | Promise<any>
): CloudFunction<FirebaseAlertData<NewAnrIssuePayload>> {
return onOperation<NewAnrIssuePayload>(
newAnrIssueAlert,
appIdOrOptsOrHandler,
handler
);
}
/** @internal */
export function onOperation<T>(
alertType: string,
appIdOrOptsOrHandler:
| string
| CrashlyticsOptions
| ((event: CrashlyticsEvent<T>) => any | Promise<any>),
handler: (event: CrashlyticsEvent<T>) => any | Promise<any>
): CloudFunction<FirebaseAlertData<T>> {
if (typeof appIdOrOptsOrHandler === 'function') {
handler = appIdOrOptsOrHandler as (
event: CrashlyticsEvent<T>
) => any | Promise<any>;
appIdOrOptsOrHandler = {};
}
const [opts, appId] = getOptsAndApp(
appIdOrOptsOrHandler as string | CrashlyticsOptions
);
const func = (raw: CloudEvent<unknown>) => {
return handler(raw as CrashlyticsEvent<T>);
};
func.run = handler;
func.__endpoint = getEndpointAnnotation(opts, alertType, appId);
return func;
}
/**
* @internal
* Helper function to parse the function opts and appId.
*/
export function getOptsAndApp(
appIdOrOpts: string | CrashlyticsOptions
): [options.EventHandlerOptions, string | undefined] {
let opts: options.EventHandlerOptions;
let appId: string | undefined;
if (typeof appIdOrOpts === 'string') {
opts = {};
appId = appIdOrOpts;
} else {
appId = appIdOrOpts.appId;
opts = { ...appIdOrOpts };
delete (opts as any).appId;
}
return [opts, appId];
}