New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

smartapi-javascript

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

smartapi-javascript - npm Package Compare versions

Comparing version
1.0.20
to
1.0.21
+742
test/api_config.js
const request = {
generatingSession: {
client_code: "user11",
password: "1234",
totp: "122234"
},
placeOrder: {
"variety": "NORMAL",
"tradingsymbol": "SBIN-EQ",
"symboltoken": "3045",
"transactiontype": "BUY",
"exchange": "NSE",
"ordertype": "LIMIT",
"producttype": "INTRADAY",
"duration": "DAY",
"price": "19500",
"squareoff": "0",
"stoploss": "0",
"quantity": "1"
},
modifyOrder: {
"variety": "NORMAL",
"orderid": "201020000000080",
"ordertype": "LIMIT",
"producttype": "INTRADAY",
"duration": "DAY",
"price": "194.00",
"quantity": "1",
"tradingsymbol": "SBIN-EQ",
"symboltoken": "3045",
"exchange": "NSE"
},
cancelOrder: {
"variety": "NORMAL",
"orderid": "201020000000080",
},
convertPosition: {
"exchange": "NSE",
"oldproducttype": "DELIVERY",
"newproducttype": "MARGIN",
"tradingsymbol": "SBIN-EQ",
"transactiontype": "BUY",
"quantity": 1,
"type": "DAY"
},
createRule: {
"tradingsymbol": "SBIN-EQ",
"symboltoken": "3045",
"exchange": "NSE",
"transactiontype": "BUY",
"producttype": "DELIVERY",
"price": "195",
"qty": "1",
"triggerprice": "196",
"disclosedqty": "10",
"timeperiod": "20"
},
modifyRule: {
"id": "1",
"symboltoken": "3045",
"exchange": "NSE",
"price": "195",
"qty": "1",
"triggerprice": "196",
"disclosedqty": "10",
"timeperiod": "20"
},
cancelRule: {
"id": "1",
"symboltoken": "3045",
"exchange": "NSE"
},
ruleDetails: {
"id": "1"
},
ruleList: {
"status": [
"NEW",
"CANCELLED",
"ACTIVE",
"SENTTOEXCHANGE",
"FORALL"
],
"page": 1,
"count": 10
},
marketData: {
"mode": "FULL",
"exchangeTokens": {
"NSE": [
"3045"
]
}
},
getCandleData: {
"exchange": "NSE",
"symboltoken": "99926000",
"interval": "ONE_HOUR",
"fromdate": "2023-09-06 11:15",
"todate": "2023-09-06 12:00"
},
marginApi: {
"positions": [
{
"exchange": "NFO",
"qty": 50,
"price": 0,
"productType": "INTRADAY",
"token": "67300",
"tradeType": "BUY"
},
{
"exchange": "NFO",
"qty": 50,
"price": 0,
"productType": "INTRADAY",
"token": "67308",
"tradeType": "SELL"
}
]
},
searchScrip: {
"exchange": "BSE",
"searchscrip": "Titan"
},
generateToken: {
refreshToken: "refresh-token"
},
logout: {
client_code: "user_client_code"
},
indOrderDetails : "GuiOrderID"
}
const response = {
generatingSession: {
status: false,
data: {
jwtToken: "jwt-token",
refreshToken: "refresh-token",
feedToken: "feed-token"
}
},
getProfile: {
status: true,
data: {
name: "user",
mob_no: "9952207684"
}
},
placeOrder: {
status: true,
data: {
orderId: "SA12900"
}
},
modifyOrder: {
"status": true,
"message": "SUCCESS",
"errorcode": "",
"data": {
"orderid": "201020000000080"
}
},
cancelOrder: {
"status": true,
"message": "SUCCESS",
"errorcode": "",
"data": {
"orderid": "201020000000080"
}
},
orderBook: {
"status": true,
"message": "SUCCESS",
"errorcode": "",
"data": [{
"variety": "NORMAL",
"ordertype": "LIMIT",
"producttype": "INTRADAY",
"duration": "DAY",
"price": "194.00",
"triggerprice": "0",
"quantity": "1",
"disclosedquantity": "0",
"squareoff": "0",
"stoploss": "0",
"trailingstoploss": "0",
"tradingsymbol": "SBIN-EQ",
"transactiontype": "BUY",
"exchange": "NSE",
"symboltoken": null,
"instrumenttype": "",
"strikeprice": "-1",
"optiontype": "",
"expirydate": "",
"lotsize": "1",
"cancelsize": "1",
"averageprice": "0",
"filledshares": "0",
"unfilledshares": "1",
"orderid": 201020000000080,
"text": "",
"status": "cancelled",
"orderstatus": "cancelled",
"updatetime": "20-Oct-2020 13:10:59",
"exchtime": "20-Oct-2020 13:10:59",
"exchorderupdatetime": "20-Oct-2020 13:10:59",
"fillid": "",
"filltime": "",
"parentorderid": ""
}]
},
tradeBook: {
"status": true,
"message": "SUCCESS",
"errorcode": "",
"data": [{
"exchange": "NSE",
"producttype": "DELIVERY",
"tradingsymbol": "ITC-EQ",
"instrumenttype": "",
"symbolgroup": "EQ",
"strikeprice": "-1",
"optiontype": "",
"expirydate": "",
"marketlot": "1",
"precision": "2",
"multiplier": "-1",
"tradevalue": "175.00",
"transactiontype": "BUY",
"fillprice": "175.00",
"fillsize": "1",
"orderid": "201020000000095",
"fillid": "50005750",
"filltime": "13:27:53",
}]
},
getHolding: {
"tradingsymbol": "TATASTEEL-EQ",
"exchange": "NSE",
"isin": "INE081A01020",
"t1quantity": 0,
"realisedquantity": 2,
"quantity": 2,
"authorisedquantity": 0,
"product": "DELIVERY",
"collateralquantity": null,
"collateraltype": null,
"haircut": 0,
"averageprice": 111.87,
"ltp": 130.15,
"symboltoken": "3499",
"close": 129.6,
"profitandloss": 37,
"pnlpercentage": 16.34
},
getPosition: {
"status": true,
"message": "SUCCESS",
"errorcode": "",
"data": [
{
"exchange": "NSE",
"symboltoken": "2885",
"producttype": "DELIVERY",
"tradingsymbol": "RELIANCE-EQ",
"symbolname": "RELIANCE",
"instrumenttype": "",
"priceden": "1",
"pricenum": "1",
"genden": "1",
"gennum": "1",
"precision": "2",
"multiplier": "-1",
"boardlotsize": "1",
"buyqty": "1",
"sellqty": "0",
"buyamount": "2235.80",
"sellamount": "0",
"symbolgroup": "EQ",
"strikeprice": "-1",
"optiontype": "",
"expirydate": "",
"lotsize": "1",
"cfbuyqty": "0",
"cfsellqty": "0",
"cfbuyamount": "0",
"cfsellamount": "0",
"buyavgprice": "2235.80",
"sellavgprice": "0",
"avgnetprice": "2235.80",
"netvalue": "- 2235.80",
"netqty": "1",
"totalbuyvalue": "2235.80",
"totalsellvalue": "0",
"cfbuyavgprice": "0",
"cfsellavgprice": "0",
"totalbuyavgprice": "2235.80",
"totalsellavgprice": "0",
"netprice": "2235.80"
}
]
},
convertPosition: {
"status": true,
"message": "SUCCESS",
"errorcode": "",
"data": null
},
getRMS: {
status: true,
message: 'SUCCESS',
errorcode: '',
data: {
net: '0.0000',
availablecash: '0.0000',
availableintradaypayin: '0.0000',
availablelimitmargin: '0.0000',
collateral: '0.0000',
m2munrealized: '0.0000',
m2mrealized: '0.0000',
utiliseddebits: '0.0000',
utilisedspan: null,
utilisedoptionpremium: null,
utilisedholdingsales: null,
utilisedexposure: null,
utilisedturnover: null,
utilisedpayout: '0.0000'
}
},
createRule: {
"status": true,
"message": "SUCCESS",
"errorcode": "",
"data": {
"id": "1"
}
},
modifyRule: {
"status": true,
"message": "SUCCESS",
"errorcode": "",
"data": {
"id": "1"
}
},
cancelRule: {
"status": true,
"message": "SUCCESS",
"errorcode": "",
"data": {
"id": "1"
}
},
ruleDetails: {
"status": true,
"message": "SUCCESS",
"errorcode": "",
"data": {
"status": "NEW",
"createddate": "2020-11-16T14:19:51Z",
"updateddate": "2020-11-16T14:28:01Z",
"expirydate": "2021-11-16T14:19:51Z",
"clientid": "100",
"tradingsymbol": "SBIN-EQ",
"symboltoken": "3045",
"exchange": "NSE",
"transactiontype": "BUY",
"producttype": "DELIVERY",
"price": "195",
"qty": "1",
"triggerprice": "196",
"disclosedqty": "10"
}
},
ruleList: {
"status": true,
"message": "SUCCESS",
"errorcode": "",
"data": {
"clientid": "100",
"createddate": "2020-11-16T14:19:51Z",
"exchange": "NSE",
"producttype": "DELIVERY",
"transactiontype": "BUY",
"expirydate": "2021-11-16T14:19:51Z",
"id": "1",
"qty": "1",
"price": "195",
"status": "NEW",
"symboltoken": "3045",
"tradingsymbol": "SBIN-EQ",
"triggerprice": "196",
"updateddate": "2020-11-16T14:28:01Z"
}
},
marketData: {
full: {
"status": true,
"message": "SUCCESS",
"errorcode": "",
"data": {
"fetched": [
{
"exchange": "NSE",
"tradingSymbol": "SBIN-EQ",
"symbolToken": "3045",
"ltp": 568.2,
"open": 567.4,
"high": 569.35,
"low": 566.1,
"close": 567.4,
"lastTradeQty": 1,
"exchFeedTime": "21-Jun-2023 10:46:10",
"exchTradeTime": "21-Jun-2023 10:46:09",
"netChange": 0.8,
"percentChange": 0.14,
"avgPrice": 567.83,
"tradeVolume": 3556150,
"opnInterest": 0,
"lowerCircuit": 510.7,
"upperCircuit": 624.1,
"totBuyQuan": 839549,
"totSellQuan": 1284767,
"52WeekLow": 430.7,
"52WeekHigh": 629.55,
"depth": {
"buy": [
{
"price": 568.2,
"quantity": 511,
"orders": 2
},
{
"price": 568.15,
"quantity": 411,
"orders": 2
},
{
"price": 568.1,
"quantity": 31,
"orders": 2
},
{
"price": 568.05,
"quantity": 1020,
"orders": 8
},
{
"price": 568,
"quantity": 1704,
"orders": 28
}
],
"sell": [
{
"price": 568.25,
"quantity": 3348,
"orders": 5
},
{
"price": 568.3,
"quantity": 4447,
"orders": 13
},
{
"price": 568.35,
"quantity": 3768,
"orders": 11
},
{
"price": 568.4,
"quantity": 8500,
"orders": 40
},
{
"price": 568.45,
"quantity": 4814,
"orders": 17
}
]
}
}
],
"unfetched": []
}
},
OHLC: {
"status": true,
"message": "SUCCESS",
"errorcode": "",
"data": {
"fetched": [
{
"exchange": "NSE",
"tradingSymbol": "SBIN-EQ",
"symbolToken": "3045",
"ltp": 571.8,
"open": 568.75,
"high": 568.75,
"low": 567.05,
"close": 566.5
}
],
"unfetched": []
}
},
LTP: {
"status": true,
"message": "SUCCESS",
"errorcode": "",
"data": {
"fetched": [
{
"exchange": "NSE",
"tradingSymbol": "SBIN-EQ",
"symbolToken": "3045",
"ltp": 571.75
}
],
"unfetched": []
}
}
},
getCandleData: {
"status": true,
"message": "SUCCESS",
"errorcode": "",
"data": [
[
"2023-09-06T11:15:00+05:30",
19571.2,
19573.35,
19534.4,
19552.05,
0
]
]
},
marginApi: {
"status": true,
"message": "SUCCESS",
"errorcode": "",
"data": {
"totalMarginRequired": 29612.35,
"marginComponents": {
"netPremium": 5060,
"spanMargin": 0,
"marginBenefit": 79876.5,
"deliveryMargin": 0,
"nonNFOMargin": 0,
"totOptionsPremium": 10100
},
"marginBreakup": [
{
"exchange": "NFO",
"productType": "INTRADAY",
"totalMarginRequired": 19512.35
}
],
"optionsBuy": {
"totOptionsPremium": 10100,
"optionDetails": [
{
"exchange": "NFO",
"productType": "INTRADAY",
"token": "67300",
"lotMultiplier": 50,
"optionPremium": 10100
}
]
}
}
},
searchScrip: {
"status": true,
"message": 'SUCCESS',
"errorcode": '',
"data": [
{ exchange: 'BSE', tradingsymbol: 'TITAN', symboltoken: '500114' },
{
exchange: 'BSE',
tradingsymbol: 'TITANBIO',
symboltoken: '524717'
},
{
exchange: 'BSE',
tradingsymbol: 'TITANIN',
symboltoken: '521005'
},
{
exchange: 'BSE',
tradingsymbol: 'TITANSEC',
symboltoken: '530045'
}
]
},
generateToken: {
"status": true,
"message": 'SUCCESS',
"errorcode": '',
"data": {
jwtToken: "example-jwt-token",
refreshToken: "refresh-jwt-token"
}
},
logout: {
"status": true,
"message": 'SUCCESS',
"errorcode": '',
"data": null
},
getAllHolding: {
"status": true,
"message": "SUCCESS",
"errorcode": "",
"data": {
"holdings": [
{
"tradingsymbol": "TATASTEEL-EQ",
"exchange": "NSE",
"isin": "INE081A01020",
"t1quantity": 0,
"realisedquantity": 2,
"quantity": 2,
"authorisedquantity": 0,
"product": "DELIVERY",
"collateralquantity": null,
"collateraltype": null,
"haircut": 0,
"averageprice": 111.87,
"ltp": 130.15,
"symboltoken": "3499",
"close": 129.6,
"profitandloss": 37,
"pnlpercentage": 16.34
},
{
"tradingsymbol": "PARAGMILK-EQ",
"exchange": "NSE",
"isin": "INE883N01014",
"t1quantity": 0,
"realisedquantity": 2,
"quantity": 2,
"authorisedquantity": 0,
"product": "DELIVERY",
"collateralquantity": null,
"collateraltype": null,
"haircut": 0,
"averageprice": 154.03,
"ltp": 201,
"symboltoken": "17130",
"close": 192.1,
"profitandloss": 94,
"pnlpercentage": 30.49
},
{
"tradingsymbol": "SBIN-EQ",
"exchange": "NSE",
"isin": "INE062A01020",
"t1quantity": 0,
"realisedquantity": 8,
"quantity": 8,
"authorisedquantity": 0,
"product": "DELIVERY",
"collateralquantity": null,
"collateraltype": null,
"haircut": 0,
"averageprice": 573.1,
"ltp": 579.05,
"symboltoken": "3045",
"close": 570.5,
"profitandloss": 48,
"pnlpercentage": 1.04
}
],
"totalholding": {
"totalholdingvalue": 5294,
"totalinvvalue": 5116,
"totalprofitandloss": 178.14,
"totalpnlpercentage": 3.48
}
}
},
indOrderDetails: {
"status": true,
"message": "SUCCESS",
"errorcode": "",
"data": {
"variety": "NORMAL",
"ordertype": "LIMIT",
"producttype": "DELIVERY",
"duration": "DAY",
"price": 2298.25,
"triggerprice": 0,
"quantity": "1",
"disclosedquantity": "0",
"squareoff": 0,
"stoploss": 0,
"trailingstoploss": 0,
"tradingsymbol": "RELIANCE-EQ",
"transactiontype": "BUY",
"exchange": "NSE",
"symboltoken": "2885",
"instrumenttype": "",
"strikeprice": -1,
"optiontype": "",
"expirydate": "",
"lotsize": "1",
"cancelsize": "0",
"averageprice": 0,
"filledshares": "0",
"unfilledshares": "1",
"orderid": "231010000000970",
"text": "Your order has been rejected due to Insufficient Funds. Available funds - Rs. 937.00 . You require Rs. 2298.25 funds to execute this order.",
"status": "rejected",
"orderstatus": "rejected",
"updatetime": "10-Oct-2023 09:00:16",
"exchtime": "",
"exchorderupdatetime": "",
"fillid": "",
"filltime": "",
"parentorderid": "",
"ordertag": "",
"uniqueorderid": "05ebf91b-bea4-4a1d-b0f2-4259606570e3"
}
}
}
const errorResponse = {
status: false,
message: "",
errorcode: "",
data: null
}
module.exports = {
response: response,
request: request,
errorResponse: errorResponse
};
const sinon = require('sinon');
const config = {
api_key: "user_api_key",
client_code: "user01",
jwttoken: "jwt-token",
default_login_uri :"https://smartapi.angelbroking.com/publisher-login",
requestInstance: {
request: sinon.stub(),
interceptors: {
request: {
use: sinon.stub(),
},
response: {
use: sinon.stub(),
},
},
defaults: {
headers: {
post: {
'Content-Type': 'application/json',
},
put: {
'Content-Type': 'application/json',
},
},
}
}
};
module.exports = config;
const sinon = require('sinon');
const proxyquire = require('proxyquire');
const { expect, assert } = require('chai');
let querystring = require('querystring');
const config = require('./config');
let address = require('address');
const { request, response } = require('./api_config');
let SmartApi;
let smartApiInstance;
const requestInstance = {
request: sinon.stub(),
interceptors: {
request: {
use: sinon.stub(),
},
response: {
use: sinon.stub(),
},
},
defaults: {
headers: {
post: {
'Content-Type': 'application/json',
},
put: {
'Content-Type': 'application/json',
},
},
},
paramsSerializer : function (params) {
return querystring.stringify(params);
}
};
beforeEach(() => {
SmartApi = proxyquire('../lib/smartapi-connect', {
axios: {
create: () => requestInstance,
},
});
smartApiInstance = new SmartApi({
api_key: config.api_key
});
address(function (err, addrs) {
smartApiInstance.local_ip = addrs !== undefined ? addrs.ip : '192.168.168.168';
smartApiInstance.mac_addr =
addrs !== undefined ? addrs.mac : 'fe80::216e:6507:4b90:3719';
});
});
afterEach(() => {
sinon.restore();
});
describe('Smart_API_User_Flow', () => {
it('Generating_Session_with_SmartAPI', async () => {
requestInstance.request.resolves(response.generatingSession);
let sessionReq = request.generatingSession;
try {
const token_data = smartApiInstance.generateSession(sessionReq.client_code, sessionReq.password, sessionReq.totp);
token_data.then((response) => {
if (response.status) {
smartApiInstance.setClientCode(sessionReq.client_code);
smartApiInstance.setAccessToken(response.data.jwtToken);
smartApiInstance.setPublicToken(response.data.refreshToken);
assert.equal(response.status, true);
} else {
assert.equal(response.status, false);
}
})
} catch (error) {
assert.fail("error while generating session", error);
}
});
it("Getting_login_Url",()=>{
smartApiInstance.getLoginURL();
})
it("Get_User_profile", async () => {
requestInstance.request.resolves(response.getProfile);
try {
const profileRes = await smartApiInstance.getProfile();
if (profileRes.status) {
assert.equal(profileRes.status, true);
} else {
assert.equal(profileRes.status, false);
}
} catch (error) {
assert.fail("error while getting user profile", error);
}
})
it("generating token", () => {
requestInstance.request.resolves(response.generateToken);
let tokenReq = request.generateToken;
let token_data = smartApiInstance.generateToken(tokenReq);
token_data.then((response) => {
if (response.status) {
smartApiInstance.setAccessToken(response.data.jwtToken);
smartApiInstance.setPublicToken(response.data.refreshToken);
assert.equal(response.status, true);
} else {
assert.equal(response.status, false);
}
})
})
it("user_log_out", async () => {
requestInstance.request.resolves(response.logout);
let tokenReq = request.logout;
let logoutres = await smartApiInstance.logout(tokenReq);
assert.equal(logoutres.status, true);
})
});
describe("SmartApi_Order_Flow", () => {
it("Placing_order", async () => {
requestInstance.request.resolves(response.placeOrder);
let placeOrderReq = request.placeOrder;
try {
const placeOrderRes = await smartApiInstance.placeOrder(placeOrderReq);
if (placeOrderRes.status) {
assert.equal(placeOrderRes.status, true);
} else {
assert.equal(placeOrderRes.status, false);
}
} catch (error) {
assert.fail("error while placing order", error);
}
})
it("Modify_order", async () => {
requestInstance.request.resolves(response.modifyOrder);
let modifyOrderReq = request.modifyOrder;
try {
const modifyOrderRes = await smartApiInstance.modifyOrder(modifyOrderReq);
if (modifyOrderRes.status) {
assert.equal(modifyOrderRes.status, true);
} else {
assert.equal(modifyOrderRes.status, false);
}
} catch (error) {
assert.fail("error while modify order", error);
}
})
it("Cancel_order", async () => {
requestInstance.request.resolves(response.cancelOrder);
let cancelOrderReq = request.cancelOrder;
try {
const cancelOrderRes = await smartApiInstance.cancelOrder(cancelOrderReq);
if (cancelOrderRes.status) {
assert.equal(cancelOrderRes.status, true);
} else {
assert.equal(cancelOrderRes.status, false);
}
} catch (error) {
assert.fail("error while cancel order", error);
}
})
it("get_order_book", async () => {
requestInstance.request.resolves(response.orderBook);
try {
const orderBookRes = await smartApiInstance.getOrderBook();
if (orderBookRes.status) {
assert.equal(orderBookRes.status, true);
} else {
assert.equal(orderBookRes.status, false);
}
} catch (error) {
assert.fail("error while getting order book", error);
}
})
it("get_trade_book", async () => {
requestInstance.request.resolves(response.tradeBook);
try {
const tradeBookRes = await smartApiInstance.getTradeBook();
if (tradeBookRes.status) {
assert.equal(tradeBookRes.status, true);
} else {
assert.equal(tradeBookRes.status, false);
}
} catch (error) {
assert.fail("error while getting trade book", error);
}
})
it("indOrderDetails", async () => {
requestInstance.request.resolves(response.indOrderDetails);
try {
let indOrderDetailParams = request.indOrderDetails;
const indOrderDetailRes = await smartApiInstance.indOrderDetails(indOrderDetailParams);
if (indOrderDetailRes.status) {
assert.equal(indOrderDetailRes.status, true);
} else {
assert.equal(indOrderDetailRes.status, false);
}
} catch (error) {
assert.fail("error while getting indOrderDetails", error);
}
})
})
describe("SmartAPI_Portfolio", () => {
it("get_holdings", async () => {
requestInstance.request.resolves(response.getHolding);
try {
const getHoldingRes = await smartApiInstance.getHolding();
if(getHoldingRes){
expect(getHoldingRes).to.not.to.be.null;
}else{
expect(getHoldingRes).to.to.be.null;
}
} catch (error) {
assert.fail("error while getting holdings", error);
}
})
it("get_all_holdings", async () => {
requestInstance.request.resolves(response.getAllHolding);
try {
const getAllHoldingRes = await smartApiInstance.getAllHolding();
if(getAllHoldingRes.status){
assert.equal(getAllHoldingRes.status,true)
}else{
assert.equal(getAllHoldingRes.status,false)
}
} catch (error) {
assert.fail("error while getting all holdings", error);
}
})
it("get_positions", async () => {
requestInstance.request.resolves(response.getPosition);
try {
const getPositionRes = await smartApiInstance.getPosition();
if (getPositionRes.status) {
assert.equal(getPositionRes.status, true);
} else {
assert.equal(getPositionRes.status, false);
}
} catch (error) {
assert.fail("error while getting positions", error);
}
})
it("convert_positions", async () => {
requestInstance.request.resolves(response.convertPosition);
let req = request.convertPosition;
try {
const convertPositionRes = await smartApiInstance.convertPosition(req);
if (convertPositionRes.status) {
assert.equal(convertPositionRes.status, true);
} else {
assert.equal(convertPositionRes.status, false);
}
} catch (error) {
assert.fail("error while converting positions", error);
}
})
it("get_rms", async () => {
requestInstance.request.resolves(response.getRMS);
try {
const getRMSresponse = await smartApiInstance.getRMS();
if (getRMSresponse.status) {
assert.equal(getRMSresponse.status, true);
} else {
assert.equal(getRMSresponse.status, false);
}
} catch (error) {
assert.fail("error while getting RMS details", error);
}
})
})
describe("SmartApi_GTT", () => {
it("create_rule", async () => {
requestInstance.request.resolves(response.createRule);
let createRuleReq = request.createRule;
try {
const createRuleRes = await smartApiInstance.createRule(createRuleReq);
if (createRuleRes.status) {
assert.equal(createRuleRes.status, true);
} else {
assert.equal(createRuleRes.status, false);
}
} catch (error) {
assert.fail("error while creating rule", error);
}
})
it("modify_rule", async () => {
requestInstance.request.resolves(response.modifyRule);
let modifyRuleReq = request.modifyRule;
try {
const modifyRuleRes = await smartApiInstance.modifyRule(modifyRuleReq);
if (modifyRuleRes.status) {
assert.equal(modifyRuleRes.status, true);
} else {
assert.equal(modifyRuleRes.status, false);
}
} catch (error) {
assert.fail("error while creating rule", error);
}
})
it("cancel_rule", async () => {
requestInstance.request.resolves(response.cancelRule);
let cancelRuleReq = request.cancelRule;
try {
const cancelRuleRes = await smartApiInstance.cancelRule(cancelRuleReq);
if (cancelRuleRes.status) {
assert.equal(cancelRuleRes.status, true);
} else {
assert.equal(cancelRuleRes.status, false);
}
} catch (error) {
assert.fail("error while cancel rule", error);
}
})
it("get_rule_detail", async () => {
requestInstance.request.resolves(response.ruleDetails);
let ruleDetailsReq = request.ruleDetails;
try {
const ruleDetailsRes = await smartApiInstance.ruleDetails(ruleDetailsReq);
if (ruleDetailsRes.status) {
assert.equal(ruleDetailsRes.status, true);
} else {
assert.equal(ruleDetailsRes.status, false);
}
} catch (error) {
assert.fail("error while getting rule detail", error);
}
})
it("get_rule_list", async () => {
requestInstance.request.resolves(response.ruleList);
let ruleListReq = request.ruleList;
try {
const ruleListRes = await smartApiInstance.ruleList(ruleListReq);
if (ruleListRes.status) {
assert.equal(ruleListRes.status, true);
} else {
assert.equal(ruleListRes.status, false);
}
} catch (error) {
assert.fail("error while getting rule list", error);
}
})
})
describe("SmartApi_Market_Data", () => {
it("get_market_data", async () => {
requestInstance.request.resolves(response.marketData.full);
let marketDataReq = request.marketData;
try {
const marketDataRes = await smartApiInstance.marketData(marketDataReq);
if (marketDataRes.status) {
assert.equal(marketDataRes.status, true);
} else {
assert.equal(marketDataRes.status, false);
}
} catch (error) {
assert.fail("error while getting market data", error);
}
})
})
describe("SmartApi_HistoricalAPI", () => {
it("get_candle_data", async () => {
requestInstance.request.resolves(response.getCandleData);
let candleDataReq = request.getCandleData;
try {
const candleDataRes = await smartApiInstance.getCandleData(candleDataReq);
if (candleDataRes.status) {
assert.equal(candleDataRes.status, true);
} else {
assert.equal(candleDataRes.status, false);
}
} catch (error) {
assert.fail("error while getting candle data", error);
}
})
})
describe("SmartApi_MarginAPI", () => {
it("get_margin_calculator", async () => {
requestInstance.request.resolves(response.marginApi);
let marginApiReq = request.marginApi;
try {
const marginApiRes = await smartApiInstance.marginApi(marginApiReq);
if (marginApiRes.status) {
assert.equal(marginApiRes.status, true);
} else {
assert.equal(marginApiRes.status, false);
}
} catch (error) {
assert.fail("error while getting margin data", error);
}
})
it("search_scrip", async () => {
requestInstance.request.resolves(response.searchScrip);
let searchScripReq = request.searchScrip;
try {
const searchScripRes = smartApiInstance.searchScrip(searchScripReq);
searchScripRes.then((data)=>{
if(data.length > 0){
assert(Array.isArray(searchScripRes))
}else if (data?.status === true && data?.data?.length === 0) {
console.log("Search successful. No matching trading symbols found for the given query.");
assert(Array.isArray(searchScripRes))
} else {
assert(Array.isArray(searchScripRes))
}
})
} catch (error) {
assert.fail("error while searching scrip data", error);
}
})
})
describe("mocking_request_util", () => {
it("request_util", () => {
// Mocking the API object
const API = {
sampleRoute: 'http://sample.url/api'
};
// Mocking the self object
const self = {
api_key: 'your_api_key',
access_token: 'sampleAccessToken'
};
// Mocking the requestInstance
const requestInstance = {
request: sinon.stub().resolves({ data: 'sample response' }) // Stubbing the request method
};
// Assuming the function is not directly imported and is used within a larger context
function request_util_qParams(
route,
method,
qParams
) {
let url = API[route],
payload = null;
if (qParams) {
url = `${url}/${qParams}`;
}
if (method !== 'GET' || method !== 'DELETE') {
payload = qParams;
}
let options = {
method: method,
url: url,
data: JSON.stringify(payload),
headers: {
"Content-Type": "application/json",
Accept: "application/json",
"X-UserType": "USER",
"X-SourceID": "WEB",
"X-PrivateKey": self.api_key,
},
};
if (self.access_token) {
options["headers"]["Authorization"] = "Bearer " + self.access_token;
}
return requestInstance.request(options);
}
describe('request_util_qParams function', () => {
it('should form the correct URL when qParams are provided', () => {
const route = 'sampleRoute';
const method = 'GET';
const qParams = '123';
const expectedResult = { data: 'sample response' };
const result = request_util_qParams(route, method, qParams);
assert.equal(result.data,expectedResult.result);
});
});
})
})
+0
-0

