Socket
Socket
Sign inDemoInstall

@starport/client-js

Package Overview
Dependencies
Maintainers
2
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@starport/client-js - npm Package Compare versions

Comparing version 0.1.14 to 0.1.22

.prettierrc

2

babel.config.js

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

module.exports = {
module.exports={
presets: [

@@ -3,0 +3,0 @@ '@babel/preset-typescript',

@@ -16,2 +16,4 @@ "use strict";

var _SpClientError = _interopRequireDefault(require("./errors/SpClientError"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

@@ -45,3 +47,3 @@

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

@@ -74,10 +76,4 @@ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

_defineProperty(_assertThisInitialized(_this), "connectedPromise", void 0);
_defineProperty(_assertThisInitialized(_this), "socket", void 0);
_defineProperty(_assertThisInitialized(_this), "connectRes", void 0);
_defineProperty(_assertThisInitialized(_this), "connectRej", void 0);
_defineProperty(_assertThisInitialized(_this), "signingClient", void 0);

@@ -100,15 +96,3 @@

if (_this.wsAddr) {
_this.connectedPromise = new Promise(function (res, rej) {
_this.connectRes = res;
_this.connectRej = rej;
});
try {
_this.socket = new _reconnectingWebsocket["default"](_this.wsAddr);
} catch (e) {
_this.connectRej();
throw "WS node unavailable";
}
_this.socket = new _reconnectingWebsocket["default"](_this.wsAddr);
_this.socket.onopen = _this.onOpenWS.bind(_assertThisInitialized(_this));

@@ -159,56 +143,15 @@ _this.socket.onmessage = _this.onMessageWS.bind(_assertThisInitialized(_this));

key: "switchWS",
value: function () {
var _switchWS = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(wsAddr) {
var _this2 = this;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
this.emit("ws-status", false);
this.wsAddr = wsAddr;
this.connectedPromise = new Promise(function (res, rej) {
_this2.connectRes = res;
_this2.connectRej = rej;
});
_context2.prev = 3;
this.socket = new _reconnectingWebsocket["default"](this.wsAddr);
_context2.next = 11;
break;
case 7:
_context2.prev = 7;
_context2.t0 = _context2["catch"](3);
this.connectRej();
throw "WS node unavailable";
case 11:
this.socket.onopen = this.onOpenWS.bind(this);
this.socket.onmessage = this.onMessageWS.bind(this);
this.socket.onerror = this.onErrorWS.bind(this);
this.socket.onclose = this.onCloseWS.bind(this);
return _context2.abrupt("return", this.connectedPromise);
case 16:
case "end":
return _context2.stop();
}
}
}, _callee2, this, [[3, 7]]);
}));
function switchWS(_x2) {
return _switchWS.apply(this, arguments);
}
return switchWS;
}()
value: function switchWS(wsAddr) {
this.emit('ws-status', false);
this.wsAddr = wsAddr;
this.socket = new _reconnectingWebsocket["default"](this.wsAddr);
}
}, {
key: "switchRPC",
value: function () {
var _switchRPC = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(rpcAddr) {
var _switchRPC = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(rpcAddr) {
var registry;
return regeneratorRuntime.wrap(function _callee3$(_context3) {
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context3.prev = _context3.next) {
switch (_context2.prev = _context2.next) {
case 0:

@@ -218,3 +161,3 @@ this.rpcAddr = rpcAddr;

if (!this.signingClient) {
_context3.next = 6;
_context2.next = 6;
break;

@@ -224,3 +167,3 @@ }

registry = _objectSpread({}, this.signingClient.registry);
_context3.next = 5;
_context2.next = 5;
return _stargate.SigningStargateClient.connectWithSigner(this.rpcAddr, this.signer, {

@@ -231,13 +174,13 @@ registry: registry

case 5:
this.signingClient = _context3.sent;
this.signingClient = _context2.sent;
case 6:
case "end":
return _context3.stop();
return _context2.stop();
}
}
}, _callee3, this);
}, _callee2, this);
}));
function switchRPC(_x3) {
function switchRPC(_x2) {
return _switchRPC.apply(this, arguments);

@@ -251,29 +194,30 @@ }

value: function () {
var _connectivityTest = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
return regeneratorRuntime.wrap(function _callee4$(_context4) {
var _connectivityTest = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context4.prev = _context4.next) {
switch (_context3.prev = _context3.next) {
case 0:
if (!this.apiAddr) {
_context4.next = 10;
_context3.next = 10;
break;
}
_context4.prev = 1;
_context4.next = 4;
return _axios["default"].get(this.apiAddr + "/node_info");
_context3.prev = 1;
_context3.next = 4;
return _axios["default"].get(this.apiAddr + '/node_info');
case 4:
this.emit("api-status", true);
_context4.next = 10;
this.emit('api-status', true);
_context3.next = 10;
break;
case 7:
_context4.prev = 7;
_context4.t0 = _context4["catch"](1);
_context3.prev = 7;
_context3.t0 = _context3["catch"](1);
if (!_context4.t0.response) {
this.emit("api-status", false);
if (!_context3.t0.response) {
this.emit('api-status', false);
console.error(new _SpClientError["default"]('Client-js:API', 'API Node unavailable'));
} else {
this.emit("api-status", true);
this.emit('api-status', true);
}

@@ -283,23 +227,24 @@

if (!this.rpcAddr) {
_context4.next = 20;
_context3.next = 20;
break;
}
_context4.prev = 11;
_context4.next = 14;
_context3.prev = 11;
_context3.next = 14;
return _axios["default"].get(this.rpcAddr);
case 14:
this.emit("rpc-status", true);
_context4.next = 20;
this.emit('rpc-status', true);
_context3.next = 20;
break;
case 17:
_context4.prev = 17;
_context4.t1 = _context4["catch"](11);
_context3.prev = 17;
_context3.t1 = _context3["catch"](11);
if (!_context4.t1.response) {
this.emit("rpc-status", false);
if (!_context3.t1.response) {
console.error(new _SpClientError["default"]('Client-js:API', 'RPC Node unavailable'));
this.emit('rpc-status', false);
} else {
this.emit("api-status", true);
this.emit('api-status', true);
}

@@ -309,6 +254,6 @@

case "end":
return _context4.stop();
return _context3.stop();
}
}
}, _callee4, this, [[1, 7], [11, 17]]);
}, _callee3, this, [[1, 7], [11, 17]]);
}));

