Socket
Socket
Sign inDemoInstall

@vitrical/utils

Package Overview
Dependencies
40
Maintainers
1
Versions
76
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

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 = {

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc