|
| 1 | +// eslint-disable-next-line max-classes-per-file |
| 2 | +import { inject, Injectable } from '@angular/core' |
| 3 | +import { ContentType } from '@shiftcode/utilities' |
| 4 | +import { CommonHttpHeader } from '@shiftcode/utilities' |
| 5 | + |
| 6 | +import { CLOUD_WATCH_LOG_V2_CONFIG } from './cloud-watch-log-config.injection-token' |
| 7 | + |
| 8 | +export interface LogStream { |
| 9 | + logStreamName: string |
| 10 | + creationTime: number |
| 11 | + lastIngestionTime: number | null |
| 12 | +} |
| 13 | + |
| 14 | +export interface LogEvent { |
| 15 | + message: string |
| 16 | + timestamp: number |
| 17 | +} |
| 18 | + |
| 19 | +export interface WriteLogEvents { |
| 20 | + logEvents: LogEvent[] |
| 21 | +} |
| 22 | + |
| 23 | +export class HttpError extends Error { |
| 24 | + override readonly name: string = 'HttpError' |
| 25 | + |
| 26 | + constructor( |
| 27 | + readonly statusCode: number, |
| 28 | + message: string, |
| 29 | + ) { |
| 30 | + super(`${statusCode} - ${message}`) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +export class HttpApiError extends HttpError { |
| 35 | + override readonly name: string = 'HttpApiError' |
| 36 | + |
| 37 | + constructor( |
| 38 | + statusCode: number, |
| 39 | + readonly errorCode: string, |
| 40 | + message: string, |
| 41 | + ) { |
| 42 | + super(statusCode, `${message} (${errorCode})`) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +enum ApiPath { |
| 47 | + STREAMS = 'streams', |
| 48 | + STREAM_LOGS = 'logs', |
| 49 | +} |
| 50 | + |
| 51 | +@Injectable({ providedIn: 'root' }) |
| 52 | +export class CloudWatchLogV2ApiService { |
| 53 | + private readonly apiUrl = inject(CLOUD_WATCH_LOG_V2_CONFIG).apiUrl |
| 54 | + |
| 55 | + async createLogStream(logStreamName: string): Promise<void> { |
| 56 | + const resp = await fetch(new URL(ApiPath.STREAMS, this.apiUrl), { |
| 57 | + method: 'POST', |
| 58 | + headers: { [CommonHttpHeader.CONTENT_TYPE]: ContentType.JSON }, |
| 59 | + body: JSON.stringify({ logStreamName }), |
| 60 | + }) |
| 61 | + await this.handleError(resp) |
| 62 | + } |
| 63 | + |
| 64 | + async describeLogStream(logStreamName: string): Promise<LogStream> { |
| 65 | + const result = await fetch(new URL(`${ApiPath.STREAMS}/${logStreamName}`, this.apiUrl), { |
| 66 | + method: 'GET', |
| 67 | + headers: { [CommonHttpHeader.CONTENT_TYPE]: ContentType.JSON }, |
| 68 | + }) |
| 69 | + await this.handleError(result) |
| 70 | + return (await result.json()) as LogStream |
| 71 | + } |
| 72 | + |
| 73 | + async writeLogs(logStreamName: string, logs: LogEvent[]): Promise<void> { |
| 74 | + // we do not use the sendBeacon API since it will fail with cors preflight and would require ugly workarounds |
| 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 | + keepalive: true, // do not abort request on page unload |
| 80 | + }) |
| 81 | + await this.handleError(resp) |
| 82 | + } |
| 83 | + |
| 84 | + private async handleError(resp: Response): Promise<void> { |
| 85 | + if (!resp.ok) { |
| 86 | + const errorResponse: object = (await resp.json().catch(() => ({}))) ?? {} |
| 87 | + if ( |
| 88 | + 'message' in errorResponse && |
| 89 | + typeof errorResponse.message === 'string' && |
| 90 | + 'code' in errorResponse && |
| 91 | + typeof errorResponse.code === 'string' |
| 92 | + ) { |
| 93 | + throw new HttpApiError(resp.status, errorResponse.code, errorResponse.message) |
| 94 | + } else { |
| 95 | + throw new HttpError(resp.status, 'unknown error') |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments