Skip to content

Commit c66c9db

Browse files
committed
refactor(browser): complete browser refactoring
1 parent 4c51840 commit c66c9db

40 files changed

+293
-602
lines changed

packages/browser/README-zh_CN.md

Lines changed: 2 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -15,121 +15,9 @@ yarn add @ohbug/browser
1515
## 使用
1616

1717
```javascript
18-
import { init } from '@ohbug/browser'
18+
import Ohbug from '@ohbug/browser'
1919

20-
init({ apiKey: 'YOUR_API_KEY' })
21-
```
22-
23-
## API
24-
25-
### init
26-
27-
这是对 `init` 配置的描述。
28-
29-
```typescript
30-
interface OhbugConfig {
31-
apiKey: string
32-
appVersion?: string
33-
appType?: string
34-
beforeReport?: (event: OhbugEvent<any>) => OhbugEvent<any>
35-
reported?: (event: OhbugEvent<any>) => void
36-
}
37-
```
38-
39-
#### apiKey
40-
41-
这里作为客户端的唯一标识。
42-
43-
#### appVersion
44-
45-
您应该提供 app 的版本号/标识符,以便于定位问题出现的时机。
46-
47-
#### appType
48-
49-
如果您的 app 的代码库包含不同的入口,但向同一个服务上报,则可能需要添加 `appType` 表示问题来源的入口类型。
50-
51-
#### beforeReport
52-
53-
用于上报前对收集到的信息做一定处理。
54-
55-
#### reported
56-
57-
用于上报后的特定操作。
58-
59-
### captureMessage
60-
61-
用于报告自定义信息。
62-
63-
```javascript
64-
import { captureMessage } from '@ohbug/browser'
65-
66-
captureMessage('error info')
67-
```
68-
69-
### feedback
70-
71-
用于收集用户反馈。
72-
73-
```javascript
74-
import { feedback } from '@ohbug/browser'
75-
76-
btn.addEventListener('click', () => {
77-
feedback()
78-
})
79-
```
80-
81-
## 插件
82-
83-
### 例子
84-
85-
基于 [perfume.js](https://github.com/Zizzamia/perfume.js) 封装的插件使用。
86-
87-
```
88-
yarn add @ohbug/plugin-perfume
89-
```
90-
91-
```javascript
92-
import { applyPlugin } from '@ohbug/core'
93-
import ohbugPluginPerfume from '@ohbug/plugin-perfume'
94-
import { init } from '@ohbug/browser'
95-
96-
const enhancer = applyPlugin(ohbugPluginPerfume)
97-
init({ apiKey: 'YOUR_API_KEY' }, enhancer)
98-
```
99-
100-
### 自定义插件
101-
102-
示例
103-
104-
```javascript
105-
class myPlugin {
106-
// capture 用于自定义信息的捕获
107-
// 使用 createEvent 封装捕获到的信息
108-
// 使用 collect 传递信息给 Ohbug 用于上报
109-
capture({ createEvent, collect }) {
110-
a.addEventListener('error', (e) => {
111-
// do something
112-
const event = createEvent('TYPE', e)
113-
collect(event)
114-
})
115-
}
116-
117-
// state 用于承载额外自定义信息
118-
// 返回任意格式 object,最终这些信息将出现在 `event.state` 中
119-
state(event) {
120-
return {
121-
user: 'user_1'
122-
}
123-
}
124-
125-
// event 用于对已有信息进行二次处理
126-
event(event) {
127-
return {
128-
...event,
129-
foo: 'bar'
130-
}
131-
}
132-
}
20+
Ohbug.init({ apiKey: 'YOUR_API_KEY' })
13321
```
13422

13523
### 插件列表

packages/browser/README.md

Lines changed: 2 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -15,129 +15,9 @@ yarn add @ohbug/browser
1515
## Usage
1616

1717
```javascript
18-
import { init } from '@ohbug/browser'
18+
import Ohbug from '@ohbug/browser'
1919

20-
init({ apiKey: 'YOUR_API_KEY' })
21-
```
22-
23-
## API
24-
25-
### init
26-
27-
Here is a description of the config of `init`.
28-
29-
```typescript
30-
interface OhbugConfig {
31-
apiKey: string
32-
appVersion?: string
33-
appType?: string
34-
beforeReport?: (event: OhbugEvent<any>) => OhbugEvent<any>
35-
reported?: (event: OhbugEvent<any>) => void
36-
}
37-
```
38-
39-
#### apiKey
40-
41-
Your project API key.
42-
43-
#### appVersion
44-
45-
The version number of your app. Conveniently locate the problem version.
46-
47-
#### appType
48-
49-
The type of your app. If your app's codebase contains different entries, but reports to the same service, you may need to add `appType` to indicate the type of entry for the source of the problem.
50-
51-
#### beforeReport
52-
53-
Used to do some processing on the collected information before reporting.
54-
55-
#### reported
56-
57-
Used to perform specific operations after reporting.
58-
59-
### captureMessage
60-
61-
Used to report custom information
62-
63-
```javascript
64-
import { captureMessage } from '@ohbug/browser'
65-
66-
captureMessage('error info')
67-
```
68-
69-
### feedback
70-
71-
Used to collect user feedback
72-
73-
```javascript
74-
import { feedback } from '@ohbug/browser'
75-
76-
btn.addEventListener('click', () => {
77-
feedback()
78-
})
79-
```
80-
81-
## Plugin
82-
83-
### Example
84-
85-
Use of plugin based on [perfume.js](https://github.com/Zizzamia/perfume.js) package.
86-
87-
```
88-
yarn add @ohbug/plugin-perfume
89-
```
90-
91-
```javascript
92-
import { applyPlugin } from '@ohbug/core'
93-
import ohbugPluginPerfume from '@ohbug/plugin-perfume'
94-
import { init } from '@ohbug/browser'
95-
96-
const enhancer = applyPlugin(ohbugPluginPerfume)
97-
init({ apiKey: 'YOUR_API_KEY' }, enhancer)
98-
```
99-
100-
### Custom plugin
101-
102-
Example
103-
104-
```javascript
105-
106-
const capture = ({ createEvent, collect }) => {
107-
a.addEventListener('error', (e) => {
108-
// do something
109-
const event = createEvent('TYPE', e)
110-
collect(event)
111-
})
112-
}
113-
114-
class myPlugin {
115-
// capture Capture of custom information
116-
// Use createEvent to encapsulate captured information
117-
// Use collect to pass information to Ohbug for reporting capture({ createEvent, collect }) {
118-
a.addEventListener('error', (e) => {
119-
// do something
120-
const event = createEvent('TYPE', e)
121-
collect(event)
122-
})
123-
}
124-
125-
// state is used to carry additional custom information
126-
// Returns an object in any format, and eventually this information will appear in event.state
127-
state(event) {
128-
return {
129-
user: 'user_1'
130-
}
131-
}
132-
133-
// event is used for secondary processing of existing information
134-
event(event) {
135-
return {
136-
...event,
137-
foo: 'bar'
138-
}
139-
}
140-
}
20+
Ohbug.init({ apiKey: 'YOUR_API_KEY' })
14121
```
14222

14323
### List of plugins

packages/browser/__tests__/async.test.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Client } from '@ohbug/core'
2+
import { BrowserClient } from '../src/client'
3+
jest.mock('@ohbug/core')
4+
5+
const mockClient = jest.fn()
6+
;(Client as jest.Mock).mockImplementation((...args) => {
7+
mockClient(...args)
8+
})
9+
10+
const apiKey = 'API_KEY_TEST'
11+
12+
describe('@ohbug/browser/client', () => {
13+
it('should execute init inside core package', () => {
14+
BrowserClient.init({ apiKey })
15+
16+
expect(mockClient).toBeCalledTimes(1)
17+
})
18+
})

packages/browser/__tests__/init.test.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { OhbugEvent } from '@ohbug/types'
2+
import { notifier } from '../src/notifier'
3+
4+
const mockSendBeacon = jest.fn()
5+
const event = { type: 'test' } as OhbugEvent<any>
6+
7+
describe('@ohbug/browser/client', () => {
8+
it('notify via `navigator.sendBeacon`', () => {
9+
navigator.sendBeacon = mockSendBeacon
10+
11+
notifier(event)
12+
13+
expect(mockSendBeacon).toBeCalledTimes(1)
14+
})
15+
})

packages/browser/__tests__/report.test.ts

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

packages/browser/src/async.ts

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

0 commit comments

Comments
 (0)