(
url: string,
options?: RequestInit
)
| 24 | }; |
| 25 | |
| 26 | public async fetchWithDefaults<T>( |
| 27 | url: string, |
| 28 | options?: RequestInit |
| 29 | ): Promise<T | string> { |
| 30 | let response; |
| 31 | |
| 32 | if (this.currentCluster) { |
| 33 | const headers = new Headers(options?.headers); |
| 34 | if (!headers.has("X-Kubecontext")) { |
| 35 | headers.set("X-Kubecontext", this.currentCluster); |
| 36 | } |
| 37 | response = await fetch(url, { ...options, headers }); |
| 38 | } else { |
| 39 | response = await fetch(url, options); |
| 40 | } |
| 41 | |
| 42 | if (!response.ok) { |
| 43 | const error = await response.text(); |
| 44 | throw new Error(error); |
| 45 | } |
| 46 | |
| 47 | const contentType = response.headers.get("Content-Type") || ""; |
| 48 | if (!contentType) { |
| 49 | return {} as unknown as T; |
| 50 | } else if (contentType.includes("text/plain")) { |
| 51 | return await response.text(); |
| 52 | } else { |
| 53 | return (await response.json()) as T; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | public async fetchWithSafeDefaults<T>({ |
| 58 | url, |
no outgoing calls
no test coverage detected