Skip to content

Commit

Permalink
Merge pull request #105 from codex-team/impr-event-type
Browse files Browse the repository at this point in the history
chore(types): improve event type
  • Loading branch information
neSpecc authored Oct 13, 2024
2 parents 12527ce + 5780b0f commit 7dd8c26
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 31 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ jobs:
CI_JOB_NUMBER: 1
steps:
- uses: actions/checkout@v1
- name: Use Node.js 12.x
- name: Use Node.js 16.x
uses: actions/setup-node@v1
with:
node-version: 12.x
node-version: 16.x
- run: yarn install
- run: yarn lint-test
# - uses: andresz1/[email protected]
# with:
# github_token: ${{ secrets.GITHUB_TOKEN }}
# skip_step: build
# skip_step: build
2 changes: 1 addition & 1 deletion .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
node-version: 16
registry-url: https://registry.npmjs.org/
- run: yarn
- run: yarn lint-test
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Initialization settings:
| `token` | string | **required** | Your project's Integration Token |
| `release` | string/number | optional | Unique identifier of the release. Used for source map consuming (see below) |
| `user` | {id: string, name?: string, image?: string, url?: string} | optional | Current authenticated user |
| `context` | object | optional | Any data you want to pass with every message. |
| `context` | object | optional | Any data you want to pass with every message. Has limitation of length. |
| `vue` | Vue constructor | optional | Pass Vue constructor to set up the [Vue integration](#integrate-to-vue-application) |
| `disableGlobalErrorsHandling` | boolean | optional | Do not initialize global errors handling |
| `beforeSend` | function(event) => event | optional | This Method allows you to filter any data you don't want sending to Hawk |
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hawk.so/javascript",
"version": "3.0.4",
"version": "3.0.5",
"description": "JavaScript errors tracking for Hawk.so",
"main": "./dist/hawk.js",
"types": "./dist/index.d.ts",
Expand Down
16 changes: 7 additions & 9 deletions src/catcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { VueIntegration } from './integrations/vue';
import { generateRandomId } from './utils';
import {
AffectedUser,
BacktraceFrame,
EventContext,
JavaScriptAddons,
VueIntegrationAddons,
Expand Down Expand Up @@ -267,8 +266,6 @@ export default class Catcher {
* @param errorFormatted - formatted error to send
*/
private sendErrorFormatted(errorFormatted: CatcherMessage): void {
console.log('send', errorFormatted);

this.transport.send(errorFormatted)
.catch((sendingError) => {
log('WebSocket sending error', 'error', sendingError);
Expand All @@ -290,6 +287,7 @@ export default class Catcher {
user: this.getUser(),
addons: this.getAddons(error),
backtrace: await this.getBacktrace(error),
catcherVersion: this.version,
};

/**
Expand Down Expand Up @@ -338,7 +336,7 @@ export default class Catcher {
*
* @param error - caught error
*/
private getType(error: Error | string): string {
private getType(error: Error | string): HawkJavaScriptEvent['type'] {
const notAnError = !(error instanceof Error);

/**
Expand All @@ -355,7 +353,7 @@ export default class Catcher {
/**
* Release version
*/
private getRelease(): string | null {
private getRelease(): HawkJavaScriptEvent['release'] {
return this.release || null;
}

Expand All @@ -382,7 +380,7 @@ export default class Catcher {
*
* @param context - any additional data passed by user
*/
private getContext(context?: EventContext): EventContext {
private getContext(context?: EventContext): HawkJavaScriptEvent['context'] {
const contextMerged = {};

if (this.context !== undefined) {
Expand All @@ -399,7 +397,7 @@ export default class Catcher {
/**
* Current authenticated user
*/
private getUser(): AffectedUser | null {
private getUser(): HawkJavaScriptEvent['user'] {
return this.user || null;
}

Expand Down Expand Up @@ -432,7 +430,7 @@ export default class Catcher {
*
* @param error - event from which to get backtrace
*/
private async getBacktrace(error: Error | string): Promise<BacktraceFrame[] | null> {
private async getBacktrace(error: Error | string): Promise<HawkJavaScriptEvent['backtrace']> {
const notAnError = !(error instanceof Error);

/**
Expand All @@ -457,7 +455,7 @@ export default class Catcher {
*
* @param {Error|string} error — caught error
*/
private getAddons(error: Error | string): JavaScriptAddons {
private getAddons(error: Error | string): HawkJavaScriptEvent['addons'] {
const { innerWidth, innerHeight } = window;
const userAgent = window.navigator.userAgent;
const location = window.location.href;
Expand Down
10 changes: 2 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import 'regenerator-runtime/runtime';
import Catcher from './catcher';
import { HawkInitialSettings, AffectedUser } from './types/hawk-initial-settings';
import { HawkJavaScriptEvent } from './types/event';
export { AffectedUser } from '@hawk.so/types';
export * from './types';

export default Catcher;

export {
HawkInitialSettings,
AffectedUser,
HawkJavaScriptEvent
};
5 changes: 5 additions & 0 deletions src/modules/sanitizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export default class Sanitizer {
/**
* If object in stringified JSON will reach this length,
* it will be represented as "<big object>"
*
* @todo export this value to the settings
*/
private static readonly maxObjectLen: number = 500;

Expand Down Expand Up @@ -266,6 +268,9 @@ export default class Sanitizer {
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-unused-vars-experimental,no-unused-vars
private static formatBigObject(target: any): string {
/**
* @todo save big object structure, but not the content
*/
return '<big object>';
}
}
46 changes: 45 additions & 1 deletion src/types/event.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
import type { EventData, JavaScriptAddons } from '@hawk.so/types';

/**
* Event data with JS specific addons
*/
type JSEventData = EventData<JavaScriptAddons>;

/**
* Event will be sent to Hawk by Hawk JavaScript SDK
*
* The listed EventData properties will always be sent, so we define them as required in the type
*/
export type HawkJavaScriptEvent = EventData<JavaScriptAddons>;
export type HawkJavaScriptEvent = Omit<JSEventData, 'type' | 'release' | 'user' | 'context' | 'addons' | 'backtrace' | 'catcherVersion'> & {
/**
* Event type: TypeError, ReferenceError etc
*/
type: JSEventData['type'];

/**
* Current release (aka version, revision) of an application
*/
release: JSEventData['release'] | null;

/**
* Current authenticated user
*/
user: JSEventData['user'] | null;

/**
* Any other information collected and passed by user
*/
context: JSEventData['context'];

/**
*
* Catcher-specific information
*/
addons: JSEventData['addons'];

/**
* Stack
* From the latest call to the earliest
*/
backtrace: JSEventData['backtrace'] | null;

/**
* Catcher version
*/
catcherVersion: JSEventData['catcherVersion'];
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface HawkInitialSettings {
* Enable debug mode
* Send raw event's data additionally in addons field by key 'RAW_EVENT_DATA'
*/
debug: boolean;
debug?: boolean;

/**
* Current release and bundle version
Expand Down Expand Up @@ -66,7 +66,3 @@ export interface HawkInitialSettings {
*/
beforeSend?(event: HawkJavaScriptEvent): HawkJavaScriptEvent;
}

export {
AffectedUser
};
2 changes: 1 addition & 1 deletion src/types/index.d.ts → src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import CatcherMessage from './catcher-message';
import HawkInitialSettings from './hawk-initial-settings';
import { HawkInitialSettings } from './hawk-initial-settings';
import { HawkJavaScriptEvent } from './event';

export {
Expand Down
2 changes: 1 addition & 1 deletion stats.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

Size: 7.3 KB with all dependencies, minified and gzipped
Size: 7.37 KB with all dependencies, minified and gzipped

0 comments on commit 7dd8c26

Please sign in to comment.