@flipstream/sdk 0.8.0 → 0.10.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.
Files changed (104) hide show
  1. package/CHANGELOG.md +92 -0
  2. package/README.md +707 -66
  3. package/dist/api/backend.d.ts +182 -0
  4. package/dist/api/backend.js +314 -0
  5. package/dist/api/backend.js.map +1 -0
  6. package/dist/api/created.d.ts +5 -0
  7. package/dist/api/created.js +15 -0
  8. package/dist/api/created.js.map +1 -0
  9. package/dist/api/credentials.d.ts +36 -0
  10. package/dist/api/credentials.js +94 -0
  11. package/dist/api/credentials.js.map +1 -0
  12. package/dist/api/datasets.d.ts +94 -0
  13. package/dist/api/datasets.js +212 -0
  14. package/dist/api/datasets.js.map +1 -0
  15. package/dist/api/handoff.d.ts +34 -0
  16. package/dist/api/handoff.js +52 -0
  17. package/dist/api/handoff.js.map +1 -0
  18. package/dist/api/ids.d.ts +7 -0
  19. package/dist/api/ids.js +27 -0
  20. package/dist/api/ids.js.map +1 -0
  21. package/dist/api/index.d.ts +9 -1
  22. package/dist/api/log.d.ts +95 -0
  23. package/dist/api/log.js +213 -0
  24. package/dist/api/log.js.map +1 -0
  25. package/dist/api/me.d.ts +58 -0
  26. package/dist/api/me.js +64 -0
  27. package/dist/api/me.js.map +1 -0
  28. package/dist/api/members.d.ts +48 -0
  29. package/dist/api/members.js +102 -0
  30. package/dist/api/members.js.map +1 -0
  31. package/dist/api/organization.d.ts +11 -0
  32. package/dist/api/organization.js +15 -0
  33. package/dist/api/organization.js.map +1 -0
  34. package/dist/api/page.d.ts +33 -0
  35. package/dist/api/page.js +38 -0
  36. package/dist/api/page.js.map +1 -0
  37. package/dist/api/time.d.ts +3 -0
  38. package/dist/api/time.js +49 -0
  39. package/dist/api/time.js.map +1 -0
  40. package/dist/api/unsent.d.ts +6 -0
  41. package/dist/api/unsent.js +9 -0
  42. package/dist/api/unsent.js.map +1 -0
  43. package/dist/api/wallet.d.ts +45 -0
  44. package/dist/api/wallet.js +102 -0
  45. package/dist/api/wallet.js.map +1 -0
  46. package/dist/api/workspaces.d.ts +70 -0
  47. package/dist/api/workspaces.js +108 -0
  48. package/dist/api/workspaces.js.map +1 -0
  49. package/dist/auth/discovery.js +1 -1
  50. package/dist/auth/discovery.js.map +1 -1
  51. package/dist/auth/signIn.js +7 -2
  52. package/dist/auth/signIn.js.map +1 -1
  53. package/dist/core/body.js +23 -5
  54. package/dist/core/body.js.map +1 -1
  55. package/dist/core/call.d.ts +6 -0
  56. package/dist/core/call.js +1 -1
  57. package/dist/core/call.js.map +1 -1
  58. package/dist/core/fetch.d.ts +5 -0
  59. package/dist/core/fetch.js +7 -1
  60. package/dist/core/fetch.js.map +1 -1
  61. package/dist/core/identify.d.ts +63 -0
  62. package/dist/core/identify.js +122 -0
  63. package/dist/core/identify.js.map +1 -0
  64. package/dist/core/names.d.ts +100 -0
  65. package/dist/core/names.js +114 -0
  66. package/dist/core/names.js.map +1 -0
  67. package/dist/core/read.d.ts +1 -1
  68. package/dist/core/read.js +15 -6
  69. package/dist/core/read.js.map +1 -1
  70. package/dist/core/result.d.ts +67 -6
  71. package/dist/core/result.js +39 -6
  72. package/dist/core/result.js.map +1 -1
  73. package/dist/core/secrets.d.ts +2 -0
  74. package/dist/core/secrets.js +33 -0
  75. package/dist/core/secrets.js.map +1 -0
  76. package/dist/core/transport.d.ts +12 -4
  77. package/dist/flipstream.d.ts +341 -5
  78. package/dist/flipstream.js +203 -13
  79. package/dist/flipstream.js.map +1 -1
  80. package/dist/index.d.ts +2 -2
  81. package/dist/index.js +1 -1
  82. package/dist/index.js.map +1 -1
  83. package/dist/node/clientSecret.js +2 -1
  84. package/dist/node/clientSecret.js.map +1 -1
  85. package/dist/node/index.d.ts +1 -0
  86. package/dist/node/index.js +1 -0
  87. package/dist/node/index.js.map +1 -1
  88. package/dist/node/keychainChild.js +10 -6
  89. package/dist/node/keychainChild.js.map +1 -1
  90. package/dist/node/keychainStore.d.ts +6 -3
  91. package/dist/node/keychainStore.js +6 -3
  92. package/dist/node/keychainStore.js.map +1 -1
  93. package/dist/node/session.d.ts +78 -29
  94. package/dist/node/session.js +74 -52
  95. package/dist/node/session.js.map +1 -1
  96. package/dist/node/store.d.ts +11 -1
  97. package/dist/node/store.js +14 -1
  98. package/dist/node/store.js.map +1 -1
  99. package/dist/query/query.d.ts +15 -10
  100. package/dist/query/query.js +21 -11
  101. package/dist/query/query.js.map +1 -1
  102. package/dist/version.gen.js +1 -1
  103. package/dist/version.gen.js.map +1 -1
  104. package/package.json +2 -2
