Skip to content

Commit

Permalink
Add HTTP method parameter for fetch calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-osman committed Feb 19, 2023
1 parent c05fa06 commit e075c66
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 15 deletions.
2 changes: 1 addition & 1 deletion ui/src/components/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function Header() {

function handleLogout(e) {
e.preventDefault()
api.fetch('/auth/logout', {})
api.fetch('/auth/logout', 'post')
.then(() => {
navigate('/login')
})
Expand Down
2 changes: 1 addition & 1 deletion ui/src/components/tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function Tool({ src, url, data, ...props }) {

function handleClick() {
setIsLoading(true)
api.fetch(url, data)
api.fetch(url, method, data)
.catch(e => popup.error(e.message))
.finally(() => setIsLoading(false))
}
Expand Down
19 changes: 7 additions & 12 deletions ui/src/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@ function ApiProvider({ children }) {
const [isActive, setIsActive] = useState(false)
const [status, setStatus] = useState(null)

async function fetchInternal(url, data) {
let init
if (typeof data !== 'undefined') {
init = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}
async function fetchInternal(url, method, data) {
let init = { method }
if (data != undefined) {
init['headers'] = { 'Content-Type': 'application/json' }
init['body'] = JSON.stringify(data)
}
const response = await fetch(url, init)
if (!response.ok) {
Expand All @@ -41,10 +36,10 @@ function ApiProvider({ children }) {
const api = {
isActive,
status,
fetch: async (url, data) => {
fetch: async (url, method, data) => {
setIsActive(true)
try {
return await fetchInternal(url, data)
return await fetchInternal(url, method, data)
} finally {
setIsActive(false)
}
Expand Down
2 changes: 1 addition & 1 deletion ui/src/routes/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function Login() {

function handleSubmit(e) {
e.preventDefault()
api.fetch('/auth/login', { username, password })
api.fetch('/auth/login', 'post', { username, password })
.then(() => {
navigate(url)
})
Expand Down

0 comments on commit e075c66

Please sign in to comment.