@@ -323,10 +268,5 @@

}, {
key: "connectWS",
value: function connectWS() {
return this.connectedPromise;
}
}, {
key: "onErrorWS",
value: function onErrorWS() {
this.connectRej();
console.error(new _SpClientError["default"]('Client-js:WS', 'Could not connect to websocket.'));
}

@@ -336,3 +276,3 @@ }, {

value: function onCloseWS() {
this.emit("ws-status", false);
this.emit('ws-status', false);
}

@@ -342,8 +282,7 @@ }, {

value: function onOpenWS() {
this.connectRes();
this.emit("ws-status", true);
this.emit('ws-status', true);
this.socket.send(JSON.stringify({
jsonrpc: "2.0",
method: "subscribe",
id: "1",
jsonrpc: '2.0',
method: 'subscribe',
id: '1',
params: ["tm.event = 'NewBlock'"]

@@ -357,4 +296,4 @@ }));

if (result.data && result.data.type === "tendermint/event/NewBlock") {
this.emit("newblock", JSON.parse(msg.data).result);
if (result.data && result.data.type === 'tendermint/event/NewBlock') {
this.emit('newblock', JSON.parse(msg.data).result);
}

@@ -365,33 +304,33 @@ }

value: function () {
var _query = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5(url) {
var _query = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(url) {
var params,
response,
_args5 = arguments;
return regeneratorRuntime.wrap(function _callee5$(_context5) {
_args4 = arguments;
return regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context5.prev = _context5.next) {
switch (_context4.prev = _context4.next) {
case 0:
params = _args5.length > 1 && _args5[1] !== undefined ? _args5[1] : "";
_context5.prev = 1;
_context5.next = 4;
params = _args4.length > 1 && _args4[1] !== undefined ? _args4[1] : '';
_context4.prev = 1;
_context4.next = 4;
return _axios["default"].get(this.apiAddr + url + params);
case 4:
response = _context5.sent;
return _context5.abrupt("return", response.data);
response = _context4.sent;
return _context4.abrupt("return", response.data);
case 8:
_context5.prev = 8;
_context5.t0 = _context5["catch"](1);
throw "Could not access API: " + this.apiAddr + url + params;
_context4.prev = 8;
_context4.t0 = _context4["catch"](1);
console.error(new _SpClientError["default"]('Client-js:API', 'Could not access API: ' + url));
case 11:
case "end":
return _context5.stop();
return _context4.stop();
}
}
}, _callee5, this, [[1, 8]]);
}, _callee4, this, [[1, 8]]);
}));
function query(_x4) {
function query(_x3) {
return _query.apply(this, arguments);

@@ -406,3 +345,3 @@ }

var value = query[key];
return encodeURIComponent(key) + "=" + encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : "".concat(value));
return encodeURIComponent(key) + '=' + encodeURIComponent(Array.isArray(value) ? value.join(',') : typeof value === 'number' ? value : "".concat(value));
}

