@graffiti-garden/api 0.6.4 → 1.0.0

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/src/1-api.ts CHANGED
@@ -2,12 +2,9 @@ import type {
2
2
  GraffitiObjectUrl,
3
3
  GraffitiObject,
4
4
  GraffitiObjectBase,
5
- GraffitiPatch,
6
5
  GraffitiSession,
7
- GraffitiPutObject,
6
+ GraffitiPostObject,
8
7
  GraffitiObjectStream,
9
- ChannelStats,
10
- GraffitiChannelStatsStream,
11
8
  GraffitiObjectStreamContinue,
12
9
  } from "./2-types";
13
10
  import type { JSONSchema } from "json-schema-to-ts";
@@ -44,14 +41,13 @@ import type { JSONSchema } from "json-schema-to-ts";
44
41
  * ## API Overview
45
42
  *
46
43
  * The Graffiti API provides applications with methods for {@link login} and {@link logout},
47
- * methods to store data objects using standard database operations ({@link put}, {@link get}, {@link patch}, and {@link delete}),
48
- * and a method to {@link discover} data objects from other people.
44
+ * methods to interact with data objects using standard database operations ({@link post}, {@link get}, and {@link delete}),
45
+ * and a method to {@link discover} data objects created by others.
49
46
  * These data objects have a couple structured properties:
50
47
  * - {@link GraffitiObjectBase.url | `url`} (string): A globally unique identifier and locator for the object.
51
48
  * - {@link GraffitiObjectBase.actor | `actor`} (string): An unforgeable identifier for the creator of the object.
52
- * - {@link GraffitiObjectBase.allowed | `allowed`} (string[] | undefined): An array of the identities who are allowed to access the object (undefined for public objects).
49
+ * - {@link GraffitiObjectBase.allowed | `allowed`} (string[] | undefined): An array of the actors who are allowed to access the object (undefined for public objects).
53
50
  * - {@link GraffitiObjectBase.channels | `channels`} (string[]): An array of the *contexts* in which the object should appear.
54
- * - {@link GraffitiObjectBase.lastModified | `revision`} (number): A number to compare different versions of an object.
55
51
  *
56
52
  * All other data is stored in the object's unstructured {@link GraffitiObjectBase.value | `value`} property.
57
53
  * This data can be used to represent social artifacts (e.g. posts, profiles) and activities (e.g. likes, follows).
@@ -99,59 +95,45 @@ import type { JSONSchema } from "json-schema-to-ts";
99
95
  * which are Graffiti's means of organizing data contextually, and a concept called "total reification",
100
96
  * which handles explains how moderation, collaboration, and other interactions are managed.
101
97
  *
102
- * @groupDescription CRUD Methods
103
- * Methods for {@link put | creating}, {@link get | reading}, {@link patch | updating},
98
+ * @groupDescription 1 - Single-Object Methods
99
+ * Methods for {@link post | creating}, {@link get | reading},
104
100
  * and {@link delete | deleting} {@link GraffitiObjectBase | Graffiti objects}.
105
- * @groupDescription Query Methods
101
+ * @groupDescription 2 - Multi-Object Methods
106
102
  * Methods that retrieve or accumulate information about multiple {@link GraffitiObjectBase | Graffiti objects} at a time.
107
- * @groupDescription Session Management
103
+ * @groupDescription 3 - Media Methods
104
+ * Methods for {@link postMedia | creating}, {@link getMedia | reading},
105
+ * and {@link deleteMedia | deleting} media data.
106
+ * @groupDescription 4 - Session Management
108
107
  * Methods and properties for logging in and out.
109
108
  */