@@ -1,14 +1,61 @@
1
+ import { type Credential } from './api/credentials.js';
2
+ import { type Dataset, type DatasetListRequest, type DatasetType } from './api/datasets.js';
3
+ import { type LogAddRequest, type LogEntry, type LogListRequest, type LogUpdateRequest } from './api/log.js';
4
+ import { type Me } from './api/me.js';
5
+ import { type Member } from './api/members.js';
6
+ import type { Page, PageRequest } from './api/page.js';
7
+ import { type Balance, type Ledger } from './api/wallet.js';
8
+ import { type Workspace } from './api/workspaces.js';
1
9
  import { type TokenAsk } from './core/fetch.js';
2
10
  import type { ApiResult } from './core/result.js';
3
11
  import type { ServiceName, Transport } from './core/transport.js';
4
12
  import { type Health } from './query/health.js';
5
13
  import { type QueryRequest } from './query/query.js';
6
14
  import type { Catalog, CatalogSource, QueryResponse } from './spec/query-planner/types.gen.js';
7
- /** Each service's base URL. Only public hosts (D410); a call goes only to its own service's. */
8
- export type Hosts = Readonly<Partial<Record<ServiceName, string>>>;
15
+ /** Each service's base URL; a call goes only to its own service's. */
16
+ export type Hosts = Readonly<Partial<Record<ServiceName, string>> & {
17
+ /**
18
+ * The console, for the addresses a hand-off gives a person (D542): never a destination, so no
19
+ * call goes to it and no token either. Set it (`http://localhost:5173` for development) to name
20
+ * the console before Flipstream's own is live; until then, a hand-off gives no address without
21
+ * it. It must be https, or http on this machine, with no query, fragment or credentials (a path
22
+ * is kept), or it's a TypeError when the client is made.
23
+ */
24
+ readonly console?: string;
25
+ }>;
9
26
  /** Asked before every attempt, and once more after a 401 with the token the host refused. */
10
27
  export type TokenFunction = (ask: TokenAsk) => string | Promise<string>;
11
- export type FlipstreamOptions =
28
+ /**
29
+ * Who a client's requests say sent them (#192, D527), whichever way the client is made. In Node,
30
+ * each signed-in request to an API host carries them; the signed-out `health` and the auth server
31
+ * never get them. In a browser, none go yet: the hosts' CORS doesn't allow the headers until
32
+ * query-planner#48.
33
+ */
34
+ export interface Identification {
35
+ /**
36
+ * The app making the calls, required: every signed-in request says which app sent it, as
37
+ * `User-Agent: <name>/<version> flipstream-sdk/<version> node/<version>`. Each is 1 to 64 of
38
+ * RFC 9110's token characters (letters, digits and !#$%&'*+-.^_`|~), or a TypeError at creation.
39
+ */
40
+ readonly app: {
41
+ readonly name: string;
42
+ readonly version: string;
43
+ };
44
+ /**
45
+ * The agent running the app, when one does, under the same rule: sent as `baggage`, with
46
+ * OpenTelemetry's names `gen_ai.agent.name` and `gen_ai.agent.version`.
47
+ */
48
+ readonly agent?: {
49
+ readonly name: string;
50
+ readonly version: string;
51
+ };
52
+ /**
53
+ * The conversation or run the calls belong to, as `gen_ai.conversation.id`: 1 to 128 of A-Z,
54
+ * a-z, 0-9, `.`, `_`, `:` and `-`, or a TypeError at creation. A call's own replaces it.
55
+ */
56
+ readonly conversationId?: string;
57
+ }
58
+ export type FlipstreamOptions = Identification & (
12
59
  /** The console: its own transport, which attaches the token and decides what may go. */
13
60
  {
14
61
  readonly transport: Transport;
@@ -33,12 +80,24 @@ export type FlipstreamOptions =
33
80
  /** Plain http to a loopback host, for development and tests only. Off unless set. */
34
81
  readonly allowLocalhostHttp?: boolean;
35
82
  readonly transport?: never;
36
- };
83
+ });
37
84
  export interface CallOptions {
38
85
  /** Ends the call at once: its answer is `aborted`. */
39
86
  readonly signal?: AbortSignal;
40
87
  /** The whole call's deadline, every attempt and wait included; the endpoint's own when left out. */
41
88
  readonly deadlineMs?: number;
89
+ /**
90
+ * This call's conversation, in place of the client's, under the same rule. One that breaks it
91
+ * refuses the call, with nothing sent. `health` sends none, but a broken one refuses it too.
92
+ */
93
+ readonly conversationId?: string;
94
+ /**
95
+ * The caller's own W3C trace context, carried on: the call keeps its trace-id and flags, with a
96
+ * parent-id of its own for each attempt, and its result's `traceId` is that trace-id (#251). One
97
+ * that isn't a valid version-00 traceparent is dropped, as W3C Trace Context says, and the call
98
+ * starts its own trace.
99
+ */
100
+ readonly traceparent?: string;
42
101
  }
43
102
  /** What `createFlipstream` gives: each call settles into an `ApiResult`, never a thrown error (D412). */
