Update api functions to fix event editing
This commit is contained in:
parent
9ac969ec78
commit
f2c1107276
9 changed files with 85 additions and 49 deletions
|
|
@ -1,16 +1,19 @@
|
|||
const API_URL = process.env.NODE_ENV === 'production' ? 'https://api-dot-crabfit.uc.r.appspot.com' : 'http://localhost:8080'
|
||||
|
||||
const handleError = error => {
|
||||
if (error.response && error.response.status) {
|
||||
console.error('[Error handler] res:', error.response)
|
||||
if (error && error.status) {
|
||||
console.error('[Error handler] res:', error)
|
||||
}
|
||||
return Promise.reject(error.response)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
|
||||
const api = {
|
||||
get: async endpoint => {
|
||||
try {
|
||||
const response = await fetch(API_URL + endpoint)
|
||||
if (!response.ok) {
|
||||
throw response
|
||||
}
|
||||
const json = await response.json()
|
||||
return Promise.resolve(json)
|
||||
} catch (error) {
|
||||
|
|
@ -19,16 +22,50 @@ const api = {
|
|||
},
|
||||
post: async (endpoint, data, options = {}) => {
|
||||
try {
|
||||
const response = await fetch(API_URL + endpoint, data, options)
|
||||
return Promise.resolve(response)
|
||||
const response = await fetch(API_URL + endpoint, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
...options
|
||||
})
|
||||
if (!response.ok) {
|
||||
throw response
|
||||
}
|
||||
|
||||
//TODO: hack until api update
|
||||
try {
|
||||
const json = await response.json()
|
||||
return Promise.resolve(json)
|
||||
} catch (e) {
|
||||
return Promise.resolve(response)
|
||||
}
|
||||
} catch (error) {
|
||||
return handleError(error)
|
||||
}
|
||||
},
|
||||
patch: async (endpoint, data) => {
|
||||
patch: async (endpoint, data, options = {}) => {
|
||||
try {
|
||||
const response = await fetch(API_URL + endpoint, data)
|
||||
return Promise.resolve(response)
|
||||
const response = await fetch(API_URL + endpoint, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
...options
|
||||
})
|
||||
if (!response.ok) {
|
||||
throw response
|
||||
}
|
||||
|
||||
//TODO: hack until api update
|
||||
try {
|
||||
const json = await response.json()
|
||||
return Promise.resolve(json)
|
||||
} catch (e) {
|
||||
return Promise.resolve(response)
|
||||
}
|
||||
} catch (error) {
|
||||
return handleError(error)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue