standard-json-api-connectors
Version:
Standardized HTTP JSON requests with standardized errors.
26 lines (24 loc) • 787 B
JavaScript
import throwOnError from './helpers/throwOnError.js'
import createResponseObject from './helpers/createResponseObject.js'
import { CorsError } from 'standard-api-errors'
export default (fetch, apiUrl, generateRoute, generateHeaderFields = () => ({}), options = {}) => async (params) => {
let response
const requestOptions = {
method: 'DELETE',
headers: {
...generateHeaderFields(params),
'Content-Type': 'application/json'
}
}
if (options.timeout) {
requestOptions.signal = AbortSignal.timeout(options.timeout)
}
try {
response = await fetch(apiUrl + generateRoute(params), requestOptions)
} catch (error) {
console.log(error)
throw new CorsError()
}
await throwOnError(response)
return await createResponseObject(response)
}