techne-sdk 0.3.3 → 0.3.4
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 +60 -0
- package/README.md +5 -4
- package/dist/auth/app-key.js +2 -2
- package/dist/auth/index.js +1 -1
- package/dist/catalog/catalog.gen.js +4 -12
- package/dist/connect.d.ts +96 -12
- package/dist/connect.js +83 -12
- package/dist/item.d.ts +130 -0
- package/dist/item.js +74 -0
- package/dist/links/index.d.ts +15 -8
- package/dist/links/index.js +18 -0
- package/dist/put/contacts.d.ts +2 -2
- package/dist/put/index.d.ts +6 -9
- package/dist/put/index.js +8 -166
- package/dist/put/put.d.ts +14 -0
- package/dist/put/put.js +206 -0
- package/dist/query/compose.d.ts +246 -0
- package/dist/query/compose.js +572 -0
- package/dist/query/hosts.d.ts +98 -1
- package/dist/query/hosts.js +143 -2
- package/dist/query/index.d.ts +60 -29
- package/dist/query/index.js +55 -26
- package/dist/react/context.d.ts +18 -0
- package/dist/react/context.js +19 -0
- package/dist/react/index.d.ts +14 -0
- package/dist/react/index.js +14 -0
- package/dist/react/query.d.ts +75 -0
- package/dist/react/query.js +116 -0
- package/dist/read/index.d.ts +60 -0
- package/dist/read/index.js +107 -0
- package/dist/resend.d.ts +25 -0
- package/dist/resend.js +52 -0
- package/dist/versions/entities.gen.d.ts +8 -0
- package/dist/versions/entities.gen.js +9 -0
- package/dist/versions/index.d.ts +59 -17
- package/dist/versions/index.js +156 -22
- package/dist/versions/react.d.ts +1 -1
- package/dist/versions/react.js +1 -1
- package/dist/versions/text.d.ts +19 -0
- package/dist/versions/text.js +73 -0
- package/package.json +17 -5
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,66 @@
|
|
|
3
3
|
`techne-sdk` on npm and the `techne-sdk` crate share a version. Each entry names what changes for an app or
|
|
4
4
|
a crate user.
|
|
5
5
|
|
|
6
|
+
## 0.3.4 (2026-11-10)
|
|
7
|
+
|
|
8
|
+
Lexicons `v0.1.22-techne.64` as in 0.3.3; the platform's published API re-pinned to platform `07fc8a1ec`, whose
|
|
9
|
+
one change is `social.scenius.ask`'s `timeChip` (known values now `now`, `today`, `thisWeek`; seen in
|
|
10
|
+
`techne-sdk/catalog`). The crate is unchanged and stays 0.3.3. This release brings in the platform's own SDK code (platform `07fc8a1ec` `sdk/`) that Beacon
|
|
11
|
+
imports, so Beacon can run on `techne-sdk`. No export is removed. What changes for an app already on
|
|
12
|
+
0.3.3:
|
|
13
|
+
|
|
14
|
+
- **`Techne` is wider.** It is now `Clients` (`did`, `pds`, `platform` as in 0.3.3's `Techne`, and an optional `claims`) plus
|
|
15
|
+
seven methods: `query`, `get`, `versions`, `put`, `queryOptions`, `getOptions`, `versionsOptions`. Code
|
|
16
|
+
that reads a `Techne` is unchanged; code that builds one by hand (a test double, an object literal typed
|
|
17
|
+
`Techne`) must now type it `Clients`, or make it with `bind(clients)`.
|
|
18
|
+
- **`versionName` never shows a service handle.** A version with no name written by a `mirror-` account
|
|
19
|
+
(or tagged `mirror`) is named by its calendar's site, else `"Imported calendar"`; one by a `rule-`
|
|
20
|
+
account is `"Someone"`. 0.3.3 showed the handle.
|
|
21
|
+
- **`eventState` counts a description that contains, or is contained in, the one shown as agreeing**
|
|
22
|
+
(after decoding text). A version that only adds a longer write-up moves from `differs` to `same`, and
|
|
23
|
+
the field's conflict count drops; 0.3.3 compared descriptions as equal or not.
|
|
24
|
+
- **`useEvent`'s `pill` is `null` while the versions agree.** It follows `versionPill`'s new default
|
|
25
|
+
(below): 0.3.3 showed `"N versions"` whenever there were two or more.
|
|
26
|
+
- **`MAX_PAGE` is 500 (was 100)**, same name: see `techne-sdk/query` below.
|
|
27
|
+
|
|
28
|
+
What is new:
|
|
29
|
+
|
|
30
|
+
- **The client has its reads and its write.** `connect`, `connectHosted` and the new `connectAnonymous`
|
|
31
|
+
(public reads, no session) answer a `Techne` with `query`, `get`, `versions` (each answering items with
|
|
32
|
+
the check's verdict) and their TanStack Query options forms (`queryOptions`, `getOptions`,
|
|
33
|
+
`versionsOptions`), and `put` (the same write as `techne-sdk/put`'s `put`, `deletes` included).
|
|
34
|
+
`bind(clients)` makes one over clients made elsewhere; `connect`'s platform now defaults to
|
|
35
|
+
`TECHNE_PLATFORM_DID`. Also `resolveAppview`, `didDocumentUrl`, `PLC_DIRECTORY`, and `Clients`.
|
|
36
|
+
- **`put` sends once more when a call got no response at all** (the connection lost before any answer): it
|
|
37
|
+
runs again from the start, re-reading first, so nothing is written or deleted twice. A status (a 500
|
|
38
|
+
too) or an abort is not retried. `connectHosted` and `connectAnonymous` also resend a POST that got no
|
|
39
|
+
response once, with the same `Idempotency-Key` (`resendingOnce`, `IDEMPOTENCY_KEY`). `plan` takes `now`
|
|
40
|
+
as a `Date` or as unix seconds. A delete whose answer the first run lost answers `deleted: true` on the
|
|
41
|
+
second run, which finds the record gone (or, rarely, when that call never arrived and nothing was there). `PutOptions` (`wait`, `deletes`) is exported
|
|
42
|
+
from `techne-sdk/put`.
|
|
43
|
+
- **`techne-sdk/react/query`: the client's reads as hooks over TanStack Query**: `useRecordQuery`,
|
|
44
|
+
`useRecord`, `useVersions`, `defaultQueryClient`. `techne-sdk/react` keeps `useEvent` and adds
|
|
45
|
+
`TechneProvider` and `useTechne`, and still needs React only. `@tanstack/react-query` 5 is a new
|
|
46
|
+
optional peer dependency, needed only by an app that imports `techne-sdk/react/query`.
|
|
47
|
+
- **`techne-sdk/query` composes.** `compose` builds one `pub.techne.draft.query.composeRecords` call of
|
|
48
|
+
several steps, a later one reading what an earlier one answered (`Composition`, `Composed`, `StepRef`,
|
|
49
|
+
`MAX_STEP_*`, `ENVELOPE_ONLY`); `emulateCompose`, `withPost` and `testReader` run it against a stand-in.
|
|
50
|
+
"Hosted by" on one composed call: `hostSteps`, `hostsFrom`, `HOST_ENVELOPE`, `HOST_SOURCES_QUERY`. Also
|
|
51
|
+
`listRecordsCounted`, `answerOf`, `curationOf` (now exported), `shownHandle`, and the `has` condition
|
|
52
|
+
(`HasCondition`) in a query's `where`.
|
|
53
|
+
- **`MAX_PAGE` is now 500** (the lexicon's `listRecords` `limit` maximum), so `listAllRecords` asks pages
|
|
54
|
+
of 500; the new **`MAX_BATCH` (100)** is what one `getRecords` call or one `in` list takes. URI lists
|
|
55
|
+
are still sent in runs that also fit the 8 KB request line.
|
|
56
|
+
- **`getRecordsAnswer`**: a `handle.invalid` handle is left out of `handles`, and `defaults` keeps only
|
|
57
|
+
object values (`Map<string, Record<string, unknown>>`).
|
|
58
|
+
- **`techne-sdk/versions`**: `versionPill` is the platform's: before a pick, `"N versions"` and only when the
|
|
59
|
+
versions conflict (`{ hideAgreeing: false }` shows it whenever there are two or more); after a pick, who
|
|
60
|
+
that version is from. It now answers `accessibilityHint` too. `eventState` reads text fields decoded
|
|
61
|
+
(`decodeTextFields`: no `&`), and a version's name is never `handle.invalid`. New: `shownVersion`,
|
|
62
|
+
`isImported`, `IMPORTED_CALENDAR`, `conflictsOf`.
|
|
63
|
+
- **`techne-sdk/links`**: a PDS refusing a link call for a missing scope throws `LinkScopeError` (links
|
|
64
|
+
need a platform session, or the identity methods' `rpc:` scopes).
|
|
65
|
+
|
|
6
66
|
## 0.3.3 (2026-11-10)
|
|
7
67
|
|
|
8
68
|
Built on lexicons `v0.1.22-techne.64` (b1407281) and the platform's published API at platform
|
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ The SDK depends on two things only: the published lexicons (the `lexicons` repo
|
|
|
10
10
|
## Install
|
|
11
11
|
|
|
12
12
|
```sh
|
|
13
|
-
npm i techne-sdk # react >= 19
|
|
13
|
+
npm i techne-sdk # optional peers: react >= 19 for techne-sdk/react, and @tanstack/react-query 5 too for techne-sdk/react/query
|
|
14
14
|
```
|
|
15
15
|
|
|
16
16
|
Every import goes through the package's `exports`, so the resolver has to read them:
|
|
@@ -23,12 +23,13 @@ Every import goes through the package's `exports`, so the resolver has to read t
|
|
|
23
23
|
| `techne-sdk` (also `techne-sdk/generated`) | the generated XRPC client (`AtpBaseClient`) and every lexicon type |
|
|
24
24
|
| `techne-sdk/lexicons` | the generated lexicon dictionary (`schemaDict`, `schemas`, `lexicons`, `ids`): every lexicon document the client was built from, for reading descriptions at run time (large; keep it server side) |
|
|
25
25
|
| `techne-sdk/types/<nsid path>` | one lexicon's types, e.g. `techne-sdk/types/social/scenius/getContactInvite` |
|
|
26
|
-
| `techne-sdk/react` | `useEvent(uri)`: one commons event or place as its versions, with the check's verdict |
|
|
26
|
+
| `techne-sdk/react` | `TechneProvider` / `useTechne` (the client in React), and `useEvent(uri)`: one commons event or place as its versions, with the check's verdict (React only) |
|
|
27
|
+
| `techne-sdk/react/query` | the client's reads as hooks over TanStack Query (`useRecordQuery`, `useRecord`, `useVersions`, `defaultQueryClient`); needs `@tanstack/react-query` |
|
|
27
28
|
| `techne-sdk/versions` | the same state without React: `eventState`, `verifyAnswer`, version labels |
|
|
28
29
|
| `techne-sdk/auth` | sign-in ("Sign in with Techne"): `signIn`, `signUp`, `restore`, `signOut`, `clientMetadata`, `scopes`, `appKey`, `appSpace` (browser OAuth) |
|
|
29
|
-
| `techne-sdk/connect` | `connect(session, platform)` / `connectHosted(did, fetchHandler)`: the
|
|
30
|
+
| `techne-sdk/connect` | `connect(session, platform?)` / `connectHosted(did, fetchHandler)` / `connectAnonymous(platform?)`: the client over one session, with its reads (`query`, `get`, `versions`, their options forms) and `put`; `bind(clients)` over clients made elsewhere |
|
|
30
31
|
| `techne-sdk/put` | `put(techne, records, { deletes })`: the one write, deletes included (public deletes hinted to the platform); `put(techne, [], { deletes })` for deletes alone |
|
|
31
|
-
| `techne-sdk/query` | the platform's generic queries: `listRecords`, `getRecords`, hosts |
|
|
32
|
+
| `techne-sdk/query` | the platform's generic queries: `listRecords`, `getRecords`, `compose` (several steps in one `composeRecords` call), hosts (`hostsOf`, `hostSteps`/`hostsFrom`) |
|
|
32
33
|
| `techne-sdk/links` | link tokens: create, redeem, check, revoke |
|
|
33
34
|
| `techne-sdk/wasm` | the check itself: `validate`, `decide`, `verify` (`validate` knows the built-in lexicons only; a runtime type is checked by the platform on write) |
|
|
34
35
|
| `techne-sdk/catalog` | the browsable lexicon catalog |
|
package/dist/auth/app-key.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* An app's own private space (`pub.techne.draft.space.private`): its key and address. The key is the
|
|
3
|
-
* platform's `sidecar_rkey(client_id)` (
|
|
3
|
+
* platform's `sidecar_rkey(client_id)` (platform crates/regenos-lexicons/src/sidecar.rs): RFC 4648 base32,
|
|
4
4
|
* lowercase, no padding, of the SHA-256 of the client id's UTF-8 bytes. Synchronous (no WebCrypto), so
|
|
5
5
|
* `scopes()` and `clientMetadata()` stay synchronous.
|
|
6
6
|
*/
|
|
@@ -61,7 +61,7 @@ export function sha256(bytes) {
|
|
|
61
61
|
outView.setUint32(i * 4, h[i]);
|
|
62
62
|
return out;
|
|
63
63
|
}
|
|
64
|
-
/** RFC 4648 base32, lowercase, no padding (
|
|
64
|
+
/** RFC 4648 base32, lowercase, no padding (platform `base32_lower_nopad`). */
|
|
65
65
|
function base32LowerNoPad(bytes) {
|
|
66
66
|
const alphabet = 'abcdefghijklmnopqrstuvwxyz234567';
|
|
67
67
|
let out = '';
|
package/dist/auth/index.js
CHANGED
|
@@ -86,7 +86,7 @@ function scopeList({ platform = TECHNE_PLATFORM_DID, types = [], everything, pub
|
|
|
86
86
|
...(publish ? [`rpc:${NSID.REGISTRY_PUBLISH_TYPE}?aud=${aud}`] : []),
|
|
87
87
|
];
|
|
88
88
|
}
|
|
89
|
-
/** A `repo` scope for `collections`, every action (
|
|
89
|
+
/** A `repo` scope for `collections`, every action (platform `regenos_appview::nsid_rewrite::repo_scope`,
|
|
90
90
|
* crates/regenos-appview/src/nsid_rewrite.rs). */
|
|
91
91
|
function repoScope(collections) {
|
|
92
92
|
const actions = 'action=create&action=update&action=delete';
|
|
@@ -17577,15 +17577,11 @@ export const catalog = {
|
|
|
17577
17577
|
"name": "timeChip",
|
|
17578
17578
|
"type": "string (enum)",
|
|
17579
17579
|
"required": false,
|
|
17580
|
-
"description": "The app's time preset that names the interpreted window; absent when the window is
|
|
17580
|
+
"description": "The app's time preset that names the interpreted window (Now, Today, This week); absent when the window is any other one (a clock time, a daypart, another day, a second day, the weekend or next week), which the app shows as a custom window.",
|
|
17581
17581
|
"knownValues": [
|
|
17582
17582
|
"now",
|
|
17583
17583
|
"today",
|
|
17584
|
-
"
|
|
17585
|
-
"tomorrow",
|
|
17586
|
-
"weekend",
|
|
17587
|
-
"thisWeek",
|
|
17588
|
-
"nextWeek"
|
|
17584
|
+
"thisWeek"
|
|
17589
17585
|
]
|
|
17590
17586
|
}
|
|
17591
17587
|
],
|
|
@@ -17734,13 +17730,9 @@ export const catalog = {
|
|
|
17734
17730
|
"knownValues": [
|
|
17735
17731
|
"now",
|
|
17736
17732
|
"today",
|
|
17737
|
-
"
|
|
17738
|
-
"tomorrow",
|
|
17739
|
-
"weekend",
|
|
17740
|
-
"thisWeek",
|
|
17741
|
-
"nextWeek"
|
|
17733
|
+
"thisWeek"
|
|
17742
17734
|
],
|
|
17743
|
-
"description": "The app's time preset that names the interpreted window; absent when the window is
|
|
17735
|
+
"description": "The app's time preset that names the interpreted window (Now, Today, This week); absent when the window is any other one (a clock time, a daypart, another day, a second day, the weekend or next week), which the app shows as a custom window."
|
|
17744
17736
|
}
|
|
17745
17737
|
}
|
|
17746
17738
|
}
|
package/dist/connect.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* techne-sdk/connect — the SDK's
|
|
2
|
+
* techne-sdk/connect — the SDK's client over one signed-in session (techne P13.3): two clients, the
|
|
3
|
+
* reads (`query`, `get`, `versions` and their options forms, SDK1) and the one write (`put`).
|
|
3
4
|
*
|
|
4
5
|
* Everything goes through the user's own PDS with the user's own session (an atproto OAuth session's
|
|
5
6
|
* `did` and `fetchHandler`, or anything shaped like it). `pds` calls the PDS itself (the user's repo).
|
|
@@ -8,34 +9,117 @@
|
|
|
8
9
|
* (iss = the user, aud = the platform, lxm = the method), and the platform authenticates that (Lane B).
|
|
9
10
|
* So every read carries its viewer, and the app holds no credential for the platform.
|
|
10
11
|
*
|
|
11
|
-
*
|
|
12
|
+
* `platform` defaults to {@link TECHNE_PLATFORM_DID} (`src/platform.ts`, the one place the name is
|
|
13
|
+
* spelled); staging passes its own DID.
|
|
14
|
+
*
|
|
15
|
+
* An app that holds only a platform session uses {@link connectHosted} instead. A page nobody has signed
|
|
16
|
+
* in to yet uses {@link connectAnonymous}: public reads straight from the platform, no write.
|
|
12
17
|
*/
|
|
13
18
|
import type { FetchHandler } from '@atproto/xrpc';
|
|
14
19
|
import { AtpBaseClient } from './generated/index.js';
|
|
20
|
+
import type { IncludeOf, ItemFor, RecordOfQuery, RecordOfUri } from './item.js';
|
|
21
|
+
import type { Query } from './query/index.js';
|
|
22
|
+
import type { PutRecord, PutResult } from './put/index.js';
|
|
23
|
+
import { type PutOptions } from './put/put.js';
|
|
24
|
+
import { type GetOpts, type Page, type QueryOpts, type Versions } from './read/index.js';
|
|
25
|
+
import type { Claims } from './wasm/index.js';
|
|
26
|
+
import { TECHNE_PLATFORM_DID } from './platform.js';
|
|
27
|
+
import { IDEMPOTENCY_KEY, resendingOnce } from './resend.js';
|
|
28
|
+
export type { GetOpts, Page, QueryOpts, Versions };
|
|
29
|
+
export { IDEMPOTENCY_KEY, resendingOnce, TECHNE_PLATFORM_DID };
|
|
15
30
|
/** A signed-in session: an atproto OAuth session satisfies it. */
|
|
16
31
|
export interface Session {
|
|
17
32
|
did: string;
|
|
18
33
|
fetchHandler: FetchHandler;
|
|
19
34
|
}
|
|
20
|
-
/**
|
|
21
|
-
export interface
|
|
35
|
+
/** What `connect` and `connectHosted` take beside the session. */
|
|
36
|
+
export interface ConnectOptions {
|
|
37
|
+
/**
|
|
38
|
+
* The claims the app holds (the commons root, the credentials and declarations it has read): every
|
|
39
|
+
* read's verdict is checked over them. Without them a record whose trust needs the chain (a commons
|
|
40
|
+
* sidecar about others' records) comes back invalid, "untrusted: … the root does not endorse".
|
|
41
|
+
*/
|
|
42
|
+
claims?: Claims;
|
|
43
|
+
}
|
|
44
|
+
/** The clients for one signed-in user, and the claims its reads are checked over. */
|
|
45
|
+
export interface Clients {
|
|
22
46
|
/** The signed-in user's DID: the repo every write goes to. */
|
|
23
47
|
did: string;
|
|
24
48
|
/** The user's PDS. */
|
|
25
49
|
pds: AtpBaseClient;
|
|
26
50
|
/** The platform, reached through the user's PDS (`atproto-proxy`). */
|
|
27
51
|
platform: AtpBaseClient;
|
|
52
|
+
/** The claims the app holds ({@link ConnectOptions.claims}). */
|
|
53
|
+
claims?: Claims;
|
|
54
|
+
}
|
|
55
|
+
/** One read as TanStack Query takes it. */
|
|
56
|
+
export interface ReadOptions<K extends readonly unknown[], R> {
|
|
57
|
+
queryKey: K;
|
|
58
|
+
queryFn: () => Promise<R>;
|
|
28
59
|
}
|
|
29
|
-
/** The
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
60
|
+
/** The SDK's client for one signed-in user: its clients, its reads and its one write. */
|
|
61
|
+
export interface Techne extends Clients {
|
|
62
|
+
/** One page of a query document; a list across authors answers curated records. Valid items by default
|
|
63
|
+
* (`ValidItem<T>`); `{ include: 'all' }` any verdict (`Item<T>`). */
|
|
64
|
+
query<Q extends Query, O extends QueryOpts = {}>(q: Q, opts?: O): Promise<Page<RecordOfQuery<Q>, IncludeOf<O>>>;
|
|
65
|
+
/** One record: the curated view by default, `{ prefer: 'mine' }` the user's own version, `{ exact: true }`
|
|
66
|
+
* exactly that record. `null` when there is nothing to read there, or (unless `{ include: 'all' }`) when
|
|
67
|
+
* it is not valid. */
|
|
68
|
+
get<U extends string, O extends GetOpts = {}>(uri: U, opts?: O): Promise<ItemFor<RecordOfUri<U>, IncludeOf<O>> | null>;
|
|
69
|
+
/** The record as its versions (ranked and grouped) and its conflicts. `null` when there is nothing there. */
|
|
70
|
+
versions<U extends string>(uri: U): Promise<Versions<RecordOfUri<U>> | null>;
|
|
71
|
+
/** Write records, and with `deletes` delete some (`techne-sdk/put` says how). */
|
|
72
|
+
put(records: PutRecord[], options?: PutOptions): Promise<PutResult>;
|
|
73
|
+
/** The options forms (TanStack Query's `{ queryKey, queryFn }`). Each key ends with the viewer's DID, so
|
|
74
|
+
* one viewer's reads are never another's, and a prefix (`['techne', 'versions']`) still names them all. */
|
|
75
|
+
queryOptions<Q extends Query, O extends QueryOpts = {}>(q: Q, opts?: O): ReadOptions<readonly ['techne', 'query', Q, QueryOpts, string], Page<RecordOfQuery<Q>, IncludeOf<O>>>;
|
|
76
|
+
getOptions<U extends string, O extends GetOpts = {}>(uri: U, opts?: O): ReadOptions<readonly ['techne', 'record', U, GetOpts, string], ItemFor<RecordOfUri<U>, IncludeOf<O>> | null>;
|
|
77
|
+
versionsOptions<U extends string>(uri: U): ReadOptions<readonly ['techne', 'versions', U, string], Versions<RecordOfUri<U>> | null>;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The client over clients made elsewhere: `clients` with the reads and the write bound to them. `connect`
|
|
81
|
+
* and `connectHosted` are this over the clients they make; an app that holds its own (or a test's stand-ins
|
|
82
|
+
* for them) binds them here.
|
|
83
|
+
*/
|
|
84
|
+
export declare function bind(clients: Clients): Techne;
|
|
85
|
+
/** The client for `session`, with `platform` the platform's service DID (its document names the
|
|
86
|
+
* `#atproto_appview` endpoint, so a host move changes nothing here). The session's `fetchHandler` is
|
|
87
|
+
* called on the session, so an OAuthSession (whose `fetchHandler` is a method) works as it comes. A POST
|
|
88
|
+
* that got no response is not resent here: the PDS may not pass the `Idempotency-Key` on, so only `put`
|
|
89
|
+
* runs again (it re-reads first). */
|
|
90
|
+
export declare function connect(session: Session, platform?: string, options?: ConnectOptions): Techne;
|
|
91
|
+
/** What `connectAnonymous` takes beside the platform. */
|
|
92
|
+
export interface AnonymousOptions extends ConnectOptions {
|
|
93
|
+
/** The fetch every request goes through (the DID document and the reads); `globalThis.fetch` by default. */
|
|
94
|
+
fetch?: typeof globalThis.fetch;
|
|
95
|
+
/** The PLC directory a did:plc platform is resolved at; {@link PLC_DIRECTORY} by default. Staging's
|
|
96
|
+
* platform DID lives on staging's own PLC, so staging passes that one's URL. */
|
|
97
|
+
plcUrl?: string;
|
|
98
|
+
}
|
|
99
|
+
/** The public PLC directory: where a did:plc is resolved unless a `plcUrl` names another. */
|
|
100
|
+
export declare const PLC_DIRECTORY = "https://plc.directory";
|
|
101
|
+
/** Where `did`'s document is: `<plcUrl>/<did>` for a did:plc (`https://plc.directory` by default),
|
|
102
|
+
* `https://<host>/.well-known/did.json` (or `https://<host>/<path>/did.json`) for a did:web. */
|
|
103
|
+
export declare function didDocumentUrl(did: string, plcUrl?: string): string;
|
|
104
|
+
/** The `#atproto_appview` service endpoint `did`'s document names. The reads are sent to `/xrpc/…` on it,
|
|
105
|
+
* so it is taken as an origin: a path on it is dropped (`new URL('/xrpc/…', endpoint)`), as atproto does
|
|
106
|
+
* for a service endpoint. */
|
|
107
|
+
export declare function resolveAppview(did: string, fetch?: typeof globalThis.fetch, plcUrl?: string): Promise<string>;
|
|
108
|
+
/**
|
|
109
|
+
* The client for nobody signed in: the platform reached directly, with no session and no PDS, so it reads
|
|
110
|
+
* only what anyone may read. The platform's endpoint is its DID document's `#atproto_appview` service
|
|
111
|
+
* (an origin: {@link resolveAppview}), resolved on the first read and kept (a failed resolution is not
|
|
112
|
+
* kept: the next read tries again). A did:plc platform is resolved at `plcUrl` ({@link PLC_DIRECTORY} by
|
|
113
|
+
* default). `did` is `''`, so its query keys never meet a viewer's; `put` rejects, asking to sign in. A
|
|
114
|
+
* POST that got no response is sent once more with the same `Idempotency-Key` ({@link resendingOnce}).
|
|
115
|
+
*/
|
|
116
|
+
export declare function connectAnonymous(platform?: string, options?: AnonymousOptions): Techne;
|
|
34
117
|
/**
|
|
35
|
-
* The
|
|
118
|
+
* The client for an app that holds only a platform session (techne P19.1, option (a)): the platform is
|
|
36
119
|
* the OAuth client and holds the write credential, so both clients call the platform. `pds` reaches the
|
|
37
120
|
* platform's `com.atproto.repo.applyWrites` and `getRecord`, which it relays to the repo's PDS with the
|
|
38
121
|
* credential it holds; `put` runs unchanged. `fetchHandler` carries the platform session (its cookie or
|
|
39
|
-
* device token) to the platform's origin.
|
|
122
|
+
* device token) to the platform's origin. A POST that got no response is sent once more with the same
|
|
123
|
+
* `Idempotency-Key` ({@link resendingOnce}), so the platform runs it once.
|
|
40
124
|
*/
|
|
41
|
-
export declare function connectHosted(did: string, fetchHandler: FetchHandler): Techne;
|
|
125
|
+
export declare function connectHosted(did: string, fetchHandler: FetchHandler, options?: ConnectOptions): Techne;
|
package/dist/connect.js
CHANGED
|
@@ -1,23 +1,94 @@
|
|
|
1
1
|
import { AtpBaseClient } from './generated/index.js';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
import { put } from './put/put.js';
|
|
3
|
+
import { get, query, versions } from './read/index.js';
|
|
4
|
+
import { appviewRef, APPVIEW_SERVICE_ID, TECHNE_PLATFORM_DID } from './platform.js';
|
|
5
|
+
import { IDEMPOTENCY_KEY, resendingOnce } from './resend.js';
|
|
6
|
+
export { IDEMPOTENCY_KEY, resendingOnce, TECHNE_PLATFORM_DID };
|
|
7
|
+
/**
|
|
8
|
+
* The client over clients made elsewhere: `clients` with the reads and the write bound to them. `connect`
|
|
9
|
+
* and `connectHosted` are this over the clients they make; an app that holds its own (or a test's stand-ins
|
|
10
|
+
* for them) binds them here.
|
|
11
|
+
*/
|
|
12
|
+
export function bind(clients) {
|
|
13
|
+
const techne = {
|
|
14
|
+
...clients,
|
|
15
|
+
query: (q, opts) => query(techne, q, opts),
|
|
16
|
+
get: (uri, opts) => get(techne, uri, opts),
|
|
17
|
+
versions: (uri) => versions(techne, uri),
|
|
18
|
+
put: (records, options) => put(techne, records, options),
|
|
19
|
+
queryOptions: (q, opts) => ({ queryKey: ['techne', 'query', q, opts ?? {}, clients.did], queryFn: () => query(techne, q, opts) }),
|
|
20
|
+
getOptions: (uri, opts) => ({ queryKey: ['techne', 'record', uri, opts ?? {}, clients.did], queryFn: () => get(techne, uri, opts) }),
|
|
21
|
+
versionsOptions: (uri) => ({ queryKey: ['techne', 'versions', uri, clients.did], queryFn: () => versions(techne, uri) }),
|
|
22
|
+
};
|
|
23
|
+
return techne;
|
|
24
|
+
}
|
|
25
|
+
/** The client for `session`, with `platform` the platform's service DID (its document names the
|
|
26
|
+
* `#atproto_appview` endpoint, so a host move changes nothing here). The session's `fetchHandler` is
|
|
27
|
+
* called on the session, so an OAuthSession (whose `fetchHandler` is a method) works as it comes. A POST
|
|
28
|
+
* that got no response is not resent here: the PDS may not pass the `Idempotency-Key` on, so only `put`
|
|
29
|
+
* runs again (it re-reads first). */
|
|
30
|
+
export function connect(session, platform = TECHNE_PLATFORM_DID, options = {}) {
|
|
8
31
|
const fetchHandler = (url, init) => session.fetchHandler(url, init);
|
|
9
32
|
const proxied = new AtpBaseClient(fetchHandler);
|
|
10
33
|
proxied.setHeader('atproto-proxy', appviewRef(platform));
|
|
11
|
-
return { did: session.did, pds: new AtpBaseClient(fetchHandler), platform: proxied };
|
|
34
|
+
return bind({ did: session.did, pds: new AtpBaseClient(fetchHandler), platform: proxied, claims: options.claims });
|
|
35
|
+
}
|
|
36
|
+
/** The public PLC directory: where a did:plc is resolved unless a `plcUrl` names another. */
|
|
37
|
+
export const PLC_DIRECTORY = 'https://plc.directory';
|
|
38
|
+
const SIGN_IN_TO_WRITE = 'techne-sdk/connect: sign in to write (this client is anonymous: public reads only)';
|
|
39
|
+
/** Where `did`'s document is: `<plcUrl>/<did>` for a did:plc (`https://plc.directory` by default),
|
|
40
|
+
* `https://<host>/.well-known/did.json` (or `https://<host>/<path>/did.json`) for a did:web. */
|
|
41
|
+
export function didDocumentUrl(did, plcUrl = PLC_DIRECTORY) {
|
|
42
|
+
if (did.startsWith('did:plc:'))
|
|
43
|
+
return `${plcUrl.replace(/\/+$/, '')}/${did}`;
|
|
44
|
+
if (did.startsWith('did:web:')) {
|
|
45
|
+
const [host, ...path] = did.slice('did:web:'.length).split(':').map(decodeURIComponent);
|
|
46
|
+
return path.length ? `https://${host}/${path.join('/')}/did.json` : `https://${host}/.well-known/did.json`;
|
|
47
|
+
}
|
|
48
|
+
throw new Error(`techne-sdk/connect: cannot resolve ${did} (only did:web and did:plc)`);
|
|
49
|
+
}
|
|
50
|
+
/** The `#atproto_appview` service endpoint `did`'s document names. The reads are sent to `/xrpc/…` on it,
|
|
51
|
+
* so it is taken as an origin: a path on it is dropped (`new URL('/xrpc/…', endpoint)`), as atproto does
|
|
52
|
+
* for a service endpoint. */
|
|
53
|
+
export async function resolveAppview(did, fetch = globalThis.fetch, plcUrl) {
|
|
54
|
+
const url = didDocumentUrl(did, plcUrl);
|
|
55
|
+
const res = await fetch(url);
|
|
56
|
+
if (!res.ok)
|
|
57
|
+
throw new Error(`techne-sdk/connect: ${url} answered ${res.status}`);
|
|
58
|
+
const doc = (await res.json());
|
|
59
|
+
const services = Array.isArray(doc?.service) ? doc.service : [];
|
|
60
|
+
const service = services.find((s) => s?.id === APPVIEW_SERVICE_ID || s?.id === appviewRef(did));
|
|
61
|
+
if (typeof service?.serviceEndpoint !== 'string')
|
|
62
|
+
throw new Error(`techne-sdk/connect: ${did} names no ${APPVIEW_SERVICE_ID} service`);
|
|
63
|
+
return service.serviceEndpoint;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The client for nobody signed in: the platform reached directly, with no session and no PDS, so it reads
|
|
67
|
+
* only what anyone may read. The platform's endpoint is its DID document's `#atproto_appview` service
|
|
68
|
+
* (an origin: {@link resolveAppview}), resolved on the first read and kept (a failed resolution is not
|
|
69
|
+
* kept: the next read tries again). A did:plc platform is resolved at `plcUrl` ({@link PLC_DIRECTORY} by
|
|
70
|
+
* default). `did` is `''`, so its query keys never meet a viewer's; `put` rejects, asking to sign in. A
|
|
71
|
+
* POST that got no response is sent once more with the same `Idempotency-Key` ({@link resendingOnce}).
|
|
72
|
+
*/
|
|
73
|
+
export function connectAnonymous(platform = TECHNE_PLATFORM_DID, options = {}) {
|
|
74
|
+
const fetch = (input, init) => (options.fetch ?? globalThis.fetch)(input, init);
|
|
75
|
+
let endpoint;
|
|
76
|
+
const resolve = () => (endpoint ??= resolveAppview(platform, fetch, options.plcUrl).catch((e) => ((endpoint = undefined), Promise.reject(e))));
|
|
77
|
+
const direct = resendingOnce(async (url, init) => fetch(new URL(url, await resolve()), init));
|
|
78
|
+
const nowhere = async () => Promise.reject(new Error(SIGN_IN_TO_WRITE));
|
|
79
|
+
const techne = bind({ did: '', pds: new AtpBaseClient(nowhere), platform: new AtpBaseClient(direct), claims: options.claims });
|
|
80
|
+
techne.put = async () => Promise.reject(new Error(SIGN_IN_TO_WRITE));
|
|
81
|
+
return techne;
|
|
12
82
|
}
|
|
13
83
|
/**
|
|
14
|
-
* The
|
|
84
|
+
* The client for an app that holds only a platform session (techne P19.1, option (a)): the platform is
|
|
15
85
|
* the OAuth client and holds the write credential, so both clients call the platform. `pds` reaches the
|
|
16
86
|
* platform's `com.atproto.repo.applyWrites` and `getRecord`, which it relays to the repo's PDS with the
|
|
17
87
|
* credential it holds; `put` runs unchanged. `fetchHandler` carries the platform session (its cookie or
|
|
18
|
-
* device token) to the platform's origin.
|
|
88
|
+
* device token) to the platform's origin. A POST that got no response is sent once more with the same
|
|
89
|
+
* `Idempotency-Key` ({@link resendingOnce}), so the platform runs it once.
|
|
19
90
|
*/
|
|
20
|
-
export function connectHosted(did, fetchHandler) {
|
|
21
|
-
const platform = new AtpBaseClient(fetchHandler);
|
|
22
|
-
return { did, pds: platform, platform };
|
|
91
|
+
export function connectHosted(did, fetchHandler, options = {}) {
|
|
92
|
+
const platform = new AtpBaseClient(resendingOnce(fetchHandler));
|
|
93
|
+
return bind({ did, pds: platform, platform, claims: options.claims });
|
|
23
94
|
}
|
package/dist/item.d.ts
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The SDK's items (internal; their types reach apps through `techne-sdk/connect` and `techne-sdk/react`):
|
|
3
|
+
* the one shape every read answers (`Item<T>`), typed URIs, and the check that
|
|
4
|
+
* gives each item its verdict.
|
|
5
|
+
*
|
|
6
|
+
* An item is `{ uri, cid, record, verdict, problems, curated, byline }`:
|
|
7
|
+
*
|
|
8
|
+
* - `uri` is typed (`Uri<Place>`), so a hook handed `item.uri` comes back typed. A bare string is no
|
|
9
|
+
* `Uri<Place>` (only an item, or `Place.uri(s)`, makes one); a read taking a bare string answers
|
|
10
|
+
* `unknown` records.
|
|
11
|
+
* - `record` is a `T` only when the record passed the lexicon check: a `valid` or `pending` item (pending
|
|
12
|
+
* comes after the lexicon check, `techne-lexicon-check::decide`). An `invalid` one's record is `unknown`.
|
|
13
|
+
* A read with `include: 'valid'` (the default) answers {@link ValidItem}s; `include: 'all'` the union.
|
|
14
|
+
* - `verdict` is the SDK's own answer for the record (`valid`, `pending` or `invalid`), from the pure
|
|
15
|
+
* check run here as WebAssembly (`decide`). techne.computer has already left out what privacy and
|
|
16
|
+
* safety require (spaces the caller may not read, admission, hidden accounts and moderation) before
|
|
17
|
+
* anything comes back, so the check never sees those: `include: 'all'` adds pending and invalid
|
|
18
|
+
* records the platform answered, never blocked or hidden ones.
|
|
19
|
+
* - `problems` says why: the check's reasons for an invalid record, one plain sentence for a pending one
|
|
20
|
+
* (what it is waiting for), empty for a valid one.
|
|
21
|
+
* - `curated`: the record is a live curated record (the commons' composition of several versions).
|
|
22
|
+
* - `byline`: for a curated record, whose version speaks first and whose version each field came from
|
|
23
|
+
* (the provenance composition). `null` otherwise. Items never carry the versions themselves: that is
|
|
24
|
+
* `techne.versions(uri)`.
|
|
25
|
+
*
|
|
26
|
+
* `cid` is there so an item can be pointed at (a strongRef needs it).
|
|
27
|
+
*/
|
|
28
|
+
import { type Claims } from './wasm/index.js';
|
|
29
|
+
declare const uriOf: unique symbol;
|
|
30
|
+
declare const recordOf: unique symbol;
|
|
31
|
+
/** An AT-URI whose record is a `T`. Only an item's `uri` or a type's `uri(s)` makes one: a bare string is
|
|
32
|
+
* not one (a read that takes a bare string answers `unknown`). */
|
|
33
|
+
export type Uri<T = unknown> = string & {
|
|
34
|
+
readonly [uriOf]: T;
|
|
35
|
+
};
|
|
36
|
+
/** The record type a typed URI names (`unknown` for a bare string). */
|
|
37
|
+
export type RecordOfUri<U> = U extends Uri<infer T> ? T : unknown;
|
|
38
|
+
/** Marks a query document with the record type it answers; `Place.query(…)` makes one. */
|
|
39
|
+
export interface Typed<T> {
|
|
40
|
+
readonly [recordOf]?: T;
|
|
41
|
+
}
|
|
42
|
+
/** The record type a typed query answers (`unknown` for an untyped one). */
|
|
43
|
+
export type RecordOfQuery<Q> = Q extends Typed<infer T> ? T : unknown;
|
|
44
|
+
export type Verdict = 'valid' | 'pending' | 'invalid';
|
|
45
|
+
/** Which items a read answers: `valid` (the default) or `all` (adds pending and invalid ones, with their
|
|
46
|
+
* problems). Neither ever adds what techne.computer left out (blocked, hidden, unadmitted). */
|
|
47
|
+
export type Include = 'valid' | 'all';
|
|
48
|
+
/** One version a byline names: its URI and its author. */
|
|
49
|
+
export interface BylineRef {
|
|
50
|
+
uri: string;
|
|
51
|
+
author: string;
|
|
52
|
+
}
|
|
53
|
+
/** Whose version a curated record's fields came from (its provenance composition). */
|
|
54
|
+
export interface Byline extends BylineRef {
|
|
55
|
+
/** Each top-level field the composition names, and the version it came from. */
|
|
56
|
+
fields: Record<string, BylineRef>;
|
|
57
|
+
}
|
|
58
|
+
/** What every item carries, whatever its verdict. */
|
|
59
|
+
interface ItemBase<T> {
|
|
60
|
+
uri: Uri<T>;
|
|
61
|
+
cid: string;
|
|
62
|
+
/** Why it is not valid: the check's reasons when invalid, what it waits for when pending. */
|
|
63
|
+
problems: string[];
|
|
64
|
+
/** A live curated record (the commons composed it from versions). */
|
|
65
|
+
curated: boolean;
|
|
66
|
+
/** For a curated record: whose version speaks first, and each field's source. `null` otherwise. */
|
|
67
|
+
byline: Byline | null;
|
|
68
|
+
}
|
|
69
|
+
/** A valid item: its record passed every check. */
|
|
70
|
+
export interface ValidItem<T = unknown> extends ItemBase<T> {
|
|
71
|
+
verdict: 'valid';
|
|
72
|
+
record: T;
|
|
73
|
+
}
|
|
74
|
+
/** A pending item: its record passed the lexicon check and waits for a claim (or the reach limit). */
|
|
75
|
+
export interface PendingItem<T = unknown> extends ItemBase<T> {
|
|
76
|
+
verdict: 'pending';
|
|
77
|
+
record: T;
|
|
78
|
+
}
|
|
79
|
+
/** An invalid item: its record may not even be a `T` (`problems` says why). */
|
|
80
|
+
export interface InvalidItem<T = unknown> extends ItemBase<T> {
|
|
81
|
+
verdict: 'invalid';
|
|
82
|
+
record: unknown;
|
|
83
|
+
}
|
|
84
|
+
/** One record as every read answers it, told apart by `verdict`. */
|
|
85
|
+
export type Item<T = unknown> = ValidItem<T> | PendingItem<T> | InvalidItem<T>;
|
|
86
|
+
/** What a read's options ask for: `'all'` when they may ask for every item (`include: 'all'`, or an
|
|
87
|
+
* `include` not known until run time), else `'valid'`. */
|
|
88
|
+
export type IncludeOf<O> = O extends {
|
|
89
|
+
include?: infer I;
|
|
90
|
+
} ? ([Exclude<I, undefined>] extends ['valid'] ? 'valid' : unknown extends I ? 'valid' : 'all') : 'valid';
|
|
91
|
+
/** The item a read answers for `include` `I`: a {@link ValidItem} by default, any {@link Item} for `all`. */
|
|
92
|
+
export type ItemFor<T, I extends Include> = I extends 'all' ? Item<T> : ValidItem<T>;
|
|
93
|
+
/** The problem a pending record held by the reach limit carries. */
|
|
94
|
+
export declare const HELD = "held: the reach limit holds this back from the people it would reach until it admits it";
|
|
95
|
+
/** The problem a pending record waiting for a claim carries. */
|
|
96
|
+
export declare const AWAITING_CLAIM = "pending: a claim it needs has not been seen yet (an admission, a membership or a vouch)";
|
|
97
|
+
/** The author DID of an AT-URI (`''` when it is not one). */
|
|
98
|
+
export declare const authorOf: (uri: string) => string;
|
|
99
|
+
/** The collection of an AT-URI (`''` when it is not one). */
|
|
100
|
+
export declare const collectionOf: (uri: string) => string;
|
|
101
|
+
/** The parts of one record the check reads. */
|
|
102
|
+
export interface Checked {
|
|
103
|
+
uri: string;
|
|
104
|
+
collection: string;
|
|
105
|
+
author: string;
|
|
106
|
+
value: unknown;
|
|
107
|
+
/** The space a permissioned record counts in. */
|
|
108
|
+
space?: string;
|
|
109
|
+
/** When the platform first saw it (its indexed time), RFC 3339. */
|
|
110
|
+
time?: string;
|
|
111
|
+
/** The platform answered it as held back by the reach limit (its author's own share). */
|
|
112
|
+
held?: boolean;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The verdict for one record the platform answered, over the claims the app holds. A permissioned record
|
|
116
|
+
* the platform answered passed its space's write policy there (the platform applies admission before
|
|
117
|
+
* anything comes back), so that admission is taken as given here; anything else the check needs and the
|
|
118
|
+
* app does not hold (a membership, a vouch) leaves the record pending.
|
|
119
|
+
*/
|
|
120
|
+
export declare function check(r: Checked, claims?: Claims): {
|
|
121
|
+
verdict: Verdict;
|
|
122
|
+
problems: string[];
|
|
123
|
+
};
|
|
124
|
+
/** The byline of a curated record from its provenance `composition` (`null` when it has none). */
|
|
125
|
+
export declare function bylineOf(composition: unknown): Byline | null;
|
|
126
|
+
/** Whether `include` keeps `item`. */
|
|
127
|
+
export declare const keeps: <T, I extends Include>(item: Item<T>, include: I | undefined) => item is ItemFor<T, I>;
|
|
128
|
+
/** Keep the items `include` asks for. */
|
|
129
|
+
export declare function included<T, I extends Include = 'valid'>(items: Item<T>[], include?: I): ItemFor<T, I>[];
|
|
130
|
+
export {};
|
package/dist/item.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The SDK's items (internal; their types reach apps through `techne-sdk/connect` and `techne-sdk/react`):
|
|
3
|
+
* the one shape every read answers (`Item<T>`), typed URIs, and the check that
|
|
4
|
+
* gives each item its verdict.
|
|
5
|
+
*
|
|
6
|
+
* An item is `{ uri, cid, record, verdict, problems, curated, byline }`:
|
|
7
|
+
*
|
|
8
|
+
* - `uri` is typed (`Uri<Place>`), so a hook handed `item.uri` comes back typed. A bare string is no
|
|
9
|
+
* `Uri<Place>` (only an item, or `Place.uri(s)`, makes one); a read taking a bare string answers
|
|
10
|
+
* `unknown` records.
|
|
11
|
+
* - `record` is a `T` only when the record passed the lexicon check: a `valid` or `pending` item (pending
|
|
12
|
+
* comes after the lexicon check, `techne-lexicon-check::decide`). An `invalid` one's record is `unknown`.
|
|
13
|
+
* A read with `include: 'valid'` (the default) answers {@link ValidItem}s; `include: 'all'` the union.
|
|
14
|
+
* - `verdict` is the SDK's own answer for the record (`valid`, `pending` or `invalid`), from the pure
|
|
15
|
+
* check run here as WebAssembly (`decide`). techne.computer has already left out what privacy and
|
|
16
|
+
* safety require (spaces the caller may not read, admission, hidden accounts and moderation) before
|
|
17
|
+
* anything comes back, so the check never sees those: `include: 'all'` adds pending and invalid
|
|
18
|
+
* records the platform answered, never blocked or hidden ones.
|
|
19
|
+
* - `problems` says why: the check's reasons for an invalid record, one plain sentence for a pending one
|
|
20
|
+
* (what it is waiting for), empty for a valid one.
|
|
21
|
+
* - `curated`: the record is a live curated record (the commons' composition of several versions).
|
|
22
|
+
* - `byline`: for a curated record, whose version speaks first and whose version each field came from
|
|
23
|
+
* (the provenance composition). `null` otherwise. Items never carry the versions themselves: that is
|
|
24
|
+
* `techne.versions(uri)`.
|
|
25
|
+
*
|
|
26
|
+
* `cid` is there so an item can be pointed at (a strongRef needs it).
|
|
27
|
+
*/
|
|
28
|
+
import { decide } from './wasm/index.js';
|
|
29
|
+
/** The problem a pending record held by the reach limit carries. */
|
|
30
|
+
export const HELD = 'held: the reach limit holds this back from the people it would reach until it admits it';
|
|
31
|
+
/** The problem a pending record waiting for a claim carries. */
|
|
32
|
+
export const AWAITING_CLAIM = 'pending: a claim it needs has not been seen yet (an admission, a membership or a vouch)';
|
|
33
|
+
/** The author DID of an AT-URI (`''` when it is not one). */
|
|
34
|
+
export const authorOf = (uri) => (uri.startsWith('at://') ? (uri.slice(5).split('/')[0] ?? '') : '');
|
|
35
|
+
/** The collection of an AT-URI (`''` when it is not one). */
|
|
36
|
+
export const collectionOf = (uri) => (uri.startsWith('at://') ? (uri.slice(5).split('/')[1] ?? '') : '');
|
|
37
|
+
/**
|
|
38
|
+
* The verdict for one record the platform answered, over the claims the app holds. A permissioned record
|
|
39
|
+
* the platform answered passed its space's write policy there (the platform applies admission before
|
|
40
|
+
* anything comes back), so that admission is taken as given here; anything else the check needs and the
|
|
41
|
+
* app does not hold (a membership, a vouch) leaves the record pending.
|
|
42
|
+
*/
|
|
43
|
+
export function check(r, claims = {}) {
|
|
44
|
+
const rkey = r.uri.split('/')[4] ?? '';
|
|
45
|
+
const admissions = r.space ? [...(claims.admissions ?? []), { author: r.author, space: r.space, admitted: true }] : claims.admissions;
|
|
46
|
+
const d = decide({ collection: r.collection, author: r.author, rkey, body: r.value, space: r.space, firstSeen: r.time }, { ...claims, admissions });
|
|
47
|
+
if (d.decision === 'invalid')
|
|
48
|
+
return { verdict: 'invalid', problems: d.reason.split('; ') };
|
|
49
|
+
if (r.held)
|
|
50
|
+
return { verdict: 'pending', problems: [HELD] };
|
|
51
|
+
if (d.decision === 'pending')
|
|
52
|
+
return { verdict: 'pending', problems: [AWAITING_CLAIM] };
|
|
53
|
+
return { verdict: 'valid', problems: [] };
|
|
54
|
+
}
|
|
55
|
+
/** The byline of a curated record from its provenance `composition` (`null` when it has none). */
|
|
56
|
+
export function bylineOf(composition) {
|
|
57
|
+
if (!composition || typeof composition !== 'object')
|
|
58
|
+
return null;
|
|
59
|
+
const { byline, fields } = composition;
|
|
60
|
+
if (typeof byline !== 'string')
|
|
61
|
+
return null;
|
|
62
|
+
const out = { uri: byline, author: authorOf(byline), fields: {} };
|
|
63
|
+
for (const f of Array.isArray(fields) ? fields : []) {
|
|
64
|
+
if (f && typeof f.field === 'string' && typeof f.from === 'string')
|
|
65
|
+
out.fields[f.field] = { uri: f.from, author: authorOf(f.from) };
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
/** Whether `include` keeps `item`. */
|
|
70
|
+
export const keeps = (item, include) => include === 'all' || item.verdict === 'valid';
|
|
71
|
+
/** Keep the items `include` asks for. */
|
|
72
|
+
export function included(items, include) {
|
|
73
|
+
return items.filter((i) => keeps(i, include));
|
|
74
|
+
}
|