pinafore/routes/_utils/ajax.js

28 lines
629 B
JavaScript
Raw Normal View History

2018-02-09 06:29:29 +00:00
export async function post (url, body) {
return (await fetch(url, {
2018-01-13 22:19:51 +00:00
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
})).json()
}
2018-02-09 06:29:29 +00:00
export function paramsString (paramsObject) {
2018-01-13 22:19:51 +00:00
let params = new URLSearchParams()
Object.keys(paramsObject).forEach(key => {
2018-01-14 03:23:05 +00:00
params.set(key, paramsObject[key])
2018-01-13 22:19:51 +00:00
})
return params.toString()
}
2018-02-09 06:29:29 +00:00
export async function get (url, headers = {}) {
return (await fetch(url, {
2018-01-13 22:19:51 +00:00
method: 'GET',
headers: Object.assign(headers, {
2018-02-09 06:29:29 +00:00
'Accept': 'application/json'
2018-01-13 22:19:51 +00:00
})
})).json()
2018-02-09 06:29:29 +00:00
}