Socket
Socket
Sign inDemoInstall

caccl-send-request

Package Overview
Dependencies
Maintainers
0
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

caccl-send-request - npm Package Compare versions

Comparing version 2.2.6 to 2.2.7

lib/ResponseType.d.ts

2

lib/ErrorCode.d.ts
declare enum ErrorCode {
NotConnected = "CSR1",
SelfSigned = "CSR2",
NotJSON = "CSR3"
ResponseParseError = "CSR3"
}
export default ErrorCode;

@@ -8,3 +8,3 @@ "use strict";

ErrorCode["SelfSigned"] = "CSR2";
ErrorCode["NotJSON"] = "CSR3";
ErrorCode["ResponseParseError"] = "CSR3";
})(ErrorCode || (ErrorCode = {}));

@@ -11,0 +11,0 @@ ;

@@ -0,1 +1,2 @@

import ResponseType from './ResponseType';
/**

@@ -14,2 +15,3 @@ * Sends and retries an http request

* send cross-domain credentials even if not in dev mode
* @param [opts.responseType=ResponseType.JSON] expected response type
* @returns { body, status, headers } on success

@@ -29,2 +31,3 @@ */

sendCrossDomainCredentials?: boolean;
responseType?: ResponseType;
}) => Promise<{

@@ -31,0 +34,0 @@ body: any;

@@ -59,2 +59,3 @@ "use strict";

var ErrorCode_1 = __importDefault(require("./ErrorCode"));
var ResponseType_1 = __importDefault(require("./ResponseType"));
/**

@@ -73,9 +74,10 @@ * Sends and retries an http request

* send cross-domain credentials even if not in dev mode
* @param [opts.responseType=ResponseType.JSON] expected response type
* @returns { body, status, headers } on success
*/
var sendRequest = function (opts) { return __awaiter(void 0, void 0, void 0, function () {
var thisIsDev, sendCrossDomainCredentials, numRetries, method, params, stringifiedParams, query, url, headers, data, encodedData, response, responseHeaders_1, responseBody, jsonErr_1, err_1;
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
var thisIsDev, sendCrossDomainCredentials, numRetries, method, params, stringifiedParams, query, url, headers, data, encodedData, response, responseHeaders_1, responseBody, _a, err_1, err_2;
var _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:

@@ -85,3 +87,3 @@ thisIsDev = (opts.host === 'localhost:8080');

|| thisIsDev
|| ((_a = opts.headers) === null || _a === void 0 ? void 0 : _a.credentials) === 'include');
|| ((_b = opts.headers) === null || _b === void 0 ? void 0 : _b.credentials) === 'include');
numRetries = (opts.numRetries ? opts.numRetries : 0);

@@ -133,5 +135,5 @@ method = (opts.method || 'GET');

}
_b.label = 1;
_c.label = 1;
case 1:
_b.trys.push([1, 7, , 8]);
_c.trys.push([1, 11, , 12]);
return [4 /*yield*/, fetch(url, {

@@ -151,3 +153,3 @@ method: method,

case 2:
response = _b.sent();
response = _c.sent();
responseHeaders_1 = {};

@@ -157,23 +159,30 @@ response.headers.forEach(function (value, key) {

});
_b.label = 3;
responseBody = void 0;
_c.label = 3;
case 3:
_b.trys.push([3, 5, , 6]);
return [4 /*yield*/, response.json()];
case 4:
responseBody = _b.sent();
return [2 /*return*/, {
body: responseBody,
status: response.status,
headers: responseHeaders_1,
}];
_c.trys.push([3, 9, , 10]);
_a = opts.responseType;
switch (_a) {
case ResponseType_1.default.Text: return [3 /*break*/, 4];
case ResponseType_1.default.JSON: return [3 /*break*/, 6];
}
return [3 /*break*/, 6];
case 4: return [4 /*yield*/, response.text()];
case 5:
jsonErr_1 = _b.sent();
// Not JSON
responseBody = _c.sent();
return [3 /*break*/, 8];
case 6: return [4 /*yield*/, response.json()];
case 7:
responseBody = _c.sent();
return [3 /*break*/, 8];
case 8: return [3 /*break*/, 10];
case 9:
err_1 = _c.sent();
throw new caccl_error_1.default({
message: "Response was not valid JSON: ".concat(jsonErr_1 === null || jsonErr_1 === void 0 ? void 0 : jsonErr_1.message),
code: ErrorCode_1.default.NotJSON,
message: "Failed to parse response as ".concat(opts.responseType, ": ").concat(err_1 === null || err_1 === void 0 ? void 0 : err_1.message),
code: ErrorCode_1.default.ResponseParseError,
});
case 6: return [3 /*break*/, 8];
case 7:
err_1 = _b.sent();
case 10: return [3 /*break*/, 12];
case 11:
err_2 = _c.sent();
// Request failed! Check if we have more attempts

@@ -185,3 +194,3 @@ if (numRetries > 0) {

// Self-signed certificate error:
if (err_1.message.includes('self signed certificate')) {
if (err_2.message.includes('self signed certificate')) {
throw new caccl_error_1.default({

@@ -194,6 +203,6 @@ message: 'We refused to send a request because the receiver has self-signed certificates.',

throw new caccl_error_1.default({
message: "We encountered an error when trying to send a network request. If this issue persists, contact an admin. Error: ".concat(err_1 === null || err_1 === void 0 ? void 0 : err_1.message),
message: "We encountered an error when trying to send a network request. If this issue persists, contact an admin. Error: ".concat(err_2 === null || err_2 === void 0 ? void 0 : err_2.message),
code: ErrorCode_1.default.NotConnected,
});
case 8: return [2 /*return*/];
case 12: return [2 /*return*/];
}

@@ -200,0 +209,0 @@ });

{
"name": "caccl-send-request",
"version": "2.2.6",
"version": "2.2.7",
"description": "The default request sender used throughout the CACCL project.",

@@ -5,0 +5,0 @@ "main": "./lib/index.js",

@@ -6,5 +6,5 @@ // Highest error code: CSR3

SelfSigned = 'CSR2',
NotJSON = 'CSR3',
ResponseParseError = 'CSR3',
};
export default ErrorCode;

@@ -9,2 +9,3 @@ // Import libs

import ErrorCode from './ErrorCode';
import ResponseType from './ResponseType';

@@ -24,2 +25,3 @@ /**

* send cross-domain credentials even if not in dev mode
* @param [opts.responseType=ResponseType.JSON] expected response type
* @returns { body, status, headers } on success

@@ -36,2 +38,3 @@ */

sendCrossDomainCredentials?: boolean,
responseType?: ResponseType,
},

@@ -144,15 +147,18 @@ ): Promise<{

// Process json response
// Process response based on responseType
let responseBody: any;
try {
const responseBody = await response.json();
return {
body: responseBody,
status: response.status,
headers: responseHeaders,
};
} catch (jsonErr) {
// Not JSON
switch (opts.responseType) {
case ResponseType.Text:
responseBody = await response.text();
break;
case ResponseType.JSON:
default:
responseBody = await response.json();
break;
}
} catch (err) {
throw new CACCLError({
message: `Response was not valid JSON: ${(jsonErr as any)?.message}`,
code: ErrorCode.NotJSON,
message: `Failed to parse response as ${opts.responseType}: ${(err as any)?.message}`,
code: ErrorCode.ResponseParseError,
});

@@ -159,0 +165,0 @@ }

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc