@vitrical/utils
Advanced tools
Comparing version 1.3.3 to 1.3.4
38
api.js
@@ -8,3 +8,2 @@ "use strict"; | ||
const axios_1 = __importDefault(require("axios")); | ||
const axios_fetch_adapter_1 = __importDefault(require("@vespaiach/axios-fetch-adapter")); | ||
function isFetchAvailable() { | ||
@@ -79,4 +78,39 @@ return typeof fetch !== 'undefined'; | ||
const newStack = new Error().stack; | ||
if (isFetchAvailable()) { | ||
const request = fetch(url, { | ||
method, | ||
body: body && typeof body === 'string' ? body : JSON.stringify(body), | ||
headers: { | ||
...headers, | ||
...(body && typeof body === 'string' | ||
? { 'Content-Type': 'text/plain' } | ||
: { 'Content-Type': 'application/json' }), | ||
}, | ||
}); | ||
request | ||
.then(async (res) => { | ||
const body = await res.json(); | ||
if (res.status >= 400) { | ||
const apiError = new ApiError(`Request failed with status code ${res.status} ( ${body?.msg ?? body?.error ?? body?.message ?? ''} )`); | ||
apiError.body = body; | ||
apiError.headers = res.headers; | ||
apiError.status = res.status; | ||
apiError.stack = newStack; | ||
apiError.url = url; | ||
apiError.msg = body?.msg ?? body?.error ?? body?.message ?? ''; | ||
reject(apiError); | ||
return; | ||
} | ||
resolve({ | ||
status: res.status, | ||
body, | ||
headers: res.headers, | ||
}); | ||
}) | ||
.catch((err) => { | ||
reject(err); | ||
}); | ||
return; | ||
} | ||
const request = axios_1.default.request({ | ||
adapter: isFetchAvailable() ? axios_fetch_adapter_1.default : undefined, | ||
method, | ||
@@ -83,0 +117,0 @@ url, |
{ | ||
"name": "@vitrical/utils", | ||
"version": "1.3.3", | ||
"version": "1.3.4", | ||
"description": "Collection of useful functions and typings", | ||
@@ -26,3 +26,2 @@ "main": "index.js", | ||
"@types/superagent": "^4.1.16", | ||
"@vespaiach/axios-fetch-adapter": "^0.3.1", | ||
"axios": "^1.4.0", | ||
@@ -29,0 +28,0 @@ "jsonwebtoken": "^9.0.0", |
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
51278
6
867
2
- Removed@vespaiach/axios-fetch-adapter@0.3.1(transitive)