some initial API refactoring
This commit is contained in:
parent
bd9fcf619d
commit
4a59c42395
20 changed files with 1368 additions and 1567 deletions
87
src/services/api/helpers.js
Normal file
87
src/services/api/helpers.js
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
import { RegistrationError, StatusCodeError } from 'src/services/errors/errors'
|
||||
|
||||
export const promisedRequest = ({
|
||||
method,
|
||||
url,
|
||||
params,
|
||||
payload,
|
||||
formData,
|
||||
credentials,
|
||||
headers = {},
|
||||
}) => {
|
||||
const options = {
|
||||
method,
|
||||
credentials: 'same-origin',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
...headers,
|
||||
},
|
||||
}
|
||||
if (!formData) {
|
||||
options.headers['Content-Type'] = 'application/json'
|
||||
}
|
||||
if (params) {
|
||||
url +=
|
||||
'?' +
|
||||
Object.entries(params)
|
||||
.map(
|
||||
([key, value]) =>
|
||||
encodeURIComponent(key) + '=' + encodeURIComponent(value),
|
||||
)
|
||||
.join('&')
|
||||
}
|
||||
if (formData || payload) {
|
||||
options.body = formData || JSON.stringify(payload)
|
||||
}
|
||||
|
||||
if (credentials) {
|
||||
options.headers = {
|
||||
...options.headers,
|
||||
...authHeaders(credentials),
|
||||
}
|
||||
}
|
||||
|
||||
return fetch(url, options).then((response) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
// 204 is "No content", which fails to parse json (as you'd might think)
|
||||
if (response.ok && response.status === 204) resolve()
|
||||
|
||||
return response
|
||||
.json()
|
||||
.then((json) => {
|
||||
if (!response.ok) {
|
||||
return reject(
|
||||
new StatusCodeError(
|
||||
response.status,
|
||||
json,
|
||||
{ url, options },
|
||||
response,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
json._response = response
|
||||
|
||||
return resolve(json)
|
||||
})
|
||||
.catch((error) => {
|
||||
return reject(
|
||||
new StatusCodeError(
|
||||
response.status,
|
||||
error,
|
||||
{ url, options },
|
||||
response,
|
||||
),
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const authHeaders = (accessToken) => {
|
||||
if (accessToken) {
|
||||
return { Authorization: `Bearer ${accessToken}` }
|
||||
} else {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue