Skip to content

Commit

Permalink
fix: for lowercase symbols (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
asrafalih authored Jan 28, 2023
1 parent 79defac commit 2bc3f3e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('Class: NseIndia', () => {
expect(symbols.length).toBeGreaterThan(1000)
})
test('getEquityDetails', async () => {
const details = await nseIndia.getEquityDetails(symbol)
const details = await nseIndia.getEquityDetails(symbol.toLowerCase())
expect(getDataSchema(details,IS_TYPE_STRICT)).toMatchSnapshot(API_RESPONSE_VALIDATION)
expect(details.info.symbol).toBe(symbol)
})
Expand Down
24 changes: 14 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,25 @@ export class NseIndia {
* @returns
*/
getEquityDetails(symbol: string): Promise<EquityDetails> {
return this.getDataByEndpoint(`/api/quote-equity?symbol=${encodeURIComponent(symbol)}`)
return this.getDataByEndpoint(`/api/quote-equity?symbol=${encodeURIComponent(symbol.toUpperCase())}`)
}
/**
*
* @param symbol
* @returns
*/
getEquityTradeInfo(symbol: string): Promise<EquityTradeInfo> {
return this.getDataByEndpoint(`/api/quote-equity?symbol=${encodeURIComponent(symbol)}&section=trade_info`)
return this.getDataByEndpoint(`/api/quote-equity?symbol=${encodeURIComponent(symbol
.toUpperCase())}&section=trade_info`)
}
/**
*
* @param symbol
* @returns
*/
getEquityCorporateInfo(symbol: string): Promise<EquityCorporateInfo> {
return this.getDataByEndpoint(`/api/quote-equity?symbol=${encodeURIComponent(symbol)}&section=corp_info`)
return this.getDataByEndpoint(`/api/quote-equity?symbol=${encodeURIComponent(symbol
.toUpperCase())}&section=corp_info`)
}
/**
*
Expand All @@ -148,7 +150,7 @@ export class NseIndia {
* @returns
*/
async getEquityIntradayData(symbol: string, isPreOpenData = false): Promise<IntradayData> {
const details = await this.getEquityDetails(symbol)
const details = await this.getEquityDetails(symbol.toUpperCase())
const identifier = details.info.identifier
let url = `/api/chart-databyindex?index=${identifier}`
if (isPreOpenData)
Expand All @@ -162,14 +164,14 @@ export class NseIndia {
* @returns
*/
async getEquityHistoricalData(symbol: string, range?: DateRange): Promise<EquityHistoricalData[]> {
const data = await this.getEquityDetails(symbol)
const data = await this.getEquityDetails(symbol.toUpperCase())
const activeSeries = data.info.activeSeries.length ? data.info.activeSeries[0] : /* istanbul ignore next */ 'EQ'
if (!range) {
range = { start: new Date(data.metadata.listingDate), end: new Date() }
}
const dateRanges = getDateRangeChunks(range.start, range.end, 66)
const promises = dateRanges.map(async (dateRange) => {
const url = `/api/historical/cm/equity?symbol=${encodeURIComponent(symbol)}` +
const url = `/api/historical/cm/equity?symbol=${encodeURIComponent(symbol.toUpperCase())}` +
`&series=[%22${activeSeries}%22]&from=${dateRange.start}&to=${dateRange.end}`
return this.getDataByEndpoint(url)
})
Expand All @@ -181,15 +183,16 @@ export class NseIndia {
* @returns
*/
getEquitySeries(symbol: string): Promise<SeriesData> {
return this.getDataByEndpoint(`/api/historical/cm/equity/series?symbol=${encodeURIComponent(symbol)}`)
return this.getDataByEndpoint(`/api/historical/cm/equity/series?symbol=${encodeURIComponent(symbol
.toUpperCase())}`)
}
/**
*
* @param index
* @returns
*/
getEquityStockIndices(index: string): Promise<IndexDetails> {
return this.getDataByEndpoint(`/api/equity-stockIndices?index=${encodeURIComponent(index)}`)
return this.getDataByEndpoint(`/api/equity-stockIndices?index=${encodeURIComponent(index.toUpperCase())}`)
}
/**
*
Expand All @@ -198,7 +201,7 @@ export class NseIndia {
* @returns
*/
getIndexIntradayData(index: string, isPreOpenData = false): Promise<IntradayData> {
let endpoint = `/api/chart-databyindex?index=${index}&indices=true`
let endpoint = `/api/chart-databyindex?index=${index.toUpperCase()}&indices=true`
if (isPreOpenData)
endpoint += '&preopen=true'
return this.getDataByEndpoint(endpoint)
Expand All @@ -213,7 +216,8 @@ export class NseIndia {
const dateRanges = getDateRangeChunks(range.start, range.end, 360)
const promises = dateRanges.map(async (dateRange) => {
const endpoint = '/products/dynaContent/equities/indices/historicalindices.jsp' +
`?indexType=${encodeURIComponent(index)}&fromDate=${dateRange.start}&toDate=${dateRange.end}`
`?indexType=${encodeURIComponent(index
.toUpperCase())}&fromDate=${dateRange.start}&toDate=${dateRange.end}`
const html: string = await this.getDataByEndpoint(endpoint, true)
const $ = cheerio.load(html)
const historical: any[] = []
Expand Down

0 comments on commit 2bc3f3e

Please sign in to comment.