UNPKG

simple-fetch-ts

Version:

`simple-fetch-ts` is a TypeScript library designed to simplify HTTP requests using a builder-pattern approach. It provides a fluent API for creating, configuring, and executing various HTTP methods (`GET`, `POST`, `PUT`, `PATCH`, `DELETE`) with type safet

42 lines (41 loc) 1.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.tsPut = void 0; const request_error_1 = require("../../errors/request-error"); const get_content_type_1 = require("../../utility/get-content-type"); const tsPut = async (url, requestBody, requestHeaders = {}, signal) => { const contentType = (0, get_content_type_1.getContentType)(requestHeaders).toLowerCase(); const body = contentType === "application/json" && requestBody && typeof requestBody === "object" ? JSON.stringify(requestBody) : requestBody; try { const response = await fetch(url, { method: "PUT", body, headers: requestHeaders, signal, }); if (!response.ok) { const errorText = await response .text() .catch(() => "Unable to parse response text"); throw new request_error_1.SimpleFetchRequestError("PUT", url, response.status, response.statusText, errorText); } const data = await response.json(); return { data, status: response.status, headers: response.headers, raw: response, }; } catch (error) { if (error instanceof request_error_1.SimpleFetchRequestError) { throw error; } throw new Error(error instanceof Error ? error.message : "An unknown error occurred"); } }; exports.tsPut = tsPut;