@qrvey/fetch
Advanced tools
Comparing version 1.1.2 to 1.1.3
@@ -163,11 +163,30 @@ 'use strict'; | ||
} | ||
static async getResponseData(isJsonResponse, response) { | ||
const clonedResponse = response.clone(); | ||
let responseData = null; | ||
try { | ||
if (isJsonResponse) { | ||
responseData = await response.json(); | ||
} else { | ||
responseData = await response.text(); | ||
} | ||
} catch (_) { | ||
responseData = await clonedResponse.text(); | ||
} | ||
return responseData; | ||
} | ||
static async handleResponse(response) { | ||
const responseContentType = response.headers.get("content-type"); | ||
const isJsonResponse = responseContentType && responseContentType.includes("application/json"); | ||
const responseData = await (isJsonResponse ? response.json() : response.text()); | ||
if (response.ok) return responseData; | ||
const responseData = await this.getResponseData( | ||
!!isJsonResponse, | ||
response | ||
); | ||
if (response.ok) { | ||
return responseData; | ||
} | ||
throw new RestClientError(response.url, responseData); | ||
} | ||
static fetchData(url, requestOptions) { | ||
return fetch(url, requestOptions).then(this.handleResponse).catch((error) => { | ||
return fetch(url, requestOptions).then(this.handleResponse.bind(this)).catch((error) => { | ||
if (error instanceof RestClientError) throw error; | ||
@@ -174,0 +193,0 @@ throw new RestClientError(url, error); |
@@ -5,3 +5,3 @@ { | ||
"main": "dist/cjs/index.js", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"license": "MIT", | ||
@@ -8,0 +8,0 @@ "publishConfig": { |
# @qrvey/fetch | ||
![install size](https://packagephobia.now.sh/badge?p=%40qrvey%2Ffetch) | ||
![coverage](https://img.shields.io/badge/unit_test_coverage-0%25-brightgreen) | ||
![coverage](https://img.shields.io/badge/unit_test_coverage-96%25-brightgreen) | ||
@@ -6,0 +6,0 @@ **@qrvey/fetch** is a lightweight and reliable library for making RESTful requests in Node.js applications. It uses the native `fetch` library (Node.js 18+), avoiding the need for external dependencies. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
59335
522
2