UNPKG

@cranberry-money/shared-services

Version:

Platform-agnostic API services with pure functions and dependency injection. Includes auth, portfolios, instruments, countries, sectors, and more.

87 lines 2.88 kB
import { INSTRUMENT_ENDPOINTS } from '@cranberry-money/shared-constants'; let configuredApiClient = null; export const configureInstruments = (apiClient) => { configuredApiClient = apiClient; }; const getConfiguredClient = () => { if (!configuredApiClient) { throw new Error('Instruments service not configured. Call configureInstruments(apiClient) before using instrument functions.'); } return configuredApiClient; }; export const getInstruments = (params) => { const transformedParams = { ...params }; if (params?.is_etf !== undefined) { transformedParams.is_etf = params.is_etf; } if (params?.is_fund !== undefined) { transformedParams.is_fund = params.is_fund; } const apiParams = { ...transformedParams, }; if (params?.min_price !== undefined) { apiParams.min_price = params.min_price.toString(); } if (params?.max_price !== undefined) { apiParams.max_price = params.max_price.toString(); } return getConfiguredClient().get(INSTRUMENT_ENDPOINTS.BASE, { params: apiParams, }); }; export const getInstrumentByUuid = (uuid) => { return getConfiguredClient().get(`${INSTRUMENT_ENDPOINTS.BASE}${uuid}/`); }; export const getInstrumentSnapshots = (instrumentUuid) => { return getConfiguredClient().get(`${INSTRUMENT_ENDPOINTS.BASE}${instrumentUuid}/snapshots/`); }; export const getInstrumentSnapshotsFiltered = (instrumentUuid, params) => { return getConfiguredClient().get(`${INSTRUMENT_ENDPOINTS.BASE}${instrumentUuid}/snapshots/`, { params, }); }; export const getBatchInstrumentSnapshots = async (requests) => { const promises = requests.map(async ({ instrumentUuid, ...params }) => { try { const response = await getInstrumentSnapshotsFiltered(instrumentUuid, params); return { instrumentUuid, data: response.data, }; } catch (error) { console.error(`Failed to fetch snapshots for instrument ${instrumentUuid}:`, error); return { instrumentUuid, error: error instanceof Error ? error.message : 'Failed to fetch data', }; } }); return Promise.all(promises); }; export const searchInstruments = (searchTerm, params) => { return getInstruments({ ...params, search: searchTerm, }); }; export const getInstrumentsByExchange = (exchangeUuid, params) => { return getInstruments({ ...params, exchange_uuid: exchangeUuid, }); }; export const getETFs = (params) => { return getInstruments({ ...params, is_etf: true, }); }; export const getFunds = (params) => { return getInstruments({ ...params, is_fund: true, }); }; //# sourceMappingURL=instruments.js.map