@crvouga/mockingbird-service-odx 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +5 -0
- package/README.md +145 -0
- package/dist/chunk-23TC3WDO.js +359 -0
- package/dist/chunk-23TC3WDO.js.map +7 -0
- package/dist/chunk-OYDT3HEE.js +3242 -0
- package/dist/chunk-OYDT3HEE.js.map +7 -0
- package/dist/cli.js +19 -0
- package/dist/cli.js.map +7 -0
- package/dist/index.d.ts +1015 -0
- package/dist/index.js +35 -0
- package/dist/index.js.map +7 -0
- package/dist/server.d.ts +1306 -0
- package/dist/server.js +12 -0
- package/dist/server.js.map +7 -0
- package/package.json +88 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,1015 @@
|
|
|
1
|
+
import { Hono } from 'hono';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Anything that can answer a Fetch `Request` with a `Response`.
|
|
5
|
+
*
|
|
6
|
+
* Every Mockingbird service implements this, and every runtime adapter consumes it.
|
|
7
|
+
* It is the only contract shared across the whole graph.
|
|
8
|
+
*/
|
|
9
|
+
interface FetchAPI$1 {
|
|
10
|
+
fetch(request: Request): Promise<Response>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The single source of time for a service.
|
|
15
|
+
*
|
|
16
|
+
* Every timestamp a mock writes reads from here, so a suite moves time instead of
|
|
17
|
+
* sleeping: appointment windows, result delays and expiries become reachable in
|
|
18
|
+
* milliseconds. A frozen clock also makes timestamps reproducible from a seed.
|
|
19
|
+
*/
|
|
20
|
+
type ClockState = {
|
|
21
|
+
/** Current epoch milliseconds. */
|
|
22
|
+
now: number;
|
|
23
|
+
/** True while time does not advance on its own. */
|
|
24
|
+
frozen: boolean;
|
|
25
|
+
/** Milliseconds this clock adds to its underlying source. */
|
|
26
|
+
offsetMs: number;
|
|
27
|
+
};
|
|
28
|
+
type Clock = {
|
|
29
|
+
now(): number;
|
|
30
|
+
/** Pin the clock to an exact instant, keeping it frozen if it already was. */
|
|
31
|
+
set(epochMs: number): void;
|
|
32
|
+
/** Move the clock forward, or back with a negative delta. */
|
|
33
|
+
advance(deltaMs: number): void;
|
|
34
|
+
/** Stop time at the current instant. */
|
|
35
|
+
freeze(): void;
|
|
36
|
+
/** Resume from the current instant. */
|
|
37
|
+
unfreeze(): void;
|
|
38
|
+
/** Drop back to the underlying source, live. */
|
|
39
|
+
reset(): void;
|
|
40
|
+
state(): ClockState;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/** Bind values accepted by Mockingbird's SQLite port (matches sqlite-mem / better-sqlite3). */
|
|
44
|
+
type SqliteValue$1 = null | number | bigint | string | Uint8Array | boolean;
|
|
45
|
+
/** Mutation counters returned by {@link SqliteStatement.run}. */
|
|
46
|
+
type SqliteRunResult$1 = {
|
|
47
|
+
changes: number;
|
|
48
|
+
lastInsertRowid: number | bigint;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Prepared statement bound to a {@link SqliteClient}.
|
|
52
|
+
*
|
|
53
|
+
* Pass bind values as rest arguments on each call (no sticky `bind()`).
|
|
54
|
+
*/
|
|
55
|
+
interface SqliteStatement$1 {
|
|
56
|
+
run(...params: SqliteValue$1[]): SqliteRunResult$1;
|
|
57
|
+
all<T = Record<string, unknown>>(...params: SqliteValue$1[]): T[];
|
|
58
|
+
get<T = Record<string, unknown>>(...params: SqliteValue$1[]): T | undefined;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Sync SQLite client port owned by Mockingbird.
|
|
62
|
+
*
|
|
63
|
+
* Duck-typed so `@crvouga/mockingbird-service-sqlite` `Database`, better-sqlite3, and wrapped
|
|
64
|
+
* `bun:sqlite` instances all work when they expose this surface.
|
|
65
|
+
*/
|
|
66
|
+
interface SqliteClient$1 {
|
|
67
|
+
exec(sql: string): void;
|
|
68
|
+
prepare(sql: string): SqliteStatement$1;
|
|
69
|
+
transaction<T>(fn: () => T): T;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Every stored record carries a monotonically increasing sequence for stable ordering. */
|
|
73
|
+
type Stored<T> = {
|
|
74
|
+
seq: number;
|
|
75
|
+
value: T;
|
|
76
|
+
};
|
|
77
|
+
type ListRecordsOptions<T> = {
|
|
78
|
+
/** Keep only records passing the predicate. */
|
|
79
|
+
where?: (value: T, seq: number) => boolean;
|
|
80
|
+
/** Sort order; default newest first. */
|
|
81
|
+
order?: "newest" | "oldest";
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* A SQLite-backed table of JSON records addressed by id. Ordering is by insertion
|
|
85
|
+
* sequence, never by id lexicographic order, so list semantics stay stable.
|
|
86
|
+
*/
|
|
87
|
+
declare class Collection<T> {
|
|
88
|
+
private readonly sqlite;
|
|
89
|
+
private readonly namespace;
|
|
90
|
+
private readonly name;
|
|
91
|
+
constructor(sqlite: SqliteClient$1, namespace: string, name: string);
|
|
92
|
+
private bumpCollectionSeq;
|
|
93
|
+
nextSequence(): number;
|
|
94
|
+
get(id: string): T | undefined;
|
|
95
|
+
has(id: string): boolean;
|
|
96
|
+
/** Insert a new record, assigning it the next sequence number. */
|
|
97
|
+
insert(id: string, value: T): Stored<T>;
|
|
98
|
+
/** Replace an existing record's value, keeping its position. */
|
|
99
|
+
update(id: string, value: T): Stored<T> | undefined;
|
|
100
|
+
delete(id: string): boolean;
|
|
101
|
+
/** How many records the collection holds, without reading them. */
|
|
102
|
+
count(): number;
|
|
103
|
+
list(options?: ListRecordsOptions<T>): Array<Stored<T> & {
|
|
104
|
+
id: string;
|
|
105
|
+
}>;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Seeded pseudo-random numbers, so anything a mock invents — ids, jitter, which
|
|
110
|
+
* request a percentage fault hits — is reproducible from a seed.
|
|
111
|
+
*
|
|
112
|
+
* mulberry32: small, fast, and stable across runtimes, which matters more here
|
|
113
|
+
* than statistical quality.
|
|
114
|
+
*/
|
|
115
|
+
type Rng = {
|
|
116
|
+
/** Next value in `[0, 1)`. */
|
|
117
|
+
next(): number;
|
|
118
|
+
/** Next integer in `[min, max]`. */
|
|
119
|
+
int(min: number, max: number): number;
|
|
120
|
+
/** Restart the stream from its seed. */
|
|
121
|
+
reset(): void;
|
|
122
|
+
/** Serializable engine state used by deterministic checkpoints. */
|
|
123
|
+
state(): number;
|
|
124
|
+
/** Restore a state previously returned by {@link state}. */
|
|
125
|
+
setState(state: number): void;
|
|
126
|
+
seed: number;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* A deliberate failure injected in front of an operation.
|
|
131
|
+
*
|
|
132
|
+
* This is how a suite reaches the vendor's failure modes without the vendor: the
|
|
133
|
+
* quota error that only appears when a shared sandbox is full, the 429 that only
|
|
134
|
+
* appears under load, the 5xx that proves a retry path works.
|
|
135
|
+
*/
|
|
136
|
+
type FaultRule = {
|
|
137
|
+
/** Stable id, so a suite can retire exactly the rule it added. */
|
|
138
|
+
id: string;
|
|
139
|
+
/** Fault only this operation. Omit to match every operation. */
|
|
140
|
+
operationId?: string;
|
|
141
|
+
/** Fault only this HTTP method, case-insensitive. Omit to match every method. */
|
|
142
|
+
method?: string;
|
|
143
|
+
/** Fault only paths starting with this prefix. Omit to match every path. */
|
|
144
|
+
pathPrefix?: string;
|
|
145
|
+
/**
|
|
146
|
+
* Fault only this namespace. Omit (or `"*"`) to fault every namespace — which is what
|
|
147
|
+
* an in-process caller usually wants, and what a parallel worker usually does not:
|
|
148
|
+
* rules added through `POST /__admin/faults` default to the calling namespace.
|
|
149
|
+
*/
|
|
150
|
+
namespace?: string;
|
|
151
|
+
/**
|
|
152
|
+
* Status of the injected response. Omit for a rule that only delays (`delayMs` /
|
|
153
|
+
* `latencyMs`), only drops the connection (`drop`), or only switches on an `effect`:
|
|
154
|
+
* the request then still reaches the service.
|
|
155
|
+
*/
|
|
156
|
+
status?: number;
|
|
157
|
+
/** Response body, serialized as JSON. A string is sent as-is. */
|
|
158
|
+
body?: unknown;
|
|
159
|
+
headers?: Record<string, string>;
|
|
160
|
+
/** Retire the rule after this many faults. Omit to keep it until removed. */
|
|
161
|
+
count?: number;
|
|
162
|
+
/** Fault this fraction of matching requests, `0`–`1`. Default `1`. */
|
|
163
|
+
rate?: number;
|
|
164
|
+
/** Hold the response back this long, to exercise timeouts. */
|
|
165
|
+
delayMs?: number;
|
|
166
|
+
/** Alias of `delayMs`. */
|
|
167
|
+
latencyMs?: number;
|
|
168
|
+
/**
|
|
169
|
+
* Drop the connection instead of answering: an in-process `fetch` rejects with a
|
|
170
|
+
* `TypeError`, and a served mock destroys the socket. Models "unknown outcome" failures.
|
|
171
|
+
*/
|
|
172
|
+
drop?: boolean;
|
|
173
|
+
/**
|
|
174
|
+
* A named service behaviour to switch on for the matching request instead of (or
|
|
175
|
+
* before) a canned response, e.g. `created_but_500` or `numeric_tracking_id`. Services
|
|
176
|
+
* read it with `faultEffects(request)`.
|
|
177
|
+
*/
|
|
178
|
+
effect?: string;
|
|
179
|
+
/** Parameters for `effect`. */
|
|
180
|
+
params?: Record<string, unknown>;
|
|
181
|
+
/** From the preset this rule was expanded from, if any. */
|
|
182
|
+
preset?: string;
|
|
183
|
+
};
|
|
184
|
+
/** A fault that fired for one request. */
|
|
185
|
+
type FaultHit = {
|
|
186
|
+
id: string;
|
|
187
|
+
/** The injected response; absent when the rule only delays, drops, or sets an effect. */
|
|
188
|
+
response?: Response;
|
|
189
|
+
drop?: boolean;
|
|
190
|
+
effect?: {
|
|
191
|
+
name: string;
|
|
192
|
+
params: Record<string, unknown>;
|
|
193
|
+
};
|
|
194
|
+
};
|
|
195
|
+
/**
|
|
196
|
+
* A named, documented fault a suite switches on by name
|
|
197
|
+
* (`POST /__admin/faults {"preset": "rate_limited"}`): one or more rules, and optionally a
|
|
198
|
+
* webhook delivery fault.
|
|
199
|
+
*/
|
|
200
|
+
type FaultPreset = {
|
|
201
|
+
description: string;
|
|
202
|
+
rules?: Omit<FaultRule, "id">[];
|
|
203
|
+
webhook?: {
|
|
204
|
+
mode: "duplicate" | "reorder" | "drop";
|
|
205
|
+
count?: number;
|
|
206
|
+
};
|
|
207
|
+
};
|
|
208
|
+
/** What a request looks like to the fault matcher. */
|
|
209
|
+
type FaultCandidate = {
|
|
210
|
+
operationId: string | undefined;
|
|
211
|
+
method: string;
|
|
212
|
+
path: string;
|
|
213
|
+
namespace: string;
|
|
214
|
+
};
|
|
215
|
+
type FaultRegistry = {
|
|
216
|
+
add(rule: FaultRule): FaultRule;
|
|
217
|
+
list(): (FaultRule & {
|
|
218
|
+
remaining: number | null;
|
|
219
|
+
hits: number;
|
|
220
|
+
})[];
|
|
221
|
+
remove(id: string): boolean;
|
|
222
|
+
clear(): void;
|
|
223
|
+
/**
|
|
224
|
+
* Every fault this request should get, in rule order, stopping at the first that answers
|
|
225
|
+
* or drops (effect-only and delay-only rules let later rules match too). Consumes one of
|
|
226
|
+
* each matching rule's remaining uses.
|
|
227
|
+
*/
|
|
228
|
+
take(candidate: FaultCandidate): Promise<FaultHit[]>;
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
/** One handled request, as the structured log sees it. */
|
|
232
|
+
type RequestLog = {
|
|
233
|
+
service: string;
|
|
234
|
+
namespace: string;
|
|
235
|
+
operationId: string | undefined;
|
|
236
|
+
method: string;
|
|
237
|
+
path: string;
|
|
238
|
+
status: number;
|
|
239
|
+
durationMs: number;
|
|
240
|
+
/** True when the path matched no operation in the contract. */
|
|
241
|
+
unmatched: boolean;
|
|
242
|
+
/** Set when a fault rule produced the response. */
|
|
243
|
+
faultId?: string;
|
|
244
|
+
/** Resource ids the handler touched (`userId`, `orderId`, …), when the service reports them. */
|
|
245
|
+
ids?: Record<string, string>;
|
|
246
|
+
/** Set when the service created a resource the request referred to but that did not exist. */
|
|
247
|
+
adopted?: boolean;
|
|
248
|
+
};
|
|
249
|
+
type MetricsReport = {
|
|
250
|
+
requests: number;
|
|
251
|
+
/** Counts keyed `<operationId> <status>`. */
|
|
252
|
+
byOperation: Record<string, number>;
|
|
253
|
+
/**
|
|
254
|
+
* Paths that matched no operation, most frequent first.
|
|
255
|
+
*
|
|
256
|
+
* This is the early-warning signal: a consumer calling something the mock does
|
|
257
|
+
* not implement shows up here as a count, before it fails a suite as a 404.
|
|
258
|
+
*/
|
|
259
|
+
unmatched: {
|
|
260
|
+
method: string;
|
|
261
|
+
path: string;
|
|
262
|
+
count: number;
|
|
263
|
+
}[];
|
|
264
|
+
faults: number;
|
|
265
|
+
totalDurationMs: number;
|
|
266
|
+
};
|
|
267
|
+
type Metrics = {
|
|
268
|
+
record(entry: RequestLog): void;
|
|
269
|
+
report(): MetricsReport;
|
|
270
|
+
reset(): void;
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
/** One journal entry: a request log stamped with when (on the mock clock) it was handled. */
|
|
274
|
+
type JournalEntry = RequestLog & {
|
|
275
|
+
at: string;
|
|
276
|
+
};
|
|
277
|
+
type JournalQuery = {
|
|
278
|
+
/** Only this namespace. Omit for every namespace, oldest first across all of them. */
|
|
279
|
+
namespace?: string;
|
|
280
|
+
operationId?: string;
|
|
281
|
+
status?: number;
|
|
282
|
+
/** Only entries at or after this instant (epoch ms). */
|
|
283
|
+
since?: number;
|
|
284
|
+
/** At most this many, the most recent kept. */
|
|
285
|
+
limit?: number;
|
|
286
|
+
};
|
|
287
|
+
type Journal = {
|
|
288
|
+
readonly size: number;
|
|
289
|
+
record(entry: JournalEntry): void;
|
|
290
|
+
list(query?: JournalQuery): JournalEntry[];
|
|
291
|
+
/** Forget one namespace's entries, or every namespace's. */
|
|
292
|
+
clear(namespace?: string): void;
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
/** Credential → namespace mapping behind `PUT /__admin/credentials`. */
|
|
296
|
+
type CredentialRegistry = {
|
|
297
|
+
set(credential: string, namespace: string): void;
|
|
298
|
+
get(credential: string): string | undefined;
|
|
299
|
+
remove(credential: string): boolean;
|
|
300
|
+
clear(): void;
|
|
301
|
+
entries(): {
|
|
302
|
+
credential: string;
|
|
303
|
+
namespace: string;
|
|
304
|
+
}[];
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
/** A stable identifier for a point in a {@link Timeline}. */
|
|
308
|
+
type CheckpointId = string;
|
|
309
|
+
/** An immutable node in a timeline's checkpoint DAG. */
|
|
310
|
+
type Checkpoint<T> = Readonly<{
|
|
311
|
+
id: CheckpointId;
|
|
312
|
+
branch: string;
|
|
313
|
+
parent: CheckpointId | null;
|
|
314
|
+
/** Logical time supplied by the timeline's injected clock. */
|
|
315
|
+
at: number;
|
|
316
|
+
value: T;
|
|
317
|
+
}>;
|
|
318
|
+
type TimelineOptions = {
|
|
319
|
+
/** Logical clock used to stamp checkpoints. Defaults to a deterministic counter. */
|
|
320
|
+
now?: () => number;
|
|
321
|
+
/** Maximum retained checkpoints. Branch heads are never collected. Default 1,000. */
|
|
322
|
+
maxCheckpoints?: number;
|
|
323
|
+
/** Customize deterministic checkpoint IDs. */
|
|
324
|
+
id?: (sequence: number) => CheckpointId;
|
|
325
|
+
};
|
|
326
|
+
type CommitOptions = {
|
|
327
|
+
branch?: string;
|
|
328
|
+
/** Parent checkpoint. Defaults to the selected branch's current head. */
|
|
329
|
+
parent?: CheckpointId | null;
|
|
330
|
+
};
|
|
331
|
+
type ForkOptions = {
|
|
332
|
+
/** Checkpoint to fork from. Defaults to the main branch's head. */
|
|
333
|
+
from?: CheckpointId;
|
|
334
|
+
};
|
|
335
|
+
/**
|
|
336
|
+
* Small, storage-agnostic checkpoint DAG shared by service runtimes. Its values may be immutable
|
|
337
|
+
* records, namespace images, or copy-on-write SQL engine snapshots.
|
|
338
|
+
*
|
|
339
|
+
* Values are retained by reference. Engines can therefore use persistent/COW snapshots while
|
|
340
|
+
* simpler services can use immutable values. IDs and GC order are deterministic, and all IO
|
|
341
|
+
* (the logical clock) is injected.
|
|
342
|
+
*/
|
|
343
|
+
declare class Timeline<T> {
|
|
344
|
+
readonly maxCheckpoints: number;
|
|
345
|
+
private readonly now;
|
|
346
|
+
private readonly makeId;
|
|
347
|
+
private readonly nodes;
|
|
348
|
+
private readonly heads;
|
|
349
|
+
/** Unreferenced nodes in the exact order they became collectible. */
|
|
350
|
+
private readonly evictable;
|
|
351
|
+
/** Branch heads plus explicit retainers. Absent means zero. */
|
|
352
|
+
private readonly references;
|
|
353
|
+
private readonly explicitPins;
|
|
354
|
+
private sequence;
|
|
355
|
+
constructor(options?: TimelineOptions);
|
|
356
|
+
/** Capture a new immutable value and move `branch` to it. */
|
|
357
|
+
commit(value: T, options?: CommitOptions): Checkpoint<T>;
|
|
358
|
+
/** Create a branch pointer without copying its checkpoint value. */
|
|
359
|
+
fork(branch: string, options?: ForkOptions): Checkpoint<T> | undefined;
|
|
360
|
+
/** Move a branch pointer to an existing checkpoint. */
|
|
361
|
+
checkout(branch: string, id: CheckpointId): Checkpoint<T>;
|
|
362
|
+
get(id: CheckpointId): Checkpoint<T>;
|
|
363
|
+
head(branch?: string): Checkpoint<T> | undefined;
|
|
364
|
+
hasBranch(branch: string): boolean;
|
|
365
|
+
branches(): Readonly<Record<string, CheckpointId>>;
|
|
366
|
+
checkpoints(): readonly Checkpoint<T>[];
|
|
367
|
+
/** Number of retained checkpoints without allocating an array. */
|
|
368
|
+
get size(): number;
|
|
369
|
+
/** Pin a checkpoint independently of branch heads (used by compatibility snapshot handles). */
|
|
370
|
+
retain(id: CheckpointId): Checkpoint<T>;
|
|
371
|
+
/** Release one explicit pin. Branch heads remain pinned until moved or deleted. */
|
|
372
|
+
release(id: CheckpointId): boolean;
|
|
373
|
+
deleteBranch(branch: string): boolean;
|
|
374
|
+
/**
|
|
375
|
+
* Deterministically discard oldest unpinned checkpoints. Collection is O(number removed):
|
|
376
|
+
* commits never scan pinned nodes or the retained history. Parents are metadata rather than a
|
|
377
|
+
* storage dependency, so a retained node remains usable after pruning.
|
|
378
|
+
*/
|
|
379
|
+
gc(max?: number): CheckpointId[];
|
|
380
|
+
private collect;
|
|
381
|
+
private moveHead;
|
|
382
|
+
private addReference;
|
|
383
|
+
private removeReference;
|
|
384
|
+
private assertBranch;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Anything that can answer a Fetch `Request` with a `Response`.
|
|
389
|
+
*
|
|
390
|
+
* Every Mockingbird service implements this, and every runtime adapter consumes it.
|
|
391
|
+
* It is the only contract shared across the whole graph.
|
|
392
|
+
*/
|
|
393
|
+
interface FetchAPI {
|
|
394
|
+
fetch(request: Request): Promise<Response>;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* A point-in-time copy of everything a service namespace holds.
|
|
399
|
+
*
|
|
400
|
+
* All service state lives in the two core tables keyed by namespace, so a snapshot
|
|
401
|
+
* is generic: any service gets per-test rollback without knowing its own schema.
|
|
402
|
+
* Restoring is much cheaper than rebuilding a namespace from a corpus.
|
|
403
|
+
*/
|
|
404
|
+
type NamespaceSnapshot = {
|
|
405
|
+
namespace: string;
|
|
406
|
+
records: {
|
|
407
|
+
collection: string;
|
|
408
|
+
id: string;
|
|
409
|
+
seq: number;
|
|
410
|
+
value: string;
|
|
411
|
+
}[];
|
|
412
|
+
sequences: {
|
|
413
|
+
name: string;
|
|
414
|
+
kind: string;
|
|
415
|
+
value: number;
|
|
416
|
+
}[];
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
type WebhookEndpoint = {
|
|
420
|
+
/** Stable id; generated when omitted. */
|
|
421
|
+
id?: string;
|
|
422
|
+
url: string;
|
|
423
|
+
secret?: string;
|
|
424
|
+
/** Event types to deliver; omit or include `"*"` for every type. */
|
|
425
|
+
events?: string[];
|
|
426
|
+
/** Deliver only messages whose tags include all of these (e.g. `{ account: "mso" }`). */
|
|
427
|
+
tags?: Record<string, string>;
|
|
428
|
+
/** The public URL the receiver verifies signatures against (Twilio), when it differs. */
|
|
429
|
+
signUrl?: string;
|
|
430
|
+
headers?: Record<string, string>;
|
|
431
|
+
};
|
|
432
|
+
type WebhookMessage = {
|
|
433
|
+
id: string;
|
|
434
|
+
namespace: string;
|
|
435
|
+
type: string;
|
|
436
|
+
body: string;
|
|
437
|
+
contentType: string;
|
|
438
|
+
tags: Record<string, string>;
|
|
439
|
+
headers?: Record<string, string>;
|
|
440
|
+
/** Wall-clock ISO-8601 time of publication. */
|
|
441
|
+
publishedAt: string;
|
|
442
|
+
};
|
|
443
|
+
type WebhookAttempt = {
|
|
444
|
+
attempt: number;
|
|
445
|
+
at: string;
|
|
446
|
+
status: number | null;
|
|
447
|
+
error: string | null;
|
|
448
|
+
durationMs: number;
|
|
449
|
+
/** Exact receiver response body, when one was returned. */
|
|
450
|
+
responseBody?: string | null;
|
|
451
|
+
};
|
|
452
|
+
type WebhookDelivery = {
|
|
453
|
+
id: string;
|
|
454
|
+
messageId: string;
|
|
455
|
+
namespace: string;
|
|
456
|
+
type: string;
|
|
457
|
+
endpointId: string;
|
|
458
|
+
url: string;
|
|
459
|
+
state: "pending" | "delivered" | "failed" | "dropped";
|
|
460
|
+
attempts: WebhookAttempt[];
|
|
461
|
+
};
|
|
462
|
+
/** A delivery-level fault: what happens to the next `count` messages in a namespace. */
|
|
463
|
+
type WebhookFault = {
|
|
464
|
+
mode: "duplicate" | "reorder" | "drop";
|
|
465
|
+
/** Messages affected; default 1. */
|
|
466
|
+
count?: number;
|
|
467
|
+
};
|
|
468
|
+
type PublishInput = {
|
|
469
|
+
namespace: string;
|
|
470
|
+
type: string;
|
|
471
|
+
/** Exact body; objects are JSON-encoded. */
|
|
472
|
+
body: string | Record<string, unknown> | unknown[];
|
|
473
|
+
/** Default `application/json`, or form-encoded when `form` is given. */
|
|
474
|
+
contentType?: string;
|
|
475
|
+
/** Form parameters, when the vendor posts `application/x-www-form-urlencoded`. */
|
|
476
|
+
form?: Record<string, string>;
|
|
477
|
+
tags?: Record<string, string>;
|
|
478
|
+
/** Message-specific delivery headers, captured as part of durable message state. */
|
|
479
|
+
headers?: Record<string, string>;
|
|
480
|
+
/** Message id; generated when omitted. */
|
|
481
|
+
id?: string;
|
|
482
|
+
};
|
|
483
|
+
type WebhookHub = {
|
|
484
|
+
publish(input: PublishInput): WebhookMessage;
|
|
485
|
+
/** Replace a namespace's own endpoints (`PUT /__admin/webhook-endpoints`). */
|
|
486
|
+
setEndpoints(namespace: string, endpoints: WebhookEndpoint[]): WebhookEndpoint[];
|
|
487
|
+
/** The endpoints a namespace delivers to: its own, plus the global ones. */
|
|
488
|
+
endpoints(namespace: string): WebhookEndpoint[];
|
|
489
|
+
messages(namespace?: string): WebhookMessage[];
|
|
490
|
+
deliveries(namespace?: string): WebhookDelivery[];
|
|
491
|
+
replay(deliveryId: string): Promise<WebhookDelivery | undefined>;
|
|
492
|
+
/** Run every pending retry (and release held reordered messages) now. */
|
|
493
|
+
flush(): Promise<void>;
|
|
494
|
+
/** Resolve once nothing is in flight. */
|
|
495
|
+
idle(): Promise<void>;
|
|
496
|
+
fault(namespace: string, fault: WebhookFault): void;
|
|
497
|
+
clear(namespace?: string): void;
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
/** What the runtime needs from a service: a Fetch handler it can reset. */
|
|
501
|
+
type ServiceInstance = FetchAPI & {
|
|
502
|
+
reset(): Promise<void>;
|
|
503
|
+
};
|
|
504
|
+
type ServiceTimelineState = Readonly<{
|
|
505
|
+
snapshot: NamespaceSnapshot;
|
|
506
|
+
clock: Readonly<ReturnType<Clock["state"]>>;
|
|
507
|
+
rngState: number;
|
|
508
|
+
}>;
|
|
509
|
+
type ServiceCheckpoint = Checkpoint<ServiceTimelineState>;
|
|
510
|
+
type ServiceRuntime<T extends ServiceInstance> = FetchAPI & {
|
|
511
|
+
readonly name: string;
|
|
512
|
+
readonly sqlite: SqliteClient$1;
|
|
513
|
+
readonly clock: Clock;
|
|
514
|
+
readonly faults: FaultRegistry;
|
|
515
|
+
readonly metrics: Metrics;
|
|
516
|
+
readonly journal: Journal;
|
|
517
|
+
readonly rng: Rng;
|
|
518
|
+
readonly credentials: CredentialRegistry;
|
|
519
|
+
/** The webhook hub, when the service has outbound webhooks. */
|
|
520
|
+
readonly webhooks: WebhookHub | undefined;
|
|
521
|
+
/** Expand a named preset into fault rules (and webhook faults) for `namespace`. */
|
|
522
|
+
applyPreset(name: string, namespace?: string, overrides?: Partial<FaultRule>): FaultRule[];
|
|
523
|
+
/** The instance behind `namespace` (the default one when omitted), created on first use. */
|
|
524
|
+
instance(namespace?: string): T;
|
|
525
|
+
/** Public names of every namespace created so far. */
|
|
526
|
+
namespaces(): string[];
|
|
527
|
+
/** Reset one namespace, or every namespace with `"*"`. */
|
|
528
|
+
reset(namespace?: string): Promise<void>;
|
|
529
|
+
snapshot(namespace?: string): NamespaceSnapshot;
|
|
530
|
+
restore(snapshot: NamespaceSnapshot, namespace?: string): void;
|
|
531
|
+
/** Capture the current branch. Mutating HTTP calls do this automatically. */
|
|
532
|
+
checkpoint(namespace?: string, branch?: string): ServiceCheckpoint;
|
|
533
|
+
/** Create an isolated branch, optionally from a historical checkpoint. */
|
|
534
|
+
branch(name: string, options?: {
|
|
535
|
+
namespace?: string;
|
|
536
|
+
at?: string;
|
|
537
|
+
}): ServiceCheckpoint;
|
|
538
|
+
/** Restore a branch, clock, and PRNG to a checkpoint. */
|
|
539
|
+
checkout(checkpoint: string, options?: {
|
|
540
|
+
namespace?: string;
|
|
541
|
+
branch?: string;
|
|
542
|
+
}): void;
|
|
543
|
+
/** Inspect the retained history for a namespace. */
|
|
544
|
+
timeline(namespace?: string): Timeline<ServiceTimelineState>;
|
|
545
|
+
};
|
|
546
|
+
|
|
547
|
+
/** Options every provider constructor accepts. */
|
|
548
|
+
type APIOptions = {
|
|
549
|
+
/** Sync SQLite client. Defaults to `@crvouga/mockingbird-service-sqlite`. */
|
|
550
|
+
sqlite?: SqliteClient$1;
|
|
551
|
+
/** Clock used for `created`-style fields. Default `Date.now`. */
|
|
552
|
+
now?: () => number;
|
|
553
|
+
/**
|
|
554
|
+
* Storage namespace for this instance's records. Instances sharing one SQLite
|
|
555
|
+
* client stay isolated when their namespaces differ. Defaults to the service name.
|
|
556
|
+
*/
|
|
557
|
+
namespace?: string;
|
|
558
|
+
};
|
|
559
|
+
|
|
560
|
+
/** Bind values accepted by Mockingbird's SQLite port (matches sqlite-mem / better-sqlite3). */
|
|
561
|
+
type SqliteValue = null | number | bigint | string | Uint8Array | boolean;
|
|
562
|
+
/** Mutation counters returned by {@link SqliteStatement.run}. */
|
|
563
|
+
type SqliteRunResult = {
|
|
564
|
+
changes: number;
|
|
565
|
+
lastInsertRowid: number | bigint;
|
|
566
|
+
};
|
|
567
|
+
/**
|
|
568
|
+
* Prepared statement bound to a {@link SqliteClient}.
|
|
569
|
+
*
|
|
570
|
+
* Pass bind values as rest arguments on each call (no sticky `bind()`).
|
|
571
|
+
*/
|
|
572
|
+
interface SqliteStatement {
|
|
573
|
+
run(...params: SqliteValue[]): SqliteRunResult;
|
|
574
|
+
all<T = Record<string, unknown>>(...params: SqliteValue[]): T[];
|
|
575
|
+
get<T = Record<string, unknown>>(...params: SqliteValue[]): T | undefined;
|
|
576
|
+
}
|
|
577
|
+
/**
|
|
578
|
+
* Sync SQLite client port owned by Mockingbird.
|
|
579
|
+
*
|
|
580
|
+
* Duck-typed so `@crvouga/mockingbird-service-sqlite` `Database`, better-sqlite3, and wrapped
|
|
581
|
+
* `bun:sqlite` instances all work when they expose this surface.
|
|
582
|
+
*/
|
|
583
|
+
interface SqliteClient {
|
|
584
|
+
exec(sql: string): void;
|
|
585
|
+
prepare(sql: string): SqliteStatement;
|
|
586
|
+
transaction<T>(fn: () => T): T;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* The partner labs and biomarker elements ODX answers from `/v1/partner/labs` and
|
|
591
|
+
* `/v1/elements/{labId}`. Element ids are the ones our QA harness and backend rely on
|
|
592
|
+
* (`packages/qa/src/world/http/odx-client.ts` PHENO_AGE_*: the nine phenotypic-age inputs our
|
|
593
|
+
* biological-age service requires by name), mapped from the lab's LOINC codes so an HL7 OBX-3
|
|
594
|
+
* of `1751-7^Albumin` imports as element 506. No live recording exists (the vendor is retired);
|
|
595
|
+
* names, units and ranges follow ODX's conventional-US defaults.
|
|
596
|
+
*/
|
|
597
|
+
type OdxLab = {
|
|
598
|
+
labId: number;
|
|
599
|
+
name: string;
|
|
600
|
+
isCurrentLab: boolean;
|
|
601
|
+
};
|
|
602
|
+
type ElementDef = {
|
|
603
|
+
elementId: number;
|
|
604
|
+
elementName: string;
|
|
605
|
+
elementGenderType: "Both" | "Male" | "Female";
|
|
606
|
+
cuUnit: string;
|
|
607
|
+
siUnit: string;
|
|
608
|
+
cuToSiConversionFactor: number;
|
|
609
|
+
/** LOINC / lab codes an HL7 OBX-3 may carry for this element. */
|
|
610
|
+
codes: string[];
|
|
611
|
+
optimal: [number, number];
|
|
612
|
+
standard: [number, number];
|
|
613
|
+
};
|
|
614
|
+
declare const LABS: readonly OdxLab[];
|
|
615
|
+
declare const ELEMENTS: readonly ElementDef[];
|
|
616
|
+
|
|
617
|
+
/** One result as ODX returns it (`OdxElement` in our consumer). */
|
|
618
|
+
type ResultElement = {
|
|
619
|
+
elementValue: number;
|
|
620
|
+
/** `""` for an exact value, `<` / `>` (or `<=` / `>=`) for a bound. Never null. */
|
|
621
|
+
comparison: string;
|
|
622
|
+
unit: string;
|
|
623
|
+
elementId: number;
|
|
624
|
+
elementName: string;
|
|
625
|
+
optimalRangeLow: number;
|
|
626
|
+
optimalRangeHigh: number;
|
|
627
|
+
standardRangeLow: number;
|
|
628
|
+
standardRangeHigh: number;
|
|
629
|
+
};
|
|
630
|
+
type ImportLog = {
|
|
631
|
+
observationIdentifier: string | null;
|
|
632
|
+
observationIdentifierText: string | null;
|
|
633
|
+
status: string | null;
|
|
634
|
+
};
|
|
635
|
+
/** One HL7 v2 OBX segment, reduced to what an import reads. PID/NTE are never kept. */
|
|
636
|
+
type Observation = {
|
|
637
|
+
code: string;
|
|
638
|
+
text: string;
|
|
639
|
+
value: string;
|
|
640
|
+
units: string;
|
|
641
|
+
range: string;
|
|
642
|
+
};
|
|
643
|
+
/**
|
|
644
|
+
* The OBX segments of an HL7 v2 message (segments split on CR or LF, fields on `|`,
|
|
645
|
+
* components on `^`): OBX-3 identifier `code^text`, OBX-5 value, OBX-6 units, OBX-7 range.
|
|
646
|
+
* Returns `undefined` when the text is not an HL7 message (no MSH header).
|
|
647
|
+
*/
|
|
648
|
+
declare const parseObservations: (hl7: string) => Observation[] | undefined;
|
|
649
|
+
/** Pick the element an observation maps to: by lab code, `EL<id>`, or name; gendered by the patient. */
|
|
650
|
+
declare const matchElement: (code: string, text: string, gender: string) => ElementDef | undefined;
|
|
651
|
+
|
|
652
|
+
/** A practice patient as `OdxPatient` (our consumer's type) plus the partner (our) user id. */
|
|
653
|
+
type PatientRecord = {
|
|
654
|
+
patientId: number;
|
|
655
|
+
practiceId: string;
|
|
656
|
+
createdDate: string;
|
|
657
|
+
lastUpdatedDate: string;
|
|
658
|
+
userTitle: string | null;
|
|
659
|
+
userFirstName: string | null;
|
|
660
|
+
userLastName: string | null;
|
|
661
|
+
firstName: string;
|
|
662
|
+
lastName: string;
|
|
663
|
+
nickname: string | null;
|
|
664
|
+
dateOfBirth: string | null;
|
|
665
|
+
gender: string;
|
|
666
|
+
homePhone: string | null;
|
|
667
|
+
workPhone: string | null;
|
|
668
|
+
mobile: string | null;
|
|
669
|
+
email: string;
|
|
670
|
+
address: string | null;
|
|
671
|
+
address2: string | null;
|
|
672
|
+
address3: string | null;
|
|
673
|
+
city: string | null;
|
|
674
|
+
province: string | null;
|
|
675
|
+
postalCode: string | null;
|
|
676
|
+
country: string | null;
|
|
677
|
+
userId: string | null;
|
|
678
|
+
workspaceId: number;
|
|
679
|
+
/** Set by `POST …/partner/{localUserId}`; admin-visible only. */
|
|
680
|
+
partnerUserId: string | null;
|
|
681
|
+
};
|
|
682
|
+
/** One imported lab test. The HL7 text itself is never stored (its PID segment is PHI). */
|
|
683
|
+
type PatientTestRecord = {
|
|
684
|
+
patientTestId: number;
|
|
685
|
+
patientId: number;
|
|
686
|
+
labProfileId: number;
|
|
687
|
+
testDate: string;
|
|
688
|
+
unitType: string;
|
|
689
|
+
createdDate: string;
|
|
690
|
+
lastUpdatedDate: string;
|
|
691
|
+
userId: string | null;
|
|
692
|
+
practiceId: string;
|
|
693
|
+
labId: number;
|
|
694
|
+
externalReference: string | null;
|
|
695
|
+
externalMessageControlId: string | null;
|
|
696
|
+
externalPatientTestId: string | null;
|
|
697
|
+
results: ResultElement[];
|
|
698
|
+
importLogs: ImportLog[] | null;
|
|
699
|
+
menstrualPhase: string;
|
|
700
|
+
isFasting: boolean;
|
|
701
|
+
};
|
|
702
|
+
type WebhookRecord = {
|
|
703
|
+
partnerWebhookId: number;
|
|
704
|
+
signingKey: string;
|
|
705
|
+
createDate: string;
|
|
706
|
+
entityEvents: {
|
|
707
|
+
PatientTest: string[];
|
|
708
|
+
};
|
|
709
|
+
webhookUrl: string;
|
|
710
|
+
};
|
|
711
|
+
/** Per-namespace knobs, set through `PUT /__admin/settings`; cleared on reset. */
|
|
712
|
+
type Settings = {
|
|
713
|
+
/** Accepted `ApiKey` values; empty accepts any non-empty key. */
|
|
714
|
+
apiKeys: string[];
|
|
715
|
+
/** A webhook registered in every namespace from the start (`--webhook-url`, `--signing-key`). */
|
|
716
|
+
presetWebhook: {
|
|
717
|
+
url: string;
|
|
718
|
+
signingKey: string;
|
|
719
|
+
} | null;
|
|
720
|
+
};
|
|
721
|
+
declare const DEFAULT_SETTINGS: Settings;
|
|
722
|
+
declare const ID_BASES: {
|
|
723
|
+
readonly patient: 100000;
|
|
724
|
+
readonly test: 700000;
|
|
725
|
+
readonly webhook: 0;
|
|
726
|
+
readonly message: 0;
|
|
727
|
+
};
|
|
728
|
+
declare class OdxState {
|
|
729
|
+
private readonly namespace;
|
|
730
|
+
private readonly seed;
|
|
731
|
+
readonly patients: Collection<PatientRecord>;
|
|
732
|
+
readonly tests: Collection<PatientTestRecord>;
|
|
733
|
+
readonly webhooks: Collection<WebhookRecord>;
|
|
734
|
+
readonly settings: Collection<Settings>;
|
|
735
|
+
private readonly counters;
|
|
736
|
+
constructor(sqlite: SqliteClient, namespace: string, seed: {
|
|
737
|
+
settings: Partial<Settings>;
|
|
738
|
+
timestamp: () => string;
|
|
739
|
+
});
|
|
740
|
+
/** Numeric ids per kind: patients 100001…, tests 700001…, webhooks 1…. */
|
|
741
|
+
nextId(kind: keyof typeof ID_BASES): number;
|
|
742
|
+
current(): Settings;
|
|
743
|
+
update(patch: Partial<Settings>): Settings;
|
|
744
|
+
ensureSeeded(): void;
|
|
745
|
+
addWebhook(url: string, events: string[], signingKey?: string): WebhookRecord;
|
|
746
|
+
patient(practiceId: string, patientId: string | number): PatientRecord | undefined;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/**
|
|
750
|
+
* The slice of OpenAPI 3.1 (and JSON Schema 2020-12) Mockingbird understands.
|
|
751
|
+
* Unknown keys (including `x-*` extensions) are preserved on every object.
|
|
752
|
+
*/
|
|
753
|
+
type JsonPrimitive = string | number | boolean | null;
|
|
754
|
+
type JsonValue = JsonPrimitive | JsonValue[] | {
|
|
755
|
+
[key: string]: JsonValue;
|
|
756
|
+
};
|
|
757
|
+
type ReferenceObject = {
|
|
758
|
+
$ref: string;
|
|
759
|
+
description?: string;
|
|
760
|
+
summary?: string;
|
|
761
|
+
};
|
|
762
|
+
type SchemaType = "string" | "number" | "integer" | "boolean" | "object" | "array" | "null";
|
|
763
|
+
type SchemaObject = {
|
|
764
|
+
$ref?: string;
|
|
765
|
+
type?: SchemaType | SchemaType[];
|
|
766
|
+
title?: string;
|
|
767
|
+
description?: string;
|
|
768
|
+
format?: string;
|
|
769
|
+
enum?: JsonValue[];
|
|
770
|
+
const?: JsonValue;
|
|
771
|
+
default?: JsonValue;
|
|
772
|
+
example?: JsonValue;
|
|
773
|
+
examples?: JsonValue[];
|
|
774
|
+
nullable?: boolean;
|
|
775
|
+
deprecated?: boolean;
|
|
776
|
+
readOnly?: boolean;
|
|
777
|
+
writeOnly?: boolean;
|
|
778
|
+
minimum?: number;
|
|
779
|
+
maximum?: number;
|
|
780
|
+
exclusiveMinimum?: number;
|
|
781
|
+
exclusiveMaximum?: number;
|
|
782
|
+
multipleOf?: number;
|
|
783
|
+
minLength?: number;
|
|
784
|
+
maxLength?: number;
|
|
785
|
+
pattern?: string;
|
|
786
|
+
minItems?: number;
|
|
787
|
+
maxItems?: number;
|
|
788
|
+
uniqueItems?: boolean;
|
|
789
|
+
items?: SchemaObject;
|
|
790
|
+
prefixItems?: SchemaObject[];
|
|
791
|
+
minProperties?: number;
|
|
792
|
+
maxProperties?: number;
|
|
793
|
+
required?: string[];
|
|
794
|
+
properties?: Record<string, SchemaObject>;
|
|
795
|
+
additionalProperties?: boolean | SchemaObject;
|
|
796
|
+
propertyNames?: SchemaObject;
|
|
797
|
+
oneOf?: SchemaObject[];
|
|
798
|
+
anyOf?: SchemaObject[];
|
|
799
|
+
allOf?: SchemaObject[];
|
|
800
|
+
not?: SchemaObject;
|
|
801
|
+
discriminator?: {
|
|
802
|
+
propertyName: string;
|
|
803
|
+
mapping?: Record<string, string>;
|
|
804
|
+
};
|
|
805
|
+
[extension: `x-${string}`]: unknown;
|
|
806
|
+
};
|
|
807
|
+
type ParameterLocation = "path" | "query" | "header" | "cookie";
|
|
808
|
+
type ParameterObject = {
|
|
809
|
+
name: string;
|
|
810
|
+
in: ParameterLocation;
|
|
811
|
+
description?: string;
|
|
812
|
+
required?: boolean;
|
|
813
|
+
deprecated?: boolean;
|
|
814
|
+
style?: string;
|
|
815
|
+
explode?: boolean;
|
|
816
|
+
schema?: SchemaObject;
|
|
817
|
+
content?: Record<string, MediaTypeObject>;
|
|
818
|
+
example?: JsonValue;
|
|
819
|
+
[extension: `x-${string}`]: unknown;
|
|
820
|
+
};
|
|
821
|
+
type MediaTypeObject = {
|
|
822
|
+
schema?: SchemaObject;
|
|
823
|
+
example?: JsonValue;
|
|
824
|
+
examples?: Record<string, unknown>;
|
|
825
|
+
encoding?: Record<string, unknown>;
|
|
826
|
+
[extension: `x-${string}`]: unknown;
|
|
827
|
+
};
|
|
828
|
+
type RequestBodyObject = {
|
|
829
|
+
description?: string;
|
|
830
|
+
required?: boolean;
|
|
831
|
+
content: Record<string, MediaTypeObject>;
|
|
832
|
+
[extension: `x-${string}`]: unknown;
|
|
833
|
+
};
|
|
834
|
+
type HeaderObject = {
|
|
835
|
+
description?: string;
|
|
836
|
+
required?: boolean;
|
|
837
|
+
schema?: SchemaObject;
|
|
838
|
+
[extension: `x-${string}`]: unknown;
|
|
839
|
+
};
|
|
840
|
+
type ResponseObject = {
|
|
841
|
+
description: string;
|
|
842
|
+
headers?: Record<string, HeaderObject | ReferenceObject>;
|
|
843
|
+
content?: Record<string, MediaTypeObject>;
|
|
844
|
+
[extension: `x-${string}`]: unknown;
|
|
845
|
+
};
|
|
846
|
+
type ResponsesObject = Record<string, ResponseObject | ReferenceObject>;
|
|
847
|
+
type SecurityRequirementObject = Record<string, string[]>;
|
|
848
|
+
type OperationObject = {
|
|
849
|
+
operationId?: string;
|
|
850
|
+
summary?: string;
|
|
851
|
+
description?: string;
|
|
852
|
+
tags?: string[];
|
|
853
|
+
deprecated?: boolean;
|
|
854
|
+
parameters?: Array<ParameterObject | ReferenceObject>;
|
|
855
|
+
requestBody?: RequestBodyObject | ReferenceObject;
|
|
856
|
+
responses: ResponsesObject;
|
|
857
|
+
security?: SecurityRequirementObject[];
|
|
858
|
+
[extension: `x-${string}`]: unknown;
|
|
859
|
+
};
|
|
860
|
+
declare const HTTP_METHODS: readonly ["get", "put", "post", "delete", "options", "head", "patch", "trace"];
|
|
861
|
+
type HttpMethod = (typeof HTTP_METHODS)[number];
|
|
862
|
+
type PathItemObject = {
|
|
863
|
+
summary?: string;
|
|
864
|
+
description?: string;
|
|
865
|
+
parameters?: Array<ParameterObject | ReferenceObject>;
|
|
866
|
+
[extension: `x-${string}`]: unknown;
|
|
867
|
+
} & Partial<Record<HttpMethod, OperationObject>>;
|
|
868
|
+
type SecuritySchemeObject = {
|
|
869
|
+
type: "apiKey" | "http" | "oauth2" | "openIdConnect" | "mutualTLS";
|
|
870
|
+
description?: string;
|
|
871
|
+
name?: string;
|
|
872
|
+
in?: ParameterLocation;
|
|
873
|
+
scheme?: string;
|
|
874
|
+
bearerFormat?: string;
|
|
875
|
+
flows?: Record<string, unknown>;
|
|
876
|
+
openIdConnectUrl?: string;
|
|
877
|
+
[extension: `x-${string}`]: unknown;
|
|
878
|
+
};
|
|
879
|
+
type ComponentsObject = {
|
|
880
|
+
schemas?: Record<string, SchemaObject>;
|
|
881
|
+
responses?: Record<string, ResponseObject>;
|
|
882
|
+
parameters?: Record<string, ParameterObject>;
|
|
883
|
+
requestBodies?: Record<string, RequestBodyObject>;
|
|
884
|
+
headers?: Record<string, HeaderObject>;
|
|
885
|
+
securitySchemes?: Record<string, SecuritySchemeObject>;
|
|
886
|
+
[extension: `x-${string}`]: unknown;
|
|
887
|
+
};
|
|
888
|
+
type ServerObject = {
|
|
889
|
+
url: string;
|
|
890
|
+
description?: string;
|
|
891
|
+
variables?: Record<string, unknown>;
|
|
892
|
+
[extension: `x-${string}`]: unknown;
|
|
893
|
+
};
|
|
894
|
+
type InfoObject = {
|
|
895
|
+
title: string;
|
|
896
|
+
version: string;
|
|
897
|
+
description?: string;
|
|
898
|
+
[extension: `x-${string}`]: unknown;
|
|
899
|
+
};
|
|
900
|
+
type OpenAPIDocument = {
|
|
901
|
+
openapi: string;
|
|
902
|
+
info: InfoObject;
|
|
903
|
+
servers?: ServerObject[];
|
|
904
|
+
paths: Record<string, PathItemObject>;
|
|
905
|
+
components?: ComponentsObject;
|
|
906
|
+
security?: SecurityRequirementObject[];
|
|
907
|
+
tags?: Array<{
|
|
908
|
+
name: string;
|
|
909
|
+
description?: string;
|
|
910
|
+
}>;
|
|
911
|
+
[extension: `x-${string}`]: unknown;
|
|
912
|
+
};
|
|
913
|
+
|
|
914
|
+
declare const document: OpenAPIDocument;
|
|
915
|
+
type OperationId = "ListPartnerLabs" | "ListElements" | "CreatePatient" | "UpdatePatient" | "DeletePatient" | "LinkPartnerUser" | "ListPatients" | "CreateTestResults" | "CreatePatientTest" | "UpdatePatientTest" | "ListPatientTests" | "GenerateFunctionalHealthReport" | "ListWebhooks" | "RegisterWebhook" | "UpdateWebhook";
|
|
916
|
+
type SupportedOperationId = "ListPartnerLabs" | "ListElements" | "CreatePatient" | "UpdatePatient" | "DeletePatient" | "LinkPartnerUser" | "ListPatients" | "CreateTestResults" | "CreatePatientTest" | "UpdatePatientTest" | "ListPatientTests" | "GenerateFunctionalHealthReport" | "ListWebhooks" | "RegisterWebhook" | "UpdateWebhook";
|
|
917
|
+
declare const operationIds: readonly ["ListPartnerLabs", "ListElements", "CreatePatient", "UpdatePatient", "DeletePatient", "LinkPartnerUser", "ListPatients", "CreateTestResults", "CreatePatientTest", "UpdatePatientTest", "ListPatientTests", "GenerateFunctionalHealthReport", "ListWebhooks", "RegisterWebhook", "UpdateWebhook"];
|
|
918
|
+
declare const supportedOperationIds: readonly ["ListPartnerLabs", "ListElements", "CreatePatient", "UpdatePatient", "DeletePatient", "LinkPartnerUser", "ListPatients", "CreateTestResults", "CreatePatientTest", "UpdatePatientTest", "ListPatientTests", "GenerateFunctionalHealthReport", "ListWebhooks", "RegisterWebhook", "UpdateWebhook"];
|
|
919
|
+
|
|
920
|
+
/** The header our guard (`OdxSignatureGuard`) reads. */
|
|
921
|
+
declare const SIGNATURE_HEADER = "optimaldx-signature";
|
|
922
|
+
/** ODX's signature: UPPERCASE hex HMAC-SHA256 of the raw body under the webhook's signing key. */
|
|
923
|
+
declare const signOdx: (signingKey: string, body: string) => Promise<string>;
|
|
924
|
+
/**
|
|
925
|
+
* Every named ODX misbehaviour our consumer branches on, switched on with
|
|
926
|
+
* `POST /__admin/faults {"preset": "<name>"}` (add `count` to limit it).
|
|
927
|
+
*/
|
|
928
|
+
declare const ODX_PRESETS: Record<string, FaultPreset>;
|
|
929
|
+
type OdxRuntimeOptions = {
|
|
930
|
+
sqlite?: SqliteClient;
|
|
931
|
+
clock?: Clock;
|
|
932
|
+
seed?: number | string;
|
|
933
|
+
adminKey?: string;
|
|
934
|
+
onLog?: (entry: RequestLog) => void;
|
|
935
|
+
settings?: Partial<Settings>;
|
|
936
|
+
/**
|
|
937
|
+
* Register this webhook in every namespace from the start (`POST /odx/webhook` on our
|
|
938
|
+
* backend), with this signing key (deterministically derived from `seed` when omitted).
|
|
939
|
+
* More can be registered through
|
|
940
|
+
* `POST /v1/webhook`, exactly as `manageWebhooks` does.
|
|
941
|
+
*/
|
|
942
|
+
webhook?: {
|
|
943
|
+
url: string;
|
|
944
|
+
signingKey?: string;
|
|
945
|
+
};
|
|
946
|
+
retryDelaysMs?: readonly number[];
|
|
947
|
+
fetch?: (request: Request) => Promise<Response>;
|
|
948
|
+
};
|
|
949
|
+
type OdxRuntime = ServiceRuntime<OdxAPI> & {
|
|
950
|
+
readonly webhooks: WebhookHub;
|
|
951
|
+
};
|
|
952
|
+
/**
|
|
953
|
+
* The ODX mock with Mockingbird's full service contract: `/health`, `/__admin/*`, namespaces
|
|
954
|
+
* by header, by `/ns/<name>` path prefix, or by `ApiKey`
|
|
955
|
+
* (`PUT /__admin/credentials {"credentials": {"<OPTIMAL_API_KEY>": "<namespace>"}}`), clock
|
|
956
|
+
* control, fault presets, signed PatientTest webhooks and a request journal.
|
|
957
|
+
*/
|
|
958
|
+
declare const createRuntime: (options?: OdxRuntimeOptions) => OdxRuntime;
|
|
959
|
+
|
|
960
|
+
declare const ODX_NAMESPACE = "odx";
|
|
961
|
+
type OdxEventType = "Created" | "Updated" | "Deleted";
|
|
962
|
+
/** How the next webhook is signed: correctly, with a short (wrong-length) or a wrong digest. */
|
|
963
|
+
type SignatureMode = "valid" | "short" | "bad";
|
|
964
|
+
/** The webhook body ODX posts (`OdxWebhookDataSchema` in our receiver). */
|
|
965
|
+
type OdxWebhook = {
|
|
966
|
+
entityType: "PatientTest";
|
|
967
|
+
eventType: OdxEventType;
|
|
968
|
+
data: PatientTestRecord;
|
|
969
|
+
};
|
|
970
|
+
type OdxAPIOptions = APIOptions & {
|
|
971
|
+
settings?: Partial<Settings>;
|
|
972
|
+
/** Called for every PatientTest event; the runtime signs and delivers it. */
|
|
973
|
+
onWebhook?: (event: OdxWebhook, signature: SignatureMode) => void;
|
|
974
|
+
};
|
|
975
|
+
/** The `ApiKey` header (how requests map to namespaces). */
|
|
976
|
+
declare const apiKeyCredential: (request: Request) => string | undefined;
|
|
977
|
+
/**
|
|
978
|
+
* Stateful mock of the Optimal DX partner API.
|
|
979
|
+
*
|
|
980
|
+
* Patients and tests live per practice; HL7 imports map OBX codes to elements through the lab
|
|
981
|
+
* element corpus; every test create/update emits a signed PatientTest webhook to each
|
|
982
|
+
* registered webhook URL.
|
|
983
|
+
*/
|
|
984
|
+
declare class OdxAPI implements FetchAPI$1 {
|
|
985
|
+
readonly app: Hono;
|
|
986
|
+
readonly sqlite: SqliteClient;
|
|
987
|
+
readonly state: OdxState;
|
|
988
|
+
private readonly service;
|
|
989
|
+
private readonly now;
|
|
990
|
+
private readonly onWebhook;
|
|
991
|
+
constructor(options?: OdxAPIOptions);
|
|
992
|
+
fetch(request: Request): Promise<Response>;
|
|
993
|
+
reset(): Promise<void>;
|
|
994
|
+
private iso;
|
|
995
|
+
private listElements;
|
|
996
|
+
private savePatient;
|
|
997
|
+
private deletePatient;
|
|
998
|
+
private linkPartner;
|
|
999
|
+
private listPatients;
|
|
1000
|
+
private listTests;
|
|
1001
|
+
private testBase;
|
|
1002
|
+
private createTestResults;
|
|
1003
|
+
private saveHl7Test;
|
|
1004
|
+
private storeTest;
|
|
1005
|
+
private testResponse;
|
|
1006
|
+
/** Emit a PatientTest webhook for a stored test (tests, the admin route, `Deleted`). */
|
|
1007
|
+
emit(patientTestId: number | string, eventType: OdxEventType, signature?: SignatureMode): OdxWebhook | undefined;
|
|
1008
|
+
private report;
|
|
1009
|
+
private saveWebhook;
|
|
1010
|
+
patients(): PatientRecord[];
|
|
1011
|
+
tests(): PatientTestRecord[];
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
export { DEFAULT_SETTINGS, ELEMENTS, LABS, ODX_NAMESPACE, ODX_PRESETS, OdxAPI, SIGNATURE_HEADER, apiKeyCredential, createRuntime, document, matchElement, operationIds, parseObservations, signOdx, supportedOperationIds };
|
|
1015
|
+
export type { ElementDef, FetchAPI$1 as FetchAPI, ImportLog, Observation, OdxAPIOptions, OdxEventType, OdxLab, OdxRuntime, OdxRuntimeOptions, OdxWebhook, OperationId, PatientRecord, PatientTestRecord, ResultElement, Settings, SignatureMode, SqliteClient, SupportedOperationId, WebhookRecord };
|