refactor promisedRequest to use async/await

This commit is contained in:
Henry Jameson 2026-06-17 13:33:30 +03:00
commit 04a0b0b8a8

View file

@ -66,7 +66,7 @@ export const paramsString = (params = {}) => {
) )
} }
export const promisedRequest = ({ export const promisedRequest = async ({
method, method,
url, url,
params, params,
@ -107,47 +107,41 @@ export const promisedRequest = ({
} }
} }
return fetch(url, options).then((response) => { const response = await fetch(url, options)
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 // 204 is "No content", which fails to parse json (as you'd might think)
.json() if (response.ok && response.status === 204) return { _response: response }
.then((json) => {
if (!response.ok) {
return reject(
new StatusCodeError(
response.status,
json,
{ url, options },
response,
),
)
}
if (typeof json !== 'object') { if (!response.ok) {
return resolve({ throw new StatusCodeError(
_response: response, response.status,
_value: json, json,
}) { url, options },
} response,
json._response = response )
}
return resolve(json) try {
}) const json = await response.json()
.catch((error) => {
return reject( if (typeof json !== 'object') {
new StatusCodeError( return {
response.status, _response: response,
error, _value: json,
{ url, options }, }
response, }
),
) json._response = response
})
}) return json
}) } catch (error) {
throw new StatusCodeError(
response.status,
error,
{ url, options },
response,
)
}
} }
const authHeaders = (accessToken) => { const authHeaders = (accessToken) => {