@standards/client 1.0.0-alpha.419 → 1.0.0-alpha.425

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.
@@ -1,6 +1,7 @@
1
1
 
2
- import { r as MultipartRequestOptions, s as TransportConfig, t as HttpTransport } from "./http-transport-BxJhcXjy.js";
3
- import { AggregateRecordsOptions, BundleBuilder, DocumentFile, ExtractRecord, ExtractRecordInputStrict, ExtractRecordUpdateStrict, FilterRule, ObjectBuilderLike, RecordAccessRole, RecordAggregates, RecordAudience, RuleValue, SchemaSourceResolution, SyncBundleResult, UsageReport, UsageScope } from "@standards/schema";
2
+ import { C as OperationTransport, T as TransportConfig, r as InProcessTransport, t as AppClient, v as HttpTransport } from "./app-_CiXRLoD.js";
3
+ import { AggregateRecordsOptions, BundleBuilder, ExtractRecord, ExtractRecordInputStrict, ExtractRecordUpdateStrict, FilterRule, ObjectBuilderLike, RecordAccessRole, RecordAggregates, RecordAudience, RuleValue, SchemaSourceResolution, SyncBundleResult } from "@standards/schema";
4
+ import { ActorId } from "@standards/schema/api";
4
5
  /**
5
6
  * What a sync carries besides the bundle itself.
6
7
  *
@@ -12,123 +13,10 @@ import { AggregateRecordsOptions, BundleBuilder, DocumentFile, ExtractRecord, Ex
12
13
  interface SyncBundleOptions {
13
14
  resolutions?: SchemaSourceResolution[];
14
15
  }
15
- /**
16
- * The two verbs a sync needs, typed non-generically: the real `Transport`
17
- * satisfies it, and so does a hand-rolled stub, which a `Pick` of the generic
18
- * signatures would reject.
19
- */
20
- interface SyncTransport {
21
- get(path: string): Promise<unknown>;
22
- put(path: string, body?: unknown): Promise<unknown>;
23
- }
16
+ /** Both calls are contract operations, on any transport. */
17
+ type SyncTransport = Pick<HttpTransport, "call">;
24
18
  /** GET the installed summary; PUT the built bundle when its hash, label, description or uses differ, or it was never synced. */
25
19
  declare function syncBundle(transport: SyncTransport, bundle: BundleBuilder<string, readonly ObjectBuilderLike[]>, options?: SyncBundleOptions): Promise<SyncBundleResult>;
