|
| 1 | +import { BacktraceAttachment } from './model/attachment/BacktraceAttachment.js'; |
| 2 | +import { |
| 3 | + BacktraceAttachmentResponse, |
| 4 | + BacktraceReportSubmissionResult, |
| 5 | + BacktraceRequestHandler, |
| 6 | + BacktraceSubmitBody, |
| 7 | + BacktraceSubmitResponse, |
| 8 | + RequestBacktraceReportSubmission, |
| 9 | +} from './model/http/index.js'; |
| 10 | +import { |
| 11 | + BacktraceSubmitSummedMetricsBody, |
| 12 | + BacktraceSubmitUniqueMetricsBody, |
| 13 | +} from './model/http/model/metric/request/BacktraceSubmitMetricsBody.js'; |
| 14 | +import { MetricsUrlInformation } from './modules/metrics/MetricsUrlInformation.js'; |
| 15 | + |
| 16 | +export interface BacktraceCoreApiOptions { |
| 17 | + readonly url: string; |
| 18 | + readonly token?: string; |
| 19 | + |
| 20 | + readonly metrics?: { |
| 21 | + readonly url?: string; |
| 22 | + }; |
| 23 | + |
| 24 | + readonly requestBacktraceReportSubmission?: RequestBacktraceReportSubmission; |
| 25 | +} |
| 26 | + |
| 27 | +export class BacktraceCoreApi { |
| 28 | + private readonly _summedMetricsSubmissionUrl?: string; |
| 29 | + private readonly _uniqueMetricsSubmissionUrl?: string; |
| 30 | + |
| 31 | + private readonly _requestBacktraceReportSubmission: RequestBacktraceReportSubmission; |
| 32 | + |
| 33 | + constructor( |
| 34 | + options: BacktraceCoreApiOptions, |
| 35 | + private readonly _requestHandler: BacktraceRequestHandler, |
| 36 | + ) { |
| 37 | + this._summedMetricsSubmissionUrl = MetricsUrlInformation.generateSummedEventsUrl( |
| 38 | + options.metrics?.url ?? 'https://events.backtrace.io', |
| 39 | + options.url, |
| 40 | + options.token, |
| 41 | + ); |
| 42 | + |
| 43 | + this._uniqueMetricsSubmissionUrl = MetricsUrlInformation.generateUniqueEventsUrl( |
| 44 | + options.metrics?.url ?? 'https://events.backtrace.io', |
| 45 | + options.url, |
| 46 | + options.token, |
| 47 | + ); |
| 48 | + |
| 49 | + this._requestBacktraceReportSubmission = |
| 50 | + options.requestBacktraceReportSubmission ?? |
| 51 | + new RequestBacktraceReportSubmission( |
| 52 | + { |
| 53 | + url: options.url, |
| 54 | + }, |
| 55 | + this._requestHandler, |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + public sendReport( |
| 60 | + data: BacktraceSubmitBody, |
| 61 | + attachments: BacktraceAttachment[], |
| 62 | + abortSignal?: AbortSignal, |
| 63 | + ): Promise<BacktraceReportSubmissionResult<BacktraceSubmitResponse>> { |
| 64 | + return this._requestBacktraceReportSubmission.send(data, attachments, abortSignal); |
| 65 | + } |
| 66 | + |
| 67 | + public sendAttachment( |
| 68 | + rxid: string, |
| 69 | + attachment: BacktraceAttachment, |
| 70 | + abortSignal?: AbortSignal, |
| 71 | + ): Promise<BacktraceReportSubmissionResult<BacktraceAttachmentResponse>> { |
| 72 | + return this._requestBacktraceReportSubmission.sendAttachment(rxid, attachment, abortSignal); |
| 73 | + } |
| 74 | + |
| 75 | + public sendUniqueMetrics( |
| 76 | + metrics: BacktraceSubmitUniqueMetricsBody, |
| 77 | + abortSignal?: AbortSignal, |
| 78 | + ): Promise<BacktraceReportSubmissionResult<unknown>> { |
| 79 | + if (!this._uniqueMetricsSubmissionUrl) { |
| 80 | + throw new Error('Unique metrics URL is not available.'); |
| 81 | + } |
| 82 | + |
| 83 | + return this._requestHandler.post(this._uniqueMetricsSubmissionUrl, JSON.stringify(metrics), abortSignal); |
| 84 | + } |
| 85 | + |
| 86 | + public sendSummedMetrics( |
| 87 | + metrics: BacktraceSubmitSummedMetricsBody, |
| 88 | + abortSignal?: AbortSignal, |
| 89 | + ): Promise<BacktraceReportSubmissionResult<unknown>> { |
| 90 | + if (!this._summedMetricsSubmissionUrl) { |
| 91 | + throw new Error('Summed metrics URL is not available.'); |
| 92 | + } |
| 93 | + |
| 94 | + return this._requestHandler.post(this._summedMetricsSubmissionUrl, JSON.stringify(metrics), abortSignal); |
| 95 | + } |
| 96 | +} |
0 commit comments