techne-sdk 0.3.3 → 0.3.8

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 (66) hide show
  1. package/CHANGELOG.md +160 -0
  2. package/README.md +5 -4
  3. package/dist/auth/app-key.js +2 -2
  4. package/dist/auth/index.js +1 -1
  5. package/dist/catalog/catalog.gen.d.ts +24 -24
  6. package/dist/catalog/catalog.gen.js +356 -66
  7. package/dist/connect.d.ts +96 -12
  8. package/dist/connect.js +83 -12
  9. package/dist/generated/lexicons.d.ts +140 -36
  10. package/dist/generated/lexicons.js +77 -18
  11. package/dist/generated/nsid.d.ts +4 -0
  12. package/dist/generated/nsid.js +4 -0
  13. package/dist/generated/nsid.json +4 -0
  14. package/dist/generated/types/pub/techne/draft/beacon.d.ts +2 -0
  15. package/dist/generated/types/pub/techne/draft/collective.d.ts +1 -1
  16. package/dist/generated/types/pub/techne/draft/identity/attest.d.ts +3 -0
  17. package/dist/generated/types/pub/techne/draft/identity/attest.js +7 -0
  18. package/dist/generated/types/pub/techne/draft/identity/beginAddMethod.d.ts +3 -0
  19. package/dist/generated/types/pub/techne/draft/identity/beginAddMethod.js +7 -0
  20. package/dist/generated/types/pub/techne/draft/identity/finishAddMethod.d.ts +6 -0
  21. package/dist/generated/types/pub/techne/draft/identity/finishAddMethod.js +14 -0
  22. package/dist/generated/types/pub/techne/draft/identity/resolve.d.ts +6 -0
  23. package/dist/generated/types/pub/techne/draft/membership.d.ts +1 -1
  24. package/dist/generated/types/pub/techne/draft/permissioned/deleteRecord.d.ts +3 -0
  25. package/dist/generated/types/pub/techne/draft/permissioned/deleteRecord.js +7 -0
  26. package/dist/generated/types/pub/techne/draft/permissioned/putRecord.d.ts +3 -0
  27. package/dist/generated/types/pub/techne/draft/permissioned/putRecord.js +7 -0
  28. package/dist/generated/types/pub/techne/draft/permissioned/putRecords.d.ts +4 -1
  29. package/dist/generated/types/pub/techne/draft/permissioned/putRecords.js +7 -0
  30. package/dist/generated/types/pub/techne/draft/summary.d.ts +2 -0
  31. package/dist/item.d.ts +130 -0
  32. package/dist/item.js +74 -0
  33. package/dist/links/index.d.ts +15 -8
  34. package/dist/links/index.js +18 -0
  35. package/dist/put/contacts.d.ts +2 -2
  36. package/dist/put/index.d.ts +6 -9
  37. package/dist/put/index.js +8 -166
  38. package/dist/put/put.d.ts +14 -0
  39. package/dist/put/put.js +206 -0
  40. package/dist/query/compose.d.ts +246 -0
  41. package/dist/query/compose.js +572 -0
  42. package/dist/query/hosts.d.ts +98 -1
  43. package/dist/query/hosts.js +143 -2
  44. package/dist/query/index.d.ts +60 -29
  45. package/dist/query/index.js +55 -26
  46. package/dist/react/context.d.ts +18 -0
  47. package/dist/react/context.js +19 -0
  48. package/dist/react/index.d.ts +14 -0
  49. package/dist/react/index.js +14 -0
  50. package/dist/react/query.d.ts +75 -0
  51. package/dist/react/query.js +116 -0
  52. package/dist/read/index.d.ts +60 -0
  53. package/dist/read/index.js +107 -0
  54. package/dist/resend.d.ts +25 -0
  55. package/dist/resend.js +52 -0
  56. package/dist/versions/entities.gen.d.ts +8 -0
  57. package/dist/versions/entities.gen.js +9 -0
  58. package/dist/versions/index.d.ts +59 -17
  59. package/dist/versions/index.js +156 -22
  60. package/dist/versions/react.d.ts +1 -1
  61. package/dist/versions/react.js +1 -1
  62. package/dist/versions/text.d.ts +19 -0
  63. package/dist/versions/text.js +73 -0
  64. package/dist/wasm/techne-wasm.gen.js +1 -1
  65. package/dist/wasm/techne-wasm2js.gen.js +8928 -8927
  66. package/package.json +17 -5