26
- /**
27
- * What the files and documents methods upload: a `File` (or any `Blob` carrying
28
- * a `name`) with a non-empty filename and MIME type. The server checks the MIME
29
- * type, the extension and the file signature against each other, so all three
30
- * travel with the bytes. A `Buffer` or a path is not an input: wrap the bytes in
31
- * `new File([bytes], "name.png", { type: "image/png" })`.
32
- */
33
- type FileInput = Blob & {
34
- readonly name: string;
35
- };
36
- /**
37
- * The timeout of one upload's request, in ms (#4741). A large file on a slow
38
- * link outlives the client's `timeoutMs` (30 s by default) and surfaces as a
39
- * connection failure, possibly after the server stored it — so raise it for
40
- * large uploads rather than retrying a timed-out attach.
41
- */
42
- interface UploadTimeoutOption {
43
- timeoutMs?: number;
44
- }
45
- interface AttachToRecordInput extends UploadTimeoutOption {
46
- objectName: string;
47
- recordId: string;
48
- /** The record's `document` attribute; the server defaults to `attachments`. */
49
- attributeName?: string;
50
- /** Append to this existing document's pack instead of creating a document. */
51
- documentId?: string;
52
- /** The new document's title; the server defaults to the filename without its extension. */
53
- title?: string;
54
- /**
55
- * The bytes to upload, private, in the same request — the input
56
- * `files.upload` takes. The attach route takes one file per call: append
57
- * more by passing the returned `document.id` as `documentId`. An
58
- * already-uploaded file is not an input, so a public upload can never end up
59
- * in a document.
60
- */
61
- file: FileInput;
62
- }
63
- interface AttachToRecordResult {
64
- document: {
65
- id: string;
66
- title: string;
67
- };
68
- file: DocumentFile;
69
- /** `created`: a new document on the record; `appended`: added to `documentId`'s pack. */
70
- action: "created" | "appended";
71
- }
72
- interface DocumentsApi {
73
- /**
74
- * Upload a file and attach it to a record's document attribute in ONE
75
- * multipart `POST /records/:object/:id/documents/attach`. The server
76
- * validates the file against the attribute and deletes the upload if the
77
- * attach fails, so an error never leaves an orphaned file.
78
- */
79
- attachToRecord(input: AttachToRecordInput): Promise<AttachToRecordResult>;
80
- /** The document's files in pack order (`GET /documents/:id/files`). */
81
- listFiles(documentId: string): Promise<DocumentFile[]>;
82
- }
83
- /** The two verbs this module needs, typed non-generically so a stub satisfies it. */
84
- interface DocumentsTransport {
85
- get(path: string): Promise<unknown>;
86
- postMultipart(path: string, form: FormData, options?: MultipartRequestOptions): Promise<unknown>;
87
- }
88
- declare function createDocumentsApi(transport: DocumentsTransport): DocumentsApi;
89
- interface UploadFileBase extends UploadTimeoutOption {
90
- file: FileInput;
91
- /** Stored as the file's folder (`folderPath` on `POST /files/upload`). */
92
- folderPath?: string;
93
- }
94
- /** Anyone holding the returned `publicUrl` can open the file. A public file never joins a document. */
95
- interface UploadPublicFileInput extends UploadFileBase {
96
- public: true;
97
- }
98
- /** The default: readable through a signed URL from `getDownloadUrl`. */
99
- interface UploadPrivateFileInput extends UploadFileBase {
100
- public?: false;
101
- }
102
- type UploadFileInput = UploadPublicFileInput | UploadPrivateFileInput;
103
- interface UploadedFileBase {
104
- id: string;
105
- /** The stored (sanitized) filename. */
106
- name: string;
107
- mimeType: string;
108
- size: number;
109
- }
110
- interface PublicUploadedFile extends UploadedFileBase {
111
- isPublic: true;
112
- /** `{publicFilesUrl}/{id}`: permanent, and never carries a credential. */
113
- publicUrl: string;
114
- }
115
- interface PrivateUploadedFile extends UploadedFileBase {
116
- isPublic: false;
117
- publicUrl?: never;
118
- }
119
- /** Discriminated on `isPublic`: only the public branch has a `publicUrl`. */
120
- type UploadedFile = PublicUploadedFile | PrivateUploadedFile;
121
- interface FilesApi {
122
- /** One multipart `POST /files/upload`. */
123
- upload(input: UploadPublicFileInput): Promise<PublicUploadedFile>;
124
- upload(input: UploadPrivateFileInput): Promise<PrivateUploadedFile>;
125
- upload(input: UploadFileInput): Promise<UploadedFile>;
126
- /** A fresh signed read URL (`GET /files/:id/url`); it expires, so ask again each time. */
127
- getDownloadUrl(fileId: string): Promise<string>;
128
- }
129
- /** The one verb this module needs: the `files` operations. */
130
- type FilesTransport = Pick<HttpTransport, "call">;
131
- declare function createFilesApi(transport: FilesTransport): FilesApi;
132
20
  /** `daily-stats` → `dailyStats`. Object names are kebab-case (enforced by the builder). */
133
21
  type CamelCase<S extends string> = S extends `${infer Head}-${infer Tail}` ? `${Head}${Capitalize<CamelCase<Tail>>}` : S;
134
22
  type Attr<TBuilder> = keyof ExtractRecord<TBuilder> & string;