@@ -412,11 +351,11 @@ }, {

value: function toQueryString(rawQuery) {
var _this3 = this;
var _this2 = this;
var query = rawQuery || {};
var keys = Object.keys(query).filter(function (key) {
return "undefined" !== typeof query[key];
return 'undefined' !== typeof query[key];
});
return keys.map(function (key) {
return _typeof(query[key]) === "object" && !Array.isArray(query[key]) ? _this3.toQueryString(query[key]) : _this3.addQueryParam(query, key);
}).join("&");
return _typeof(query[key]) === 'object' && !Array.isArray(query[key]) ? _this2.toQueryString(query[key]) : _this2.addQueryParam(query, key);
}).join('&');
}

@@ -427,3 +366,3 @@ }, {

var queryString = this.toQueryString(rawQuery);
return queryString ? "?".concat(queryString) : "";
return queryString ? "?".concat(queryString) : '';
}

@@ -433,11 +372,11 @@ }, {

value: function () {
var _request = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6(_ref2) {
var _request = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5(_ref2) {
var body, path, query, method, url, response, data;
return regeneratorRuntime.wrap(function _callee6$(_context6) {
return regeneratorRuntime.wrap(function _callee5$(_context5) {
while (1) {
switch (_context6.prev = _context6.next) {
switch (_context5.prev = _context5.next) {
case 0:
body = _ref2.body, path = _ref2.path, query = _ref2.query, method = _ref2.method;
url = this.apiAddr + path + this.addQueryParams(query);
_context6.prev = 2;
_context5.prev = 2;
response = (0, _axios["default"])({

@@ -448,27 +387,27 @@ url: url,

headers: {
Accept: "application/json",
"Content-Type": "application/json;charset=UTF-8"
Accept: 'application/json',
'Content-Type': 'application/json;charset=UTF-8'
}
});
_context6.next = 6;
_context5.next = 6;
return response;
case 6:
data = _context6.sent;
return _context6.abrupt("return", data);
data = _context5.sent;
return _context5.abrupt("return", data);
case 10:
_context6.prev = 10;
_context6.t0 = _context6["catch"](2);
throw "Could not access API: " + url;
_context5.prev = 10;
_context5.t0 = _context5["catch"](2);
console.error(new _SpClientError["default"]('Client-js:API', 'Could not access API: ' + url));
case 13:
case "end":
return _context6.stop();
return _context5.stop();
}
}
}, _callee6, this, [[2, 10]]);
}, _callee5, this, [[2, 10]]);
}));
function request(_x5) {
function request(_x4) {
return _request.apply(this, arguments);

@@ -484,2 +423,3 @@ }

exports["default"] = SPClient;
exports["default"] = SPClient;
//# sourceMappingURL=SPClient.js.map
{
"name": "@starport/client-js",
"version": "0.1.14",
"version": "0.1.22",
"description": "A generic JS client for the Websocket, high-level (Cosmos) and low-level (Tendermint) APIs",

@@ -19,4 +19,4 @@ "author": "Tendermint, Inc <hello@tendermint.com>",

"scripts": {
"watch": "babel src --out-dir lib --extensions \".ts,.tsx\" --watch",
"build": "babel src --out-dir lib --extensions \".ts,.tsx\"",
"watch": "babel src --out-dir lib --extensions \".ts,.tsx\" --watch --source-maps",
"build": "babel src --out-dir lib --extensions \".ts,.tsx\" --source-maps",
"tsc": "tsc",

@@ -26,19 +26,22 @@ "test": "echo \"Error: run tests from root\" && exit 1"

"devDependencies": {
"@babel/cli": "^7.12.16",
"@babel/core": "^7.12.16",
"@babel/plugin-proposal-class-properties": "^7.12.13",
"@babel/preset-env": "^7.12.16",
"@babel/preset-typescript": "^7.12.16",
"@cosmjs/proto-signing": "^0.24.0-alpha.25",
"@babel/cli": "^7.13.0",
"@babel/core": "^7.13.8",
"@babel/plugin-proposal-class-properties": "^7.13.0",
"@babel/preset-env": "^7.13.9",
"@babel/preset-typescript": "^7.13.0",
"@cosmjs/proto-signing": "^0.24.0-alpha.26",
"@types/axios": "^0.14.0",
"@types/node": ">=12.0.0 < 13",
"@types/reconnectingwebsocket": "^1.0.6",
"@typescript-eslint/eslint-plugin": "^4.15.1",
"@typescript-eslint/parser": "^4.15.1",
"@typescript-eslint/eslint-plugin": "^4.16.1",
"@typescript-eslint/parser": "^4.16.1",
"babel-loader": "^8.2.2",
"eslint": "^7.20.0",
"typescript": "^4.1.5"
"eslint": "^7.21.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-prettier": "^3.3.1",
"prettier": "^2.2.1",
"typescript": "^4.2.3"
},
"dependencies": {
"@cosmjs/stargate": "^0.24.0-alpha.25",
"@cosmjs/stargate": "^0.24.0-alpha.26",
"@types/axios": "^0.14.0",

@@ -45,0 +48,0 @@ "@types/reconnectingwebsocket": "^1.0.6",

@@ -0,0 +0,0 @@ # `@starport/client-js`

@@ -1,216 +0,199 @@

import { EventEmitter } from "events";
import ReconnectingWebSocket from "reconnecting-websocket";
import axios, { AxiosPromise, AxiosResponse } from "axios";
import {
SigningStargateClient,
} from "@cosmjs/stargate";
import { OfflineDirectSigner, Registry } from "@cosmjs/proto-signing";
import { EventEmitter } from 'events';
import ReconnectingWebSocket from 'reconnecting-websocket';
import axios, { AxiosPromise, AxiosResponse } from 'axios';
import { SigningStargateClient } from '@cosmjs/stargate';
import { OfflineDirectSigner, Registry } from '@cosmjs/proto-signing';
import SpClientError from './errors/SpClientError';
export interface IClientConfig {
apiAddr: string;
rpcAddr?: string;
wsAddr?: string;
apiAddr: string;
rpcAddr?: string;
wsAddr?: string;
}
export type QueryParamsType = Record<string | number, any>;
export interface IFullRequestParams {
body?: unknown;
path: string;
query?: QueryParamsType;
method: "GET" | "POST" | "PUT";
body?: unknown;
path: string;
query?: QueryParamsType;
method: 'GET' | 'POST' | 'PUT';
}
export default class SPClient extends EventEmitter {
private apiAddr: string;
private rpcAddr: string;
private wsAddr: string;
private connectedPromise: Promise<void>;
private socket: ReconnectingWebSocket;
private connectRes;
private connectRej;
public signingClient;
private signer: OfflineDirectSigner;
private timer: ReturnType<typeof setInterval>;
private apiAddr: string;
private rpcAddr: string;
private wsAddr: string;
constructor({ apiAddr, rpcAddr, wsAddr }: IClientConfig) {
super();
this.apiAddr = apiAddr;
this.rpcAddr = rpcAddr;
this.wsAddr = wsAddr;
const poll: any = this.connectivityTest.bind(this);
this.timer = setInterval(poll, 5000);
this.connectivityTest();
if (this.wsAddr) {
this.connectedPromise = new Promise((res, rej) => {
this.connectRes = res;
this.connectRej = rej;
});
try {
this.socket = new ReconnectingWebSocket(this.wsAddr);
} catch (e) {
this.connectRej();
throw "WS node unavailable";
}
this.socket.onopen = this.onOpenWS.bind(this);
this.socket.onmessage = this.onMessageWS.bind(this);
this.socket.onerror = this.onErrorWS.bind(this);
this.socket.onclose = this.onCloseWS.bind(this);
}
private socket: ReconnectingWebSocket;
public signingClient;
private signer: OfflineDirectSigner;
private timer: ReturnType<typeof setInterval>;
constructor({ apiAddr, rpcAddr, wsAddr }: IClientConfig) {
super();
this.apiAddr = apiAddr;
this.rpcAddr = rpcAddr;
this.wsAddr = wsAddr;
const poll: any = this.connectivityTest.bind(this);
this.timer = setInterval(poll, 5000);
this.connectivityTest();
if (this.wsAddr) {
this.socket = new ReconnectingWebSocket(this.wsAddr);
this.socket.onopen = this.onOpenWS.bind(this);
this.socket.onmessage = this.onMessageWS.bind(this);
this.socket.onerror = this.onErrorWS.bind(this);
this.socket.onclose = this.onCloseWS.bind(this);
}
public async useSigner(signer: OfflineDirectSigner): Promise<void> {
this.signingClient = await SigningStargateClient.connectWithSigner(
this.rpcAddr,
signer
);
this.signer = signer;
}
public async useSigner(signer: OfflineDirectSigner): Promise<void> {
this.signingClient = await SigningStargateClient.connectWithSigner(
this.rpcAddr,
signer,
);
this.signer = signer;
}
public switchAPI(apiAddr: string): void {
this.apiAddr = apiAddr;
}
public switchWS(wsAddr: string): void {
this.emit('ws-status', false);
this.wsAddr = wsAddr;
this.socket = new ReconnectingWebSocket(this.wsAddr);
}
public async switchRPC(rpcAddr: string): Promise<void> {
this.rpcAddr = rpcAddr;
if (this.signingClient) {
const registry: Registry = { ...this.signingClient.registry };
this.signingClient = await SigningStargateClient.connectWithSigner(
this.rpcAddr,
this.signer,
{ registry },
);
}
public switchAPI(apiAddr: string):void {
this.apiAddr = apiAddr;
}
public async switchWS(wsAddr: string): Promise<void> {
this.emit("ws-status", false);
this.wsAddr = wsAddr;
this.connectedPromise = new Promise((res, rej) => {
this.connectRes = res;
this.connectRej = rej;
});
try {
this.socket = new ReconnectingWebSocket(this.wsAddr);
} catch (e) {
this.connectRej();
throw "WS node unavailable";
}
private async connectivityTest(): Promise<void> {
if (this.apiAddr) {
try {
await axios.get(this.apiAddr + '/node_info');
this.emit('api-status', true);
} catch (error) {
if (!error.response) {
this.emit('api-status', false);
console.error(
new SpClientError('Client-js:API', 'API Node unavailable'),
);
} else {
this.emit('api-status', true);
}
this.socket.onopen = this.onOpenWS.bind(this);
this.socket.onmessage = this.onMessageWS.bind(this);
this.socket.onerror = this.onErrorWS.bind(this);
this.socket.onclose = this.onCloseWS.bind(this);
return this.connectedPromise;
}
}
public async switchRPC(rpcAddr: string): Promise<void> {
this.rpcAddr = rpcAddr;
if (this.signingClient) {
const registry:Registry = { ...this.signingClient.registry };
this.signingClient = await SigningStargateClient.connectWithSigner(
this.rpcAddr,
this.signer,
{registry}
);
if (this.rpcAddr) {
try {
await axios.get(this.rpcAddr);
this.emit('rpc-status', true);
} catch (error) {
if (!error.response) {
console.error(
new SpClientError('Client-js:API', 'RPC Node unavailable'),
);
this.emit('rpc-status', false);
} else {
this.emit('api-status', true);
}
}
}
private async connectivityTest(): Promise<void> {
if (this.apiAddr) {
try {
await axios.get(this.apiAddr + "/node_info");
this.emit("api-status", true);
} catch (error) {
if (!error.response) {
this.emit("api-status", false);
} else {
this.emit("api-status", true);
}
}
}
if (this.rpcAddr) {
try {
await axios.get(this.rpcAddr);
this.emit("rpc-status", true);
} catch (error) {
if (!error.response) {
this.emit("rpc-status", false);
} else {
this.emit("api-status", true);
}
}
}
}
private onErrorWS(): void {
console.error(
new SpClientError('Client-js:WS', 'Could not connect to websocket.'),
);
}
private onCloseWS(): void {
this.emit('ws-status', false);
}
private onOpenWS(): void {
this.emit('ws-status', true);
this.socket.send(
JSON.stringify({
jsonrpc: '2.0',
method: 'subscribe',
id: '1',
params: ["tm.event = 'NewBlock'"],
}),
);
}
private onMessageWS(msg: any): void {
const result: any = JSON.parse(msg.data).result;
if (result.data && result.data.type === 'tendermint/event/NewBlock') {
this.emit('newblock', JSON.parse(msg.data).result);
}
public connectWS(): Promise<void> {
return this.connectedPromise;
}
public async query(url: string, params = ''): Promise<any> {
try {
const response: any = await axios.get(this.apiAddr + url + params);
return response.data;
} catch (e) {
console.error(
new SpClientError('Client-js:API', 'Could not access API: ' + url),
);
}
private onErrorWS(): void {
this.connectRej();
}
private onCloseWS(): void {
this.emit("ws-status", false);
}
private onOpenWS(): void {
this.connectRes();
this.emit("ws-status", true);
this.socket.send(
JSON.stringify({
jsonrpc: "2.0",
method: "subscribe",
id: "1",
params: ["tm.event = 'NewBlock'"],
})
);
}
private onMessageWS(msg: any): void {
const result: any = JSON.parse(msg.data).result;
if (result.data && result.data.type === "tendermint/event/NewBlock") {
this.emit("newblock", JSON.parse(msg.data).result);
}
}
public async query(url: string, params = ""): Promise<any> {
try {
const response: any = await axios.get(this.apiAddr + url + params);
return response.data;
} catch (e) {
throw "Could not access API: " + this.apiAddr + url + params;
}
}
}
private addQueryParam(query: QueryParamsType, key: string): string {
const value: any = query[key];
private addQueryParam(query: QueryParamsType, key: string): string {
const value: any = query[key];
return (
encodeURIComponent(key) +
"=" +
encodeURIComponent(
Array.isArray(value)
? value.join(",")
: typeof value === "number"
? value
: `${value}`
)
);
}
protected toQueryString(rawQuery?: QueryParamsType): string {
const query: QueryParamsType = rawQuery || {};
const keys: string[] = Object.keys(query).filter(
(key) => "undefined" !== typeof query[key]
);
return keys
.map((key) =>
typeof query[key] === "object" && !Array.isArray(query[key])
? this.toQueryString(query[key] as QueryParamsType)
: this.addQueryParam(query, key)
)
.join("&");
}
protected addQueryParams(rawQuery?: QueryParamsType): string {
const queryString: string = this.toQueryString(rawQuery);
return queryString ? `?${queryString}` : "";
}
public async request<T = any>({
body,
path,
query,
return (
encodeURIComponent(key) +
'=' +
encodeURIComponent(
Array.isArray(value)
? value.join(',')
: typeof value === 'number'
? value
: `${value}`,
)
);
}
protected toQueryString(rawQuery?: QueryParamsType): string {
const query: QueryParamsType = rawQuery || {};
const keys: string[] = Object.keys(query).filter(
(key) => 'undefined' !== typeof query[key],
);
return keys
.map((key) =>
typeof query[key] === 'object' && !Array.isArray(query[key])
? this.toQueryString(query[key] as QueryParamsType)
: this.addQueryParam(query, key),
)
.join('&');
}
protected addQueryParams(rawQuery?: QueryParamsType): string {
const queryString: string = this.toQueryString(rawQuery);
return queryString ? `?${queryString}` : '';
}
public async request<T = any>({
body,
path,
query,
method,
}: IFullRequestParams): Promise<AxiosResponse<T>> {
const url: string = this.apiAddr + path + this.addQueryParams(query);
try {
const response: AxiosPromise<any> = axios({
url,
method,
}: IFullRequestParams): Promise<AxiosResponse<T>> {
const url: string = this.apiAddr + path + this.addQueryParams(query);
try {
const response: AxiosPromise<any> = axios({
url,
method,
data: body,
headers: {
Accept: "application/json",
"Content-Type": "application/json;charset=UTF-8",
},
});
const data: AxiosResponse<any> = await response;
return data;
} catch (e) {
throw "Could not access API: " + url;
}
data: body,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json;charset=UTF-8',
},
});
const data: AxiosResponse<any> = await response;
return data;
} catch (e) {
console.error(
new SpClientError('Client-js:API', 'Could not access API: ' + url),
);
}
}
}

@@ -0,0 +0,0 @@ {

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

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