-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
42 lines (36 loc) · 1.09 KB
/
util.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
const fetch = require('isomorphic-unfetch')
const escapeHtml = unsafe => {
return unsafe
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
}
const setQueryStringParameter = (name, value) => {
let params = new URLSearchParams(location.search)
let state = { ...window.history.state }
params.set(name, value)
let to = decodeURIComponent(`${location.pathname}?${params}`)
state['url'] = to
state['as'] = to
window.history.pushState(state, '', to)
}
const toQuery = o =>
Object.keys(o)
.map(k => `${k}=${o[k]}`)
.join('&')
const queryResource = async (
resource,
{ query, projection, skip, limit, sort } = {}
) => {
let args = {
...(query && { query: encodeURI(JSON.stringify(query)) }),
...(projection && { projection: projection.join(',') }),
...(skip && { skip }),
...(limit && { limit }),
...(sort && { sort: JSON.stringify(sort) }),
}
return fetch(`/api/${resource}?${toQuery(args)}`)
}
module.exports = { escapeHtml, setQueryStringParameter, toQuery, queryResource }