@@ -145,6 +33,33 @@ type RecordInput<TBuilder> = ExtractRecordInputStrict<TBuilder> & {
145
33
  type RecordUpdate<TBuilder> = ExtractRecordUpdateStrict<TBuilder> & {
146
34
  [key: string]: unknown;
147
35
  };
36
+ /** What `create()` takes beside the values. */
37
+ interface RecordCreateOptions {
38
+ /**
39
+ * Create the record even when required attributes are missing. The values
40
+ * given are still validated.
41
+ * @default false
42
+ */
43
+ allowDraft?: boolean;
44
+ /**
45
+ * The record's `metadata`: application state kept beside its values, never
46
+ * shown as an attribute. System keys are dropped.
47
+ */
48
+ metadata?: Record<string, unknown>;
49
+ }
50
+ /**
51
+ * What `update()` takes beside the patch. No `allowDraft`: the server already
52
+ * validates every update as a draft, so a required attribute the patch leaves
53
+ * out is never refused.
54
+ */
55
+ interface RecordUpdateOptions {
56
+ /**
57
+ * Keys shallow-merged into the record's `metadata`: each one replaced whole,
58
+ * `null` removes it, the keys not named stay. Written with the patch in one
59
+ * transaction.
60
+ */
61
+ metadata?: Record<string, unknown>;
62
+ }
148
63
  /** What `export()` takes beside its columns. */
149
64
  interface RecordsExportOptions {
150
65
  /** Export only these records (at most `EXPORT_MAX_IDS`, 20 000). */
@@ -257,8 +172,27 @@ interface RecordsQuery<TBuilder> {
257
172
  all(): Promise<ExtractRecord<TBuilder>[]>;
258
173
  single(): Promise<ExtractRecord<TBuilder> | null>;
259
174
  get(id: string): Promise<ExtractRecord<TBuilder>>;
260
- create(input: RecordInput<TBuilder>): Promise<ExtractRecord<TBuilder>>;
261
- update(id: string, patch: RecordUpdate<TBuilder>): Promise<ExtractRecord<TBuilder>>;
175
+ /**
176
+ * `POST /records/<object>`. `allowDraft` creates the record with required
177
+ * attributes missing (so its input types every attribute as optional);
178
+ * `metadata` is the record's application state.
179
+ */
180
+ create(input: RecordInput<TBuilder>, options?: RecordCreateOptions): Promise<ExtractRecord<TBuilder>>;
181
+ create(input: RecordUpdate<TBuilder>, options: RecordCreateOptions & {
182
+ allowDraft: true;
183
+ }): Promise<ExtractRecord<TBuilder>>;
184
+ /**
185
+ * `PUT /records/<object>/<id>`: merges the patch into the record. The server
186
+ * validates every update as a draft, so there is no `allowDraft`.
187
+ *
188
+ * With `metadata`, the patch and the metadata are written together by one
189
+ * `record.set` step of `POST /operations`, in one transaction; the record is
190
+ * read first (`GET /records/<object>/<id>`) for the object id the step
191
+ * names. A refused step rejects with the `StandardsRequestError` its code
192
+ * maps to (`StandardsAuthError` for a 403), with that code and status but
193
+ * no field `details`, which the same refusal on `PUT` carries.
194
+ */
195
+ update(id: string, patch: RecordUpdate<TBuilder>, options?: RecordUpdateOptions): Promise<ExtractRecord<TBuilder>>;
262
196
  delete(id: string): Promise<void>;
263
197
  /**
264
198
  * `POST /records/<object>/bulk-create`, 100 inputs per request, sent one after
@@ -299,38 +233,16 @@ interface UntypedBuilder {
299
233
  /** What `standards.from("object-name")` hands back. */
300
234
  type UntypedRecordsQuery = RecordsQuery<UntypedBuilder>;
301
235
  /**
302
- * The verbs this module needs, typed non-generically: the real `Transport`
303
- * satisfies it, and so does a hand-rolled stub, which a `Pick` of the generic
304
- * signatures would reject. `postRaw` only serves `export()`, which refuses
305
- * when a stub leaves it out. Each path is its contract operation's
306
- * (`buildOperationPath`, every parameter one encoded segment); the verbs stay
307
- * the transport's.
236
+ * Every records call is a contract operation (`call`), on any transport, the
237
+ * in-process one included. `export()` alone reads the CSV as it streams, so it
238
+ * needs an HTTP transport's `postRaw`.
308
239
  */
309
- interface RecordsTransport {
310
- get(path: string): Promise<unknown>;
311
- post(path: string, body?: unknown): Promise<unknown>;
240
+ type RecordsTransport = Pick<HttpTransport, "call"> & {
312
241
  postRaw?(path: string, body: unknown): Promise<Response>;
313
- put(path: string, body?: unknown): Promise<unknown>;
314
- delete(path: string): Promise<unknown>;
315
- }
242
+ };
316
243
  /** A builder gives typed records; a bare object name is the untyped escape hatch. */
317
244
  declare function createRecordsQuery<TBuilder extends ObjectBuilderLike>(transport: RecordsTransport, target: TBuilder): RecordsQuery<TBuilder>;
318
245
  declare function createRecordsQuery(transport: RecordsTransport, target: string): UntypedRecordsQuery;
319
- interface UsageReportInput {
320
- /** First day of the window, `YYYY-MM-DD` (UTC, inclusive). */
321
- from: string;
322
- /** Last day of the window, `YYYY-MM-DD` (UTC, inclusive); at most 366 days after `from`. */
323
- to: string;
324
- /** `me` (the default): the caller's own usage. `workspace`: every member's, gated on `billing:read`. */
325
- scope?: UsageScope;
326
- }
327
- interface UsageApi {
328
- /** AI usage and cost over an inclusive UTC day window (`GET /ai/usage`). */
329
- report(input: UsageReportInput): Promise<UsageReport>;
330
- }
331
- /** The one verb this module needs: the `ai.usage` operation. */
332
- type UsageTransport = Pick<HttpTransport, "call">;
333
- declare function createUsageApi(transport: UsageTransport): UsageApi;
334
246
  type NameOf<B> = B extends {
335
247
  _types: {
336
248
  name: infer N extends string;
@@ -342,28 +254,82 @@ type ObjectsOf<TObjects extends readonly ObjectBuilderLike[]> = { readonly [B in
342
254
  type AnyBundle = BundleBuilder<string, readonly ObjectBuilderLike[]>;
343
255
  /** The object builders of every bundle in a tuple: what the typed client infers from. */
344
256
  type ObjectsOfBundles<TBundles extends readonly AnyBundle[]> = TBundles[number]["objectBuilders"];
345
- interface Standards {
346
- bundles: {
347
- /** Sync a bundle to the workspace; a no-op (`applied: false`) when it already holds this version. */
348
- sync(bundle: AnyBundle, options?: SyncBundleOptions): Promise<SyncBundleResult>;
257
+ type ContractSync = AppClient["bundles"]["sync"];
258
+ /** `bundles.sync`, either way: from your bundle's builder, or the contract's call. */
259
+ interface BundlesSync {
260
+ /**
261
+ * Build `bundle`, compare it with the version the workspace holds, and put
262
+ * it only when it changed: `{ applied: false }` when it already holds it.
263
+ */
264
+ (bundle: AnyBundle, options?: SyncBundleOptions): Promise<SyncBundleResult>;
265
+ /** The contract's call, with an artifact you built yourself. */
266
+ (...args: Parameters<ContractSync>): ReturnType<ContractSync>;
267
+ }
268
+ /**
269
+ * The client: every operation of the contract at the root, by group and
270
+ * verb (`decisions.list`, `users.get`, `files.upload`…), with your bundles'
271
+ * objects beside them as typed queries (see {@link StandardsOf}).
272
+ */
273
+ type Standards = Omit<AppClient, "bundles"> & {
274
+ readonly bundles: Omit<AppClient["bundles"], "sync"> & {
275
+ readonly sync: BundlesSync;
349
276
  };
350
277
  /** Untyped access to an object the code does not declare (created at runtime). */
351
278
  from(objectName: string): UntypedRecordsQuery;
352
- /** Upload files (private or public) and read them back through signed URLs. */
353
- files: FilesApi;
354
- /** Attach uploads to a record's document attribute and list a document's files. */
355
- documents: DocumentsApi;
356
- /** AI usage and cost of the caller or the whole workspace. */
357
- usage: UsageApi;
358
- }
279
+ /**
280
+ * Every operation of the contract, under its own name only: the way to a
281
+ * group one of your objects shares a name with, since the object wins at
282
+ * the root.
283
+ */
284
+ readonly contract: AppClient;
285
+ };
286
+ /**
287
+ * The client with your objects: each one a typed query at the root. An object
288
+ * named like a contract group takes its place there (the group stays under
289
+ * `contract`), so a group Standards adds later never changes what your code
290
+ * reads.
291
+ */
292
+ type StandardsOf<TObjects extends readonly ObjectBuilderLike[]> = Omit<Standards, keyof ObjectsOf<TObjects>> & ObjectsOf<TObjects>;
293
+ /**
294
+ * A client in a server that embeds Standards: the same client, plus `as` to
295
+ * name who acts. Absent over HTTP, where the identity is the API key's.
296
+ */
297
+ type ActingStandards<S> = S & {
298
+ /**
299
+ * The same client, every call acting as `actor`: an actor id, or `"system"`
300
+ * for the workspace's system actor. Permissions and visibility are that
301
+ * actor's; the actor the context implies, if any, is recorded as the one
302
+ * who triggered the call. Without `as`, a call acts as the context implies:
303
+ * the request's user, the job's enqueuer, the write's author, else the
304
+ * system.
305
+ */
306
+ as(actor: ActorId | "system"): ActingStandards<S>;
307
+ };
308
+ /**
309
+ * Every call `createStandards` makes is a contract operation (`call`, and
310
+ * `stream` for an `"events"` one); a records `export()` also
311
+ * needs an HTTP transport's `postRaw`, and `as` the in-process transport's
312
+ * `withCaller`.
313
+ */
314
+ type StandardsTransport = OperationTransport & Partial<Pick<HttpTransport, "postRaw">> & Partial<Pick<InProcessTransport, "withCaller">>;
359
315
  /**
360
- * The options `createStandards` takes. Every field of `TransportConfig` is
361
- * optional here: on the server a missing `baseUrl` / `apiKey` is read from
362
- * `STANDARDS_API_URL` / `STANDARDS_API_KEY`, and an explicit value always wins.
316
+ * The options `createStandards` takes: an API key's HTTP connection
317
+ * (`baseUrl`, `apiKey`, read from the environment when absent), or a
318
+ * `transport` of your own (the in-process one in a server that embeds
319
+ * Standards, `createInProcessTransport`), never both.
363
320
  */
364
- type StandardsConfig = Partial<TransportConfig>;
321
+ type StandardsConfig = Partial<TransportConfig> & {
322
+ transport?: StandardsTransport;
323
+ };
324
+ declare function createStandards<const TBundles extends readonly AnyBundle[]>(config: StandardsConfig & {
325
+ transport: InProcessTransport;
326
+ bundles: TBundles;
327
+ }): ActingStandards<StandardsOf<ObjectsOfBundles<TBundles>>>;
328
+ declare function createStandards(config: StandardsConfig & {
329
+ transport: InProcessTransport;
330
+ }): ActingStandards<Standards>;
365
331
  declare function createStandards<const TBundles extends readonly AnyBundle[]>(config: StandardsConfig & {
366
332
  bundles: TBundles;
367
- }): Standards & ObjectsOf<ObjectsOfBundles<TBundles>>;
333
+ }): StandardsOf<ObjectsOfBundles<TBundles>>;
368
334
  declare function createStandards(config?: StandardsConfig): Standards;
369
- export { PrivateUploadedFile as A, DocumentsTransport as B, UpdateManyEntry as C, CamelCase as D, createRecordsQuery as E, UploadedFile as F, SyncTransport as G, FileInput as H, createFilesApi as I, syncBundle as K, AttachToRecordInput as L, UploadFileInput as M, UploadPrivateFileInput as N, FilesApi as O, UploadPublicFileInput as P, AttachToRecordResult as R, UntypedRecordsQuery as S, UpdateManyResult as T, UploadTimeoutOption as U, createDocumentsApi as V, SyncBundleOptions as W, RecordUpdate as _, StandardsConfig as a, RecordsQuery as b, UsageReportInput as c, CreateManyError as d, CreateManyResult as f, RecordInput as g, FetchResult as h, Standards as i, PublicUploadedFile as j, FilesTransport as k, UsageTransport as l, DeleteManyResult as m, ObjectsOf as n, createStandards as o, DeleteManyError as p, ObjectsOfBundles as r, UsageApi as s, AnyBundle as t, createUsageApi as u, RecordsAggregateOptions as v, UpdateManyError as w, UntypedBuilder as x, RecordsExportOptions as y, DocumentsApi as z };
335
+ export { SyncBundleOptions as A, UntypedBuilder as C, UpdateManyResult as D, UpdateManyError as E, syncBundle as M, createRecordsQuery as O, RecordsQuery as S, UpdateManyEntry as T, RecordInput as _, ObjectsOfBundles as a, RecordsAggregateOptions as b, StandardsOf as c, CreateManyError as d, CreateManyResult as f, RecordCreateOptions as g, FetchResult as h, ObjectsOf as i, SyncTransport as j, CamelCase as k, StandardsTransport as l, DeleteManyResult as m, AnyBundle as n, Standards as o, DeleteManyError as p, BundlesSync as r, StandardsConfig as s, ActingStandards as t, createStandards as u, RecordUpdate as v, UntypedRecordsQuery as w, RecordsExportOptions as x, RecordUpdateOptions as y };