@graffiti-garden/api 0.3.0 → 0.4.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/index.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export * from "./1-api";
2
2
  export * from "./2-types";
3
3
  export * from "./3-errors";
4
- export type { JSONSchema4 } from "json-schema";
4
+ export type { JSONSchema } from "json-schema-to-ts";
@@ -38,7 +38,7 @@ export const graffitiChannelStatsTests = (
38
38
  // one value to channels[2]
39
39
  for (let i = 0; i < 3; i++) {
40
40
  for (let j = 0; j < i + 1; j++) {
41
- await graffiti.put(
41
+ await graffiti.put<{}>(
42
42
  {
43
43
  value: {
44
44
  index: j,
@@ -49,7 +49,7 @@ export const graffitiChannelStatsTests = (
49
49
  );
50
50
  }
51
51
  }
52
- await graffiti.put(
52
+ await graffiti.put<{}>(
53
53
  { value: { index: 3 }, channels: [channels[2]] },
54
54
  session,
55
55
  );
@@ -76,7 +76,7 @@ export const graffitiChannelStatsTests = (
76
76
  const channels = [randomString(), randomString(), randomString()];
77
77
 
78
78
  // Add an item with two channels
79
- const before = await graffiti.put(
79
+ const before = await graffiti.put<{}>(
80
80
  {
81
81
  value: { index: 2 },
82
82
  channels: channels.slice(1),
@@ -85,7 +85,7 @@ export const graffitiChannelStatsTests = (
85
85
  );
86
86
 
87
87
  // Add an item with all channels
88
- const first = await graffiti.put(
88
+ const first = await graffiti.put<{}>(
89
89
  { value: { index: 0 }, channels },
90
90
  session,
91
91
  );
@@ -93,7 +93,7 @@ export const graffitiChannelStatsTests = (
93
93
  await graffiti.delete(first, session);
94
94
 
95
95
  // Create a new object with only one channel
96
- const second = await graffiti.put(
96
+ const second = await graffiti.put<{}>(
97
97
  {
98
98
  value: { index: 1 },
99
99
  channels: channels.slice(2),
package/tests/crud.ts CHANGED
@@ -3,7 +3,9 @@ import type {
3
3
  Graffiti,
4
4
  GraffitiSession,
5
5
  GraffitiPatch,
6
+ JSONSchema,
6
7
  } from "@graffiti-garden/api";
8
+ import type { FromSchema } from "json-schema-to-ts";
7
9
  import {
8
10
  GraffitiErrorNotFound,
9
11
  GraffitiErrorSchemaMismatch,
@@ -43,7 +45,7 @@ export const graffitiCRUDTests = (
43
45
  const channels = [randomString(), randomString()];
44
46
 
45
47
  // Put the object
46
- const previous = await graffiti.put({ value, channels }, session);
48
+ const previous = await graffiti.put<{}>({ value, channels }, session);
47
49
  expect(previous.value).toEqual({});
48
50
  expect(previous.channels).toEqual([]);
49
51
  expect(previous.allowed).toBeUndefined();
@@ -63,7 +65,7 @@ export const graffitiCRUDTests = (
63
65
  const newValue = {
64
66
  something: "goodbye, world~ :c",
65
67
  };
66
- const beforeReplaced = await graffiti.put(
68
+ const beforeReplaced = await graffiti.put<{}>(
67
69
  { ...previous, value: newValue, channels: [] },
68
70
  session,
69
71
  );
@@ -96,7 +98,7 @@ export const graffitiCRUDTests = (
96
98
  });
97
99
 
98
100
  it("get non-existant", async () => {
99
- const putted = await graffiti.put(randomPutObject(), session);
101
+ const putted = await graffiti.put<{}>(randomPutObject(), session);
100
102
  await expect(
101
103
  graffiti.get(
102
104
  {
@@ -110,13 +112,13 @@ export const graffitiCRUDTests = (
110
112
 
111
113
  it("put, get, delete with wrong actor", async () => {
112
114
  await expect(
113
- graffiti.put(
115
+ graffiti.put<{}>(
114
116
  { value: {}, channels: [], actor: session2.actor },
115
117
  session1,
116
118
  ),
117
119
  ).rejects.toThrow(GraffitiErrorForbidden);
118
120
 
119
- const putted = await graffiti.put(
121
+ const putted = await graffiti.put<{}>(
120
122
  { value: {}, channels: [] },
121
123
  session2,
122
124
  );
@@ -139,17 +141,33 @@ export const graffitiCRUDTests = (
139
141
  type: "string",
140
142
  },
141
143
  another: {
142
- type: "integer",
144
+ type: "array",
145
+ items: {
146
+ type: "number",
147
+ },
148
+ },
149
+ deeper: {
150
+ type: "object",
151
+ properties: {
152
+ deepProp: {
153
+ type: "string",
154
+ },
155
+ },
156
+ required: ["deepProp"],
143
157
  },
144
158
  },
159
+ required: ["another", "deeper"],
145
160
  },
146
161
  },
147
- } as const;
162
+ } as const satisfies JSONSchema;
148
163
 
149
164
  const goodValue = {
150
165
  something: "hello",
151
- another: 42,
152
- } as const;
166
+ another: [1, 2, 3],
167
+ deeper: {
168
+ deepProp: "hello",
169
+ },
170
+ };
153
171
 
154
172
  const putted = await graffiti.put<typeof schema>(
155
173
  {
@@ -158,14 +176,19 @@ export const graffitiCRUDTests = (
158
176
  },
159
177
  session,
160
178
  );
161
-
162
179
  const gotten = await graffiti.get(putted, schema);
180
+
163
181
  expect(gotten.value.something).toEqual(goodValue.something);
164
182
  expect(gotten.value.another).toEqual(goodValue.another);
183
+ expect(gotten.value.another[0]).toEqual(1);
184
+ expect(gotten.value.deeper.deepProp).toEqual(goodValue.deeper.deepProp);
165
185
  });
166
186
 
167
187
  it("put and get with invalid schema", async () => {
168
- const putted = await graffiti.put({ value: {}, channels: [] }, session);
188
+ const putted = await graffiti.put<{}>(
189
+ { value: {}, channels: [] },
190
+ session,
191
+ );
169
192
  await expect(
170
193
  graffiti.get(putted, {
171
194
  properties: {
@@ -179,7 +202,7 @@ export const graffitiCRUDTests = (
179
202
  });
180
203
 
181
204
  it("put and get with wrong schema", async () => {
182
- const putted = await graffiti.put(
205
+ const putted = await graffiti.put<{}>(
183
206
  {
184
207
  value: {
185
208
  hello: "world",
@@ -210,7 +233,7 @@ export const graffitiCRUDTests = (
210
233
  };
211
234
  const allowed = [randomString()];
212
235
  const channels = [randomString()];
213
- const putted = await graffiti.put(
236
+ const putted = await graffiti.put<{}>(
214
237
  { value, allowed, channels },
215
238
  session1,
216
239
  );
@@ -238,7 +261,7 @@ export const graffitiCRUDTests = (
238
261
  };
239
262
  const allowed = [randomString(), session2.actor, randomString()];
240
263
  const channels = [randomString()];
241
- const putted = await graffiti.put(
264
+ const putted = await graffiti.put<{}>(
242
265
  {
243
266
  value,
244
267
  allowed,
@@ -270,7 +293,7 @@ export const graffitiCRUDTests = (
270
293
  const value = {
271
294
  something: "hello, world~ c:",
272
295
  };
273
- const putted = await graffiti.put({ value, channels: [] }, session);
296
+ const putted = await graffiti.put<{}>({ value, channels: [] }, session);
274
297
 
275
298
  // Wait just a bit to make sure the lastModified is different
276
299
  await new Promise((resolve) => setTimeout(resolve, 10));
@@ -295,7 +318,7 @@ export const graffitiCRUDTests = (
295
318
  });
296
319
 
297
320
  it("patch deleted object", async () => {
298
- const putted = await graffiti.put(randomPutObject(), session);
321
+ const putted = await graffiti.put<{}>(randomPutObject(), session);
299
322
  const deleted = await graffiti.delete(putted, session);
300
323
  await expect(
301
324
  graffiti.patch({}, putted, session),
@@ -310,7 +333,7 @@ export const graffitiCRUDTests = (
310
333
  },
311
334
  },
312
335
  };
313
- const putted = await graffiti.put(
336
+ const putted = await graffiti.put<{}>(
314
337
  { value: value, channels: [] },
315
338
  session,
316
339
  );
@@ -344,7 +367,7 @@ export const graffitiCRUDTests = (
344
367
  const channelsBefore = [randomString()];
345
368
  const channelsAfter = [randomString()];
346
369
 
347
- const putted = await graffiti.put(
370
+ const putted = await graffiti.put<{}>(
348
371
  { value: {}, channels: channelsBefore },
349
372
  session,
350
373
  );
@@ -360,7 +383,7 @@ export const graffitiCRUDTests = (
360
383
  });
361
384
 
362
385
  it("patch 'increment' with test", async () => {
363
- const putted = await graffiti.put(
386
+ const putted = await graffiti.put<{}>(
364
387
  {
365
388
  value: {
366
389
  counter: 1,
@@ -410,7 +433,7 @@ export const graffitiCRUDTests = (
410
433
 
411
434
  it("invalid patch", async () => {
412
435
  const object = randomPutObject();
413
- const putted = await graffiti.put(object, session);
436
+ const putted = await graffiti.put<{}>(object, session);
414
437
 
415
438
  await expect(
416
439
  graffiti.patch(
@@ -429,7 +452,7 @@ export const graffitiCRUDTests = (
429
452
  it("patch channels to be wrong", async () => {
430
453
  const object = randomPutObject();
431
454
  object.allowed = [randomString()];
432
- const putted = await graffiti.put(object, session);
455
+ const putted = await graffiti.put<{}>(object, session);
433
456
 
434
457
  const patches: GraffitiPatch[] = [
435
458
  {
package/tests/discover.ts CHANGED
@@ -2,7 +2,7 @@ import { it, expect, describe, assert, beforeAll } from "vitest";
2
2
  import type {
3
3
  Graffiti,
4
4
  GraffitiSession,
5
- JSONSchema4,
5
+ JSONSchema,
6
6
  } from "@graffiti-garden/api";
7
7
  import { randomString, nextStreamValue, randomPutObject } from "./utils";
8
8
 
@@ -31,7 +31,7 @@ export const graffitiDiscoverTests = (
31
31
  it("discover single", async () => {
32
32
  const object = randomPutObject();
33
33
 
34
- const putted = await graffiti.put(object, session);
34
+ const putted = await graffiti.put<{}>(object, session);
35
35
 
36
36
  const queryChannels = [randomString(), object.channels[0]];
37
37
  const iterator = graffiti.discover(queryChannels, {});
@@ -48,7 +48,7 @@ export const graffitiDiscoverTests = (
48
48
 
49
49
  it("discover wrong channel", async () => {
50
50
  const object = randomPutObject();
51
- await graffiti.put(object, session);
51
+ await graffiti.put<{}>(object, session);
52
52
  const iterator = graffiti.discover([randomString()], {});
53
53
  await expect(iterator.next()).resolves.toHaveProperty("done", true);
54
54
  });
@@ -56,7 +56,7 @@ export const graffitiDiscoverTests = (
56
56
  it("discover not allowed", async () => {
57
57
  const object = randomPutObject();
58
58
  object.allowed = [randomString(), randomString()];
59
- const putted = await graffiti.put(object, session1);
59
+ const putted = await graffiti.put<{}>(object, session1);
60
60
 
61
61
  const iteratorSession1 = graffiti.discover(object.channels, {}, session1);
62
62
  const value = await nextStreamValue(iteratorSession1);
@@ -77,7 +77,7 @@ export const graffitiDiscoverTests = (
77
77
  it("discover allowed", async () => {
78
78
  const object = randomPutObject();
79
79
  object.allowed = [randomString(), session2.actor, randomString()];
80
- const putted = await graffiti.put(object, session1);
80
+ const putted = await graffiti.put<{}>(object, session1);
81
81
 
82
82
  const iteratorSession2 = graffiti.discover(object.channels, {}, session2);
83
83
  const value = await nextStreamValue(iteratorSession2);
@@ -92,13 +92,13 @@ export const graffitiDiscoverTests = (
92
92
  for (const prop of ["name", "actor", "lastModified"] as const) {
93
93
  it(`discover for ${prop}`, async () => {
94
94
  const object1 = randomPutObject();
95
- const putted1 = await graffiti.put(object1, session1);
95
+ const putted1 = await graffiti.put<{}>(object1, session1);
96
96
 
97
97
  const object2 = randomPutObject();
98
98
  object2.channels = object1.channels;
99
99
  // Make sure the lastModified is different for the query
100
100
  await new Promise((r) => setTimeout(r, 20));
101
- const putted2 = await graffiti.put(object2, session2);
101
+ const putted2 = await graffiti.put<{}>(object2, session2);
102
102
 
103
103
  const iterator = graffiti.discover(object1.channels, {
104
104
  properties: {
@@ -118,10 +118,10 @@ export const graffitiDiscoverTests = (
118
118
 
119
119
  it("discover with lastModified range", async () => {
120
120
  const object = randomPutObject();
121
- const putted1 = await graffiti.put(object, session);
121
+ const putted1 = await graffiti.put<{}>(object, session);
122
122
  // Make sure the lastModified is different
123
123
  await new Promise((r) => setTimeout(r, 20));
124
- const putted2 = await graffiti.put(object, session);
124
+ const putted2 = await graffiti.put<{}>(object, session);
125
125
 
126
126
  expect(putted1.name).not.toEqual(putted2.name);
127
127
  expect(putted1.lastModified).toBeLessThan(putted2.lastModified);
@@ -129,8 +129,7 @@ export const graffitiDiscoverTests = (
129
129
  const gtIterator = graffiti.discover([object.channels[0]], {
130
130
  properties: {
131
131
  lastModified: {
132
- minimum: putted2.lastModified,
133
- exclusiveMinimum: true,
132
+ exclusiveMinimum: putted2.lastModified,
134
133
  },
135
134
  },
136
135
  });
@@ -138,8 +137,7 @@ export const graffitiDiscoverTests = (
138
137
  const gtIteratorEpsilon = graffiti.discover([object.channels[0]], {
139
138
  properties: {
140
139
  lastModified: {
141
- minimum: putted2.lastModified - 0.1,
142
- exclusiveMinimum: true,
140
+ exclusiveMinimum: putted2.lastModified - 0.1,
143
141
  },
144
142
  },
145
143
  });
@@ -169,8 +167,7 @@ export const graffitiDiscoverTests = (
169
167
  const ltIterator = graffiti.discover(object.channels, {
170
168
  properties: {
171
169
  lastModified: {
172
- maximum: putted1.lastModified,
173
- exclusiveMaximum: true,
170
+ exclusiveMaximum: putted1.lastModified,
174
171
  },
175
172
  },
176
173
  });
@@ -179,8 +176,7 @@ export const graffitiDiscoverTests = (
179
176
  const ltIteratorEpsilon = graffiti.discover(object.channels, {
180
177
  properties: {
181
178
  lastModified: {
182
- maximum: putted1.lastModified + 0.1,
183
- exclusiveMaximum: true,
179
+ exclusiveMaximum: putted1.lastModified + 0.1,
184
180
  },
185
181
  },
186
182
  });
@@ -212,7 +208,7 @@ export const graffitiDiscoverTests = (
212
208
  it("discover schema allowed, as and not as owner", async () => {
213
209
  const object = randomPutObject();
214
210
  object.allowed = [randomString(), session2.actor, randomString()];
215
- await graffiti.put(object, session1);
211
+ await graffiti.put<{}>(object, session1);
216
212
 
217
213
  const iteratorSession1 = graffiti.discover(
218
214
  object.channels,
@@ -304,7 +300,7 @@ export const graffitiDiscoverTests = (
304
300
  it("discover schema channels, as and not as owner", async () => {
305
301
  const object = randomPutObject();
306
302
  object.channels = [randomString(), randomString(), randomString()];
307
- await graffiti.put(object, session1);
303
+ await graffiti.put<{}>(object, session1);
308
304
 
309
305
  const iteratorSession1 = graffiti.discover(
310
306
  [object.channels[0], object.channels[2]],
@@ -400,9 +396,9 @@ export const graffitiDiscoverTests = (
400
396
  not: {
401
397
  required: ["allowed"],
402
398
  },
403
- } satisfies JSONSchema4;
399
+ } satisfies JSONSchema;
404
400
 
405
- await graffiti.put(publicO, session1);
401
+ await graffiti.put<{}>(publicO, session1);
406
402
  const iterator = graffiti.discover(
407
403
  publicO.channels,
408
404
  publicSchema,
@@ -415,7 +411,7 @@ export const graffitiDiscoverTests = (
415
411
 
416
412
  const restricted = randomPutObject();
417
413
  restricted.allowed = [];
418
- await graffiti.put(restricted, session1);
414
+ await graffiti.put<{}>(restricted, session1);
419
415
  const iterator2 = graffiti.discover(
420
416
  restricted.channels,
421
417
  publicSchema,
@@ -427,17 +423,17 @@ export const graffitiDiscoverTests = (
427
423
  it("discover query for values", async () => {
428
424
  const object1 = randomPutObject();
429
425
  object1.value = { test: randomString() };
430
- await graffiti.put(object1, session);
426
+ await graffiti.put<{}>(object1, session);
431
427
 
432
428
  const object2 = randomPutObject();
433
429
  object2.channels = object1.channels;
434
430
  object2.value = { test: randomString(), something: randomString() };
435
- await graffiti.put(object2, session);
431
+ await graffiti.put<{}>(object2, session);
436
432
 
437
433
  const object3 = randomPutObject();
438
434
  object3.channels = object1.channels;
439
435
  object3.value = { other: randomString(), something: randomString() };
440
- await graffiti.put(object3, session);
436
+ await graffiti.put<{}>(object3, session);
441
437
 
442
438
  const counts = new Map<string, number>();
443
439
  for (const property of ["test", "something", "other"] as const) {
@@ -464,7 +460,7 @@ export const graffitiDiscoverTests = (
464
460
 
465
461
  it("discover for deleted content", async () => {
466
462
  const object = randomPutObject();
467
- const putted = await graffiti.put(object, session);
463
+ const putted = await graffiti.put<{}>(object, session);
468
464
  const deleted = await graffiti.delete(putted, session);
469
465
 
470
466
  const iterator = graffiti.discover(object.channels, {});
@@ -481,9 +477,9 @@ export const graffitiDiscoverTests = (
481
477
  // Do this a bunch to check for concurrency issues
482
478
  for (let i = 0; i < 10; i++) {
483
479
  const object1 = randomPutObject();
484
- const putted = await graffiti.put(object1, session);
480
+ const putted = await graffiti.put<{}>(object1, session);
485
481
  const object2 = randomPutObject();
486
- const replaced = await graffiti.put(
482
+ const replaced = await graffiti.put<{}>(
487
483
  {
488
484
  ...putted,
489
485
  ...object2,
@@ -520,7 +516,7 @@ export const graffitiDiscoverTests = (
520
516
 
521
517
  it("discover for patched allowed", async () => {
522
518
  const object = randomPutObject();
523
- const putted = await graffiti.put(object, session);
519
+ const putted = await graffiti.put<{}>(object, session);
524
520
  await graffiti.patch(
525
521
  {
526
522
  allowed: [{ op: "add", path: "", value: [] }],
@@ -543,7 +539,7 @@ export const graffitiDiscoverTests = (
543
539
 
544
540
  const putPromises = Array(100)
545
541
  .fill(0)
546
- .map(() => graffiti.put(object, session));
542
+ .map(() => graffiti.put<{}>(object, session));
547
543
  await Promise.all(putPromises);
548
544
 
549
545
  const iterator = graffiti.discover(object.channels, {});
package/tests/orphans.ts CHANGED
@@ -32,7 +32,7 @@ export const graffitiOrphanTests = (
32
32
 
33
33
  const object = randomPutObject();
34
34
  object.channels = [];
35
- const putted = await graffiti.put(object, session);
35
+ const putted = await graffiti.put<{}>(object, session);
36
36
  const orphanIterator2 = graffiti.recoverOrphans({}, session);
37
37
  let numResults = 0;
38
38
  for await (const orphan of orphanIterator2) {
@@ -49,12 +49,12 @@ export const graffitiOrphanTests = (
49
49
  it("replaced orphan, no longer", async () => {
50
50
  const object = randomPutObject();
51
51
  object.channels = [];
52
- const putOrphan = await graffiti.put(object, session);
52
+ const putOrphan = await graffiti.put<{}>(object, session);
53
53
 
54
54
  // Wait for the put to be processed
55
55
  await new Promise((resolve) => setTimeout(resolve, 10));
56
56
 
57
- const putNotOrphan = await graffiti.put(
57
+ const putNotOrphan = await graffiti.put<{}>(
58
58
  {
59
59
  ...putOrphan,
60
60
  ...object,