-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathparams.js
More file actions
38 lines (32 loc) · 1.05 KB
/
params.js
File metadata and controls
38 lines (32 loc) · 1.05 KB
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
import getQueryStringParams from '../shared/getQueryStringParams'
import seserializeParam from '../shared/serializeParam'
import serializeSearch from '../shared/serializeSearch'
import router from './router'
import segments, { resetSegments } from './segments'
import state from './state'
const paramsProxyHandler = {
set(target, name, value) {
const serializedValue = seserializeParam(value)
target[name] = serializedValue
const search = serializeSearch(target)
router.url = router.path + (search ? '?' : '') + search
return true
},
get(target, name) {
if (target[name] === false) return false
if (segments[name] === false) return false
return target[name] || segments[name] || ''
},
}
const params = { ...state.params }
delete state.params
const proxy = new Proxy(params, paramsProxyHandler)
export function updateParams(query) {
resetSegments()
const delta = getQueryStringParams(query)
for (const key of Object.keys({ ...delta, ...params })) {
params[key] = delta[key]
}
return proxy
}
export default proxy