@extra-fyers/http
Version:
A Javascript interface for FYERS API {http}.
207 lines (202 loc) • 6.93 kB
JavaScript
;
var https = require('https');
function queryString(data) {
if (data == null)
return '';
var a = new URLSearchParams();
for (var k in data)
a.append(k, data[k]);
return '?' + a.toString();
}
function getHttpError(error) {
var a = error;
a.code = 0;
a.response = null;
return a;
}
function getHttpResponseError(error, code, response) {
var a = error;
a.code = code;
a.response = response;
return a;
}
function hasHeader(key, o) {
if (o.headers == null)
return false;
if (o.headers.hasOwnProperty(key))
return true;
if (o.headers.hasOwnProperty(key.toLowerCase()))
return true;
return false;
}
function setHeaders(headers, o) {
if (o.body != null && !hasHeader('Content-Type', o)) {
headers['content-type'] = 'application/json; charset=utf-8';
}
if (!hasHeader('Accept', o)) {
headers['accept'] = 'application/json, text/*';
}
}
function httpRequest(o) {
return new Promise((resolve, reject) => {
var { url, method, headers, timeout } = o;
setHeaders(headers, o);
var req = https.request(url, { method, headers, timeout }, res => {
var body = '';
res.setEncoding('utf8');
res.on('error', e => reject(getHttpResponseError(e, res.statusCode, body)));
res.on('data', chunk => body += chunk);
res.on('end', () => resolve(body));
});
req.on('error', e => reject(getHttpError(e)));
if (o.body != null) {
req.useChunkedEncodingByDefault = true;
req.write(JSON.stringify(o.body));
}
req.end();
});
}
function httpRequestText(options) {
return httpRequest(options);
}
async function httpRequestJson(options) {
var response = await httpRequest(options);
return JSON.parse(response);
}
const API_URL = 'https://api.fyers.in/api/v2/';
const DATA_URL = 'https://api.fyers.in/data-rest/v2/';
const SYMBOLS_URL = 'https://public.fyers.in/sym_details/';
function requestStep(auth, method, path, query, body) {
var url = path + queryString(query);
var headers = {};
if (auth != null)
headers['authorization'] = auth.app_id + ':' + auth.access_token;
return { url, method, headers, body };
}
function requestText(auth, method, path, query, body) {
return httpRequestText(requestStep(auth, method, path, query, body));
}
function requestJson(auth, method, path, query, body) {
return httpRequestJson(requestStep(auth, method, path, query, body));
}
function requestApi(auth, method, path, query, body) {
return requestJson(auth, method, API_URL + path, query, body);
}
function requestData(auth, method, path, query, body) {
return requestJson(auth, method, DATA_URL + path, query, body);
}
function requestSymbols(auth, method, path, query, body) {
return requestText(auth, method, SYMBOLS_URL + path, query, body);
}
function loginStep1(options) {
return requestStep(null, 'GET', API_URL + 'generate-authcode', options, null);
}
function loginStep2(options) {
return requestStep(null, 'POST', API_URL + 'validate-authcode', null, options);
}
function getProfile(auth) {
return requestApi(auth, 'GET', 'profile', null, null);
}
function getFunds(auth) {
return requestApi(auth, 'GET', 'funds', null, null);
}
function getHoldings(auth) {
return requestApi(auth, 'GET', 'holdings', null, null);
}
function getOrder(auth, options) {
return requestApi(auth, 'GET', 'orders', options, null);
}
function getOrders(auth) {
return requestApi(auth, 'GET', 'orders', null, null);
}
function getPositions(auth) {
return requestApi(auth, 'GET', 'positions', null, null);
}
function getTrades(auth) {
return requestApi(auth, 'GET', 'tradebook', null, null);
}
function placeOrder(auth, options) {
return requestApi(auth, 'POST', 'orders', null, options);
}
function placeOrders(auth, options) {
return requestApi(auth, 'POST', 'orders-multi', null, options);
}
function modifyOrder(auth, options) {
return requestApi(auth, 'PUT', 'orders', null, options);
}
function modifyOrders(auth, options) {
return requestApi(auth, 'PUT', 'orders-multi', null, options);
}
function cancelOrder(auth, options) {
return requestApi(auth, 'DELETE', 'orders', null, options);
}
function cancelOrders(auth, options) {
return requestApi(auth, 'DELETE', 'orders-multi', null, options);
}
function exitPosition(auth, options) {
return requestApi(auth, 'DELETE', 'positions', null, options);
}
function exitAllPositions(auth) {
return requestApi(auth, 'DELETE', 'positions', null, {});
}
function convertPosition(auth, options) {
return requestApi(auth, 'PUT', 'positions', null, options);
}
function getMarketStatus(auth) {
return requestApi(auth, 'GET', 'market-status', null, null);
}
function getMarketHistory(auth, options) {
return requestData(auth, 'GET', 'history/', options, null);
}
function getMarketQuotes(auth, options) {
return requestData(auth, 'GET', 'quotes/', options, null);
}
function getMarketDepth(auth, options) {
return requestData(auth, 'GET', 'depth/', options, null);
}
function getSymbolMaster(auth, options) {
var { exchange, segment } = options;
return requestSymbols(null, 'GET', exchange + '_' + segment + '.csv', null, null);
}
function generateEdisTpin(auth) {
return requestApi(auth, 'GET', 'tpin', null, null);
}
function getEdisTransactions(auth) {
return requestApi(auth, 'GET', 'details', null, null);
}
function submitEdisHoldingsStep(auth, options) {
return requestStep(auth, 'POST', 'index', null, options);
}
function inquireEdisTransaction(auth, options) {
return requestApi(auth, 'POST', 'inquiry', null, options);
}
exports.API_URL = API_URL;
exports.DATA_URL = DATA_URL;
exports.SYMBOLS_URL = SYMBOLS_URL;
exports.cancelOrder = cancelOrder;
exports.cancelOrders = cancelOrders;
exports.convertPosition = convertPosition;
exports.exitAllPositions = exitAllPositions;
exports.exitPosition = exitPosition;
exports.generateEdisTpin = generateEdisTpin;
exports.getEdisTransactions = getEdisTransactions;
exports.getFunds = getFunds;
exports.getHoldings = getHoldings;
exports.getMarketDepth = getMarketDepth;
exports.getMarketHistory = getMarketHistory;
exports.getMarketQuotes = getMarketQuotes;
exports.getMarketStatus = getMarketStatus;
exports.getOrder = getOrder;
exports.getOrders = getOrders;
exports.getPositions = getPositions;
exports.getProfile = getProfile;
exports.getSymbolMaster = getSymbolMaster;
exports.getTrades = getTrades;
exports.inquireEdisTransaction = inquireEdisTransaction;
exports.loginStep1 = loginStep1;
exports.loginStep2 = loginStep2;
exports.modifyOrder = modifyOrder;
exports.modifyOrders = modifyOrders;
exports.placeOrder = placeOrder;
exports.placeOrders = placeOrders;
exports.submitEdisHoldingsStep = submitEdisHoldingsStep;