44
103
  export interface Flipstream {
@@ -52,9 +111,286 @@ export interface Flipstream {
52
111
  /**
53
112
  * Whether the planner answers, signed out: `GET /health` with no token. It goes through neither
54
113
  * the app's transport nor the token function, since they hold the token, but straight to the
55
- * query host, under the same destination rule. A 200 with an object of strings is up.
114
+ * query host, under the same destination rule. A 200 with an object of strings is up. In Node it
115
+ * says only who the SDK is, `User-Agent: flipstream-sdk/<version> node/<version>` with a
116
+ * `traceparent`, and none of the client's `app`, `agent` or `conversationId` (D535): with no
117
+ * token, nothing could check that they don't hold it.
56
118
  */
57
119
  health(options?: CallOptions): Promise<ApiResult<Health>>;
120
+ /**
121
+ * Who you are: your organisation, and you in it, with your role (#67). It reads your organisation
122
+ * on the admin host beside the auth host's userinfo, then the person userinfo names, under one
123
+ * deadline (the call's, or 30 s). In Node, the admin read carries the call's trace and the app's
124
+ * identification; the auth reads carry none, as sign-in's don't. The first failure, the
125
+ * organisation's, then userinfo's, then the person's, is the answer. It reads the token's own
126
+ * person and doesn't apply `members`' people-only rule; an API key's token isn't tested with it
127
+ * yet (service accounts come with pulse-admin#93).
128
+ */
129
+ me(options?: CallOptions): Promise<ApiResult<Me>>;
130
+ /** Your organisation (#225). */
131
+ readonly organization: {
132
+ /**
133
+ * Renaming your organisation is a person's to do, in the console (D542 item 1). Never a write:
134
+ * it reads your organisation, then answers `needs_person` with `why`, and the organisation's
135
+ * settings page as `url`, with what it shows as `sees`, where a console can be named. In the
136
+ * console, Owners and Admins change the name and the short name. `name` is never echoed.
137
+ */
138
+ rename(request: {
139
+ readonly name: string;
140
+ }, options?: CallOptions): Promise<ApiResult<never>>;
141
+ };
142
+ /**
143
+ * Your organisation's workspaces (#208), through the admin API. Each call reads your organisation
144
+ * first, for its id, under one deadline for both reads (the call's, or 30 s); its failure is the
145
+ * answer. A workspace of another organisation is `invalid`, never dropped or shown.
146
+ */
147
+ readonly workspaces: {
148
+ /**
149
+ * One page: `limit` (100 when left out, at most 1,000) and `offset` (0). A limit or offset that
150
+ * isn't a whole number in range rejects with a TypeError, with nothing sent. The order is the
151
+ * backend's, by name alone (D550): pages read at different times can overlap or skip a
152
+ * workspace when workspaces are added, removed or renamed in between, or when two share a name.
153
+ * A changed `total` shows some of that; an unchanged one doesn't prove none happened. For a
154
+ * complete, stable list, read it in one page, of up to 1,000.
155
+ */
156
+ list(request?: PageRequest, options?: CallOptions): Promise<ApiResult<Page<Workspace>>>;
157
+ /** One workspace, by its id. An id that can't stay one path segment is `refused` (`path`), with nothing sent. */
158
+ get(request: {
159
+ readonly workspace_id: string;
160
+ }, options?: CallOptions): Promise<ApiResult<Workspace>>;
161
+ /**
162
+ * A new workspace in your organisation, named `name` (#209): the SDK's first write. It sends the
163
+ * name alone, with your organisation, and the backend makes the short name, unique in the
164
+ * organisation, so the answer's `slug` is the workspace's address. A request with any key but
165
+ * `name` is `refused` (`fields`, naming them), and a `name` that isn't a non-empty, non-blank
166
+ * string rejects with a TypeError, each with nothing sent. Who may create one is the backend's
167
+ * rule, not the SDK's: Owners and Admins (pulse-admin#181 aligns the backend; until then an
168
+ * Editor still succeeds). Anyone it refuses gets `forbidden`. It's never tried again. Only a
169
+ * refusal (a 4xx kind), `refused` or `no_token` shows nothing was made: after any other
170
+ * failure, `invalid` included, it may have been, so list the workspaces before you create it
171
+ * again.
172
+ */
173
+ create(request: {
174
+ readonly name: string;
175
+ }, options?: CallOptions): Promise<ApiResult<Workspace>>;
176
+ /**
177
+ * A rename is a person's, in the console (D542): it answers `needs_person`, with why and, where
178
+ * the console can be named, the workspace's settings page. It never writes, and the name isn't
179
+ * sent: the person types it there. A workspace that isn't yours is `not_found`, never handed off.
180
+ */
181
+ rename(request: {
182
+ readonly workspace_id: string;
183
+ readonly name: string;
184
+ }, options?: CallOptions): Promise<ApiResult<never>>;
185
+ /**
186
+ * A delete is a person's to confirm, in the console (D542): it answers `needs_person`, as
187
+ * `rename` does, and never writes. The console can't delete a workspace yet, and `why` says so.
188
+ */
189
+ delete(request: {
190
+ readonly workspace_id: string;
191
+ }, options?: CallOptions): Promise<ApiResult<never>>;
192
+ };
193
+ /** A workspace's logbook (#218), on the admin host: its notes on time ranges. */
194
+ readonly log: {
195
+ /**
196
+ * One page of the workspace's entries, newest first by when they were written: the backend's
197
+ * own order, since no other is asked for. When an entry was written never changes, so editing
198
+ * one doesn't move it, but pages read at different times can overlap or skip an entry when one
199
+ * is written or removed in between, and entries written at the same moment have no fixed order
200
+ * among themselves. A changed `total` shows that something was added or removed; an unchanged
201
+ * one doesn't prove nothing was. The workspace is read beside the list (#249), with your
202
+ * organisation, under one deadline: one nobody has is `not_found`, and another organisation's is
203
+ * `invalid`, never an empty page. An entry of another workspace or organisation makes the answer
204
+ * `invalid`, never dropped. A limit or offset that isn't a whole number in range rejects, with
205
+ * nothing sent, as every list's does.
206
+ */
207
+ list(request: LogListRequest, options?: CallOptions): Promise<ApiResult<Page<LogEntry>>>;
208
+ /**
209
+ * One entry, by its id, read beside your organisation (#249); `not_found` when there's none, and
210
+ * `invalid` when the answer is another entry, or another organisation's.
211
+ */
212
+ get(request: {
213
+ readonly log_id: string;
214
+ }, options?: CallOptions): Promise<ApiResult<LogEntry>>;
215
+ /**
216
+ * A new entry in a workspace's logbook (#219). Before anything is sent, a key it doesn't send is
217
+ * `refused` (`fields`, naming them), and a description that isn't a non-blank string, a time
218
+ * not written as `2026-10-05T00:00:00Z` (a fraction may follow the seconds, and an offset such
219
+ * as `+02:00`, or none, may take the place of the `Z`), or a start after the end rejects with a
220
+ * TypeError. The workspace is read first: one nobody has is `not_found`, and another
221
+ * organisation's `invalid`, with nothing written. Times go as UTC instants; one with no offset is
222
+ * read as UTC. Who may write is the backend's rule, not the SDK's: Owners, Admins and Editors
223
+ * add; an Editor changes or removes only their own entries, and Owners and Admins any. Until the
224
+ * backend enforces it (pulse-admin#161, for Editors), a Viewer can also add, change and remove
225
+ * entries, and an Editor can change anyone's. Anyone it refuses gets `forbidden`. A write is
226
+ * never tried again: only a refusal (a 4xx kind), `refused` or `no_token` shows nothing was
227
+ * written, so after any other failure, list the logbook before you add it again.
228
+ */
229
+ add(request: LogAddRequest, options?: CallOptions): Promise<ApiResult<LogEntry>>;
230
+ /**
231
+ * An entry's words or range changed (#219), with the same rules as `add`, and at least one
232
+ * change. The entry is read first (one that isn't yours is `not_found` or `invalid`, with
233
+ * nothing written), then sent back whole: what you change, and the rest exactly as stored. When
234
+ * you give a time, a start after the end, the stored one included, rejects with a TypeError, with
235
+ * nothing written. Since the whole entry goes back, a change someone else made between the read
236
+ * and the write is overwritten.
237
+ */
238
+ update(request: LogUpdateRequest, options?: CallOptions): Promise<ApiResult<LogEntry>>;
239
+ /**
240
+ * An entry removed (#219), directly: a log entry isn't one of the hand-offs. The entry is read
241
+ * first (one that isn't yours is `not_found` or `invalid`, with nothing removed); an ok is
242
+ * `null`.
243
+ */
244
+ remove(request: {
245
+ readonly log_id: string;
246
+ }, options?: CallOptions): Promise<ApiResult<null>>;
247
+ };
248
+ /**
249
+ * A workspace's datasets (#211), on the admin host: its uploads and connected accounts, the
250
+ * backend's connections. Each call reads your organisation beside the datasets, and a list reads
251
+ * its workspace too, under one deadline for every read (the call's, or 30 s). The organisation's
252
+ * failure, then the workspace's, then the datasets', is the answer, and a dataset of another
253
+ * organisation is `invalid`, never dropped or shown. The backend's answers embed each dataset's
254
+ * sign-in, so a body that isn't its JSON is never kept: a failure of the dataset's own read or
255
+ * write has no `text` (#279). The organisation's and the workspace's reads keep theirs.
256
+ */
257
+ readonly datasets: {
258
+ /**
259
+ * One page of the workspace's datasets: `limit` (100 when left out, at most 1,000) and `offset`
260
+ * (0). A limit or offset that isn't a whole number in range rejects with a TypeError, with
261
+ * nothing sent. The order is the backend's, by name alone (D550): pages read at different
262
+ * times can overlap or skip a dataset when datasets are added, removed or renamed in between,
263
+ * or when two share a name. A changed `total` shows some of that; an unchanged one doesn't
264
+ * prove none happened. For a complete, stable list, read it in one page, of up to 1,000. A
265
+ * dataset of another workspace makes the answer `invalid`, never dropped. A workspace nobody has
266
+ * is `not_found`, and another organisation's is `invalid`, never an empty page.
267
+ */
268
+ list(request: DatasetListRequest, options?: CallOptions): Promise<ApiResult<Page<Dataset>>>;
269
+ /** One dataset, by its id; `not_found` when there's none, and `invalid` when the answer is another dataset. */
270
+ get(request: {
271
+ readonly dataset_id: string;
272
+ }, options?: CallOptions): Promise<ApiResult<Dataset>>;
273
+ /**
274
+ * Pauses a dataset (#212): the answer is the `Dataset`, `active` false. Pausing a paused one
275
+ * sends the same write and answers the same. Before anything is sent, a key other than
276
+ * `dataset_id` is `refused` (`fields`, naming them), and an id that isn't a string rejects with
277
+ * a TypeError. The dataset is read first, fresh: one nobody has is `not_found`, and another
278
+ * organisation's `invalid`, with nothing written. Then `active` alone is sent, so a rename made
279
+ * meanwhile stays. The dataset is read fresh again after the write, and the answer is that read:
280
+ * one whose `active` isn't the one asked for is `invalid`, since the write may not have been
281
+ * stored. That read comes after the write was sent, so its failure that would say nothing was
282
+ * written (a 4xx kind, `refused`, `no_token`) is `invalid` too, naming its kind. Who may is
283
+ * the backend's rule: Owners, Admins and Editors pause and resume, and a Viewer doesn't. Anyone
284
+ * it refuses gets `forbidden`. A write is never tried again: only a refusal (a 4xx kind),
285
+ * `refused` or `no_token` shows nothing was written. After any other failure it may have been;
286
+ * pausing again is safe, since it's the same write.
287
+ */
288
+ pause(request: {
289
+ readonly dataset_id: string;
290
+ }, options?: CallOptions): Promise<ApiResult<Dataset>>;
291
+ /** Resumes a dataset (#212): `active` true, with the same rules as `pause`. */
292
+ resume(request: {
293
+ readonly dataset_id: string;
294
+ }, options?: CallOptions): Promise<ApiResult<Dataset>>;
295
+ /**
296
+ * Deleting a dataset is a person's to do, in the console (D542, D546): it removes the dataset's
297
+ * data for good, and the console can't delete one yet. Never a write: it reads the dataset (one
298
+ * nobody has is `not_found`, and another organisation's `invalid`), then answers `needs_person`
299
+ * with `why`, and the dataset's page as `url`, with what it shows as `sees`, where a console can
300
+ * be named.
301
+ */
302
+ delete(request: {
303
+ readonly dataset_id: string;
304
+ }, options?: CallOptions): Promise<ApiResult<never>>;
305
+ };
306
+ /** The dataset types this version of the SDK knows (#211). */
307
+ readonly datasetTypes: {
308
+ /** The types, from the SDK's own list: no request is made, as the backend has no route for them yet. */
309
+ list(): Promise<ApiResult<readonly DatasetType[]>>;
310
+ };
311
+ /**
312
+ * Your organisation's provider sign-ins (#70), as metadata only: never a sign-in's value, which
313
+ * the SDK has no call to read. pulse-admin doesn't mask a sign-in, so the SDK's own check is the
314
+ * guard. An answer holding what may be a value (a non-empty `data`, or a key such as
315
+ * `refresh_token`, at any depth, however it's spelt) is `invalid`, naming the place and never the
316
+ * value, so nothing is answered at all; a body that isn't the service's JSON is never kept, so a
317
+ * failure of the sign-in's own read has no `text` (the organisation's read keeps its). Every read
318
+ * is held to your organisation: a sign-in of another one is `invalid`.
319
+ */
320
+ readonly credentials: {
321
+ /**
322
+ * One page of the sign-ins: `limit` (100 when left out, at most 1,000) and `offset` (0). A limit
323
+ * or offset that isn't a whole number in range rejects with a TypeError, with nothing sent. Each
324
+ * is `{ id, name, type, datasource_types, created_on }`, where `name` is the provider account it
325
+ * signs in as: for Google, that account's email address. Who may list is the backend's rule:
326
+ * today Owners, Admins, Editors and Viewers; anyone it refuses gets `forbidden`.
327
+ */
328
+ list(request?: PageRequest, options?: CallOptions): Promise<ApiResult<Page<Credential>>>;
329
+ /**
330
+ * A person revokes a sign-in, in the console (D546): it breaks every dataset that reads with it.
331
+ * Never a write: it reads the sign-in, then answers `needs_person` with `why`, and no `url` until
332
+ * a console page names a sign-in. One nobody has is `not_found`, and another organisation's is
333
+ * `invalid`, before any hand-off.
334
+ */
335
+ revoke(request: {
336
+ readonly credential_id: string;
337
+ }, options?: CallOptions): Promise<ApiResult<never>>;
338
+ /** A person deletes a sign-in, in the console (D546): the same as `revoke`, for `credential.delete`. */
339
+ delete(request: {
340
+ readonly credential_id: string;
341
+ }, options?: CallOptions): Promise<ApiResult<never>>;
342
+ };
343
+ /**
344
+ * Your organisation's prepaid wallet, read only (#73): no call writes, by design. Each call reads
345
+ * your organisation first, for the wallet's id, under one deadline for both reads; its failure is
346
+ * the answer. Who may read it is the wallet's rule, not the SDK's: today Owners and Admins, and
347
+ * Editors too once pulse-wallet#15 lands. Anyone the wallet refuses gets `forbidden`, never an
348
+ * empty ledger. Amounts are integer micro, and each answer says how many make a euro.
349
+ */
350
+ readonly wallet: {
351
+ /**
352
+ * Your balance, or null when the organisation has no wallet: a real state, so neither
353
+ * `invalid` nor €0.00, which would claim a wallet that holds nothing. A row of another wallet
354
+ * is `invalid`.
355
+ */
356
+ balance(options?: CallOptions): Promise<ApiResult<Balance | null>>;
357
+ /**
358
+ * Your whole ledger, in one answer, with no paging. Its rows carry no wallet id, so the balance
359
+ * is read beside it, and the ledger is answered only when that is your organisation's wallet.
360
+ */
361
+ transactions(options?: CallOptions): Promise<ApiResult<Ledger>>;
362
+ };
363
+ /**
364
+ * Your organisation's people (#220), from the auth host, each as `me()` answers you: one role by
365
+ * D544's rule, and `null` for none and for Flipstream's staff. People, not keys: an API user, a
366
+ * service account, is never listed, counted or answered (`get` of one is `not_found`), as the
367
+ * console's People has it. An open invitation is listed, with an empty `first_name`. Each call
368
+ * reads your organisation beside them, under one deadline for both reads (the call's, or 30 s).
369
+ * The organisation's failure, then the people's, is the answer, and a person of another
370
+ * organisation is `invalid`, never dropped or shown.
371
+ */
372
+ readonly members: {
373
+ /**
374
+ * One page: `limit` (100 when left out, at most 1,000) and `offset` (0). A limit or offset that
375
+ * isn't a whole number in range rejects with a TypeError, with nothing sent. The order is the
376
+ * backend's, by email (D550): pages read at different times can overlap or skip a person when
377
+ * people are added or removed, or an email changes, in between. A changed `total` shows some
378
+ * of that; an unchanged one doesn't prove none happened. For a complete, stable list, read it
379
+ * in one page, of up to 1,000.
380
+ */
381
+ list(request?: PageRequest, options?: CallOptions): Promise<ApiResult<Page<Member>>>;
382
+ /**
383
+ * One of your organisation's people, by their id. `not_found` when there's no such person for
384
+ * you: pulse-auth has none by that id, or your role can't read them (404), or it's one of your
385
+ * organisation's API keys, answered as an id nobody has. `forbidden` when pulse-auth refuses
386
+ * the read (403). `invalid` when the answer is another organisation's person or API key, which
387
+ * only a staff token reaches, such as Flipstream's staff acting through a swap token (the
388
+ * organisation is checked before the key), or someone else than the person asked for.
389
+ */
390
+ get(request: {
391
+ readonly member_id: string;
392
+ }, options?: CallOptions): Promise<ApiResult<Member>>;
393
+ };
58
394
  }
59
395
  /** The SDK, for one caller (D421): the console with its transport, or anyone with a token function. */
60
396
  export declare function createFlipstream(options: FlipstreamOptions): Flipstream;
@@ -1,37 +1,227 @@
1
+ import { backend, consoleBase } from './api/backend.js';
2
+ import { handOffCredential, listCredentials } from './api/credentials.js';
3
+ import { getDataset, handOffDatasetDelete, listDatasets, listDatasetTypes, pauseDataset, resumeDataset } from './api/datasets.js';
4
+ import { addLog, getLog, listLog, removeLog, updateLog } from './api/log.js';
5
+ import { readMe } from './api/me.js';
6
+ import { handOffOrganizationRename } from './api/organization.js';
7
+ import { getMember, listMembers } from './api/members.js';
8
+ import { readBalance, readLedger } from './api/wallet.js';
9
+ import { createWorkspace, handOffWorkspace, readWorkspace, readWorkspaces } from './api/workspaces.js';
1
10
  import { call } from './core/call.js';
2
- import { fetchTransport, tokenlessTransport } from './core/fetch.js';
11
+ import { fetchTransport, isLoopbackHttp, tokenlessTransport } from './core/fetch.js';
12
+ import { callerSender, identityOf, isConversationId, nodeVersion, sdkSender, traceFor, traceparent, valueHolds } from './core/identify.js';
3
13
  import { catalogGet, catalogList, consoleWarn } from './query/catalog.js';
4
14
  import { healthCheck } from './query/health.js';
5
15
  import { queryRun, unsentFields } from './query/query.js';
6
- const DEFAULT_HOSTS = { query: 'https://query.flipstream.io' };
16
+ const DEFAULT_HOSTS = {
17
+ query: 'https://query.flipstream.io',
18
+ auth: 'https://auth.flipstream.io',
19
+ // Today's admin and wallet APIs, behind #67's seam (D541, D544).
20
+ admin: 'https://pulse.admin.api.flipstream.io',
21
+ wallet: 'https://pulse.wallet.api.flipstream.io',
22
+ };
23
+ /**
24
+ * The console a hand-off names, with no trailing slash: the caller's own, checked here so a broken one
25
+ * fails when the client is made, or Flipstream's own once it's live, beside its own admin host
26
+ * (`consoleBase`). The caller's must be https, or http on this machine, since a person may click it
27
+ * (no `javascript:`), with no query, fragment or credentials, which a page's path can't follow or a
28
+ * person shouldn't be handed. The base is rebuilt from its origin and path alone. The error names
29
+ * the option, never its value.
30
+ */
31
+ function consoleOf(configured, adminIsDefault) {
32
+ if (configured === undefined)
33
+ return consoleBase(undefined, adminIsDefault);
34
+ const rule = 'an https URL, or http on this machine, with no query, fragment or credentials';
35
+ if (!URL.canParse(configured))
36
+ throw new TypeError(`createFlipstream: hosts.console must be ${rule}`);
37
+ const url = new URL(configured);
38
+ const secure = url.protocol === 'https:' || isLoopbackHttp(url);
39
+ if (!secure || url.search !== '' || url.hash !== '' || url.username !== '' || url.password !== '')
40
+ throw new TypeError(`createFlipstream: hosts.console must be ${rule}`);
41
+ return consoleBase(baseOf(`${url.origin}${url.pathname}`), adminIsDefault);
42
+ }
43
+ /** `host` with no trailing slash, so a path joins it with one. */
44
+ const baseOf = (host) => host.replace(/\/+$/, '');
45
+ /** The deadline for a call's reads together (`me`, `workspaces`, `wallet`), when the call gives none. */
46
+ const READS_DEADLINE_MS = 30_000;
47
+ const refused = (reason) => ({ ok: false, error: { kind: 'refused', reason } });
7
48
  /** The SDK, for one caller (D421): the console with its transport, or anyone with a token function. */
8
49
  export function createFlipstream(options) {
50
+ // First, so a client that can't say who it is throws before anything else is set up.
51
+ const identity = identityOf(options);
52
+ const node = nodeVersion();
53
+ // The values the caller gave, keyed by the Request that carries them, so the token function's
54
+ // transport can refuse one whose values hold the access token: only the transport has the token.
55
+ const carried = new WeakMap();
56
+ const holdsToken = (request, token) => (carried.get(request) ?? []).some((value) => valueHolds(value, token));
9
57
  const hosts = { ...DEFAULT_HOSTS, ...options.hosts };
58
+ // The calls whose requests are on their way, by each request's traceparent (one parent-id each),
59
+ // and by the function that builds them, so the result can say whether one went out (#251).
60
+ const outgoing = new Map();
61
+ const builders = new WeakMap();
62
+ // The SDK's own transports go through this fetch: a request it's handed went out, whether or
63
+ // not an answer comes back before the call ends.
64
+ const sentThrough = (send) => {
65
+ const inner = send ?? globalThis.fetch;
66
+ return (...args) => {
67
+ const sending = inner(...args);
68
+ const [input] = args;
69
+ if (input instanceof Request) {
70
+ const owner = outgoing.get(input.headers.get('traceparent') ?? '');
71
+ if (owner !== undefined)
72
+ owner.sent = true;
73
+ }
74
+ return sending;
75
+ };
76
+ };
77
+ // An app's own transport sends where the SDK can't see: an answer is the proof one went out.
78
+ const answeredThrough = (inner) => async (request) => {
79
+ const result = await inner(request);
80
+ const owner = builders.get(request.makeRequest);
81
+ if (result.kind === 'response' && owner !== undefined)
82
+ owner.sent = true;
83
+ return result;
84
+ };
10
85
  const transport = options.transport !== undefined
11
- ? options.transport
12
- : fetchTransport({ token: options.token, fetch: options.fetch, hosts, allowLocalhostHttp: options.allowLocalhostHttp });
13
- const base = (hosts.query ?? DEFAULT_HOSTS.query).replace(/\/+$/, '');
86
+ ? answeredThrough(options.transport)
87
+ : fetchTransport({ token: options.token, fetch: sentThrough(options.fetch), hosts, allowLocalhostHttp: options.allowLocalhostHttp, holdsToken });
88
+ const base = baseOf(hosts.query ?? DEFAULT_HOSTS.query);
89
+ const seam = backend({
90
+ admin: baseOf(hosts.admin ?? DEFAULT_HOSTS.admin),
91
+ auth: baseOf(hosts.auth ?? DEFAULT_HOSTS.auth),
92
+ console: consoleOf(hosts.console, hosts.admin === undefined || baseOf(hosts.admin) === DEFAULT_HOSTS.admin),
93
+ wallet: baseOf(hosts.wallet ?? DEFAULT_HOSTS.wallet),
94
+ });
14
95
  const warn = options.onWarning ?? consoleWarn;
15
96
  // For health alone (#111), whichever way the SDK was made: only the query host, so the call that
16
97
  // works signed out can go nowhere else.
17
- const signedOut = tokenlessTransport({ fetch: options.fetch, hosts: { query: base }, allowLocalhostHttp: options.allowLocalhostHttp });
18
- const send = (spec, { signal, deadlineMs } = {}, via = transport) => call(via, deadlineMs === undefined ? spec : { ...spec, deadlineMs }, signal);
98
+ const signedOut = tokenlessTransport({ fetch: sentThrough(options.fetch), hosts: { query: base }, allowLocalhostHttp: options.allowLocalhostHttp });
99
+ /**
100
+ * `spec` with what its requests say about who sent them, in Node: the SDK call's one trace, and a
101
+ * parent-id for each request, retries and a 401's resend included.
102
+ */
103
+ const identified = (spec, sender, traced) => {
104
+ const makeRequest = () => {
105
+ const built = spec.makeRequest();
106
+ const headers = new Headers(built.headers);
107
+ const header = traceparent(traced.trace);
108
+ headers.set('user-agent', sender.userAgent);
109
+ headers.set('traceparent', header);
110
+ if (sender.baggage !== undefined)
111
+ headers.set('baggage', sender.baggage);
112
+ const request = new Request(built, { headers });
113
+ carried.set(request, sender.values);
114
+ // A transport still running after its call ended (#57's late answers) builds nothing to remember.
115
+ if (!traced.ended) {
116
+ outgoing.set(header, traced);
117
+ traced.traceparents.push(header);
118
+ }
119
+ return request;
120
+ };
121
+ builders.set(makeRequest, traced);
122
+ return { ...spec, makeRequest };
123
+ };
124
+ /**
125
+ * One SDK call (#251): one trace for every request it makes, the caller's when its traceparent is
126
+ * one, and its result with that trace-id once a request carrying it went out.
127
+ */
128
+ const asOneCall = async (callOptions = {}, run) => {
129
+ const traced = { trace: traceFor(callOptions.traceparent), sent: false, ended: false, traceparents: [] };
130
+ try {
131
+ const result = await run(traced);
132
+ return traced.sent ? { ...result, traceId: traced.trace.traceId } : result;
133
+ }
134
+ finally {
135
+ traced.ended = true;
136
+ for (const header of traced.traceparents)
137
+ outgoing.delete(header);
138
+ }
139
+ };
140
+ /** One request of a call. `signedIn` is false for health alone, which says only who the SDK is (D535). */
141
+ const send = (spec, callOptions, traced, signedIn = true) => {
142
+ // One flag picks both the transport and what the request says: only the signed-in transport
143
+ // checks the caller's values against the token, so a signed-out call can never carry them.
144
+ const via = signedIn ? transport : signedOut;
145
+ const { signal, deadlineMs } = callOptions;
146
+ // Refused before anything is sent, like a field the call doesn't send: the token function isn't asked.
147
+ if (callOptions.conversationId !== undefined && !isConversationId(callOptions.conversationId))
148
+ return Promise.resolve(refused('conversation-id'));
149
+ const timed = deadlineMs === undefined ? spec : { ...spec, deadlineMs };
150
+ // The auth server never gets them, as sign-in sends none (#192): me()'s reads there go bare.
151
+ if (node === undefined || spec.service === 'auth')
152
+ return call(via, timed, signal);
153
+ const sender = signedIn ? callerSender(identity, callOptions.conversationId ?? identity.conversationId, node) : sdkSender(node);
154
+ return call(via, identified(timed, sender, traced), signal);
155
+ };
156
+ /** A call's reads under one deadline and one trace: each gets what is left of it (the call's, or 30 s). */
157
+ const readsUnder = (callOptions, traced) => {
158
+ const endsAt = Date.now() + (callOptions.deadlineMs ?? READS_DEADLINE_MS);
159
+ return (spec) => send(spec, { ...callOptions, deadlineMs: Math.max(0, endsAt - Date.now()) }, traced);
160
+ };
161
+ /** `reads` as one SDK call, under one deadline and one trace. */
162
+ const readsOf = (reads) => (callOptions = {}) => asOneCall(callOptions, (traced) => reads(readsUnder(callOptions, traced)));
19
163
  return {
20
164
  catalog: {
21
- list: (callOptions) => send(catalogList(base, warn), callOptions),
22
- get(request, callOptions) {
165
+ list: (callOptions = {}) => asOneCall(callOptions, (traced) => send(catalogList(base, warn), callOptions, traced)),
166
+ get(request, callOptions = {}) {
23
167
  const spec = catalogGet(base, request.source, warn);
24
168
  // Refused before anything is sent: the token function isn't asked, and nothing is spent.
25
- return spec === null ? Promise.resolve({ ok: false, error: { kind: 'refused' } }) : send(spec, callOptions);
169
+ return spec === null ? Promise.resolve(refused('path')) : asOneCall(callOptions, (traced) => send(spec, callOptions, traced));
26
170
  },
27
171
  },
28
172
  // Async, so a request that isn't an object rejects, as D436's do, rather than throwing here.
29
- async query(request, callOptions) {
173
+ async query(request, callOptions = {}) {
30
174
  const fields = unsentFields(request);
31
175
  // Refused before anything is sent, like catalog.get's: the token function isn't asked.
32
- return fields === null ? send(queryRun(base, request), callOptions) : { ok: false, error: { kind: 'refused', fields } };
176
+ return fields === null
177
+ ? asOneCall(callOptions, (traced) => send(queryRun(base, request), callOptions, traced))
178
+ : { ok: false, error: { kind: 'refused', reason: 'fields', fields } };
179
+ },
180
+ health: (callOptions = {}) => asOneCall(callOptions, (traced) => send(healthCheck(base), callOptions, traced, false)),
181
+ me: readsOf((read) => readMe(seam, read)),
182
+ organization: {
183
+ rename: async (_request, callOptions) => readsOf((read) => handOffOrganizationRename(seam, read))(callOptions),
184
+ },
185
+ workspaces: {
186
+ // Async, so a page that isn't one rejects, as D436's do, rather than throwing here.
187
+ list: async (request = {}, callOptions) => readsOf((read) => readWorkspaces(seam, read, request))(callOptions),
188
+ get: async (request, callOptions) => readsOf((read) => readWorkspace(seam, read, request.workspace_id))(callOptions),
189
+ create: async (request, callOptions) => readsOf((send) => createWorkspace(seam, send, request))(callOptions),
190
+ rename: async (request, callOptions) => readsOf((read) => handOffWorkspace(seam, read, request.workspace_id, 'workspace.rename'))(callOptions),
191
+ delete: async (request, callOptions) => readsOf((read) => handOffWorkspace(seam, read, request.workspace_id, 'workspace.delete'))(callOptions),
192
+ },
193
+ log: {
194
+ list: (request, callOptions) => readsOf((read) => listLog(seam, read, request))(callOptions),
195
+ get: (request, callOptions) => readsOf((read) => getLog(seam, read, request))(callOptions),
196
+ add: async (request, callOptions) => readsOf((send) => addLog(seam, send, request))(callOptions),
197
+ update: async (request, callOptions) => readsOf((send) => updateLog(seam, send, request))(callOptions),
198
+ remove: async (request, callOptions) => readsOf((send) => removeLog(seam, send, request))(callOptions),
199
+ },
200
+ datasets: {
201
+ list: (request, callOptions) => readsOf((read) => listDatasets(seam, read, request))(callOptions),
202
+ get: (request, callOptions) => readsOf((read) => getDataset(seam, read, request))(callOptions),
203
+ pause: async (request, callOptions) => readsOf((send) => pauseDataset(seam, send, request))(callOptions),
204
+ resume: async (request, callOptions) => readsOf((send) => resumeDataset(seam, send, request))(callOptions),
205
+ delete: async (request, callOptions) => readsOf((read) => handOffDatasetDelete(seam, read, request))(callOptions),
206
+ },
207
+ datasetTypes: {
208
+ list: () => Promise.resolve(listDatasetTypes()),
209
+ },
210
+ credentials: {
211
+ // Async, so a page that isn't one rejects, as D436's do, rather than throwing here.
212
+ list: async (request = {}, callOptions) => readsOf((read) => listCredentials(seam, read, request))(callOptions),
213
+ revoke: async (request, callOptions) => readsOf((read) => handOffCredential(seam, read, request.credential_id, 'credential.revoke'))(callOptions),
214
+ delete: async (request, callOptions) => readsOf((read) => handOffCredential(seam, read, request.credential_id, 'credential.delete'))(callOptions),
215
+ },
216
+ wallet: {
217
+ balance: readsOf((read) => readBalance(seam, read)),
218
+ transactions: readsOf((read) => readLedger(seam, read)),
219
+ },
220
+ members: {
221
+ // Async, so a page that isn't one rejects, as D436's do, rather than throwing here.
222
+ list: async (request = {}, callOptions) => readsOf((read) => listMembers(seam, read, request))(callOptions),
223
+ get: (request, callOptions) => readsOf((read) => getMember(seam, read, request))(callOptions),
33
224
  },
34
- health: (callOptions) => send(healthCheck(base), callOptions, signedOut),
35
225
  };
36
226
  }
37
227
  //# sourceMappingURL=flipstream.js.map