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
34 lines (33 loc) • 1.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.tsFetch = void 0;
const request_error_1 = require("../../errors/request-error");
const tsFetch = async (url, requestHeaders = {}, signal) => {
try {
const response = await fetch(url, {
method: "GET",
headers: requestHeaders,
signal,
});
if (!response.ok) {
const errorText = await response
.text()
.catch(() => "Unable to parse response text");
throw new request_error_1.SimpleFetchRequestError("GET", 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.tsFetch = tsFetch;