4. js / lib / consume.js

1
import { ProblemDetailsError } from "./ProblemDetailsError.js"
2
3
/**
4
 * Espera a que la promesa de un fetch termine. Si
5
 * hay error, lanza una excepción.
6
 * 
7
 * @param {Promise<Response> } servicio
8
 */
9
export async function consume(servicio) {
10
 const respuesta = await servicio
11
 if (respuesta.ok) {
12
  return respuesta
13
 } else {
14
  const contentType = respuesta.headers.get("Content-Type")
15
  if (contentType === "application/problem+json; charset=utf-8")
16
   throw new ProblemDetailsError(await respuesta.json())
17
  else
18
   throw new Error(respuesta.statusText)
19
 }
20
}
skip_previous skip_next