1 | import { ProblemDetails } from "./ProblemDetails.js" |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | export async function consumeJson(servicio) { |
12 | |
13 | if (typeof servicio === "string") { |
14 | servicio = fetch(servicio, { |
15 | headers: { "Accept": "application/json, application/problem+json" } |
16 | }) |
17 | } else if (!(servicio instanceof Promise)) { |
18 | throw new Error("Servicio de tipo incorrecto.") |
19 | } |
20 | |
21 | const respuesta = await servicio |
22 | |
23 | const headers = respuesta.headers |
24 | |
25 | if (respuesta.ok) { |
26 | |
27 | |
28 | if (respuesta.status === 204) { |
29 | |
30 | |
31 | return { headers, body: {} } |
32 | |
33 | } else { |
34 | |
35 | const texto = await respuesta.text() |
36 | |
37 | try { |
38 | |
39 | return { headers, body: JSON.parse(texto) } |
40 | |
41 | } catch (error) { |
42 | |
43 | |
44 | throw new ProblemDetails(respuesta.status, headers, texto, |
45 | "/error/errorinterno.html") |
46 | |
47 | } |
48 | |
49 | } |
50 | |
51 | } else { |
52 | |
53 | |
54 | const texto = await respuesta.text() |
55 | |
56 | if (texto === "") { |
57 | |
58 | |
59 | throw new ProblemDetails(respuesta.status, headers, respuesta.statusText) |
60 | |
61 | } else { |
62 | |
63 | |
64 | try { |
65 | |
66 | const { title, type, detail } = JSON.parse(texto) |
67 | |
68 | throw new ProblemDetails(respuesta.status, headers, |
69 | typeof title === "string" ? title : respuesta.statusText, |
70 | typeof type === "string" ? type : undefined, |
71 | typeof detail === "string" ? detail : undefined) |
72 | |
73 | } catch (error) { |
74 | |
75 | if (error instanceof ProblemDetails) { |
76 | |
77 | |
78 | throw error |
79 | |
80 | } else { |
81 | |
82 | throw new ProblemDetails(respuesta.status, headers, respuesta.statusText, |
83 | undefined, texto) |
84 | |
85 | } |
86 | |
87 | } |
88 | |
89 | } |
90 | |
91 | } |
92 | |
93 | } |
94 | |
95 | |
96 | window["consumeJson"] = consumeJson |