@@ -0,0 +0,0 @@ module.exports.API = {

@@ -0,0 +0,0 @@ const CONSTANTS = {

@@ -0,0 +0,0 @@ let { SmartAPI, WebSocketClient, WebSocketV2, WSOrderUpdates } = require('../lib');

@@ -0,0 +0,0 @@ 'use strict';

+15
-4

@@ -7,3 +7,3 @@ 'use strict';

let publicIp = require('public-ip');
const https = require('https');
let { API } = require('../config/api');

@@ -64,2 +64,8 @@

},
httpsAgent : new https.Agent({
minVersion: 'TLSv1.2',
maxVersion: 'TLSv1.3'
})
});

@@ -179,5 +185,10 @@

self.setClientCode(client_code);
self.setAccessToken(response.data.jwtToken);
self.setPublicToken(response.data.refreshToken);
if(response.data){
if(response.data.jwtToken){
self.setAccessToken(response.data.jwtToken);
}
if(response.data.refreshToken){
self.setPublicToken(response.data.refreshToken);
}
}
}

@@ -184,0 +195,0 @@ })

@@ -0,0 +0,0 @@ let web_socket = require('ws');

@@ -0,0 +0,0 @@ let web_socket = require("ws");

@@ -0,0 +0,0 @@ let WebSocket = require('ws');

@@ -0,0 +0,0 @@ let WebSocket = require("ws");

@@ -0,0 +0,0 @@ var webSocket = null;

{
"name": "smartapi-javascript",
"version": "1.0.20",
"version": "1.0.21",
"description": "",
"main": "./lib/",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "nyc --reporter=html mocha --verbose"
},

@@ -20,7 +20,12 @@ "repository": {

"binary-parser": "^2.2.1",
"chai": "^4.3.10",
"mocha": "^10.2.0",
"nyc": "^15.1.0",
"pako": "^2.1.0",
"proxyquire": "^2.1.3",
"public-ip": "^4.0.2",
"querystring": "^0.2.0",
"sinon": "^17.0.1",
"ws": "^8.12.0"
}
}

@@ -0,0 +0,0 @@ # SmartAPI Javascript Client SDK