@withmarfa/client 0.0.1 → 0.0.2
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/README.md +4 -2
- package/dist/index.d.ts +115 -6
- package/dist/index.js +63 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -26,9 +26,11 @@ for await (const item of everyItem) {
|
|
|
26
26
|
}
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
+
A door that takes raw bytes, such as `POST /blobs`, takes them as a `Blob`, an `ArrayBuffer`, a typed array or a `ReadableStream`, sent as they are, with the `Content-Type` the caller names, else a `Blob`'s own type, else the one the door declares: `await marfa.POST("/blobs", { body: bytes, headers: { "Content-Type": "image/png" } })`.
|
|
30
|
+
|
|
29
31
|
## The contract
|
|
30
32
|
|
|
31
|
-
The client carries the contract version it was generated for as `CONTRACT_VERSION`, and every answer the server sends carries its own as the `X-Marfa-Contract` header. The client checks that header on each answer before handing it on: one that names another contract, or a success that names none, is refused with `ContractMismatchError`. An error answer with no header is handed on as it came, since a proxy in front of the server answers without one and its status is still the truth. The credential is sent only under the `baseUrl` the client was made for, and never after a redirect. Both happen inside the client's own `fetch`, so middleware added with `client.use` sees a request without the credential and an answer already checked.
|
|
33
|
+
The client carries the contract version it was generated for as `CONTRACT_VERSION`, and every answer the server sends carries its own as the `X-Marfa-Contract` header. The client checks that header on each answer before handing it on: one that names another contract, or a success that names none, is refused with `ContractMismatchError`. An error answer with no header is handed on as it came, since a proxy in front of the server answers without one and its status is still the truth. The credential is sent only under the `baseUrl` the client was made for, and never after a redirect. One that is empty, or holds a character a bearer token cannot (RFC 6750's `b64token`), is refused by `createClient` with a `TypeError` that never repeats it. Both happen inside the client's own `fetch`, so middleware added with `client.use` sees a request without the credential and an answer already checked.
|
|
32
34
|
|
|
33
35
|
## Pages
|
|
34
36
|
|
|
@@ -40,4 +42,4 @@ It sends what the document declares and hands back what the server answered. It
|
|
|
40
42
|
|
|
41
43
|
## Generating
|
|
42
44
|
|
|
43
|
-
`src/generated/schema.ts`
|
|
45
|
+
`src/generated/schema.ts` and `src/generated/byte-bodies.ts` come from `scripts/generate-schema.ts`, which runs [openapi-typescript](https://openapi-ts.dev) with a request body of `format: binary` typed as bytes rather than a string, and `src/generated/contract.ts` from `scripts/generate-contract.ts`, all read off `openapi.json` by `pnpm generate` at the repository root. They are committed and none is edited by hand; CI's "Generated clients are fresh" job regenerates them into an emptied `src/generated/` and refuses any difference, including a file there the generator did not write. Requests go through [openapi-fetch](https://openapi-ts.dev/openapi-fetch/).
|
package/dist/index.d.ts
CHANGED
|
@@ -79,7 +79,7 @@ interface paths {
|
|
|
79
79
|
head?: never;
|
|
80
80
|
/**
|
|
81
81
|
* Update an item
|
|
82
|
-
* @description Updates an item's properties, tier, own time, edges, or natural key. Properties merge shallowly with existing values by default
|
|
82
|
+
* @description Updates an item's properties, tier, own time, edges, or natural key. Properties merge shallowly with existing values by default; when `properties_mode` is `replace` the body is the whole of the caller's properties, so a field it leaves out is cleared. `version` is required, and a write naming none is refused 400 `missing_required_field`. At the current version the write lands as sent. At a stale one the caller's genuine changes, a cleared field included, merge over the row where nothing collides, and a collision on a property, `tier`, `occurred_at` or `source_id` answers 409 with the conflict context to resolve, or is resolved by the type's merge policy under `?conflict=auto`. An item's `type` is not updatable here by default: sending one that matches the item is accepted and ignored, and sending a different one is refused with 409 `type_mismatch` rather than silently dropped. Passing `retype: true` alongside a different `type` moves the item to it, with or without `properties`, and at a stale version as at the current one where nothing collides; the properties the row ends up with are held to the type it enters, `400 invalid_properties` where they fall short, and a colliding stale move answers 409 whatever `?conflict` asks, a move onto a row another writer moved since colliding on `type`; that requires write on the type being entered as well as the one being left. `retype` naming the type the row already has changes nothing and takes no version step. Where the instance's strict-mode lever names the type, a property the type does not declare is refused `400 invalid_properties` with `details.code` `unknown_property`, judged on the properties this request carries.
|
|
83
83
|
*/
|
|
84
84
|
patch: operations["updateItem"];
|
|
85
85
|
trace?: never;
|
|
@@ -942,7 +942,7 @@ interface paths {
|
|
|
942
942
|
* Create an API key
|
|
943
943
|
* @description Creates a new API key. The plaintext `key` is returned only in this response and never shown again, so store it securely.
|
|
944
944
|
*
|
|
945
|
-
* A credential is a set of permissions and nothing else. `permissions` names the permissions the key holds
|
|
945
|
+
* A credential is a set of permissions and nothing else. `permissions` names the permissions the key holds, and anything named beyond what the creator holds is refused, so a mint can narrow and can never widen. A body naming no map and no claimed source takes the creator's whole set, permissions and maps alike; a body naming any of them holds only what it names, so a key minted with a type map and no `permissions` holds no permission. A map entry beyond the creator's is refused the same way, and a signed-in app must hold `keys.mint` to reach this route at all.
|
|
946
946
|
*
|
|
947
947
|
* `source` is the key's own, and no other unrevoked key may hold it as its own, though keys claiming it write under it too. `sources` names the sources the key claims besides it, which a write may name so its rows are keyed by the claimed source; a working key may grant only its own `source` and what it claims itself.
|
|
948
948
|
*
|
|
@@ -957,6 +957,26 @@ interface paths {
|
|
|
957
957
|
patch?: never;
|
|
958
958
|
trace?: never;
|
|
959
959
|
};
|
|
960
|
+
"/keys/current": {
|
|
961
|
+
parameters: {
|
|
962
|
+
query?: never;
|
|
963
|
+
header?: never;
|
|
964
|
+
path?: never;
|
|
965
|
+
cookie?: never;
|
|
966
|
+
};
|
|
967
|
+
/**
|
|
968
|
+
* Read the calling key
|
|
969
|
+
* @description Returns the key the request bears, without plaintext: its permissions, its maps, its claimed sources and its tier. Any key may read itself, whatever it holds, so a process handed a key can check it holds what it should and no more; every other key stays behind `keys.mint`. A signed-in app's token is not a key, and is refused.
|
|
970
|
+
*/
|
|
971
|
+
get: operations["getCurrentKey"];
|
|
972
|
+
put?: never;
|
|
973
|
+
post?: never;
|
|
974
|
+
delete?: never;
|
|
975
|
+
options?: never;
|
|
976
|
+
head?: never;
|
|
977
|
+
patch?: never;
|
|
978
|
+
trace?: never;
|
|
979
|
+
};
|
|
960
980
|
"/keys/{id}": {
|
|
961
981
|
parameters: {
|
|
962
982
|
query?: never;
|
|
@@ -1234,7 +1254,9 @@ interface paths {
|
|
|
1234
1254
|
*
|
|
1235
1255
|
* The stream opens with a `stream_cursor` frame, carrying `{ "type": "stream_cursor", "cursor": "<event id>" }` — the log position the stream opened at. It does not wait for anything to happen, so a client that subscribes and then reads a snapshot holds a resume point from the first moment rather than waiting for an event to tell it where it is. The frame deliberately carries no SSE `id:` field: on a reconnect it precedes the backlog, and a client adopting it as its cursor there would discard exactly the events it reconnected for.
|
|
1236
1256
|
*
|
|
1237
|
-
* Treat the frame as the first one delivered rather than as guaranteed. Reading the head is bounded, so a stream opened while the database is not answering carries no cursor instead of holding its events back, and a client that receives none proceeds
|
|
1257
|
+
* Treat the frame as the first one delivered rather than as guaranteed. Reading the head is bounded, so a stream opened while the database is not answering carries no cursor instead of holding its events back, and a client that receives none proceeds with no cursor of its own. Do not gate hydration on its arrival.
|
|
1258
|
+
*
|
|
1259
|
+
* Once the replay is done, and the live frames held while it ran are drained, the stream sends a `stream_live` frame, carrying `{ "type": "stream_live", "cursor": "<event id>" | null }` and no SSE `id:`. It says the prologue is over: everything up to `cursor` has been sent or withheld, and what follows is live. A frame the `type` filter or the credential withholds is not written at all, so a client cannot otherwise tell that it has caught up, and its cursor is one a client may resume from without being sent again what the replay covered. It is null only where no position is known: a head read that outran its budget with nothing to replay. A stream that ends short never sends it.
|
|
1238
1260
|
*
|
|
1239
1261
|
* The cursor is a position in one ascending sequence, and `type` and `edges` select a subset of that sequence rather than reordering it, so a cursor taken under one filter can be replayed under another without skipping or repeating a row.
|
|
1240
1262
|
*
|
|
@@ -1384,6 +1406,7 @@ interface components {
|
|
|
1384
1406
|
tier: components["schemas"]["Tier"];
|
|
1385
1407
|
occurred_at: string;
|
|
1386
1408
|
source_id: string | null;
|
|
1409
|
+
type: string;
|
|
1387
1410
|
};
|
|
1388
1411
|
MergePolicy: {
|
|
1389
1412
|
fields?: {
|
|
@@ -2356,7 +2379,7 @@ interface components {
|
|
|
2356
2379
|
source: string;
|
|
2357
2380
|
/** @description The sources a write by this key may name besides its own `source`. Empty on a key that claims nothing. */
|
|
2358
2381
|
sources?: string[];
|
|
2359
|
-
/** @description The permissions this credential holds, as the literals themselves. Omitted on a create request takes the creator's whole set;
|
|
2382
|
+
/** @description The permissions this credential holds, as the literals themselves. Omitted on a create request that names no map and no claimed source, it takes the creator's whole set; omitted beside a map or a claimed source, the key holds none. Anything named beyond what the creator holds is refused. */
|
|
2360
2383
|
permissions?: components["schemas"]["Permission"][];
|
|
2361
2384
|
/** @description The registered client that minted this key, when a signed-in app did. Absent on a key a person or another key created directly. */
|
|
2362
2385
|
oauth_client_id?: string;
|
|
@@ -8286,7 +8309,7 @@ interface operations {
|
|
|
8286
8309
|
};
|
|
8287
8310
|
requestBody: {
|
|
8288
8311
|
content: {
|
|
8289
|
-
"application/octet-stream":
|
|
8312
|
+
"application/octet-stream": Blob | ArrayBuffer | ArrayBufferView | ReadableStream<Uint8Array>;
|
|
8290
8313
|
};
|
|
8291
8314
|
};
|
|
8292
8315
|
responses: {
|
|
@@ -10291,6 +10314,92 @@ interface operations {
|
|
|
10291
10314
|
};
|
|
10292
10315
|
};
|
|
10293
10316
|
};
|
|
10317
|
+
getCurrentKey: {
|
|
10318
|
+
parameters: {
|
|
10319
|
+
query?: never;
|
|
10320
|
+
header?: never;
|
|
10321
|
+
path?: never;
|
|
10322
|
+
cookie?: never;
|
|
10323
|
+
};
|
|
10324
|
+
requestBody?: never;
|
|
10325
|
+
responses: {
|
|
10326
|
+
/** @description The calling key */
|
|
10327
|
+
200: {
|
|
10328
|
+
headers: {
|
|
10329
|
+
"X-Marfa-Contract": components["headers"]["X-Marfa-Contract"];
|
|
10330
|
+
"X-Request-ID": components["headers"]["X-Request-ID"];
|
|
10331
|
+
"X-RateLimit-Limit": components["headers"]["X-RateLimit-Limit"];
|
|
10332
|
+
"X-RateLimit-Remaining": components["headers"]["X-RateLimit-Remaining"];
|
|
10333
|
+
"X-RateLimit-Reset": components["headers"]["X-RateLimit-Reset"];
|
|
10334
|
+
[name: string]: unknown;
|
|
10335
|
+
};
|
|
10336
|
+
content: {
|
|
10337
|
+
"application/json": components["schemas"]["ApiKey"];
|
|
10338
|
+
};
|
|
10339
|
+
};
|
|
10340
|
+
/** @description Unauthorized */
|
|
10341
|
+
401: {
|
|
10342
|
+
headers: {
|
|
10343
|
+
"X-Marfa-Contract": components["headers"]["X-Marfa-Contract"];
|
|
10344
|
+
"X-Request-ID": components["headers"]["X-Request-ID"];
|
|
10345
|
+
"X-RateLimit-Limit": components["headers"]["X-RateLimit-Limit"];
|
|
10346
|
+
"X-RateLimit-Remaining": components["headers"]["X-RateLimit-Remaining"];
|
|
10347
|
+
"X-RateLimit-Reset": components["headers"]["X-RateLimit-Reset"];
|
|
10348
|
+
"X-Error-Code": components["headers"]["X-Error-Code"];
|
|
10349
|
+
[name: string]: unknown;
|
|
10350
|
+
};
|
|
10351
|
+
content: {
|
|
10352
|
+
"application/json": components["schemas"]["UnauthorizedRefusal"];
|
|
10353
|
+
};
|
|
10354
|
+
};
|
|
10355
|
+
/** @description The credential is a signed-in app's token, not a key */
|
|
10356
|
+
403: {
|
|
10357
|
+
headers: {
|
|
10358
|
+
"X-Marfa-Contract": components["headers"]["X-Marfa-Contract"];
|
|
10359
|
+
"X-Request-ID": components["headers"]["X-Request-ID"];
|
|
10360
|
+
"X-RateLimit-Limit": components["headers"]["X-RateLimit-Limit"];
|
|
10361
|
+
"X-RateLimit-Remaining": components["headers"]["X-RateLimit-Remaining"];
|
|
10362
|
+
"X-RateLimit-Reset": components["headers"]["X-RateLimit-Reset"];
|
|
10363
|
+
"X-Error-Code": components["headers"]["X-Error-Code"];
|
|
10364
|
+
[name: string]: unknown;
|
|
10365
|
+
};
|
|
10366
|
+
content: {
|
|
10367
|
+
"application/json": components["schemas"]["ForbiddenRefusal"];
|
|
10368
|
+
};
|
|
10369
|
+
};
|
|
10370
|
+
/** @description Refused by the request limiter: the credential has spent its allowance for the current window, and `Retry-After` says how long to wait. The limiter is only mounted on a deployment that enables rate limiting. */
|
|
10371
|
+
429: {
|
|
10372
|
+
headers: {
|
|
10373
|
+
"X-Marfa-Contract": components["headers"]["X-Marfa-Contract"];
|
|
10374
|
+
"X-Request-ID": components["headers"]["X-Request-ID"];
|
|
10375
|
+
"X-RateLimit-Limit": components["headers"]["X-RateLimit-Limit"];
|
|
10376
|
+
"X-RateLimit-Remaining": components["headers"]["X-RateLimit-Remaining"];
|
|
10377
|
+
"X-RateLimit-Reset": components["headers"]["X-RateLimit-Reset"];
|
|
10378
|
+
"X-Error-Code": components["headers"]["X-Error-Code"];
|
|
10379
|
+
"Retry-After": components["headers"]["Retry-After"];
|
|
10380
|
+
[name: string]: unknown;
|
|
10381
|
+
};
|
|
10382
|
+
content: {
|
|
10383
|
+
"application/json": components["schemas"]["RateLimitedRefusal"];
|
|
10384
|
+
};
|
|
10385
|
+
};
|
|
10386
|
+
/** @description The write met the database's write lock and did not get it inside the instance's busy budget, which `details.budget_ms` names. Nothing was written, and nothing about the request needs changing: retry it. */
|
|
10387
|
+
503: {
|
|
10388
|
+
headers: {
|
|
10389
|
+
"X-Marfa-Contract": components["headers"]["X-Marfa-Contract"];
|
|
10390
|
+
"X-Request-ID": components["headers"]["X-Request-ID"];
|
|
10391
|
+
"X-RateLimit-Limit": components["headers"]["X-RateLimit-Limit"];
|
|
10392
|
+
"X-RateLimit-Remaining": components["headers"]["X-RateLimit-Remaining"];
|
|
10393
|
+
"X-RateLimit-Reset": components["headers"]["X-RateLimit-Reset"];
|
|
10394
|
+
"X-Error-Code": components["headers"]["X-Error-Code"];
|
|
10395
|
+
[name: string]: unknown;
|
|
10396
|
+
};
|
|
10397
|
+
content: {
|
|
10398
|
+
"application/json": components["schemas"]["WriteContentionRefusal"];
|
|
10399
|
+
};
|
|
10400
|
+
};
|
|
10401
|
+
};
|
|
10402
|
+
};
|
|
10294
10403
|
revokeKey: {
|
|
10295
10404
|
parameters: {
|
|
10296
10405
|
query?: never;
|
|
@@ -10804,7 +10913,7 @@ interface operations {
|
|
|
10804
10913
|
};
|
|
10805
10914
|
requestBody: {
|
|
10806
10915
|
content: {
|
|
10807
|
-
"application/gzip":
|
|
10916
|
+
"application/gzip": Blob | ArrayBuffer | ArrayBufferView | ReadableStream<Uint8Array>;
|
|
10808
10917
|
};
|
|
10809
10918
|
};
|
|
10810
10919
|
responses: {
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import createFetchClient
|
|
2
|
+
import createFetchClient, {
|
|
3
|
+
defaultBodySerializer,
|
|
4
|
+
mergeHeaders
|
|
5
|
+
} from "openapi-fetch";
|
|
6
|
+
|
|
7
|
+
// src/generated/byte-bodies.ts
|
|
8
|
+
var BYTE_BODIES = {
|
|
9
|
+
"POST /blobs": "application/octet-stream",
|
|
10
|
+
"POST /admin/restore-archive": "application/gzip"
|
|
11
|
+
};
|
|
3
12
|
|
|
4
13
|
// src/generated/contract.ts
|
|
5
14
|
var CONTRACT_VERSION = 3;
|
|
@@ -20,6 +29,17 @@ var ContractMismatchError = class extends Error {
|
|
|
20
29
|
status;
|
|
21
30
|
expected;
|
|
22
31
|
};
|
|
32
|
+
var BEARER = /^[A-Za-z0-9\-._~+/]+=*$/;
|
|
33
|
+
function checkCredential(credential) {
|
|
34
|
+
if (BEARER.test(credential)) return;
|
|
35
|
+
if (credential === "") {
|
|
36
|
+
throw new TypeError("credential is empty");
|
|
37
|
+
}
|
|
38
|
+
const at = credential.search(/[^A-Za-z0-9\-._~+/=]/);
|
|
39
|
+
throw new TypeError(
|
|
40
|
+
at === -1 ? "credential is not a bearer token: '=' may only end one, after at least one other character" : `credential is not a bearer token: the character at index ${String(at)} is not one a bearer token may hold`
|
|
41
|
+
);
|
|
42
|
+
}
|
|
23
43
|
function createClient(options) {
|
|
24
44
|
const fetcher = options.fetch ?? globalThis.fetch;
|
|
25
45
|
if (/[?#]/.test(options.baseUrl)) {
|
|
@@ -32,6 +52,8 @@ function createClient(options) {
|
|
|
32
52
|
throw new TypeError(`baseUrl must be http or https, not ${base.protocol}`);
|
|
33
53
|
}
|
|
34
54
|
const baseUrl = base.href.replace(/\/+$/, "");
|
|
55
|
+
const credential = options.credential;
|
|
56
|
+
checkCredential(credential);
|
|
35
57
|
const root = new URL(`${baseUrl}/`).href;
|
|
36
58
|
const expected = String(CONTRACT_VERSION);
|
|
37
59
|
const hold = async (response) => {
|
|
@@ -48,7 +70,7 @@ function createClient(options) {
|
|
|
48
70
|
);
|
|
49
71
|
}
|
|
50
72
|
const headers = new Headers(request.headers);
|
|
51
|
-
headers.set("Authorization", `Bearer ${
|
|
73
|
+
headers.set("Authorization", `Bearer ${credential}`);
|
|
52
74
|
return hold(
|
|
53
75
|
await fetcher(new Request(request, { headers, redirect: "error" }))
|
|
54
76
|
);
|
|
@@ -70,10 +92,48 @@ function createClient(options) {
|
|
|
70
92
|
return hold(response);
|
|
71
93
|
}
|
|
72
94
|
};
|
|
73
|
-
const
|
|
95
|
+
const serialize = defaultBodySerializer;
|
|
96
|
+
const client = createFetchClient({
|
|
97
|
+
baseUrl,
|
|
98
|
+
fetch: send,
|
|
99
|
+
bodySerializer: (body, headers) => isBytes(body) ? body : serialize(body, headers)
|
|
100
|
+
});
|
|
74
101
|
client.use(gate);
|
|
102
|
+
labelBytes(client);
|
|
75
103
|
return client;
|
|
76
104
|
}
|
|
105
|
+
var isBytes = (body) => body instanceof Blob || body instanceof ArrayBuffer || ArrayBuffer.isView(body) || body instanceof ReadableStream;
|
|
106
|
+
function labelBytes(client) {
|
|
107
|
+
const label = (method, path, init) => {
|
|
108
|
+
const body = init?.body;
|
|
109
|
+
if (!init || !isBytes(body)) return init;
|
|
110
|
+
const own = body instanceof Blob ? body.type : "";
|
|
111
|
+
const type = own !== "" ? own : BYTE_BODIES[`${method} ${path}`] ?? "application/octet-stream";
|
|
112
|
+
return {
|
|
113
|
+
...init,
|
|
114
|
+
headers: mergeHeaders({ "Content-Type": type }, init.headers),
|
|
115
|
+
// fetch refuses a streamed body unless told it is sent in one
|
|
116
|
+
// direction, before any answer is read.
|
|
117
|
+
...body instanceof ReadableStream ? { duplex: "half" } : {}
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
const request = client.request;
|
|
121
|
+
const labeled = (method, path, init) => request(method, path, label(method.toUpperCase(), path, init));
|
|
122
|
+
const calls = client;
|
|
123
|
+
calls.request = labeled;
|
|
124
|
+
for (const method of [
|
|
125
|
+
"GET",
|
|
126
|
+
"PUT",
|
|
127
|
+
"POST",
|
|
128
|
+
"DELETE",
|
|
129
|
+
"OPTIONS",
|
|
130
|
+
"HEAD",
|
|
131
|
+
"PATCH",
|
|
132
|
+
"TRACE"
|
|
133
|
+
]) {
|
|
134
|
+
calls[method] = (path, init) => labeled(method, path, init);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
77
137
|
async function* pages(fetchPage) {
|
|
78
138
|
let cursor;
|
|
79
139
|
const followed = /* @__PURE__ */ new Set();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@withmarfa/client",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
},
|
|
34
34
|
"scripts": {
|
|
35
35
|
"build": "tsup",
|
|
36
|
-
"generate": "
|
|
36
|
+
"generate": "tsx scripts/generate-schema.ts && tsx scripts/generate-contract.ts",
|
|
37
37
|
"typecheck": "tsc --noEmit",
|
|
38
38
|
"test": "vitest run"
|
|
39
39
|
}
|