stock-bot/libs/utils/README.md

79 lines
3.7 KiB
Markdown

# General utilities
`@stock-bot/utils` is a private workspace package for infrastructure-neutral general
helpers. Financial/feature calculations belong to `@stock-bot/calculations`, not here.
## Supported capabilities
- `@stock-bot/utils/fetch`: `fetch` and the type-only `FetchOptions`.
- `@stock-bot/utils/user-agent`: `getRandomUserAgent` selects from a fixed local list.
- `@stock-bot/utils/common`: `createProxyUrl` encodes proxy credentials; `sleep`
returns a timer-backed promise.
- `@stock-bot/utils/dates`: `utcDates` supplies `isWeekday`, `nextWeekday`,
`previousWeekday`, strict `parseDate` (`YYYY-MM-DD`) and `formatDate`.
The root explicitly exports these six runtime values (`fetch`, `getRandomUserAgent`,
`createProxyUrl`, `sleep`, `utcDates`, `dateUtils`) plus the `FetchOptions` type.
Capability subpaths avoid loading unrelated helpers. There are no wildcard/deep exports.
```ts
import { utcDates } from '@stock-bot/utils/dates';
import { fetch, type FetchOptions } from '@stock-bot/utils/fetch';
import { getRandomUserAgent } from '@stock-bot/utils/user-agent';
const options: FetchOptions = {
headers: { 'User-Agent': getRandomUserAgent() },
timeout: 5_000,
};
const response = await fetch('https://example.com/data', options);
const next = utcDates.nextWeekday(utcDates.parseDate('2024-03-08'));
```
Fetch keeps its existing logging, Bun proxy, timeout and original-`Response` behavior.
Non-OK statuses are returned, not thrown. Timeout cancellation and caller cancellation
are not composed: a configured timeout currently replaces the supplied signal. The
user-agent list is not fingerprinting or a guarantee of provider access.
Date arithmetic is explicitly UTC, validates inputs and does not mutate the supplied
date. Weekdays are **not** exchange sessions: holidays, exchange time zones and trading
hours are not modeled. The deprecated `dateUtils` names (`isTradingDay`,
`getNextTradingDay`, `getPreviousTradingDay`, `formatDate`, `parseDate`) remain
as aliases for migration, not as verified trading-calendar APIs. This replaces the old
environment-dependent local weekday arithmetic; use `utcDates` in new code.
`@stock-bot/invariants` is the only runtime workspace dependency. `LoggerLike`
from `@stock-bot/logger` is type-only. No databases, configuration initialization,
application service containers or mandatory native bindings may be added here.
## Calculation migration and removals
The financial modules and OHLCV helpers were **relocated/preserved**, not discarded:
- Reviewed returns/analytics/window math and OHLCV processing:
`@stock-bot/calculations` and its explicit capability subpaths.
- All 105 formerly active calculation functions with their original bodies:
`@stock-bot/calculations/legacy`, an opt-in historical API, not reviewed core.
- All seven disabled numerical/indicator prototypes:
non-executable source-verified snapshots in `libs/calculations/research/`.
See [the calculation contract](../calculations/README.md) and
[the complete cleanup audit](../../docs/cleanup-audit.md) for readiness, preserved test
scenarios and deliberate removals. The empty `analyzePortfolio` placeholder remains
removed because it calculated nothing.
Import assertions directly from `@stock-bot/invariants`; the redundant utility assertion
facade was removed, not its functionality. Import domain types from `@stock-bot/types`.
No finance compatibility re-export is added to the general utility package.
## Verify
From the repository root:
```sh
bun run test:utils
bun run test:calculations
bun run check
```
The root gate includes general-utility and handler HTTP-consumer tests, calculation
regressions and architecture checks for the explicit APIs and dependency boundaries.