|
| 1 | +# Agent primer — hds-lib-js |
| 2 | + |
| 3 | +This file orients agents (Claude or others) working on `hds-lib-js`. It deliberately **does not duplicate** information that already lives in this repo — it links you to the right document for each concern. Read this first; then read the linked docs as you start a task. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Read first |
| 8 | + |
| 9 | +Before touching code, read these in order: |
| 10 | + |
| 11 | +1. **[README.md](README.md)** — what the package is, public surface, dev commands. Also lists the badge URLs and the public docs site. |
| 12 | +2. **[docs/index.md](docs/index.md)** plus the published site at **<https://healthdatasafe.github.io/hds-lib-js/>** — full user-facing API documentation. The pages under `docs/` are the source for that site. |
| 13 | +3. **[docs/getting-started.md](docs/getting-started.md)** — install + bootstrap (`settings.setServiceInfoURL`, `initHDSModel`). |
| 14 | +4. **[docs/hds-model.md](docs/hds-model.md)** — `HDSModel`, `itemsDefs`, `streams`, `eventTypes`, `datasources`, `authorizations`, `conversions`, `converters`, `appStreams`, `Overload`. |
| 15 | +5. **[docs/app-templates.md](docs/app-templates.md)** + **[AppTemplates.md](AppTemplates.md)** — `AppManagingAccount` / `AppClientAccount` / `Collector` / `CollectorClient` / `CollectorRequest` / `Contact`. The legal/consent-flow shape used by every HDS app. |
| 16 | +6. **[docs/settings.md](docs/settings.md)** — global library settings (`settings.setServiceInfoURL`, init order). Distinct from per-user `HDSSettings`. |
| 17 | +7. **[docs/localization.md](docs/localization.md)** — `localizableText` shape and `localizeText(...)` resolution rules. |
| 18 | +8. **[docs/toolkit.md](docs/toolkit.md)** — `StreamsAutoCreate`, reminders, duration parsing, event formatters. |
| 19 | +9. **[docs/utilities.md](docs/utilities.md)** — `HDSLibError`, misc helpers. |
| 20 | +10. **[CHANGELOG.md](CHANGELOG.md)** — every behaviour change worth knowing about, in reverse chronological order. Read the most recent entries before estimating an API. |
| 21 | + |
| 22 | +Anything not covered above is either internal or new; in that case, read the source under [`ts/`](ts/) and add docs as you go (do not let new public surface escape `docs/`). |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## Pryv concepts you must hold in your head |
| 27 | + |
| 28 | +`hds-lib-js` wraps and extends [`pryv` JS lib](https://github.com/pryv/lib-js). Most of what this library does is express HDS-specific patterns on top of standard Pryv objects. **Before designing anything that touches Pryv data shapes, read the corresponding section of the Pryv API reference.** |
| 29 | + |
| 30 | +Authoritative sources: |
| 31 | + |
| 32 | +- **Data in Pryv** (read this first if you've never worked with Pryv): <https://pryv.github.io/data-in-pryv/> — narrated walk-through of streams, events, accesses, and how they fit together. The clearest mental model document Pryv publishes. |
| 33 | +- Pryv concepts overview: <https://pryv.github.io/concepts/> |
| 34 | +- Pryv API reference: <https://pryv.github.io/reference/> (canonical home; `api.pryv.com/reference/` redirects here). |
| 35 | +- Pryv event-types catalogue: <https://pryv.github.io/event-types/> |
| 36 | +- Pryv guides — pick the relevant one, e.g. [data-modelling](https://pryv.github.io/guides/data-modelling/), [app-guidelines](https://pryv.github.io/guides/app-guidelines/), [webhooks](https://pryv.github.io/guides/webhooks/), [custom-auth](https://pryv.github.io/guides/custom-auth/), [consent](https://pryv.github.io/guides/consent/), [audit-logs](https://pryv.github.io/guides/audit-logs/). |
| 37 | + |
| 38 | +Key concepts you will encounter constantly here: |
| 39 | + |
| 40 | +| Pryv concept | Why it matters in hds-lib | Reference | |
| 41 | +|---|---|---| |
| 42 | +| **Service info** | Discovery endpoint published by every Pryv platform. `settings.setServiceInfoURL(url)` is the only mandatory bootstrap. The HDS data-model URL is published as an `assets.hds-model` override on top of this. | [Service info](https://pryv.github.io/reference/#service-info) | |
| 43 | +| **Streams** | Tree of clinical-domain containers. The HDS data-model defines the stream tree; `HDSModel.streams` exposes it. Permissions are stream-scoped and inherit down the tree. | [Stream](https://pryv.github.io/reference/#stream) · [methods](https://pryv.github.io/reference/#methods-streams) | |
| 44 | +| **Events** | The actual data points. Each event has `streamIds`, `type`, `content`, `time`, optional `duration`, `integrity`. HDS items map (`streamId` × `type`) → an `HDSItemDef`. | [Event](https://pryv.github.io/reference/#event) · [methods](https://pryv.github.io/reference/#methods-events) | |
| 45 | +| **Event types** | Schema + unit. HDS event types live in [`data-model/definitions/eventTypes/`](https://github.com/healthdatasafe/data-model/tree/main/definitions/eventTypes); Pryv's catalogue of standard types is at the link. `HDSModel.eventTypes.getEventTypeSymbol(type)` resolves the unit. | [Event-types catalogue](https://pryv.github.io/event-types/) | |
| 46 | +| **Accesses & permissions** | `personal`, `app`, `shared`. App auth produces an `app` access; `Collector` flows produce `shared` accesses. Levels: `read` / `contribute` / `manage`. Many HDS bugs come from confusing these. | [Access](https://pryv.github.io/reference/#access) · [methods](https://pryv.github.io/reference/#methods-accesses) | |
| 47 | +| **Authentication / app auth** | `app-web-auth3-hds` drives the OAuth-like flow. The HDS `Application` / `AppClientAccount` / `AppManagingAccount` classes wrap `pryv.Browser.AuthController`. | [Auth request](https://pryv.github.io/reference/#auth-request) · [authenticate-your-app](https://pryv.github.io/reference/#authenticate-your-app) | |
| 48 | +| **Methods (`events.get`, `events.create`, ...)** | The API verbs. `HDSConnection` wraps `pryv.Connection` and adds `apiOne(method, params)`. | [Methods](https://pryv.github.io/reference/#methods) · [method-ids](https://pryv.github.io/reference/#method-ids) | |
| 49 | +| **WebSockets / Socket.io / monitor** | Real-time event change feed. `MonitorScope` here is a thin wrapper around the Pryv socket layer. | [Call with WebSockets](https://pryv.github.io/reference/#basics-call-with-websockets) · [Subscribe to changes (WebSockets)](https://pryv.github.io/reference/#basics-subscribe-to-changes-with-websockets) | |
| 50 | +| **Integrity** | Per-event `integrity: "EVENT:0:sha256-..."` hash. Don't strip or recompute manually. | [Integrity](https://pryv.github.io/reference/#integrity) | |
| 51 | +| **HF events** | High-frequency event format (used by some bridges). Not common in HDS apps yet. | [HF events](https://pryv.github.io/reference/#hf-events) · [HF series](https://pryv.github.io/reference/#hf-series) | |
| 52 | + |
| 53 | +If a piece of code seems to be reinventing one of the concepts above, stop and check whether the Pryv lib already exposes it. The right answer is almost always "use the Pryv primitive directly." |
| 54 | + |
| 55 | +--- |
| 56 | + |
| 57 | +## Cross-repo relationships |
| 58 | + |
| 59 | +`hds-lib-js` sits at the centre of an ecosystem. Changes here ripple. Know the dependents before you change a public type. |
| 60 | + |
| 61 | +- **[data-model](https://github.com/healthdatasafe/data-model)** (`@hds/data-model`) — the YAML-defined items / streams / event types this library loads at runtime. The library never bundles the data-model; it fetches `pack.json` via service-info. Design rules (item naming, scale-hook placement, stream tree, etc.) are in [`data-model/AGENTS.md`](https://github.com/healthdatasafe/data-model/blob/main/AGENTS.md), and the YAML lives under [`data-model/definitions/`](https://github.com/healthdatasafe/data-model/tree/main/definitions). |
| 62 | +- **[hds-forms-js](https://github.com/healthdatasafe/hds-forms-js)** — React form renderers for `HDSItemDef`. Re-exports several types from `hds-lib` (e.g. `appTemplates.ItemLabels`, `appTemplates.ItemCustomization`). When you change those types, type-check `hds-forms-js` too. |
| 63 | +- **[app-web-auth3-hds](https://github.com/healthdatasafe/app-web-auth3-hds)** — auth flow used by every HDS app. Treat the public API surface as a contract. |
| 64 | +- **[lib-bridge-js](https://github.com/healthdatasafe/lib-bridge-js)** — base library for HDS server-side bridges; same compatibility concerns as any consumer. |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +## Conventions enforced in this repo |
| 69 | + |
| 70 | +These are short reminders. The detail lives in the linked source. |
| 71 | + |
| 72 | +### ESM, TS source as truth |
| 73 | + |
| 74 | +- `"type": "module"`, `"module": "nodenext"`. All imports use explicit `.ts` / `.js` extensions. |
| 75 | +- TypeScript source under [`ts/`](ts/) is the source of truth. Tests run **directly from source** (Node 24 type stripping); `npm test` does not need a build. |
| 76 | +- The package ships compiled output from [`js/`](js/) (built by `npm run prepare` → `tsc`). Consumers pulling via `git+https://...` hit the `prepare` hook on install. **Never publish without `js/` compiled.** |
| 77 | +- Webpack bundle in [`dist/`](dist/) (built by `npm run build`) is for the browser test runner only. Do not import from `dist/` in code. |
| 78 | +- `pryv` is consumed via [`ts/patchedPryv.ts`](ts/patchedPryv.ts) — declaration merging (const + namespace) so `pryv` is both value and type namespace. Do not import `pryv` directly anywhere else; always go through `patchedPryv.ts`. |
| 79 | + |
| 80 | +### Public surface |
| 81 | + |
| 82 | +Every value/type intended for external consumption is exported from [`ts/index.ts`](ts/index.ts). If a consumer needs something that is not there, **add the export** and document it under `docs/` — do not let consumers reach into deep paths. |
| 83 | + |
| 84 | +### Tests |
| 85 | + |
| 86 | +- [`tests/`](tests/) — mocha + node `assert`. One file per concept (`apptemplates.test.js`, `eventToShortText.test.js`, `itemLabels.test.js`, ...). Most tests load `pack.json` from the deployed `model.datasafe.dev` over HTTP, so the test runner needs network access. |
| 87 | +- [`tests-browser/`](tests-browser/) — same suites compiled for the browser test runner. Run via `backloop.dev`. |
| 88 | +- New tests go next to similar existing ones. New behaviour without a test is not done. |
| 89 | + |
| 90 | +### Versioning |
| 91 | + |
| 92 | +- Bump `version` in `package.json` for **every** change that consumers can observe. |
| 93 | +- Add a `## [x.y.z] - YYYY-MM-DD` block in [CHANGELOG.md](CHANGELOG.md). Group changes under `Added` / `Changed` / `Removed`. Keep the wording concrete (one bullet = one observable change). |
| 94 | +- Consumers pin to git URLs, not npm versions, but the version + changelog is still how humans read the diff. |
| 95 | + |
| 96 | +### Gotchas |
| 97 | + |
| 98 | +- **`tsc` stale output**: `tsc` may skip re-emitting a `.js` file if it already exists and didn't notice an internal change. After `npm run prepare` / `npm run build:ts`, **verify the relevant file in `js/` actually contains your change** before committing. If not, delete the stale `js/<path>.js` and re-run. |
| 99 | +- **Vite cache in consumer apps**: when you change a public type or export and a consumer app (Vite-based) doesn't pick it up, clear `<consumer>/node_modules/.vite` and restart the dev server. |
| 100 | +- **npm-link traps**: when an HDS app is npm-linked to `hds-lib`, both the app and any nested package (e.g. `hds-forms-js`'s [`src-test-app/`](https://github.com/healthdatasafe/hds-forms-js/tree/main/src-test-app)) must link the **same** `hds-lib` to avoid duplicate singletons (settings, model). If you see "two HDSModels" symptoms (e.g. `getHDSModel()` returning empty), this is the cause. |
| 101 | +- **Initialization order**: in apps that call `initBoiler(name, configDir)` (server side) or `pryv.Browser.AuthController` (client side), do so **before** any `getHDSModel()` / `HDSSettings` lookup. |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +## When in doubt |
| 106 | + |
| 107 | +- For a Pryv question: read the Pryv API reference, do not invent. |
| 108 | +- For an HDS data-model question: read [`AGENTS.md`](https://github.com/healthdatasafe/data-model/blob/main/AGENTS.md) and the YAML under [`definitions/`](https://github.com/healthdatasafe/data-model/tree/main/definitions) in [healthdatasafe/data-model](https://github.com/healthdatasafe/data-model). |
| 109 | +- For a forms / UI question: read [`README.md`](https://github.com/healthdatasafe/hds-forms-js/blob/main/README.md) and the [`src/components/`](https://github.com/healthdatasafe/hds-forms-js/tree/main/src/components) tree in [healthdatasafe/hds-forms-js](https://github.com/healthdatasafe/hds-forms-js). |
| 110 | + |
| 111 | +If the answer to a question is not in any of these places, that is a documentation bug — open an issue (or PR an addition to `docs/`) rather than guessing. |
0 commit comments