1.0.4 • Published 11 months ago

@sandeshwar/market-pulse-backend v1.0.4

Weekly downloads
-
License
AGPL-3.0-only
Repository
-
Last release
11 months ago

Market Pulse Library

A JavaScript library for fetching and managing market data with built-in caching and error handling.

Prerequisites

  • Node.js >= 14.0.0
  • Chrome Extension Environment
  • A modern web browser (Chrome, Edge)

Installation

You can install the package using npm:

npm install @sandeshwar/market-pulse-backend

Or add it to your package.json:

{
  "dependencies": {
    "@sandeshwar/market-pulse-backend": "^1.0.2"
  }
}

For local development, you can install from a local path:

npm install /path/to/market-pulse-v2/backend

Chrome Extension Setup

  1. Add the following permissions to your manifest.json:
{
  "permissions": [
    "storage"
  ],
  "host_permissions": [
    "https://finance.yahoo.com/*",
    "https://newsapi.org/*"
  ]
}
  1. Initialize the library in your extension:
// background.js or content script
import { getQuote, getWatchlist, addToWatchlist } from '@sandeshwar/market-pulse-backend';

// Example usage
const watchlist = await getWatchlist();
const appleQuote = await getQuote('AAPL');
await addToWatchlist('MSFT');

Features

  • Real-time market data with quote information
  • Historical price data
  • Stock symbol search
  • Watchlist management
  • News feed integration
  • Built-in caching mechanism (5-minute default)
  • Error handling with retry logic
  • Chrome storage integration

Usage

Basic Usage

import { getQuote, searchSymbols, addToWatchlist, getWatchlist } from '@sandeshwar/market-pulse-backend';

// Fetch real-time quote
const quote = await getQuote('RELIANCE.NS');
console.log(quote);

// Search for symbols
const results = await searchSymbols('RELI');
console.log(results);

// Manage watchlist
await addToWatchlist('RELIANCE.NS');
const watchlist = await getWatchlist();
console.log(watchlist);

API Reference

Market Data

getQuote(symbol: string): Promise<QuoteData>

Fetches real-time quote data for a given symbol.

  • Returns detailed quote information
  • Implements caching and retry mechanism
  • Validates symbol format

getHistoricalData(symbol: string, options: HistoricalDataOptions): Promise<HistoricalData[]>

Fetches historical price data for a given symbol.

  • Configurable time range and interval
  • Returns OHLCV data points

searchSymbols(query: string): Promise<SearchResult[]>

Searches for symbols based on company name or symbol.

  • Returns matching symbols with company information
  • Supports partial matches

Watchlist Management

Stock Symbol Format

  • Must be a valid NSE/BSE symbol (e.g., 'RELIANCE.NS', 'TCS.BO')
  • Maximum length: 20 characters
  • Case sensitive
  • Must include exchange suffix (.NS for NSE, .BO for BSE)

getWatchlist(): Promise<string[]>

Returns the current watchlist of stock symbols.

addToWatchlist(symbol: string): Promise<string[]>

Adds a stock symbol to the watchlist.

  • Validates symbol format
  • Returns updated watchlist
  • Throws error for invalid symbols

removeFromWatchlist(symbol: string): Promise<string[]>

Removes a stock symbol from the watchlist.

  • Validates symbol format
  • Returns updated watchlist
  • Throws error for invalid symbols

clearWatchlist(): Promise<string[]>

Clears the entire watchlist.

  • Returns empty array after clearing

Storage API

getStorageData(key: string): Promise<any>

Retrieves data from Chrome storage.

  • Returns null if key doesn't exist
  • Throws if storage API is not available

setStorageData(key: string, value: any): Promise<void>

Saves data to Chrome storage.

  • Handles Chrome runtime errors
  • Throws if storage quota is exceeded

removeStorageData(key: string): Promise<void>

Removes data from Chrome storage.

  • Handles Chrome runtime errors
  • No error if key doesn't exist

API Usage

Here are some examples of how to use the main functions provided by the library:

Fetching Market Data

import { getQuote, getHistoricalData, searchSymbols } from '@sandeshwar/market-pulse-backend';

// Fetch a quote for a specific symbol
const quote = await getQuote('AAPL');

// Fetch historical data for a symbol
const historicalData = await getHistoricalData('AAPL', '2023-01-01', '2023-12-31');

// Search for symbols
const symbols = await searchSymbols('Apple');

Managing Watchlist

import { getWatchlist, addToWatchlist, removeFromWatchlist } from '@sandeshwar/market-pulse-backend';

// Get current watchlist
const watchlist = await getWatchlist();

// Add a stock to the watchlist
await addToWatchlist('AAPL');

// Remove a stock from the watchlist
await removeFromWatchlist('AAPL');

Error Handling

The library includes robust error handling mechanisms, including retry logic and fallback to cached data when fetching market indices.

Caching Mechanism

Market data is cached for a default duration of 5 minutes. This can be configured by setting the cacheDuration parameter when initializing the library.

Storage API

The library uses the Chrome storage API for local storage operations. All storage functions return promises and handle errors gracefully.

Testing

To run tests, use the following command:

npm test

Ensure you have all necessary testing tools installed.

Dependencies

The library relies on third-party APIs for fetching market data. Ensure you have access to these APIs when using the library.

TypeScript Support

Add the following to your tsconfig.json:

{
  "compilerOptions": {
    "types": ["chrome"]
  }
}

Development

# Install dependencies
npm install

# Run all tests
npm test

# Watch mode for development
npm run test:watch

# Build for production
npm run build

Browser Compatibility

  • Chrome: v88+
  • Firefox: v78+ (requires polyfill for storage API)
  • Edge: v88+

Troubleshooting

Common Issues

  1. "Storage API not found"

    • Ensure proper Chrome Extension permissions
    • Check manifest.json configuration
  2. "Invalid symbol format"

    • Verify stock symbol includes exchange suffix
    • Check for correct case sensitivity
  3. "Network error fetching market data"

    • Check internet connectivity
    • Verify if market is open
    • Library will use cached data if available
  4. "Storage quota exceeded"

    • Clear unnecessary stored data
    • Implement data cleanup strategy

Storage Limits

  • Chrome storage quota: 5MB per extension
  • Recommended watchlist size: <= 100 symbols
1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago