UNPKG

nepse-api-helper

Version:

a wrapper to use nepse api easily since they set up weird restrictions

141 lines (140 loc) 6.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.calculateValidBodyId = calculateValidBodyId; exports.fetchMarketStatus = fetchMarketStatus; exports.fetchSecurityBriefs = fetchSecurityBriefs; exports.fetchSecurityDetail = fetchSecurityDetail; exports.fetchNepseIndex = fetchNepseIndex; const auth_1 = require("./auth"); const cache_1 = require("./cache"); const constants_1 = require("./constants"); const errors_1 = require("./errors"); const http_1 = require("./http"); function calculateValidBodyId(marketId) { const dummyData = [147, 117, 239, 143, 157, 312, 161, 612, 512, 804, 411, 527, 170, 511, 421, 667, 764, 621, 301, 106, 133, 793, 411, 511, 312, 423, 344, 346, 653, 758, 342, 222, 236, 811, 711, 611, 122, 447, 128, 199, 183, 135, 489, 703, 800, 745, 152, 863, 134, 211, 142, 564, 375, 793, 212, 153, 138, 153, 648, 611, 151, 649, 318, 143, 117, 756, 119, 141, 717, 113, 112, 146, 162, 660, 693, 261, 362, 354, 251, 641, 157, 178, 631, 192, 734, 445, 192, 883, 187, 122, 591, 731, 852, 384, 565, 596, 451, 772, 624, 691]; const currentDate = new Date(); const datePart = currentDate.getDate(); return dummyData[marketId] + marketId + 2 * datePart; } async function fetchMarketStatus(state) { try { const [newState, token] = await (0, auth_1.getAccessToken)(state); const response = await (0, http_1.fetchWithRetry)(`${constants_1.BASE_URL}/api/nots/nepse-data/market-open`, { headers: (0, http_1.createHeaders)(token), method: "GET", }); if (!response.ok) { throw (0, errors_1.createNepseError)(`Failed to get market status: ${response.status} ${response.statusText}`, 'MARKET_STATUS_ERROR'); } const marketStatus = await response.json(); return [newState, marketStatus]; } catch (error) { if (error.code) { throw error; } throw (0, errors_1.createNepseError)('Failed to get market status', 'MARKET_STATUS_ERROR', error); } } async function fetchSecurityBriefs(state) { // Check cache first const cachedBriefs = (0, cache_1.getCacheItem)(state.caches.securityBriefs, 'all'); if (cachedBriefs) { return [state, cachedBriefs]; } // Fetch fresh data try { const [newState, token] = await (0, auth_1.getAccessToken)(state); const response = await (0, http_1.fetchWithRetry)(`${constants_1.BASE_URL}/api/nots/security?nonDelisted=false`, { headers: (0, http_1.createHeaders)(token), method: "GET" }); if (!response.ok) { throw (0, errors_1.createNepseError)(`Failed to get security briefs: ${response.status} ${response.statusText}`, 'SECURITY_BRIEFS_ERROR'); } const securities = await response.json(); // Update cache in state const updatedState = { ...newState, caches: { ...newState.caches, securityBriefs: (0, cache_1.setCacheItem)(newState.caches.securityBriefs, 'all', securities, constants_1.SECURITY_BRIEF_TTL_MS) } }; return [updatedState, securities]; } catch (error) { if (error.code) { throw error; } throw (0, errors_1.createNepseError)('Failed to get security briefs', 'SECURITY_BRIEFS_ERROR', error); } } async function fetchSecurityDetail(state, symbol) { if (!symbol) { throw (0, errors_1.createNepseError)('Invalid security symbol', 'INVALID_SYMBOL'); } const normalizedSymbol = symbol.toUpperCase(); try { // Get securities list const [stateWithSecurities, securities] = await fetchSecurityBriefs(state); const security = securities.find(s => s.symbol.toUpperCase() === normalizedSymbol); if (!security) { throw (0, errors_1.createNepseError)(`Security not found: ${symbol}`, 'SECURITY_NOT_FOUND'); } // Get market status const [stateWithMarketStatus, marketStatus] = await fetchMarketStatus(stateWithSecurities); // Get access token const [newState, token] = await (0, auth_1.getAccessToken)(stateWithMarketStatus); const bodyId = calculateValidBodyId(marketStatus?.id ?? 0); const response = await (0, http_1.fetchWithRetry)(`${constants_1.BASE_URL}/api/nots/security/${security.id}`, { method: "POST", headers: (0, http_1.createHeaders)(token), body: JSON.stringify({ id: bodyId }) }); if (!response.ok) { throw (0, errors_1.createNepseError)(`Failed to get security detail: ${response.status} ${response.statusText}`, 'SECURITY_DETAIL_ERROR'); } const securityDetailResponse = await response.json(); const securityDetail = { id: securityDetailResponse.security.id, name: securityDetailResponse.security.securityName, symbol: securityDetailResponse.security.symbol, activeStatus: securityDetailResponse.security.activeStatus, businessDate: securityDetailResponse.securityDailyTradeDto.businessDate, closePrice: securityDetailResponse.securityDailyTradeDto.closePrice, lastTradePrice: securityDetailResponse.securityDailyTradeDto.lastTradedPrice, fiftyTwoWeekHigh: securityDetailResponse.securityDailyTradeDto.fiftyTwoWeekHigh, fiftyTwoWeekLow: securityDetailResponse.securityDailyTradeDto.fiftyTwoWeekLow, listingDate: securityDetailResponse.security.listingDate }; return [newState, securityDetail]; } catch (error) { if (error.code) { throw error; } throw (0, errors_1.createNepseError)(`Failed to get security detail for ${symbol}`, 'SECURITY_DETAIL_ERROR', error); } } async function fetchNepseIndex(state) { try { // Get access token const [newState, token] = await (0, auth_1.getAccessToken)(state); const response = await (0, http_1.fetchWithRetry)(`${constants_1.BASE_URL}/api/nots/nepse-index`, { method: "GET", headers: (0, http_1.createHeaders)(token) }); if (!response.ok) { throw (0, errors_1.createNepseError)(`Failed to get index details: ${response.status} ${response.statusText}`, 'INDEX_DETAIL_ERROR'); } const indexDetails = await response.json(); return [newState, indexDetails]; } catch (error) { if (error.code) { throw error; } throw (0, errors_1.createNepseError)('Failed to get nepse index details', 'SECURITY_DETAIL_ERROR', error); } }