|
1 | 1 | // eslint-disable-next-line max-classes-per-file |
2 | 2 | import { inject, Injectable } from '@angular/core' |
3 | | -import { CommonHttpHeader, ContentType } from '@shiftcode/utilities' |
| 3 | +import { ContentType } from '@shiftcode/utilities' |
| 4 | +import { CommonHttpHeader } from '@shiftcode/utilities' |
4 | 5 |
|
5 | 6 | import { CLOUD_WATCH_LOG_V2_CONFIG } from './cloud-watch-log-config.injection-token' |
6 | 7 |
|
@@ -69,17 +70,14 @@ export class CloudWatchLogV2ApiService { |
69 | 70 | return (await result.json()) as LogStream |
70 | 71 | } |
71 | 72 |
|
72 | | - writeLogs(logStreamName: string, logs: LogEvent[]): Promise<void> { |
73 | | - const url = new URL(`${ApiPath.STREAMS}/${logStreamName}/${ApiPath.STREAM_LOGS}`, this.apiUrl) |
74 | | - |
75 | | - // since the sendBeacon does not support providing headers directly, |
76 | | - // we use a blob, to achieve the same effect (content-type application/json) |
77 | | - const data = new Blob([JSON.stringify({ logEvents: logs } satisfies WriteLogEvents)], { type: 'application/json' }) |
78 | | - |
79 | | - // we use the beacon api to ensure log sending requests are not cancelled on page unload |
80 | | - const ok = navigator.sendBeacon(url, data) |
81 | | - |
82 | | - return ok ? Promise.resolve() : Promise.reject(new Error('Failed to send logs via sendBeacon')) |
| 73 | + async writeLogs(logStreamName: string, logs: LogEvent[]): Promise<void> { |
| 74 | + // todo: use beaconApi ? |
| 75 | + const resp = await fetch(new URL(`${ApiPath.STREAMS}/${logStreamName}/${ApiPath.STREAM_LOGS}`, this.apiUrl), { |
| 76 | + method: 'POST', |
| 77 | + headers: { [CommonHttpHeader.CONTENT_TYPE]: ContentType.JSON }, |
| 78 | + body: JSON.stringify({ logEvents: logs } satisfies WriteLogEvents), |
| 79 | + }) |
| 80 | + await this.handleError(resp) |
83 | 81 | } |
84 | 82 |
|
85 | 83 | private async handleError(resp: Response): Promise<void> { |
|
0 commit comments