UNPKG

kubernetes-fluent-client

Version:

A @kubernetes/client-node fluent API wrapper that leverages K8s Server Side Apply.

49 lines (48 loc) 1.45 kB
// SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: 2023-Present The Kubernetes Fluent Client Authors import { StatusCodes } from "http-status-codes"; import { fetch as undiciFetch } from "undici"; /** * Perform an async HTTP call and return the parsed JSON response, optionally * as a specific type. * * @example * ```ts * fetch<string[]>("https://example.com/api/foo"); * ``` * * @param url The URL or Request object to fetch * @param init Additional options for the request * @returns The parsed JSON response */ export async function fetch(url, init) { let data = undefined; try { const resp = await undiciFetch(url, init); const contentType = resp.headers.get("content-type") || ""; // Parse the response as JSON if the content type is JSON if (contentType.includes("application/json")) { data = (await resp.json()); } else { // Otherwise, return however the response was read data = (await resp.text()); } return { data, ok: resp.ok, status: resp.status, statusText: resp.statusText, }; } catch (e) { const status = parseInt(e?.code) || StatusCodes.BAD_REQUEST; const statusText = e?.message || "Unknown error"; return { data, ok: false, status, statusText, }; } }