@protobase/client 0.21.6 → 0.21.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.
package/dist/client.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { FilterInput, ListParams, MapParams } from "./query-string.js";
2
2
  import { ClientOptions } from "./transport.js";
3
3
  import { UploadProgress } from "./upload-request.js";
4
- import { Aggregate, AggregateParams, BatchOp, BatchResult, Facet, Histogram, HistoryPage, ListPage, MapResult, MetaResult, RecordOf, ResourceRef, Series, SeriesParams, Stored } from "./types.js";
4
+ import { Aggregate, AggregateParams, BatchOp, BatchResult, CommentPage, Facet, FormChallenge, FormProof, Histogram, HistoryPage, ListPage, MapResult, MentionableUser, MetaResult, PublicForm, RecordComment, RecordOf, ResourceRef, Series, SeriesParams, Stored } from "./types.js";
5
5
  import { KeyValue } from "@protobase/schema";
6
6
  //#region src/client.d.ts
7
7
  /** How `invoke` calls a function: GET without a body, POST with one, unless `method` says otherwise. */
@@ -52,11 +52,21 @@ export declare const createClient: (options?: ClientOptions) => {
52
52
  etag?: string;
53
53
  }) => Promise<void>;
54
54
  undelete: <R extends ResourceRef>(resource: R, key: KeyValue | readonly KeyValue[]) => Promise<Stored<RecordOf<R>>>;
55
+ generate: <R extends ResourceRef>(resource: R, key: KeyValue | readonly KeyValue[], action: string) => Promise<Stored<RecordOf<R>>>;
55
56
  reveal: (resource: ResourceRef, key: KeyValue | readonly KeyValue[], field: string) => Promise<unknown>;
56
57
  history: (resource: ResourceRef, key: KeyValue | readonly KeyValue[], { pageSize, pageToken }?: {
57
58
  pageSize?: number;
58
59
  pageToken?: string;
59
60
  }) => Promise<HistoryPage>;
61
+ comments: (resource: ResourceRef, key: KeyValue | readonly KeyValue[], { pageSize, pageToken }?: {
62
+ pageSize?: number;
63
+ pageToken?: string;
64
+ }) => Promise<CommentPage>;
65
+ comment: (resource: ResourceRef, key: KeyValue | readonly KeyValue[], { body, mentions }: {
66
+ body: string;
67
+ mentions?: string[];
68
+ }) => Promise<RecordComment>;
69
+ mentionable: (resource: ResourceRef, key: KeyValue | readonly KeyValue[], query: string) => Promise<MentionableUser[]>;
60
70
  batchWrite: (ops: BatchOp[]) => Promise<BatchResult>;
61
71
  facets: (resource: ResourceRef, field: string, o?: {
62
72
  filter?: FilterInput;
@@ -80,6 +90,9 @@ export declare const createClient: (options?: ClientOptions) => {
80
90
  }) => Promise<MetaResult>;
81
91
  invoke: <T = unknown>(name: string, { method, body, path, query, headers }?: InvokeOptions) => Promise<T>;
82
92
  upload: (resource: ResourceRef, field: string, file: Blob, { onProgress, signal }?: UploadOptions) => Promise<UploadResult>;
93
+ form: (name: string) => Promise<PublicForm>;
94
+ formChallenge: (name: string) => Promise<FormChallenge>;
95
+ submitForm: (name: string, values: Record<string, unknown>, proof: FormProof) => Promise<void>;
83
96
  };
84
97
  export type Client = ReturnType<typeof createClient>;
85
98
  //#endregion
package/dist/client.js CHANGED
@@ -16,6 +16,10 @@ const toPage = (body, response, origin) => ({
16
16
  ...body.photos !== void 0 && { photos: photosWithOrigin(body.photos, origin) },
17
17
  ...response.headers.get("x-protobase-warning") && { warning: response.headers.get("x-protobase-warning") }
18
18
  });
19
+ const commentOf = ({ created_at, ...comment }) => ({
20
+ ...comment,
21
+ createdAt: created_at
22
+ });
19
23
  const etagOf = (response) => response.headers.get("etag") ?? "";
20
24
  /** A single-record response: the record without its `permissions` member, which travels beside it. */
21
25
  const storedOf = async (response, origin) => {
@@ -123,6 +127,15 @@ const createClient = (options = {}) => {
123
127
  body: { field }
124
128
  })).json()).value;
125
129
  };