110
109
  export abstract class Graffiti {
111
110
  /**
112
- * Creates a new {@link GraffitiObjectBase | object} or replaces an existing object.
113
- * An object can only be replaced by the same {@link GraffitiObjectBase.actor | `actor`}
114
- * that created it.
111
+ * Creates a new {@link GraffitiObjectBase | object}.
115
112
  *
116
- * Replacement occurs when the {@link GraffitiObjectBase.url | `url`} of
117
- * the replaced object exactly matches an existing object's URL.
113
+ * @returns Returns the object that has been posted, complete with its
114
+ * assigned {@link GraffitiObjectBase.url | `url`} and
115
+ * {@link GraffitiObjectBase.actor | `actor`}.
118
116
  *
119
- * @throws {@link GraffitiErrorNotFound} if a {@link GraffitiObjectBase.url | `url`}
120
- * is provided that has not been created yet or the {@link GraffitiObjectBase.actor | `actor`}
121
- * is not {@link GraffitiObjectBase.allowed | `allowed`} to see it.
122
- *
123
- * @throws {@link GraffitiErrorForbidden} if the {@link GraffitiObjectBase.actor | `actor`}
124
- * is not the same `actor` as the one who created the object.
125
- *
126
- * @returns Returns the object that was replaced if one one exists, otherwise returns an object with
127
- * with an empty {@link GraffitiObjectBase.value | `value`},
128
- * {@link GraffitiObjectBase.channels | `channels`}, and {@link GraffitiObjectBase.allowed | `allowed`}
129
- * list.
130
- * The {@link GraffitiObjectBase.lastModified | `lastModified`} property of the returned object
131
- * will be updated to the time of replacement/creation.
132
- *
133
- * @group CRUD Methods
117
+ * @group 1 - Single-Object Methods
134
118
  */
135
- abstract put<Schema extends JSONSchema>(
119
+ abstract post<Schema extends JSONSchema>(
136
120
  /**
137
- * The object to be put. This object is statically type-checked against the [JSON schema](https://json-schema.org/) that can be optionally provided
138
- * as the generic type parameter. We highly recommend providing a schema to
139
- * ensure that the put object matches subsequent {@link get} or {@link discover}
121
+ * An object to post. This object is statically type-checked against the [JSON schema](https://json-schema.org/) that can be optionally provided
122
+ * as the generic type parameter. It is recommended to a schema to
123
+ * ensure that the posted object matches subsequent {@link get} or {@link discover}
140
124
  * methods.
141
125
  */
142
- object: GraffitiPutObject<Schema>,
126
+ object: GraffitiPostObject<Schema>,
143
127
  /**
144
128
  * An implementation-specific object with information to authenticate the
145
129
  * {@link GraffitiObjectBase.actor | `actor`}.
146
130
  */
147
131
  session: GraffitiSession,
148
- ): Promise<GraffitiObjectBase>;
132
+ ): Promise<GraffitiObject<Schema>>;
149
133
 
150
134
  /**
151
- * Retrieves an object from a given {@link GraffitiObjectBase.url | `url`}.
152
- *
153
- * The retrieved object is type-checked against the provided [JSON schema](https://json-schema.org/)
154
- * otherwise a {@link GraffitiErrorSchemaMismatch} is thrown.
135
+ * Retrieves an object from a given {@link GraffitiObjectBase.url | `url`} matching
136
+ * the provided `schema`.
155
137
  *
156
138
  * If the retreiving {@link GraffitiObjectBase.actor | `actor`} is not
157
139
  * the object's `actor`,
@@ -166,7 +148,7 @@ export abstract class Graffiti {
166
148
  *
167
149
  * @throws {@link GraffitiErrorSchemaMismatch} if the retrieved object does not match the provided schema.
168
150
  *
169
- * @group CRUD Methods
151
+ * @group 1 - Single-Object Methods
170
152
  */
171
153
  abstract get<Schema extends JSONSchema>(
172
154
  /**
@@ -187,61 +169,18 @@ export abstract class Graffiti {
187
169
  ): Promise<GraffitiObject<Schema>>;
188
170
 
189
171
  /**
190
- * Patches an existing object at a given {@link GraffitiObjectBase.url | `url`}.
191
- * The patching {@link GraffitiObjectBase.actor | `actor`} must be the same as the
192
- * `actor` that created the object.
193
- *
194
- * @returns Returns the original object prior to the patch with its
195
- * {@link GraffitiObjectBase.lastModified | `lastModified`}
196
- * property updated to the time of patching.
197
- *
198
- * @throws {@link GraffitiErrorNotFound} if the object does not exist, has already been deleted,
199
- * or the actor is not {@link GraffitiObjectBase.allowed | `allowed`} to access it.
200
- *
201
- * @throws {@link GraffitiErrorForbidden} if the {@link GraffitiObjectBase.actor | `actor`}
202
- * is not the same `actor` as the one who created the object.
203
- *
204
- * @group CRUD Methods
205
- */
206
- abstract patch(
207
- /**
208
- * A collection of [JSON Patch](https://jsonpatch.com) operations
209
- * to apply to the object. See {@link GraffitiPatch} for more information.
210
- */
211
- patch: GraffitiPatch,
212
- /**
213
- * The location of the object to patch.
214
- */
215
- url: string | GraffitiObjectUrl,
216
- /**
217
- * An implementation-specific object with information to authenticate the
218
- * {@link GraffitiObjectBase.actor | `actor`}.
219
- */
220
- session: GraffitiSession,
221
- ): Promise<GraffitiObjectBase>;
222
-
223
- /**
224
- * Deletes an object from a given {@link GraffitiObjectBase.url | `url`}.
172
+ * Deletes an object from a given {@link GraffitiObjectBase.url | `url`}
173
+ * that had previously been {@link post | `post`ed}.
225
174
  * The deleting {@link GraffitiObjectBase.actor | `actor`} must be the same as the
226
175
  * `actor` that created the object.
227
176
  *
228
- * It is not possible to re-{@link put} an object that has been deleted
229
- * to ensure a person's [right to be forgotten](https://en.wikipedia.org/wiki/Right_to_be_forgotten).
230
- * In cases where deleting and restoring an object is useful, an object's
231
- * {@link GraffitiObjectBase.allowed | `allowed`} property can be set to
232
- * an empty list to hide it from all actors except the creator.
233
- *
234
- * @returns Returns the object that was deleted with its
235
- * {@link GraffitiObjectBase.lastModified | `lastModified`}
236
- * property updated to the time of deletion.
237
- *
238
- * @throws {@link GraffitiErrorNotFound} if the object does not exist, has already been deleted,
177
+ * @throws {@link GraffitiErrorNotFound} if the object does not exist, has already been deleted,
239
178
  * or the actor is not {@link GraffitiObjectBase.allowed | `allowed`} to access it.
240
179
  *
241
180
  * @throws {@link GraffitiErrorForbidden} if the {@link GraffitiObjectBase.actor | `actor`}
242
181
  * is not the same `actor` as the one who created the object.
243
182
  *
244
- * @group CRUD Methods
183
+ * @group 1 - Single-Object Methods
245
184
  */
246
185
  abstract delete(
247
186
  /**
@@ -253,7 +192,7 @@ export abstract class Graffiti {
253
192
  * {@link GraffitiObjectBase.actor | `actor`}.
254
193
  */
255
194
  session: GraffitiSession,
256
- ): Promise<GraffitiObjectBase>;
195
+ ): Promise<void>;
257
196
 
258
197
  /**
259
198
  * Discovers objects created by any actor that are contained
@@ -270,7 +209,7 @@ export abstract class Graffiti {
270
209
  * string can be serialized to continue the stream after an application is closed
271
210
  * and reopened.
272
211
  *
273
- * `discover` will not return objects that the {@link GraffitiObjectBase.actor | `actor`}
212
+ * `discover` will not return objects that the querying {@link GraffitiObjectBase.actor | `actor`}
274
213
  * is not {@link GraffitiObjectBase.allowed | `allowed`} to access.
275
214
  * If the `actor` is not the creator of a discovered object,
276
215
  * the allowed list will be masked to only contain the querying actor if the
@@ -281,14 +220,11 @@ export abstract class Graffiti {
281
220
  *
282
221
  * Since different implementations may fetch data from multiple sources there is
283
222
  * no guarentee on the order that objects are returned in.
284
- * It is also possible that duplicate objects are returned and their
285
- * {@link GraffitiObjectBase.lastModified | `lastModified`} fields must be used
286
- * to determine which object is the most recent.
287
223
  *
288
224
  * @returns Returns a stream of objects that match the given {@link GraffitiObjectBase.channels | `channels`}
289
225
  * and [JSON Schema](https://json-schema.org).
290
226
  *
291
- * @group Query Methods
227
+ * @group 2 - Multi-Object Methods
292
228
  */
293
229
  abstract discover<Schema extends JSONSchema>(
294
230
  /**
@@ -309,95 +245,119 @@ export abstract class Graffiti {
309
245
  ): GraffitiObjectStream<Schema>;
310
246
 
311
247
  /**
312
- * Discovers objects **not** contained in any
313
- * {@link GraffitiObjectBase.channels | `channels`}
314
- * that were created by the querying {@link GraffitiObjectBase.actor | `actor`}
315
- * and match the given [JSON Schema](https://json-schema.org).
316
- * Unlike {@link discover}, this method will not return objects created by other actors.
248
+ * Continues a {@link GraffitiObjectStream} from a given
249
+ * {@link GraffitiObjectStreamReturn.cursor | `cursor`} string.
250
+ * The continuation will return new objects that have been {@link post | `post`ed}
251
+ * that match the original stream, and also returns the
252
+ * {@link GraffitiObjectBase.url | `url`}s of objects that
253
+ * have been {@link delete | `delete`d}, as marked by a `tombstone`.
254
+ *
255
+ * The `cursor` allows the client to
256
+ * serialize the state of the stream and continue it later.
257
+ * However this method loses any typing information that was
258
+ * present in the original stream. For better type safety
259
+ * and when serializing is not necessary, use the
260
+ * {@link GraffitiObjectStreamReturn.continue | `continue`} method
261
+ * instead, which is returned along with the `cursor` at the
262
+ * end of the original stream.
263
+ *
264
+ * @throws {@link GraffitiErrorForbidden} if the {@link GraffitiObjectBase.actor | `actor`}
265
+ * provided in the `session` is not the same as the `actor`
266
+ * that initiated the original stream.
317
267
  *
318
- * Like {@link channelStats}, this method is not useful for most applications,
319
- * but necessary for getting a global view of all an actor's Graffiti data
320
- * to implement something like Facebook's Activity Log or a debugging interface.
268
+ * @group 2 - Multi-Object Methods
269
+ */
270
+ abstract continueDiscover(
271
+ cursor: string,
272
+ session?: GraffitiSession | null,
273
+ ): GraffitiObjectStreamContinue<{}>;
274
+
275
+ /**
276
+ * Uploads media data, such as an image or video.
321
277
  *
322
- * Like {@link discover}, objects are returned asynchronously as they are discovered,
323
- * the stream will end once all leads have been exhausted, and the stream
324
- * can be continued using the {@link GraffitiObjectStreamReturn.continue | `continue`}
325
- * method or {@link GraffitiObjectStreamReturn.cursor | `cursor`} string.
278
+ * Unlike structured {@link GraffitiObjectBase | objects},
279
+ * media is not indexed for {@link discover | `discover`y} and
280
+ * must be retrieved by its exact URL using {@link getMedia}
326
281
  *
327
- * @returns Returns a stream of objects created by the querying {@link GraffitiObjectBase.actor | `actor`}
328
- * that do not belong to any {@link GraffitiObjectBase.channels | `channels`}
329
- * and match the given [JSON Schema](https://json-schema.org).
282
+ * @returns The URL that the media was posted to.
330
283
  *
331
- * @group Query Methods
284
+ * @group 3 - Media Methods
332
285
  */
333
- abstract recoverOrphans<Schema extends JSONSchema>(
286
+ abstract postMedia(
334
287
  /**
335
- * A [JSON Schema](https://json-schema.org) that orphaned objects must satisfy.
288
+ * The binary data of the media to be uploaded,
289
+ * along with its [media type](https://www.iana.org/assignments/media-types/media-types.xhtml),
290
+ * formatted as a [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
336
291
  */
337
- schema: Schema,
292
+ media: Blob,
338
293
  /**
339
294
  * An implementation-specific object with information to authenticate the
340
295
  * {@link GraffitiObjectBase.actor | `actor`}.
341
296
  */
342
297
  session: GraffitiSession,
343
- ): GraffitiObjectStream<Schema>;
298
+ ): Promise<string>;
344
299
 
345
300
  /**
346
- * Returns statistics about all the {@link GraffitiObjectBase.channels | `channels`}
347
- * that an {@link GraffitiObjectBase.actor | `actor`} has posted to.
348
- * This method will not return statistics related to any other actor's channel usage.
349
- *
350
- * Like {@link recoverOrphans}, this method is not useful for most applications,
351
- * but necessary for getting a global view of all an actor's Graffiti data
352
- * to implement something like Facebook's Activity Log or a debugging interface.
301
+ * Deletes media previously {@link postMedia | `post`ed} to a given URL.
353
302
  *
354
- * Like {@link discover}, objects are returned asynchronously as they are discovered and
355
- * the stream will end once all leads have been exhausted.
303
+ * @throws {@link GraffitiErrorNotFound} if no media at that URL exists.
356
304
  *
357
- * @group Query Methods
305
+ * @throws {@link GraffitiErrorForbidden} if the {@link GraffitiObjectBase.actor | `actor`}
306
+ * provided in the `session` is not the same as the `actor` that {@link postMedia | `post`ed}
307
+ * the media.
358
308
  *
359
- * @returns Returns a stream of statistics for each {@link GraffitiObjectBase.channels | `channel`}
360
- * that the {@link GraffitiObjectBase.actor | `actor`} has posted to.
309
+ * @group 3 - Media Methods
361
310
  */
362
- abstract channelStats(
311
+ abstract deleteMedia(
312
+ /**
313
+ * A globally unique identifier and locator for the media.
314
+ */
315
+ mediaUrl: string,
363
316
  /**
364
317
  * An implementation-specific object with information to authenticate the
365
318
  * {@link GraffitiObjectBase.actor | `actor`}.
366
319
  */
367
320
  session: GraffitiSession,
368
- ): GraffitiChannelStatsStream;
321
+ ): Promise<void>;
369
322
 
370
323
  /**
371
- * Continues a {@link GraffitiObjectStream} from a given
372
- * {@link GraffitiObjectStreamReturn.cursor | `cursor`} string.
373
- * The continuation will return new objects that have been created
374
- * that match the original stream, and also returns the
375
- * {@link GraffitiObjectBase.url | `url`}s of objects that
376
- * have been deleted, as marked by a `tombstone`.
324
+ * Retrieves media from the given media URL, adhering to the given requirements.
377
325
  *
378
- * The continuation may also include duplicates of objects that
379
- * were already returned by the original stream. This is dependent
380
- * on how much state the underlying implementation maintains.
326
+ * @throws {@link GraffitiErrorNotFound} if no media at that URL exists.
381
327
  *
382
- * The `cursor` allows the client to
383
- * serialize the state of the stream and continue it later.
384
- * However this method loses any typing information that was
385
- * present in the original stream. For better type safety
386
- * and when serializing is not necessary, use the
387
- * {@link GraffitiObjectStreamReturn.continue | `continue`} method
388
- * instead, which is returned along with the `cursor` at the
389
- * end of the original stream.
328
+ * @throws {@link GraffitiErrorTooLarge} if the media exceeds the given `maxBytes`.
390
329
  *
391
- * @throws {@link GraffitiErrorForbidden} if the {@link GraffitiObjectBase.actor | `actor`}
392
- * provided in the `session` is not the same as the `actor`
393
- * that initiated the original stream.
330
+ * @throws {@link GraffitiErrorNotAcceptable} if the media does not match the given
331
+ * `accept` specification.
394
332
  *
395
- * @group Query Methods
333
+ * @returns The URL of the retrieved media, as a [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob)
334
+ * and the {@link GraffitiObjectBase.actor | `actor`} that posted it.
335
+ *
336
+ * @group 3 - Media Methods
396
337
  */
397
- abstract continueObjectStream(
398
- cursor: string,
399
- session?: GraffitiSession | null,
400
- ): GraffitiObjectStreamContinue<{}>;
338
+ abstract getMedia(
339
+ /**
340
+ * A globally unique identifier and locator for the media.
341
+ */
342
+ mediaUrl: string,
343
+ /**
344
+ * An optional set of requirements the retrieved media must meet.
345
+ */
346
+ requirements?: {
347
+ /**
348
+ * A list of acceptable media types for the retrieved media,
349
+ * formatted as like an [HTTP Accept header](https://httpwg.org/specs/rfc9110.html#field.accept)
350
+ */
351
+ accept?: string;
352
+ /**
353
+ * The maximum size, in bytes, of the media.
354
+ */
355
+ maxBytes?: number;
356
+ },
357
+ ): Promise<{
358
+ media: Blob;
359
+ actor: string;
360
+ }>;
401
361
 
402
362
  /**
403
363
  * Begins the login process. Depending on the implementation, this may
@@ -409,38 +369,19 @@ export abstract class Graffiti {
409
369
  * asynchronously via {@link Graffiti.sessionEvents | sessionEvents}
410
370
  * as a {@link GraffitiLoginEvent} with event type `login`.
411
371
  *
412
- * @group Session Management
372
+ * @group 4 - Session Management
413
373
  */
414
374
  abstract login(
415
375
  /**
416
- * Suggestions for the permissions that the
417
- * login process should grant. The login process may not
418
- * provide the exact proposed permissions.
376
+ * A suggested actor to login as. For example, if a user tries to
377
+ * edit a post but are not logged in, the interface can infer that
378
+ * they might want to log in as the actor who created the post
379
+ * they are attempting to edit.
380
+ *
381
+ * Even if provided, the implementation should allow the user
382
+ * to log in as a different actor if they choose.
419
383
  */
420
- proposal?: {
421
- /**
422
- * A suggested actor to login as. For example, if a user tries to
423
- * edit a post but are not logged in, the interface can infer that
424
- * they might want to log in as the actor who created the post
425
- * they are attempting to edit.
426
- *
427
- * Even if provided, the implementation should allow the user
428
- * to log in as a different actor if they choose.
429
- */
430
- actor?: string;
431
- /**
432
- * A yet to be defined permissions scope. An application may use
433
- * this to indicate the minimum necessary scope needed to
434
- * operate. For example, it may need to be able read private
435
- * messages from a certain set of channels, or write messages that
436
- * follow a particular schema.
437
- *
438
- * The login process should make it clear what scope an application
439
- * is requesting and allow the user to enhance or reduce that
440
- * scope as necessary.
441
- */
442
- scope?: {};
443
- },
384
+ actor?: string,
444
385
  ): Promise<void>;
445
386
 
446
387
  /**
@@ -453,7 +394,7 @@ export abstract class Graffiti {
453
394
  * {@link Graffiti.sessionEvents | sessionEvents}
454
395
  * as a {@link GraffitiLogoutEvent} as event type `logout`.
455
396
  *
456
- * @group Session Management
397
+ * @group 4 - Session Management
457
398
  */
458
399
  abstract logout(
459
400
  /**
@@ -469,7 +410,7 @@ export abstract class Graffiti {
469
410
  * - `logout` - {@link GraffitiLogoutEvent}
470
411
  * - `initialized` - {@link GraffitiSessionInitializedEvent}
471
412
  *
472
- * @group Session Management
413
+ * @group 4 - Session Management
473
414
  */
474
415
  abstract readonly sessionEvents: EventTarget;
475
416
  }