pinafore/routes/_utils/ajax.js

27 lines
638 B
JavaScript
Raw Normal View History

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