130
+ /**
131
+ * Runs the record's AI action `action` (`a.ai`): the model writes the action's field from its prompt, as the caller.
132
+ * Resolves to the record as written; a record changed while the model answered rejects with `PreconditionFailedError`.
133
+ */
134
+ const generate = async (resource, key, action) => storedOf(await send({
135
+ method: "POST",
136
+ path: `${keyPath(resource, key)}:generate`,
137
+ body: { action }
138
+ }), origin);
126
139
  /** A page of a record's changes, newest first, for a resource with `.history()`; `pageToken` is the previous page's `nextPageToken`. */
127
140
  const history = async (resource, key, { pageSize, pageToken } = {}) => {
128
141
  const query = queryString({
@@ -142,6 +155,39 @@ const createClient = (options = {}) => {
142
155
  nextPageToken: body.next_page_token
143
156
  };
144
157
  };
158
+ /** A page of a record's comments, newest first, for a resource with `.comments()`; `pageToken` is the previous page's `nextPageToken`. */
159
+ const comments = async (resource, key, { pageSize, pageToken } = {}) => {
160
+ const query = queryString({
161
+ page_size: pageSize,
162
+ page_token: pageToken || void 0
163
+ });
164
+ const body = await (await send({
165
+ method: "GET",
166
+ path: `${keyPath(resource, key)}:comments`,
167
+ query
168
+ })).json();
169
+ return {
170
+ items: body.items.map(commentOf),
171
+ nextPageToken: body.next_page_token
172
+ };
173
+ };
174
+ /** Adds the caller's comment to a record, mentioning the users `mentions` names, who are emailed a link to it. */
175
+ const comment = async (resource, key, { body, mentions = [] }) => commentOf(await (await send({
176
+ method: "POST",
177
+ path: `${keyPath(resource, key)}:comments`,
178
+ body: {
179
+ body,
180
+ mentions
181
+ }
182
+ })).json());
183
+ /** The users a comment on a record may mention whose name contains `query`, at most 8, by name. */
184
+ const mentionable = async (resource, key, query) => {
185
+ return (await (await send({
186
+ method: "GET",
187
+ path: `${keyPath(resource, key)}:mentionable`,
188
+ query: queryString({ q: query })
189
+ })).json()).items;
190
+ };
145
191
  /** Applies several writes atomically (`POST /api/v1:batchWrite`), for example a record and its lines on Save. */
146
192
  const batchWrite = async (ops) => {
147
193
  const keyText = (key) => typeof key === "object" && key !== null && "$ref" in key ? key : encodeKey(key);
@@ -323,6 +369,34 @@ const createClient = (options = {}) => {
323
369
  onProgress?.(file.size, file.size);
324
370
  return result;
325
371
  };
372
+ const formPath = (name) => `/forms/${encodeURIComponent(name)}`;
373
+ /** The public form `name` (`GET /api/forms/<name>`): its fields as `/meta` would give them, its title and thanks text. */
374
+ const form = async (name) => (await send({
375
+ method: "GET",
376
+ path: formPath(name),
377
+ system: true
378
+ })).json();
379
+ /** A fresh bot check for the public form `name`, to solve with `altcha-lib`'s `solveChallenge` before sending it. */
380
+ const formChallenge = async (name) => (await send({
381
+ method: "GET",
382
+ path: `${formPath(name)}:challenge`,
383
+ system: true
384
+ })).json();
385
+ /**
386
+ * Sends the public form `name`: creates its record from `values`, with the solved bot check. Rejects with `ApiError`
387
+ * as a create does, a refused check as `bot-check-failed` and too many submissions as `too-many-submissions`.
388
+ */
389
+ const submitForm = async (name, values, proof) => {
390
+ await send({
391
+ method: "POST",
392
+ path: formPath(name),
393
+ body: {
394
+ values,
395
+ proof
396
+ },
397
+ system: true
398
+ });
399
+ };
326
400
  return {
327
401
  list,
328
402
  search,
@@ -331,8 +405,12 @@ const createClient = (options = {}) => {
331
405
  update,
332
406
  remove,
333
407
  undelete,
408
+ generate,
334
409
  reveal,
335
410
  history,
411
+ comments,
412
+ comment,
413
+ mentionable,
336
414
  batchWrite,
337
415
  facets,
338
416
  aggregate,
@@ -342,7 +420,10 @@ const createClient = (options = {}) => {
342
420
  seek,
343
421
  meta,
344
422
  invoke,
345
- upload
423
+ upload,
424
+ form,
425
+ formChallenge,
426
+ submitForm
346
427
  };
347
428
  };
348
429
  //#endregion
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { FilterInput, ListParams, MapParams, filterText, listWire } from "./query-string.js";
2
2
  import { ClientOptions } from "./transport.js";
3
3
  import { UploadProgress } from "./upload-request.js";
4
- import { Aggregate, AggregateGroup, AggregateParams, BatchOp, BatchRef, BatchResult, Facet, Histogram, HistogramBucket, HistoryChange, HistoryPage, ListPage, MapBounds, MapBucket, MapLayerModel, MapResult, Meta, MetaResult, PluginRef, RecordOf, RecordPermissions, RecordPhotos, RelatedLabels, ResourcePermissions, ResourceRef, Series, SeriesParams, SeriesPoint, SourceRef, Stored, StoredFile, StoredFileVariant } from "./types.js";
4
+ import { Aggregate, AggregateGroup, AggregateParams, BatchOp, BatchRef, BatchResult, CommentPage, CommentPerson, Facet, FormChallenge, FormProof, Histogram, HistogramBucket, HistoryChange, HistoryPage, ListPage, MapBounds, MapBucket, MapLayerModel, MapResult, MentionableUser, Meta, MetaResult, PluginRef, PublicForm, RecordComment, RecordOf, RecordPermissions, RecordPhotos, RelatedLabels, ResourcePermissions, ResourceRef, Series, SeriesParams, SeriesPoint, SourceRef, Stored, StoredFile, StoredFileVariant } from "./types.js";
5
5
  import { Client, InvokeOptions, UploadOptions, UploadResult, createClient } from "./client.js";
6
6
  import { ApiError, PreconditionFailedError, ProblemDetails, ProblemError, isApiError } from "./problem.js";
7
7
  import { fileUrl } from "./file-urls.js";
@@ -18,4 +18,4 @@ import { AuthSession, AuthSessionOptions, createAuthSession, tokenExpiry } from
18
18
  import { createStaticSession } from "./static-session.js";
19
19
  import { AssistantClient, AssistantClientOptions, AssistantConnection, createAssistantClient } from "./assistant.js";
20
20
  import { RuntimeClient, RuntimeClientOptions, RuntimeLastUpdate, RuntimePolicy, RuntimeStatus, RuntimeUpdateStage, createRuntimeClient } from "./runtime.js";
21
- export { type AccountSecurity, type AccountSignIn, type ActiveRole, type Aggregate, type AggregateGroup, type AggregateParams, ApiError, type AssistantClient, type AssistantClientOptions, type AssistantConnection, AuthError, type AuthSession, type AuthSessionOptions, type AuthUser, type AuthenticatorSetup, type BatchOp, type BatchRef, type BatchResult, type Client, type ClientOptions, type CurrentOrganization, type Facet, type FilterInput, type Histogram, type HistogramBucket, type HistoryChange, type HistoryPage, type InvitationPreview, type InvitationSent, type InvokeOptions, type ListPage, type ListParams, type MapBounds, type MapBucket, type MapLayerModel, type MapParams, type MapResult, type Meta, type MetaResult, type Organization, type OrganizationInvitation, type OrganizationMember, type OrganizationRole, type OrganizationsClient, type Passkey, type PlatformSignIn, type PluginRef, PreconditionFailedError, type ProblemDetails, type ProblemError, type RecordOf, type RecordPermissions, type RecordPhotos, type RelatedLabels, type RequiredSetup, type ResourcePermissions, type ResourceRef, type RoleLabel, type RoleOption, type RolesClient, type RuntimeClient, type RuntimeClientOptions, type RuntimeLastUpdate, type RuntimePolicy, type RuntimeStatus, type RuntimeUpdateStage, type Series, type SeriesParams, type SeriesPoint, type SetupStatus, type SignInMethod, type SignInPolicy, type SignInPolicyClient, type SignInPolicyState, type SignInResult, type SignInRule, type SourceMeta, type SourceRef, type StaffAccessRule, type StaffSignIn, type StaffSignInClient, type Stored, type StoredFile, type StoredFileVariant, type TwoFactorMethod, type UploadOptions, type UploadProgress, type UploadResult, createAssistantClient, createAuthSession, createClient, createRuntimeClient, createStaticSession, fileUrl, filterText, isApiError, listWire, routedClient, sourceMeta, tokenExpiry };
21
+ export { type AccountSecurity, type AccountSignIn, type ActiveRole, type Aggregate, type AggregateGroup, type AggregateParams, ApiError, type AssistantClient, type AssistantClientOptions, type AssistantConnection, AuthError, type AuthSession, type AuthSessionOptions, type AuthUser, type AuthenticatorSetup, type BatchOp, type BatchRef, type BatchResult, type Client, type ClientOptions, type CommentPage, type CommentPerson, type CurrentOrganization, type Facet, type FilterInput, type FormChallenge, type FormProof, type Histogram, type HistogramBucket, type HistoryChange, type HistoryPage, type InvitationPreview, type InvitationSent, type InvokeOptions, type ListPage, type ListParams, type MapBounds, type MapBucket, type MapLayerModel, type MapParams, type MapResult, type MentionableUser, type Meta, type MetaResult, type Organization, type OrganizationInvitation, type OrganizationMember, type OrganizationRole, type OrganizationsClient, type Passkey, type PlatformSignIn, type PluginRef, PreconditionFailedError, type ProblemDetails, type ProblemError, type PublicForm, type RecordComment, type RecordOf, type RecordPermissions, type RecordPhotos, type RelatedLabels, type RequiredSetup, type ResourcePermissions, type ResourceRef, type RoleLabel, type RoleOption, type RolesClient, type RuntimeClient, type RuntimeClientOptions, type RuntimeLastUpdate, type RuntimePolicy, type RuntimeStatus, type RuntimeUpdateStage, type Series, type SeriesParams, type SeriesPoint, type SetupStatus, type SignInMethod, type SignInPolicy, type SignInPolicyClient, type SignInPolicyState, type SignInResult, type SignInRule, type SourceMeta, type SourceRef, type StaffAccessRule, type StaffSignIn, type StaffSignInClient, type Stored, type StoredFile, type StoredFileVariant, type TwoFactorMethod, type UploadOptions, type UploadProgress, type UploadResult, createAssistantClient, createAuthSession, createClient, createRuntimeClient, createStaticSession, fileUrl, filterText, isApiError, listWire, routedClient, sourceMeta, tokenExpiry };
@@ -2,8 +2,8 @@ import { Client } from "./client.js";
2
2
  //#region src/routed-client.d.ts
3
3
  /**
4
4
  * One client over the app's own API and the sources it draws resources from: a call about a resource goes to the API
5
- * that serves it (`sourceOf` gives a source's client, or `undefined` for the app's own), and `meta` and `invoke` go to
6
- * the app's own. A batch goes to the one API all its writes are for; one that writes to two cannot be atomic, so it is
5
+ * that serves it (`sourceOf` gives a source's client, or `undefined` for the app's own), and `meta`, `invoke` and the
6
+ * public forms go to the app's own. A batch goes to the one API all its writes are for; one that writes to two cannot be atomic, so it is
7
7
  * refused before anything is sent.
8
8
  */
9
9
  export declare const routedClient: (own: Client, sourceOf: (resource: string) => Client | undefined) => Client;
@@ -2,8 +2,8 @@
2
2
  const nameOf = (resource) => typeof resource === "string" ? resource : resource.toModel().name;
3
3
  /**
4
4
  * One client over the app's own API and the sources it draws resources from: a call about a resource goes to the API
5
- * that serves it (`sourceOf` gives a source's client, or `undefined` for the app's own), and `meta` and `invoke` go to
6
- * the app's own. A batch goes to the one API all its writes are for; one that writes to two cannot be atomic, so it is
5
+ * that serves it (`sourceOf` gives a source's client, or `undefined` for the app's own), and `meta`, `invoke` and the
6
+ * public forms go to the app's own. A batch goes to the one API all its writes are for; one that writes to two cannot be atomic, so it is
7
7
  * refused before anything is sent.
8
8
  */
9
9
  const routedClient = (own, sourceOf) => {
@@ -16,8 +16,12 @@ const routedClient = (own, sourceOf) => {
16
16
  update: (resource, key, patch, etag) => to(resource).update(resource, key, patch, etag),
17
17
  remove: (resource, key, options) => to(resource).remove(resource, key, options),
18
18
  undelete: (resource, key) => to(resource).undelete(resource, key),
19
+ generate: (resource, key, action) => to(resource).generate(resource, key, action),
19
20
  reveal: (resource, key, field) => to(resource).reveal(resource, key, field),
20
21
  history: (resource, key, options) => to(resource).history(resource, key, options),
22
+ comments: (resource, key, options) => to(resource).comments(resource, key, options),
23
+ comment: (resource, key, input) => to(resource).comment(resource, key, input),
24
+ mentionable: (resource, key, query) => to(resource).mentionable(resource, key, query),
21
25
  batchWrite: (ops) => {
22
26
  const apis = [...new Set(ops.map((op) => to(op.resource)))];
23
27
  if (apis.length > 1) return Promise.reject(/* @__PURE__ */ new Error(`A batch writes to one API, but these writes are for ${[...new Set(ops.map((op) => nameOf(op.resource)))].join(", ")}, which different APIs serve`));
@@ -31,7 +35,10 @@ const routedClient = (own, sourceOf) => {
31
35
  seek: (resource, params) => to(resource).seek(resource, params),
32
36
  meta: (etag, options) => own.meta(etag, options),
33
37
  invoke: (name, options) => own.invoke(name, options),
34
- upload: (resource, field, file, options) => to(resource).upload(resource, field, file, options)
38
+ upload: (resource, field, file, options) => to(resource).upload(resource, field, file, options),
39
+ form: (name) => own.form(name),
40
+ formChallenge: (name) => own.formChallenge(name),
41
+ submitForm: (name, values, proof) => own.submitForm(name, values, proof)
35
42
  };
36
43
  };
37
44
  //#endregion
package/dist/types.d.ts CHANGED
@@ -34,6 +34,70 @@ export type HistoryChange = {
34
34
  /** The database role that wrote; `null` when unknown. */
35
35
  databaseRole: string | null;
36
36
  };
37
+ /** A public form as `/-/forms/<name>` draws it: its fields as `/meta` would give them, with their hints, and no more. */
38
+ export type PublicForm = {
39
+ name: string;
40
+ /** The page's heading, when the form gives one; else the view's singular name. */
41
+ title?: string;
42
+ /** The fields a visitor fills in, in order. */
43
+ fields: string[];
44
+ /** What the page shows once the form is sent. */
45
+ thanks: string;
46
+ /** The resource restricted to the form's fields (and its key). */
47
+ resource: ResourceModel;
48
+ /** The view's names and the form fields' hints. */
49
+ view: ViewModel;
50
+ };
51
+ /** A public form's bot check as the server hands it out: an ALTCHA proof-of-work challenge, solved with `altcha-lib`. */
52
+ export type FormChallenge = {
53
+ parameters: {
54
+ algorithm: string;
55
+ nonce: string;
56
+ salt: string;
57
+ cost: number;
58
+ keyLength: number;
59
+ keyPrefix: string;
60
+ keySignature?: string;
61
+ expiresAt?: number;
62
+ data?: Record<string, string | number | boolean | null>;
63
+ };
64
+ signature?: string;
65
+ };
66
+ /** A solved bot check, sent with the form: the challenge as it came, and the solver's answer. */
67
+ export type FormProof = {
68
+ challenge: FormChallenge;
69
+ solution: {
70
+ counter: number;
71
+ derivedKey: string;
72
+ };
73
+ };
74
+ /** Someone a comment names: its author or a person it mentions, with their name when the server knows it. */
75
+ export type CommentPerson = {
76
+ id: string;
77
+ name?: string;
78
+ };
79
+ /** One comment on a record, as `comments` reads it. */
80
+ export type RecordComment = {
81
+ id: string;
82
+ /** When it was written, ISO 8601. */
83
+ createdAt: string;
84
+ /** Plain text, as written. */
85
+ body: string;
86
+ author: CommentPerson;
87
+ /** The users it mentions, each once. */
88
+ mentions: CommentPerson[];
89
+ };
90
+ /** A page of a record's comments, newest first. */
91
+ export type CommentPage = {
92
+ items: RecordComment[];
93
+ /** `""` on the last page. */
94
+ nextPageToken: string;
95
+ };
96
+ /** Someone a comment on a record may mention: a user who may read the record. */
97
+ export type MentionableUser = {
98
+ id: string;
99
+ name: string;
100
+ };
37
101
  /** A page of a record's history, newest first. */
38
102
  export type HistoryPage = {
39
103
  items: HistoryChange[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protobase/client",
3
- "version": "0.21.6",
3
+ "version": "0.21.8",
4
4
  "description": "Typed fetch client for the Protobase REST API.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -30,8 +30,8 @@
30
30
  },
31
31
  "dependencies": {
32
32
  "@better-auth/passkey": "^1.7.7",
33
- "@protobase/layout": "0.21.6",
34
- "@protobase/schema": "0.21.6",
33
+ "@protobase/layout": "0.21.8",
34
+ "@protobase/schema": "0.21.8",
35
35
  "@simplewebauthn/browser": "^13.3.0",
36
36
  "better-auth": "^1.7.7"
37
37
  }