-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
63 lines (50 loc) · 1.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* Tiny history middleware
*/
var querystring = require('querystring')
var History = require('./lib/history')
var parse = require('url').parse
/**
* Export `middleware`
*/
module.exports = middleware['default'] = middleware
middleware.navigate = navigate
/**
* middleware middleware
*
* @param {Object} options
* @return {Function}
*/
function middleware (options) {
return function (store) {
var history = History(store).listen()
return function (next) {
return function (action) {
if (action.type !== '@@redux-routes/navigate') return next(action)
var url = parse(action.payload.url)
var location = {
hash: url.hash || undefined,
pathname: url.pathname,
search: url.search || undefined
}
var query = url.query ? qs.parse(url.query) : null
action.payload.url = url.format(location)
var result = next(action)
history.update(result)
return result
}
}
}
}
/**
* Navigate action creator
*
* @param {String} url
* @return {Object} action
*/
function navigate (url) {
return {
type: '@@redux-routes/navigate',
payload: { url: url }
}
}