@@ -0,0 +1,206 @@
1
+ /**
2
+ * `put`: the write behind `techne-sdk/put`'s `put` and the client's `techne.put` (see `./index` for what
3
+ * it does). Internal: `./index` re-exports it.
4
+ */
5
+ import { ResponseType, XRPCError } from '@atproto/xrpc';
6
+ import { NSID } from '../generated/nsid.js';
7
+ import { getRecordsAnswer } from '../query/index.js';
8
+ import { contactConflicts, ContactConflictError, storedContacts } from './contacts.js';
9
+ import { plan, planWithDeletes, PutError } from './index.js';
10
+ /** How often `wait` asks for the curated record. */
11
+ const WAIT_POLL_MS = 250;
12
+ /**
13
+ * `put(techne, records, options)`: the write (`./index` says what it does), on what `connect` returns
14
+ * (`techne.put` is this) or on any {@link Clients}. Runs once more from the start when a call got no
15
+ * response; it re-reads first, so nothing lands twice and nothing is deleted twice. Only the writes and
16
+ * deletes throw: the hints and the curation read already answer `pending`.
17
+ */
18
+ export async function put(techne, records, options = {}) {
19
+ // The deletes the first run sent: one whose answer was lost may already have happened, so the second run
20
+ // answers it deleted when it finds the record gone.
21
+ const sent = { pds: new Set(), platform: new Set() };
22
+ try {
23
+ return await putOnce(techne, records, options, sent);
24
+ }
25
+ catch (e) {
26
+ if (!gotNoResponse(e))
27
+ throw e;
28
+ return putOnce(techne, records, options, { pds: new Set(), platform: new Set() }, sent);
29
+ }
30
+ }
31
+ /** Why a private record with no space is not written (lucian 2026-11-09): the platform's private write
32
+ * needs a space, as `permissioned.putRecords` refuses a write that names none. */
33
+ function noSpace(did, collection) {
34
+ return `names no space. ${collection} is written privately, so name the space it is for: a record only you read goes in your own space.personal (at://${did}/${NSID.SPACE_PERSONAL}/self). Public records are written with com.atproto.repo.putRecord.`;
35
+ }
36
+ /** `@atproto/xrpc` wraps what `fetch` threw as an `XRPCError` with no status and it as the cause: a
37
+ * `TypeError` from the fetch layer (no response, or a body cut off), never an abort. */
38
+ function gotNoResponse(e) {
39
+ return e instanceof XRPCError && e.status === ResponseType.Unknown && e.cause instanceof TypeError;
40
+ }
41
+ /** One run of `put`. It notes in `sent` each delete it may have carried out; `sentBefore` is the earlier
42
+ * run's, so a delete found already gone that the earlier run may have carried out answers deleted. A lost
43
+ * call may also never have arrived: the PDS is read again first, and a platform delete is always sent
44
+ * again, so only one that now finds nothing there answers from `sentBefore` (deleted, even in the rare
45
+ * case that the lost call never arrived and nothing was there to begin with). */
46
+ async function putOnce(techne, records, options, sent, sentBefore) {
47
+ const { did } = techne;
48
+ const { writes, deletes } = options.deletes?.length ? planWithDeletes(did, records, options.deletes) : { writes: plan(did, records), deletes: [] };
49
+ const privates = writes.filter((w) => w.private);
50
+ const publics = writes.filter((w) => !w.private);
51
+ const publicDeletes = deletes.filter((d) => !d.private);
52
+ const unplaced = privates.filter((w) => !w.space);
53
+ if (unplaced.length)
54
+ throw new PutError(unplaced.map((w) => ({ uri: w.uri, error: 'InvalidRequest', message: noSpace(did, w.collection) })));
55
+ const contacts = writes.filter((w) => w.collection === NSID.CONTACT);
56
+ if (contacts.length) {
57
+ const gone = new Set(deletes.map((d) => d.uri));
58
+ const conflicts = contactConflicts((await storedContacts(techne)).filter((c) => !gone.has(c.uri)), contacts);
59
+ if (conflicts.length)
60
+ throw new ContactConflictError(conflicts);
61
+ }
62
+ if (privates.length) {
63
+ const { data } = await techne.platform.pub.techne.draft.permissioned.putRecords({
64
+ repo: did,
65
+ // Every private write names its space (checked above).
66
+ writes: privates.map(({ collection, rkey, record, space }) => ({ collection, rkey, record, space: space })),
67
+ });
68
+ const refused = data.results.flatMap((r, i) => r.$type === NSID.PERMISSIONED_PUT_RECORDS_WRITE_ERROR ? [{ uri: privates[i].uri, error: String(r.error), message: String(r.message) }] : []);
69
+ if (refused.length)
70
+ throw new PutError(refused);
71
+ }
72
+ const done = new Map();
73
+ // The public deletes the PDS carried out in the applyWrites, hinted after (step 5).
74
+ const pdsDeleted = [];
75
+ if (publics.length || publicDeletes.length) {
76
+ const current = await Promise.all(publics.map((w) => currentCid(techne, w)));
77
+ const ops = publics.flatMap((w, i) => current[i] === w.cid
78
+ ? []
79
+ : [{ $type: current[i] ? 'com.atproto.repo.applyWrites#update' : 'com.atproto.repo.applyWrites#create', collection: w.collection, rkey: w.rkey, value: w.record }]);
80
+ // A record already gone is not deleted again (a PDS refuses deleting what it does not hold).
81
+ const held = await Promise.all(publicDeletes.map((d) => currentCid(techne, d)));
82
+ for (const [i, d] of publicDeletes.entries()) {
83
+ if (!held[i])
84
+ continue;
85
+ ops.push({ $type: 'com.atproto.repo.applyWrites#delete', collection: d.collection, rkey: d.rkey });
86
+ sent.pds.add(d.uri);
87
+ }
88
+ if (ops.length)
89
+ await techne.pds.com.atproto.repo.applyWrites({ repo: did, writes: ops });
90
+ for (const [i, d] of publicDeletes.entries()) {
91
+ // Gone now, and deleted by the earlier run whose applyWrites answer was lost: deleted, and hinted.
92
+ if (!held[i] && !sentBefore?.pds.has(d.uri))
93
+ continue;
94
+ done.set(d.uri, true);
95
+ pdsDeleted.push(d.uri);
96
+ }
97
+ }
98
+ // Private deletes after the public leg: nothing public is left pointing at what they remove. A public
99
+ // delete the PDS did not hold goes here too: a record of that collection kept in a space, named
100
+ // without it. Deleting what is already gone answers without a `rev`, so a retry is a no-op.
101
+ for (const d of deletes) {
102
+ if (done.has(d.uri))
103
+ continue;
104
+ const { data } = await techne.platform.pub.techne.draft.permissioned.deleteRecord({ repo: did, collection: d.collection, rkey: d.rkey }).catch((e) => {
105
+ sent.platform.add(d.uri);
106
+ throw e;
107
+ });
108
+ if (data.rev)
109
+ sent.platform.add(d.uri);
110
+ // No `rev`: nothing was there, unless the earlier run's delete took it.
111
+ done.set(d.uri, Boolean(data.rev) || Boolean(sentBefore?.platform.has(d.uri)));
112
+ }
113
+ const answers = new Map();
114
+ // The public deletes are hinted beside the writes; their answer is not kept (a failed hint is ignored).
115
+ await Promise.all([...publics.map(async (w) => answers.set(w, await hint(techne, w.uri))), ...pdsDeleted.map((uri) => hint(techne, uri))]);
116
+ const curatable = publics.filter((w) => w.lexicon);
117
+ const counted = (curatable.length ? curatable : publics).map((w) => answers.get(w));
118
+ const status = counted.find((a) => a.decision === 'invalid') ?? { decision: 'pending' };
119
+ const result = {
120
+ writes: writes.map((w) => (answers.has(w) ? { ...w, status: answers.get(w) } : w)),
121
+ deletes: deletes.map((d) => ({ ...d, deleted: done.get(d.uri) ?? false })),
122
+ status,
123
+ curation: [],
124
+ };
125
+ // Only a record with a lexicon the platform did not refuse can come back curated.
126
+ const waitable = curatable.filter((w) => answers.get(w).decision !== 'invalid');
127
+ if (options.wait && waitable.length) {
128
+ const curated = await curatedOf(techne, waitable, Date.now() + options.wait);
129
+ if (curated)
130
+ result.curated = curated;
131
+ }
132
+ const { read, error } = await curationRead(techne, publics);
133
+ result.curation = curationOfWrites(publics, answers, read);
134
+ if (error !== undefined)
135
+ result.curationError = error;
136
+ return result;
137
+ }
138
+ /** The platform's curation answer for `publics`, by URI, through {@link getRecordsAnswer} (which keeps each
139
+ * GET under the length a proxy takes), and why it failed when it did (then nothing is read). */
140
+ async function curationRead(techne, publics) {
141
+ if (!publics.length)
142
+ return { read: new Map() };
143
+ const reader = async (nsid, params) => (await techne.platform.call(nsid, params)).data;
144
+ try {
145
+ return { read: (await getRecordsAnswer(reader, publics.map((w) => w.uri), { curation: true })).curation };
146
+ }
147
+ catch (error) {
148
+ return { read: new Map(), error };
149
+ }
150
+ }
151
+ /** The curation status of each of `publics` (see {@link PutResult.curation}). */
152
+ function curationOfWrites(publics, answers, read) {
153
+ return publics.map((w) => {
154
+ const known = read.get(w.uri);
155
+ const hinted = answers.get(w);
156
+ // A refusal the hint answered stands over a read that has not caught up with it.
157
+ if (hinted?.decision === 'invalid' && (!known || known.status === 'pending'))
158
+ return { uri: w.uri, status: 'notCurated', reason: hinted.reason };
159
+ return known ?? { uri: w.uri, status: 'pending' };
160
+ });
161
+ }
162
+ /** The CID the user's repo holds at `w`'s key, or `null` when it holds nothing there. */
163
+ async function currentCid(techne, w) {
164
+ try {
165
+ const { data } = await techne.pds.com.atproto.repo.getRecord({ repo: techne.did, collection: w.collection, rkey: w.rkey });
166
+ return data.cid ?? null;
167
+ }
168
+ catch (e) {
169
+ if (e instanceof XRPCError && e.error === 'RecordNotFound')
170
+ return null;
171
+ throw e;
172
+ }
173
+ }
174
+ /**
175
+ * The hint for one public record. `indexRecord` answers `decide` for the version it indexed (P1.4:
176
+ * `valid`, `pending` or `invalid` with a reason); a version the platform refused answers `invalid`.
177
+ * Anything it did not refuse answers `pending` here: `put` is pending until its curated version lands
178
+ * (`wait`). Any other failure (rate limit, an unreachable PDS) answers `pending`: the relay brings the
179
+ * record anyway.
180
+ */
181
+ async function hint(techne, uri) {
182
+ try {
183
+ const { data } = await techne.platform.pub.techne.draft.sync.indexRecord({ uri });
184
+ if (data.decision === 'invalid')
185
+ return { decision: 'invalid', reason: data.reason ?? 'invalid' };
186
+ return { decision: 'pending' };
187
+ }
188
+ catch {
189
+ return { decision: 'pending' };
190
+ }
191
+ }
192
+ /** The first curated record that names one of `sources`, at the CID just written, as its source. */
193
+ async function curatedOf(techne, sources, deadline) {
194
+ for (;;) {
195
+ for (const source of sources) {
196
+ const answer = await techne.platform.pub.techne.draft.commons.getVersions({ uri: source.uri }).then((r) => r.data, () => null);
197
+ if (answer && answer.uri !== source.uri && answer.versions.some((v) => v.uri === source.uri && v.cid === source.cid)) {
198
+ return { uri: answer.uri, cid: answer.cid, record: answer.record };
199
+ }
200
+ }
201
+ const left = deadline - Date.now();
202
+ if (left <= 0)
203
+ return undefined;
204
+ await new Promise((r) => setTimeout(r, Math.min(WAIT_POLL_MS, left)));
205
+ }
206
+ }
@@ -0,0 +1,246 @@
1
+ /**
2
+ * `pub.techne.draft.query.composeRecords` from the SDK (CQ2): several generic reads in one request, where a
3
+ * later step reads what an earlier one answered.
4
+ *
5
+ * A composition is built step by step and sent once:
6
+ *
7
+ * ```ts
8
+ * const c = compose()
9
+ * const events = c.list('events', FEED_QUERY, { params, limit: 500 })
10
+ * const prov = c.list('prov', PROVENANCE_OF_QUERY, { params: { uris: events.uri }, fields: ['subject', 'sources[].uri'] })
11
+ * const sources = c.get('sources', [prov.value('sources[].uri')], { fields: ENVELOPE_ONLY })
12
+ * const answer = await c.run(post)
13
+ * answer.list(events).records // a ListPage
14
+ * answer.records(sources) // a Map by URI
15
+ * ```
16
+ *
17
+ * A step's handle names its outputs as bindings (`events.uri` is the string `"$events.uri"`), which the platform
18
+ * fills with that output over the earlier step's answered records (deduplicated, first seen first; possibly
19
+ * empty). A step asked with `return: false` only feeds later bindings: its handle cannot be read from the answer
20
+ * (a compile error). `fields` keeps only those value paths of each answered record, and `envelope` only those of
21
+ * its own fields ({@link ENVELOPE_FIELDS}: `cid`, `author` and the rest; every field without it); `uri: false`
22
+ * drops the record's `uri` too (`value` always comes back); bindings read the whole record, before any cut.
23
+ *
24
+ * The platform's rules (the lexicon's): at most {@link MAX_STEPS} steps; a list step's `limit` at most
25
+ * {@link MAX_STEP_LIMIT} (past {@link MAX_PAGE}, one `listRecords` page, the platform reads the pages and answers
26
+ * them as one); a `return: false` list step is read in full (no `limit` or `cursor`; past {@link MAX_STEP_LIMIT}
27
+ * records it fails); a get step at most {@link MAX_STEP_URIS} URIs after binding; a bound list at most
28
+ * {@link MAX_BOUND} values; at most {@link MAX_TOTAL_RECORDS} records across the one-page steps. A signed-in caller spends
29
+ * a row budget: `run` waits and asks again on `RateLimitExceeded` ({@link COMPOSE_RETRIES} times).
30
+ *
31
+ * **A failed step fails alone.** The platform answers a step it could not answer in `errors` (its own error, or
32
+ * `Skipped` when a step it binds to failed) and every other step as usual. A step built with `optional: true` (a
33
+ * decoration a screen can show without) then reads as empty, and {@link Composed.failed} names its error; any
34
+ * other step's failure fails `run` with that step's error ({@link ComposeStepError}), as a failed call would.
35
+ *
36
+ * The wire is plain data, so a stand-in platform can answer it from its own `listRecords` and `getRecords`
37
+ * ({@link emulateCompose}): tests and Beacon's dev seed do. It is not the platform: `references` and `subject`
38
+ * come from the type's query document there; the emulation reads `subject` (or `subject.uri`) and refuses
39
+ * `references`.
40
+ */
41
+ import type * as ComposeRecords from '../generated/types/pub/techne/draft/query/composeRecords.js';
42
+ import { type ListPage, type Param, type Query, type QueryReader, type QueryRecord, type RecordsAnswer } from './index.js';
43
+ /** The one call {@link Composition.run} makes: a POST of a platform procedure with a JSON body. */
44
+ export type ComposePoster = <T>(nsid: string, body: unknown) => Promise<T>;
45
+ /**
46
+ * A {@link QueryReader} that also carries the POST its compositions run on: `compose()…run(read.post)`. An app
47
+ * holds one (its GET and its POST over the same session); a test's stand-in gets one from {@link testReader}.
48
+ * Anything that takes a `QueryReader` takes a `Reader`.
49
+ */
50
+ export type Reader = QueryReader & {
51
+ readonly post: ComposePoster;
52
+ };
53
+ /** `read` as a {@link Reader} posting with `post` (a new function: `read` itself is left alone). */
54
+ export declare function withPost(read: QueryReader, post: ComposePoster): Reader;
55
+ /** A stand-in platform's GET reader as a {@link Reader} whose POST answers `composeRecords` from those GETs
56
+ * ({@link emulateCompose}): for tests and dev seeds, never the platform. */
57
+ export declare function testReader(read: QueryReader, otherwise?: ComposePoster): Reader;
58
+ /** The most steps one call takes. */
59
+ export declare const MAX_STEPS = 12;
60
+ /** The most URIs one get step names, after its bindings are filled in. */
61
+ export declare const MAX_STEP_URIS = 500;
62
+ /** The most values one bound list holds. */
63
+ export declare const MAX_BOUND = 2000;
64
+ /** The most records the one-page steps of one call answer in all: a list step whose `limit` is over
65
+ * {@link MAX_PAGE}, or that is `return: false`, is not counted (its own limit bounds it). */
66
+ export declare const MAX_TOTAL_RECORDS = 3000;
67
+ /** The largest `limit` a list step takes, and the most records a `return: false` list step (read in full) may
68
+ * match before it fails. A step read for all of its records asks this and treats a cursor back as an error. */
69
+ export declare const MAX_STEP_LIMIT = 50000;
70
+ /** How many times `run` asks again after the platform answers `RateLimitExceeded` (the row budget refills at
71
+ * 500 rows a second, and a call is charged at most 3000). */
72
+ export declare const COMPOSE_RETRIES = 2;
73
+ /** `fields` that keep no value: a path no record has. For a step whose records count only by being there
74
+ * (a host's live source). */
75
+ export declare const ENVELOPE_ONLY: readonly string[];
76
+ /** The record fields a step's `envelope` may keep besides `uri` (kept unless the step asks `uri: false`) and
77
+ * `value` (always kept). */
78
+ export declare const ENVELOPE_FIELDS: readonly ["cid", "author", "collection", "time", "space", "source", "curated", "held", "measures", "signals"];
79
+ export type EnvelopeField = (typeof ENVELOPE_FIELDS)[number];
80
+ declare const envelopeOf: unique symbol;
81
+ declare const uriOf: unique symbol;
82
+ /** `'uri'` when a step keeps its records' `uri` (`U` true, the default), else nothing: a step asked `uri: false`,
83
+ * or one that may have been (`U` boolean), answers records without it. */
84
+ type KeptUri<U extends boolean> = [U] extends [true] ? 'uri' : never;
85
+ /** A record as a step with envelope `E` answers it: `uri` (unless `U` is false: the step asked `uri: false`),
86
+ * `value` (cut to the step's `fields`, which the type does not follow) and the `E` fields. Without an envelope
87
+ * `E` is every field: listRecords' record.
88
+ * It carries the same marker as {@link StepRef}: a record kept with more fields stands in for one needing fewer,
89
+ * never the other way, even when the field left out is optional (`space`, `curated`, `held`, `measures`,
90
+ * `signals`), which `Pick` alone would let through. A whole {@link QueryRecord} (no marker) stands in for any. */
91
+ export type StepRecord<V = Record<string, unknown>, E extends EnvelopeField = EnvelopeField, U extends boolean = true> = Pick<QueryRecord<V>, KeptUri<U> | 'value' | E> & {
92
+ /** Type only, never set: the record fields the step kept. */
93
+ readonly [envelopeOf]?: (field: E) => void;
94
+ };
95
+ /** A step's output another step can bind: the record's `uri`, `author`, `type` (collection), `space`,
96
+ * `subject` and `references` roles, or the strings at a value path. */
97
+ export type Output = 'uri' | 'author' | 'type' | 'space' | 'subject' | 'references' | `value.${string}`;
98
+ /** `"$<step>.<output>"`: an earlier step's output, filled in by the platform. */
99
+ export type Binding = `$${string}.${string}` & {
100
+ readonly __binding?: true;
101
+ };
102
+ /** A list step's param: a literal (string or list), or bindings, alone or mixed with literal strings in a list. */
103
+ export type StepParam = Param | Binding | readonly (string | Binding)[];
104
+ /** A step, by name, for later steps' bindings and for reading its answer. `R` is whether it is returned; `E` the
105
+ * record fields its envelope keeps (every one without an envelope), so a field it left out cannot be read; `U`
106
+ * whether its records keep their `uri` (false: asked `uri: false`; its `uri` binding still reads it). */
107
+ export interface StepRef<K extends 'list' | 'get', R extends boolean, E extends EnvelopeField = EnvelopeField, U extends boolean = true> {
108
+ readonly name: string;
109
+ readonly kind: K;
110
+ readonly returned: R;
111
+ readonly uri: Binding;
112
+ readonly author: Binding;
113
+ readonly type: Binding;
114
+ readonly space: Binding;
115
+ readonly subject: Binding;
116
+ readonly references: Binding;
117
+ /** The strings at `path` (`a`, `a.b`, `a[].b`) of each record's value. */
118
+ value(path: string): Binding;
119
+ /** Type only, never set: what the step's records keep. A function of `E` so a step keeping more fields stands in
120
+ * for one needing fewer, never the other way. Keyed by a private symbol, so no caller can name or set it. */
121
+ readonly [envelopeOf]?: (field: E) => void;
122
+ /** Type only, never set: whether the step's records keep their `uri`. A step that keeps it stands in for one
123
+ * read as maybe not (`U` boolean), never for one read as keeping it when it does not. */
124
+ readonly [uriOf]?: U;
125
+ }
126
+ interface Common<R extends boolean, E extends EnvelopeField, U extends boolean> {
127
+ /** False: the step's records only feed later bindings and are not in the answer. A list step so asked is read
128
+ * in full on the platform (no `limit` or `cursor`; past {@link MAX_STEP_LIMIT} records it fails). */
129
+ return?: R;
130
+ /** Value paths each answered record keeps (1 to 32). */
131
+ fields?: readonly string[];
132
+ /** The record fields each answered record keeps besides `uri` and `value` (every one when absent; `[]`: none).
133
+ * One left out is absent from the answer, so a flag (`curated`, `held`) left out reads as false. */
134
+ envelope?: readonly E[];
135
+ /** False: each answered record comes back without its own `uri` (default true). Bindings still read it, before
136
+ * the cut. For a step whose records nobody reads by URI. */
137
+ uri?: U;
138
+ /** The caller can do without this step: if it fails (or is skipped for a step it binds to), it reads as
139
+ * empty and `failed` names why, instead of failing `run`. The client's own; never sent. */
140
+ optional?: boolean;
141
+ }
142
+ export interface ListStepOpts<R extends boolean, E extends EnvelopeField = EnvelopeField, U extends boolean = true> extends Common<R, E, U> {
143
+ params?: Readonly<Record<string, StepParam>>;
144
+ /** How many records, up to {@link MAX_STEP_LIMIT}: past {@link MAX_PAGE} the platform reads `listRecords`' pages
145
+ * and answers them as one, with the cursor after the last. Not on a `return: false` step. */
146
+ limit?: number;
147
+ cursor?: string;
148
+ count?: boolean;
149
+ /** The handles of the repos of the records the page answers, as getRecords' `handles`. */
150
+ handles?: boolean;
151
+ /** The viewer's own records' curation status among those the page answers, as getRecords' `curation`. */
152
+ curation?: boolean;
153
+ }
154
+ export interface GetStepOpts<R extends boolean, E extends EnvelopeField = EnvelopeField, U extends boolean = true> extends Common<R, E, U> {
155
+ curated?: boolean;
156
+ handles?: boolean;
157
+ curation?: boolean;
158
+ defaults?: boolean;
159
+ includeBlockedByMe?: boolean;
160
+ }
161
+ /** One step as it goes on the wire (`pub.techne.draft.query.composeRecords#step`). */
162
+ export type WireStep = Record<string, unknown> & {
163
+ name: string;
164
+ };
165
+ /** Why a step has no result (`pub.techne.draft.query.composeRecords#stepError`): the error its method would
166
+ * answer alone, or `Skipped` (424) when a step it binds to failed. */
167
+ export type StepError = Omit<ComposeRecords.StepError, '$type'>;
168
+ /** A list step's page as `composeRecords` answers it: listRecords' page of {@link StepRecord}s, and asked with
169
+ * `handles` or `curation`, getRecords' for the records it answered (`#stepResult`). */
170
+ export interface ListStepPage<V = Record<string, unknown>, E extends EnvelopeField = EnvelopeField, U extends boolean = true> extends Omit<ListPage<V>, 'records'> {
171
+ records: StepRecord<V, E, U>[];
172
+ handles?: ComposeRecords.StepResult['handles'];
173
+ curation?: ComposeRecords.StepResult['curation'];
174
+ }
175
+ /** A required step of a composed call failed: the call fails as that step's method would have alone. */
176
+ export declare class ComposeStepError extends Error {
177
+ readonly step: string;
178
+ readonly status: number;
179
+ readonly error: string;
180
+ constructor(step: string, e: StepError);
181
+ }
182
+ /** What {@link Composition.run} takes beside the poster. */
183
+ export interface RunOpts {
184
+ /** How `run` waits before asking again after `RateLimitExceeded` (default: a timer). */
185
+ sleep?: (ms: number) => Promise<void>;
186
+ }
187
+ /** One `composeRecords` call being built. Steps run in the order added; a binding names only an earlier step. */
188
+ export declare class Composition {
189
+ #private;
190
+ /** The steps as they will be sent. */
191
+ get steps(): readonly WireStep[];
192
+ /** A list step: one `listRecords` page of `query` (inline, or a published query's AT-URI). */
193
+ list<R extends boolean = true, E extends EnvelopeField = EnvelopeField, U extends boolean = true>(name: string, query: Query | {
194
+ queryUri: string;
195
+ }, opts?: ListStepOpts<R, E, U>): StepRef<'list', R, E, U>;
196
+ /** A get step: `getRecords` of `uris`, each an AT-URI or a binding (deduplicated). No URIs (or only bindings
197
+ * to left-out steps): the step is left out and answers nothing. */
198
+ get<R extends boolean = true, E extends EnvelopeField = EnvelopeField, U extends boolean = true>(name: string, uris: Iterable<string | Binding>, opts?: GetStepOpts<R, E, U>): StepRef<'get', R, E, U>;
199
+ /** Send the steps (one POST) and answer them. No steps left (every one could ask for nothing): no call.
200
+ * A step that failed and is not `optional` fails the run ({@link ComposeStepError}, the first in step
201
+ * order). `sleep` waits between retries (a test's stand-in; a timer otherwise). */
202
+ run(post: ComposePoster, opts?: RunOpts): Promise<Composed>;
203
+ }
204
+ /** A get step's answer: {@link RecordsAnswer} of {@link StepRecord}s. */
205
+ export type StepAnswer<V = Record<string, unknown>, E extends EnvelopeField = EnvelopeField> = Omit<RecordsAnswer<V>, 'records'> & {
206
+ records: Map<string, StepRecord<V, E>>;
207
+ };
208
+ /** A new composition. */
209
+ export declare const compose: () => Composition;
210
+ /** A `composeRecords` answer, read by step. A step left out (it could ask for nothing), or an optional one
211
+ * that failed, answers empty. */
212
+ export declare class Composed {
213
+ #private;
214
+ constructor(steps: Record<string, unknown>, errors?: Record<string, StepError>);
215
+ /** Why `step` has no answer: its error, or `Skipped` for a step it binds to; `undefined` when it answered
216
+ * (or was left out unsent). Only an `optional` step can have failed in an answer `run` gave. */
217
+ failed(step: {
218
+ readonly name: string;
219
+ }): StepError | undefined;
220
+ /** A list step's page: its records (value cut to its `fields`), cursor and, asked with `count`, count; asked
221
+ * with `handles` or `curation`, those of the records it answered. */
222
+ list<V = Record<string, unknown>, E extends EnvelopeField = EnvelopeField, U extends boolean = true>(step: StepRef<'list', true, E, U>): ListStepPage<V, E, U>;
223
+ /** A list step asked {@link MAX_STEP_LIMIT} records: all of them. A step that still answers a cursor matched more
224
+ * than the platform reads in one step, and this throws (`<who>: step <name> matched more than …`) rather than
225
+ * answer part of the list. */
226
+ whole<V = Record<string, unknown>, E extends EnvelopeField = EnvelopeField, U extends boolean = true>(step: StepRef<'list', true, E, U>, who: string): StepRecord<V, E, U>[];
227
+ /** A get step's answer as `getRecordsAnswer` gives it: records by URI, withheld, handles, curation, defaults.
228
+ * Only a step that keeps its records' `uri`. */
229
+ answer<V = Record<string, unknown>, E extends EnvelopeField = EnvelopeField>(step: StepRef<'get', true, E, true>): StepAnswer<V, E>;
230
+ /** A step's records in the order answered, list or get: the read for a step asked `uri: false`, whose records
231
+ * {@link records} cannot key by URI. */
232
+ answered<V = Record<string, unknown>, E extends EnvelopeField = EnvelopeField, U extends boolean = true>(step: StepRef<'list' | 'get', true, E, U>): StepRecord<V, E, U>[];
233
+ /** A step's records by URI: only a step that keeps their `uri`. */
234
+ records<V = Record<string, unknown>, E extends EnvelopeField = EnvelopeField>(step: StepRef<'list' | 'get', true, E, true>): Map<string, StepRecord<V, E>>;
235
+ }
236
+ /** `value` keeping only what `paths` reach (the platform's `select`). */
237
+ export declare function selectFields(value: unknown, paths: readonly string[]): Record<string, unknown>;
238
+ /**
239
+ * A {@link ComposePoster} that answers `composeRecords` from a stand-in platform's `listRecords` and
240
+ * `getRecords` (the steps in order; a list step one page, or as many as the platform reads: its `limit` past one
241
+ * page, every page of a `return: false` step): for tests and dev seeds, never the platform. Any
242
+ * other POST goes to `otherwise` (or is refused). A stand-in read refused with 401 or 429 fails the whole call
243
+ * (thrown as is), as the platform refuses the caller before any step; any other failure is the step's.
244
+ */
245
+ export declare function emulateCompose(read: QueryReader, otherwise?: ComposePoster): ComposePoster;
246
+ export {};