Skip to content

Commit c91be1e

Browse files
committed
refactor(core): Extracted transport
- CatcherMessage replaced with CatcherMessage from @hawk.so/types - HawkJavaScriptEvent replaced with CatcherMessagePayload from @hawk.so/types - interface Transport moved to @hawk.so/core - interface Transport now has type-parameter CatcherMessageType with 'errors/javascript' as default type
1 parent eaf77a6 commit c91be1e

File tree

8 files changed

+34
-94
lines changed

8 files changed

+34
-94
lines changed

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export type { Logger, LogType } from './logger/logger';
55
export { isLoggerSet, setLogger, resetLogger, log } from './logger/logger';
66
export { isPlainObject, validateUser, validateContext, isValidEventPayload, isValidBreadcrumb } from './utils/validation';
77
export { Sanitizer } from './modules/sanitizer';
8+
export type { Transport } from './transports/transport';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { CatcherMessage, CatcherMessageType } from '@hawk.so/types';
2+
3+
/**
4+
* Transport interface — anything that can send a CatcherMessage
5+
*/
6+
export interface Transport<T extends CatcherMessageType = 'errors/javascript'> {
7+
send(message: CatcherMessage<T>): Promise<void>;
8+
}

packages/javascript/src/catcher.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export default class Catcher {
6363
/**
6464
* Catcher Type
6565
*/
66-
private readonly type: string = 'errors/javascript';
66+
private static readonly type = 'errors/javascript' as const;
6767

6868
/**
6969
* User project's Integration Token
@@ -91,13 +91,13 @@ export default class Catcher {
9191
* - Return `false` — the event will be dropped entirely.
9292
* - Any other value is invalid — the original event is sent as-is (a warning is logged).
9393
*/
94-
private readonly beforeSend: undefined | ((event: HawkJavaScriptEvent) => HawkJavaScriptEvent | false | void);
94+
private readonly beforeSend: undefined | ((event: HawkJavaScriptEvent<typeof Catcher.type>) => HawkJavaScriptEvent<typeof Catcher.type> | false | void);
9595

9696
/**
9797
* Transport for dialog between Catcher and Collector
9898
* (WebSocket decorator by default, or custom via settings.transport)
9999
*/
100-
private readonly transport: Transport;
100+
private readonly transport: Transport<typeof Catcher.type>;
101101

102102
/**
103103
* Module for parsing backtrace
@@ -410,7 +410,7 @@ export default class Catcher {
410410
*
411411
* @param errorFormatted - formatted error to send
412412
*/
413-
private sendErrorFormatted(errorFormatted: CatcherMessage): void {
413+
private sendErrorFormatted(errorFormatted: CatcherMessage<typeof Catcher.type>): void {
414414
this.transport.send(errorFormatted)
415415
.catch((sendingError) => {
416416
log('WebSocket sending error', 'error', sendingError);
@@ -423,7 +423,7 @@ export default class Catcher {
423423
* @param error - error to format
424424
* @param context - any additional data passed by user
425425
*/
426-
private async prepareErrorFormatted(error: Error | string, context?: EventContext): Promise<CatcherMessage> {
426+
private async prepareErrorFormatted(error: Error | string, context?: EventContext): Promise<CatcherMessage<typeof Catcher.type>> {
427427
let payload: HawkJavaScriptEvent = {
428428
title: this.getTitle(error),
429429
type: this.getType(error),
@@ -479,7 +479,7 @@ export default class Catcher {
479479

480480
return {
481481
token: this.token,
482-
catcherType: this.type,
482+
catcherType: Catcher.type,
483483
payload,
484484
};
485485
}
@@ -516,7 +516,7 @@ export default class Catcher {
516516
* and reject() provided with text reason instead of Error()
517517
*/
518518
if (notAnError) {
519-
return null;
519+
return undefined;
520520
}
521521

522522
return (error as Error).name;
@@ -526,7 +526,7 @@ export default class Catcher {
526526
* Release version
527527
*/
528528
private getRelease(): HawkJavaScriptEvent['release'] {
529-
return this.release !== undefined ? String(this.release) : null;
529+
return this.release !== undefined ? String(this.release) : undefined;
530530
}
531531

532532
/**
@@ -579,7 +579,7 @@ export default class Catcher {
579579
private getBreadcrumbsForEvent(): HawkJavaScriptEvent['breadcrumbs'] {
580580
const breadcrumbs = this.breadcrumbManager?.getBreadcrumbs();
581581

582-
return breadcrumbs && breadcrumbs.length > 0 ? breadcrumbs : null;
582+
return breadcrumbs && breadcrumbs.length > 0 ? breadcrumbs : undefined;
583583
}
584584

585585
/**
@@ -619,15 +619,15 @@ export default class Catcher {
619619
* and reject() provided with text reason instead of Error()
620620
*/
621621
if (notAnError) {
622-
return null;
622+
return undefined;
623623
}
624624

625625
try {
626626
return await this.stackParser.parse(error as Error);
627627
} catch (e) {
628628
log('Can not parse stack:', 'warn', e);
629629

630-
return null;
630+
return undefined;
631631
}
632632
}
633633

@@ -693,7 +693,7 @@ export default class Catcher {
693693
* @param errorFormatted - Hawk event prepared for sending
694694
* @param integrationAddons - extra addons
695695
*/
696-
private appendIntegrationAddons(errorFormatted: CatcherMessage, integrationAddons: JavaScriptCatcherIntegrations): void {
697-
Object.assign(errorFormatted.payload.addons, integrationAddons);
696+
private appendIntegrationAddons(errorFormatted: CatcherMessage<typeof Catcher.type>, integrationAddons: JavaScriptCatcherIntegrations): void {
697+
Object.assign(errorFormatted.payload.addons!, integrationAddons);
698698
}
699699
}

packages/javascript/src/modules/socket.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
import type { Transport } from '@hawk.so/core';
12
import { log } from '@hawk.so/core';
23
import type { CatcherMessage } from '@/types';
3-
import type { Transport } from '../types/transport';
4+
import type { CatcherMessageType } from '@hawk.so/types';
45

56
/**
67
* Custom WebSocket wrapper class
78
*
89
* @copyright CodeX
910
*/
10-
export default class Socket implements Transport {
11+
export default class Socket<T extends CatcherMessageType = 'errors/javascript'> implements Transport<T> {
1112
/**
1213
* Socket connection endpoint
1314
*/
@@ -32,7 +33,7 @@ export default class Socket implements Transport {
3233
* Queue of events collected while socket is not connected
3334
* They will be sent when connection will be established
3435
*/
35-
private eventsQueue: CatcherMessage[];
36+
private eventsQueue: CatcherMessage<T>[];
3637

3738
/**
3839
* Websocket instance
@@ -106,7 +107,7 @@ export default class Socket implements Transport {
106107
*
107108
* @param message - event data in Hawk Format
108109
*/
109-
public async send(message: CatcherMessage): Promise<void> {
110+
public async send(message: CatcherMessage<T>): Promise<void> {
110111
if (this.ws === null) {
111112
this.eventsQueue.push(message);
112113

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
1-
import type { HawkJavaScriptEvent } from './event';
1+
import type { CatcherMessage as HawkCatcherMessage } from '@hawk.so/types';
2+
import type { CatcherMessageType } from '@hawk.so/types/src/catchers/catcher-message';
23

34
/**
45
* Structure describing a message sending by Catcher
56
*/
6-
export interface CatcherMessage {
7-
/**
8-
* User project's Integration Token
9-
*/
10-
token: string;
11-
12-
/**
13-
* Hawk Catcher name
14-
*/
15-
catcherType: string;
16-
17-
/**
18-
* All information about the event
19-
*/
20-
payload: HawkJavaScriptEvent;
21-
}
7+
export type CatcherMessage<T extends CatcherMessageType = 'errors/javascript'> = HawkCatcherMessage<T>;
Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,7 @@
1-
import type { AffectedUser, BacktraceFrame, EventContext, EventData, JavaScriptAddons, Breadcrumb } from '@hawk.so/types';
2-
3-
/**
4-
* Event data with JS specific addons
5-
*/
6-
type JSEventData = EventData<JavaScriptAddons>;
1+
import type { CatcherMessagePayload } from '@hawk.so/types';
2+
import type { ErrorsCatcherType } from '@hawk.so/types/src/catchers/catcher-message';
73

84
/**
95
* Event will be sent to Hawk by Hawk JavaScript SDK
10-
*
11-
* The listed EventData properties will always be sent, so we define them as required in the type
126
*/
13-
export type HawkJavaScriptEvent = Omit<JSEventData, 'type' | 'release' | 'breadcrumbs' | 'user' | 'context' | 'addons' | 'backtrace' | 'catcherVersion'> & {
14-
/**
15-
* Event type: TypeError, ReferenceError etc
16-
* null for non-error events
17-
*/
18-
type: string | null;
19-
20-
/**
21-
* Current release (aka version, revision) of an application
22-
*/
23-
release: string | null;
24-
25-
/**
26-
* Breadcrumbs - chronological trail of events before the error
27-
*/
28-
breadcrumbs: Breadcrumb[] | null;
29-
30-
/**
31-
* Current authenticated user
32-
*/
33-
user: AffectedUser | null;
34-
35-
/**
36-
* Any other information collected and passed by user
37-
*/
38-
context: EventContext;
39-
40-
/**
41-
* Catcher-specific information
42-
*/
43-
addons: JavaScriptAddons;
44-
45-
/**
46-
* Stack
47-
* From the latest call to the earliest
48-
*/
49-
backtrace: BacktraceFrame[] | null;
50-
51-
/**
52-
* Catcher version
53-
*/
54-
catcherVersion: string;
55-
};
7+
export type HawkJavaScriptEvent<T extends ErrorsCatcherType = 'errors/javascript'> = CatcherMessagePayload<T>;

packages/javascript/src/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { CatcherMessage } from './catcher-message';
22
import type { HawkInitialSettings } from './hawk-initial-settings';
3-
import type { Transport } from './transport';
3+
import type { Transport } from '@hawk.so/core';
44
import type { HawkJavaScriptEvent } from './event';
55
import type { VueIntegrationData, NuxtIntegrationData, NuxtIntegrationAddons, JavaScriptCatcherIntegrations } from './integrations';
66
import type { BreadcrumbsAPI } from './breadcrumbs-api';

packages/javascript/src/types/transport.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)