Skip to content

Commit 025d50f

Browse files
committed
refactor: refactor redux-middleware and change directory name
1 parent cbbe3a8 commit 025d50f

File tree

138 files changed

+98
-108
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

138 files changed

+98
-108
lines changed

packages/browser/index.js

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

packages/core/index.js

Lines changed: 0 additions & 7 deletions
This file was deleted.
File renamed without changes.

packages/ohbug-browser/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
if (process.env.NODE_ENV === 'production') {
4+
module.exports = require('./dist/ohbug-browser.cjs.prod.js')
5+
} else {
6+
module.exports = require('./dist/ohbug-browser.cjs.js')
7+
}

packages/browser/package.json renamed to packages/ohbug-browser/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
},
1414
"license": "Apache-2.0",
1515
"main": "index.js",
16-
"module": "dist/browser.esm.js",
17-
"unpkg": "dist/browser.global.js",
16+
"module": "dist/ohbug-browser.esm.js",
17+
"unpkg": "dist/ohbug-browser.global.js",
1818
"types": "dist/index.d.ts",
1919
"files": [
2020
"index.js",
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

packages/ohbug-core/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
if (process.env.NODE_ENV === 'production') {
4+
module.exports = require('./dist/ohbug-core.cjs.prod.js')
5+
} else {
6+
module.exports = require('./dist/ohbug-core.cjs.js')
7+
}

packages/core/package.json renamed to packages/ohbug-core/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
},
1414
"license": "Apache-2.0",
1515
"main": "index.js",
16-
"module": "dist/core.esm.js",
17-
"unpkg": "dist/core.global.js",
16+
"module": "dist/ohbug-core.esm.js",
17+
"unpkg": "dist/ohbug-core.global.js",
1818
"types": "dist/index.d.ts",
1919
"files": [
2020
"index.js",
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

packages/redux-middleware/README-zh_CN.md renamed to packages/ohbug-redux-middleware/README-zh_CN.md

Lines changed: 5 additions & 3 deletions

packages/redux-middleware/README.md renamed to packages/ohbug-redux-middleware/README.md

Lines changed: 5 additions & 3 deletions

packages/redux-middleware/__tests__/middleware.test.ts renamed to packages/ohbug-redux-middleware/__tests__/middleware.test.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { createStore, applyMiddleware } from 'redux'
22
import type { Action } from 'redux'
3-
import { init as initBrowser } from '@ohbug/browser'
4-
import { getHub } from '@ohbug/core'
5-
import createOhbugMiddleware from '../src'
3+
import Ohbug from '@ohbug/browser'
64

7-
const apiKey = 'test_id'
8-
const config = { apiKey }
5+
import { createOhbugMiddleware } from '../src/middleware'
6+
7+
const apiKey = 'API_KEY_TEST'
98

109
const defaultState = 0
1110
function counter(state = defaultState, action: Action) {
@@ -20,19 +19,15 @@ function counter(state = defaultState, action: Action) {
2019
}
2120

2221
describe('redux-middleware', () => {
23-
beforeAll(() => {
24-
initBrowser(config)
25-
})
26-
2722
it('should works', () => {
23+
const client = Ohbug.init({ apiKey })
2824
const store = createStore(counter, applyMiddleware(createOhbugMiddleware()))
2925

3026
store.dispatch({ type: 'INCREMENT' })
3127
store.dispatch({ type: 'INCREMENT' })
3228
store.dispatch({ type: 'DECREMENT' })
3329

34-
const hub = getHub<Window>()
35-
const actions = hub.getActions()
30+
const actions = client._actions
3631

3732
expect(store.getState()).toBe(1)
3833
expect(actions.length).toBe(3)
@@ -45,12 +40,12 @@ describe('redux-middleware', () => {
4540
type: `[ohbug] ${action.type}`,
4641
}
4742
}
43+
const client = Ohbug.init({ apiKey })
4844
const store = createStore(counter, applyMiddleware(createOhbugMiddleware(before)))
4945

5046
store.dispatch({ type: 'INCREMENT' })
5147

52-
const hub = getHub<Window>()
53-
const actions = hub.getActions()
48+
const actions = client._actions
5449

5550
expect(store.getState()).toBe(1)
5651
expect(actions[actions.length - 1]?.data?.type).toBe('[ohbug] INCREMENT')
@@ -60,14 +55,14 @@ describe('redux-middleware', () => {
6055
function before(): false {
6156
return false
6257
}
58+
const client = Ohbug.init({ apiKey })
6359
const store = createStore(counter, applyMiddleware(createOhbugMiddleware(before)))
6460

65-
const hub = getHub<Window>()
66-
const _beforeLength = hub.getActions().length
61+
const _beforeLength = client._actions.length
6762

6863
store.dispatch({ type: 'INCREMENT' })
6964

70-
const _afterLength = hub.getActions().length
65+
const _afterLength = client._actions.length
7166

7267
expect(store.getState()).toBe(1)
7368
expect(_beforeLength).toBe(_afterLength)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
if (process.env.NODE_ENV === 'production') {
4+
module.exports = require('./dist/ohbug-redux-middleware.cjs.prod.js')
5+
} else {
6+
module.exports = require('./dist/ohbug-redux-middleware.cjs.js')
7+
}

packages/redux-middleware/package.json renamed to packages/ohbug-redux-middleware/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@
1313
},
1414
"license": "Apache-2.0",
1515
"main": "index.js",
16-
"module": "dist/redux-middleware.esm.js",
17-
"unpkg": "dist/redux-middleware.global.js",
16+
"module": "dist/ohbug-redux-middleware.esm.js",
17+
"unpkg": "dist/ohbug-redux-middleware.global.js",
1818
"types": "dist/index.d.ts",
1919
"files": [
2020
"index.js",
2121
"dist"
2222
],
23+
"dependencies": {
24+
"@ohbug/utils": "^0.0.11"
25+
},
2326
"devDependencies": {
2427
"redux": "^4.0.5"
2528
},
26-
"peerDependencies": {
27-
"@ohbug/core": "^0.0.7"
28-
},
2929
"buildOptions": {
3030
"name": "OhbugReduxMiddleware",
3131
"formats": [
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { createOhbugMiddleware as default } from './middleware'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { getOhbugObject, error } from '@ohbug/utils'
2+
import type { Middleware, Action, MiddlewareAPI } from 'redux'
3+
4+
const identity = (action: Action, _: MiddlewareAPI) => action
5+
6+
type CreateOhbugMiddlewareOption = (action: Action, store: MiddlewareAPI) => Action | false
7+
export const createOhbugMiddleware = (
8+
before: CreateOhbugMiddlewareOption = identity
9+
): Middleware => (store) => (next) => (action) => {
10+
const data = before(action, store)
11+
12+
if (data) {
13+
const ohbugObject = getOhbugObject<Window>()
14+
error(Boolean(ohbugObject), '`Ohbug.init` is not running yet!')
15+
16+
ohbugObject.client.addAction('dispatch a redux action', data, 'redux-action')
17+
}
18+
19+
return next(action)
20+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

packages/utils/__tests__/dom.test.ts renamed to packages/ohbug-utils/__tests__/dom.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('utils dom', () => {
1414
const dom = getDOM()
1515
const button = dom.querySelector('button') as HTMLButtonElement
1616
let selector
17-
button.addEventListener('click', e => {
17+
button.addEventListener('click', (e) => {
1818
selector = getSelector(e)
1919
})
2020
userEvent.click(button)

packages/ohbug-utils/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
if (process.env.NODE_ENV === 'production') {
4+
module.exports = require('./dist/ohbug-utils.cjs.prod.js')
5+
} else {
6+
module.exports = require('./dist/ohbug-utils.cjs.js')
7+
}

packages/utils/package.json renamed to packages/ohbug-utils/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
},
1414
"license": "Apache-2.0",
1515
"main": "index.js",
16-
"module": "dist/utils.esm.js",
17-
"unpkg": "dist/utils.global.js",
16+
"module": "dist/ohbug-utils.esm.js",
17+
"unpkg": "dist/ohbug-utils.global.js",
1818
"types": "dist/index.d.ts",
1919
"files": [
2020
"index.js",
File renamed without changes.

packages/utils/src/cookie.ts renamed to packages/ohbug-utils/src/cookie.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,5 @@ export const docCookies = {
6363
(sDomain ? '; domain=' + sDomain : '') +
6464
(sPath ? '; path=' + sPath : '')
6565
return true
66-
}
66+
},
6767
}
File renamed without changes.

packages/utils/src/get.ts renamed to packages/ohbug-utils/src/get.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export function getGlobal<T = Window>(): T & OhbugGlobal {
1414

1515
export function getOhbugObject<T = Window>(): OhbugObject {
1616
const global = getGlobal<T>()
17+
1718
error(Boolean(global.__OHBUG__), 'Failed to get `OhbugObject`, please confirm if `Ohbug.init`')
1819

1920
return global.__OHBUG__
File renamed without changes.
File renamed without changes.

packages/utils/src/mixin.ts renamed to packages/ohbug-utils/src/mixin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ export function parseUrl(
3434
host: match[4],
3535
path: match[5],
3636
protocol: match[2],
37-
relative: match[5] + query + fragment
37+
relative: match[5] + query + fragment,
3838
}
3939
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

packages/webpack-plugin/__tests__/helpers.ts renamed to packages/ohbug-webpack-plugin/__tests__/helpers.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ export function createCompiler(options: webpack.Configuration = {}) {
3030
output: {
3131
path: `${__dirname}/dist`,
3232
filename: '[name].[chunkhash].js',
33-
chunkFilename: '[id].[name].[chunkhash].js'
33+
chunkFilename: '[id].[name].[chunkhash].js',
3434
},
35-
...options
35+
...options,
3636
}
3737
)
3838
return compiler
@@ -57,14 +57,14 @@ export const createTestServer = (): Promise<void> =>
5757
if (err) return reject(err)
5858
server = _server
5959
resolve()
60-
}
60+
},
6161
]
6262

6363
const _server = app.listen.apply(app, args)
6464
})
6565

6666
export const closeTestServer = () =>
67-
new Promise(resolve => {
67+
new Promise((resolve) => {
6868
server &&
6969
server.close(() => {
7070
resolve()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
if (process.env.NODE_ENV === 'production') {
4+
module.exports = require('./dist/ohbug-webpack-plugin.cjs.prod.js')
5+
} else {
6+
module.exports = require('./dist/ohbug-webpack-plugin.cjs.js')
7+
}

packages/webpack-plugin/package.json renamed to packages/ohbug-webpack-plugin/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
},
1414
"license": "Apache-2.0",
1515
"main": "index.js",
16-
"module": "dist/webpack-plugin.esm.js",
17-
"unpkg": "dist/webpack-plugin.global.js",
16+
"module": "dist/ohbug-webpack-plugin.esm.js",
17+
"unpkg": "dist/ohbug-webpack-plugin.global.js",
1818
"types": "dist/index.d.ts",
1919
"files": [
2020
"index.js",

packages/redux-middleware/index.js

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

packages/redux-middleware/src/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/redux-middleware/src/middleware.ts

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

0 commit comments

Comments
 (0)