@vitrical/utils
Advanced tools
Comparing version 1.3.0 to 1.3.1
14
api.d.ts
@@ -1,2 +0,2 @@ | ||
import superagent from 'superagent'; | ||
import { AxiosError } from 'axios'; | ||
export declare class ApiError extends Error { | ||
@@ -13,7 +13,11 @@ url: string; | ||
} | ||
export declare const handleSuperagentError: (url: string, newStack: string, err: unknown, res?: superagent.Response) => ApiError | Error | unknown; | ||
export declare const handleApiError: (url: string, newStack: string, err: AxiosError) => ApiError | Error | unknown; | ||
export type CustomRequestResponse = { | ||
status: superagent.Response['status']; | ||
body: superagent.Response['body']; | ||
headers: superagent.Response['headers']; | ||
status: number; | ||
body: string | { | ||
[key: string]: any; | ||
}; | ||
headers: { | ||
[key: string]: any; | ||
}; | ||
}; | ||
@@ -20,0 +24,0 @@ export type CustomRequestOptions = { |
102
api.js
@@ -6,9 +6,9 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.makeMicroserviceRequest = exports.request = exports.handleSuperagentError = exports.ApiError = void 0; | ||
const superagent_1 = __importDefault(require("superagent")); | ||
exports.makeMicroserviceRequest = exports.request = exports.handleApiError = exports.ApiError = void 0; | ||
const axios_1 = __importDefault(require("axios")); | ||
class ApiError extends Error { | ||
} | ||
exports.ApiError = ApiError; | ||
const handleSuperagentError = (url, newStack, err, res) => { | ||
if (!res) { | ||
const handleApiError = (url, newStack, err) => { | ||
if (!err?.response) { | ||
if (typeof err === 'string') { | ||
@@ -22,3 +22,3 @@ const newError = new Error(err); | ||
} | ||
if (res.status) { | ||
if (err?.response?.status && typeof err?.response?.status === 'number' && !!err?.response?.data) { | ||
if (typeof err === 'object' && | ||
@@ -33,20 +33,28 @@ !!err && | ||
} | ||
const requestMsg = typeof res.body?.msg === 'string' | ||
? res.body.msg | ||
: typeof res.body?.error === 'string' | ||
? res.body.error | ||
: typeof res.body?.message === 'string' | ||
? res.body.message | ||
: typeof res.body === 'string' | ||
? res.body | ||
: typeof res.body?.text === 'string' | ||
? res.body.text | ||
: undefined; | ||
const body = err.response.data; | ||
let requestMsg = undefined; | ||
if (typeof body === 'string') { | ||
requestMsg = body; | ||
} | ||
else if (typeof body === 'object' && !!body) { | ||
if ('msg' in body && typeof body?.msg === 'string') { | ||
requestMsg = body.msg; | ||
} | ||
else if ('error' in body && typeof body?.error === 'string') { | ||
requestMsg = body.error; | ||
} | ||
else if ('message' in body && typeof body?.message === 'string') { | ||
requestMsg = body.message; | ||
} | ||
else if ('text' in body && typeof body?.text === 'string') { | ||
requestMsg = body.text; | ||
} | ||
} | ||
const message = requestMsg | ||
? `Request failed with status code ${res.status} ( ${requestMsg} )` | ||
: `Request failed with status code ${res.status}`; | ||
? `Request failed with status code ${err.response.status} ( ${requestMsg} )` | ||
: `Request failed with status code ${err.response.status}`; | ||
const apiError = new ApiError(message); | ||
apiError.body = res.body; | ||
apiError.headers = res.headers; | ||
apiError.status = res.status; | ||
apiError.body = err.response.data; | ||
apiError.headers = err.response.headers; | ||
apiError.status = err.response.status; | ||
apiError.stack = newStack; | ||
@@ -66,34 +74,28 @@ apiError.url = url; | ||
}; | ||
exports.handleSuperagentError = handleSuperagentError; | ||
exports.handleApiError = handleApiError; | ||
const request = ({ method, url, body, query, headers, }) => new Promise((resolve, reject) => { | ||
const newStack = new Error().stack; | ||
const req = (0, superagent_1.default)(method, url); | ||
if (body) { | ||
if (typeof body === 'string') { | ||
req.set('Content-Type', 'text/plain'); | ||
req.send(body); | ||
} | ||
else { | ||
req.set('Content-Type', 'application/json'); | ||
req.send(body); | ||
} | ||
} | ||
if (query) { | ||
req.query(query); | ||
} | ||
if (headers) { | ||
req.set(headers); | ||
} | ||
req.end((err, res) => { | ||
if (err) { | ||
reject((0, exports.handleSuperagentError)(url, newStack || '', err, res)); | ||
} | ||
else { | ||
resolve({ | ||
status: res.status, | ||
body: typeof body === 'string' ? res.text : res.body, | ||
headers: res.headers, | ||
}); | ||
} | ||
const request = axios_1.default.request({ | ||
method, | ||
url, | ||
data: body, | ||
params: query, | ||
headers: { | ||
...headers, | ||
...(body && typeof body === 'string' | ||
? { 'Content-Type': 'text/plain' } | ||
: { 'Content-Type': 'application/json' }), | ||
}, | ||
}); | ||
request | ||
.then((res) => { | ||
resolve({ | ||
status: res.status, | ||
body: res.data, | ||
headers: res.headers, | ||
}); | ||
}) | ||
.catch((err) => { | ||
reject((0, exports.handleApiError)(url, newStack || '', err)); | ||
}); | ||
}); | ||
@@ -100,0 +102,0 @@ exports.request = request; |
{ | ||
"name": "@vitrical/utils", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"description": "Collection of useful functions and typings", | ||
@@ -26,4 +26,4 @@ "main": "index.js", | ||
"@types/superagent": "^4.1.16", | ||
"axios": "^1.4.0", | ||
"jsonwebtoken": "^9.0.0", | ||
"superagent": "^8.0.9", | ||
"typescript": "^4.9.4" | ||
@@ -30,0 +30,0 @@ }, |
Sorry, the diff of this file is not supported yet
48810
838
+ Addedaxios@^1.4.0
+ Addedaxios@1.7.2(transitive)
+ Addedfollow-redirects@1.15.6(transitive)
+ Addedproxy-from-env@1.1.0(transitive)
- Removedsuperagent@^8.0.9
- Removedasap@2.0.6(transitive)
- Removedcall-bind@1.0.7(transitive)
- Removedcomponent-emitter@1.3.1(transitive)
- Removedcookiejar@2.1.4(transitive)
- Removeddebug@4.3.5(transitive)
- Removeddefine-data-property@1.1.4(transitive)
- Removeddezalgo@1.0.4(transitive)
- Removedes-define-property@1.0.0(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedfast-safe-stringify@2.1.1(transitive)
- Removedformidable@2.1.2(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedget-intrinsic@1.2.4(transitive)
- Removedgopd@1.0.1(transitive)
- Removedhas-property-descriptors@1.0.2(transitive)
- Removedhas-proto@1.0.3(transitive)
- Removedhas-symbols@1.0.3(transitive)
- Removedhasown@2.0.2(transitive)
- Removedhexoid@1.0.0(transitive)
- Removedmethods@1.1.2(transitive)
- Removedmime@2.6.0(transitive)
- Removedms@2.1.2(transitive)
- Removedobject-inspect@1.13.2(transitive)
- Removedonce@1.4.0(transitive)
- Removedqs@6.12.1(transitive)
- Removedset-function-length@1.2.2(transitive)
- Removedside-channel@1.0.6(transitive)
- Removedsuperagent@8.1.2(transitive)
- Removedwrappy@1.0.2(transitive)