@kubun/client 0.1.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/LICENSE.md ADDED
@@ -0,0 +1,57 @@
1
+ # The Prosperity Public License 3.0.0
2
+
3
+ Contributor: Paul Le Cam
4
+
5
+ Source Code: https://github.com/PaulLeCam/kubun
6
+
7
+ ## Purpose
8
+
9
+ This license allows you to use and share this software for noncommercial purposes for free and to try this software for commercial purposes for thirty days.
10
+
11
+ ## Agreement
12
+
13
+ In order to receive this license, you have to agree to its rules. Those rules are both obligations under that agreement and conditions to your license. Don't do anything with this software that triggers a rule you can't or won't follow.
14
+
15
+ ## Notices
16
+
17
+ Make sure everyone who gets a copy of any part of this software from you, with or without changes, also gets the text of this license and the contributor and source code lines above.
18
+
19
+ ## Commercial Trial
20
+
21
+ Limit your use of this software for commercial purposes to a thirty-day trial period. If you use this software for work, your company gets one trial period for all personnel, not one trial per person.
22
+
23
+ ## Contributions Back
24
+
25
+ Developing feedback, changes, or additions that you contribute back to the contributor on the terms of a standardized public software license such as [the Blue Oak Model License 1.0.0](https://blueoakcouncil.org/license/1.0.0), [the Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0.html), [the MIT license](https://spdx.org/licenses/MIT.html), or [the two-clause BSD license](https://spdx.org/licenses/BSD-2-Clause.html) doesn't count as use for a commercial purpose.
26
+
27
+ ## Personal Uses
28
+
29
+ Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, doesn't count as use for a commercial purpose.
30
+
31
+ ## Noncommercial Organizations
32
+
33
+ Use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization, or government institution doesn't count as use for a commercial purpose regardless of the source of funding or obligations resulting from the funding.
34
+
35
+ ## Defense
36
+
37
+ Don't make any legal claim against anyone accusing this software, with or without changes, alone or with other technology, of infringing any patent.
38
+
39
+ ## Copyright
40
+
41
+ The contributor licenses you to do everything with this software that would otherwise infringe their copyright in it.
42
+
43
+ ## Patent
44
+
45
+ The contributor licenses you to do everything with this software that would otherwise infringe any patents they can license or become able to license.
46
+
47
+ ## Reliability
48
+
49
+ The contributor can't revoke this license.
50
+
51
+ ## Excuse
52
+
53
+ You're excused for unknowingly breaking [Notices](#notices) if you take all practical steps to comply within thirty days of learning you broke the rule.
54
+
55
+ ## No Liability
56
+
57
+ ***As far as the law allows, this software comes as is, without any warranty or condition, and the contributor won't be liable to anyone for any damages related to this software or this license, under any kind of legal claim.***
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # Kubun client
2
+
3
+ ## Installation
4
+
5
+ ```sh
6
+ npm install @kubun/client
7
+ ```
8
+
9
+ ## License
10
+
11
+ [Prosperity Public License 3.0.0](LICENSE.md)
@@ -0,0 +1,11 @@
1
+ import { AttachmentID } from '@kubun/id';
2
+ import { Block } from 'multiformats/block';
3
+ import * as codec from 'multiformats/codecs/raw';
4
+ export type BinaryBlock = Block<Uint8Array, typeof codec.code, 18, 1>;
5
+ export declare function createBlock(value: Uint8Array): BinaryBlock;
6
+ export type Attachment = {
7
+ block: BinaryBlock;
8
+ id: AttachmentID;
9
+ };
10
+ export declare function createAttachment(value: Uint8Array, mimeType?: string): Attachment;
11
+ //# sourceMappingURL=attachments.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attachments.d.ts","sourceRoot":"","sources":["../src/attachments.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAuC,MAAM,WAAW,CAAA;AAC7E,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAA;AAC1C,OAAO,KAAK,KAAK,MAAM,yBAAyB,CAAA;AAEhD,MAAM,MAAM,WAAW,GAAG,KAAK,CAAC,UAAU,EAAE,OAAO,KAAK,CAAC,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC,CAAA;AAErE,wBAAgB,WAAW,CAAC,KAAK,EAAE,UAAU,GAAG,WAAW,CAI1D;AAED,MAAM,MAAM,UAAU,GAAG;IACvB,KAAK,EAAE,WAAW,CAAA;IAClB,EAAE,EAAE,YAAY,CAAA;CACjB,CAAA;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,UAAU,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,UAAU,CAGjF"}
@@ -0,0 +1,19 @@
1
+ import { AttachmentID, createAttachmentCID, toSHA256Digest } from '@kubun/id';
2
+ import { Block } from 'multiformats/block';
3
+ import * as codec from 'multiformats/codecs/raw';
4
+ export function createBlock(value) {
5
+ const bytes = codec.encode(value);
6
+ const cid = createAttachmentCID(toSHA256Digest(bytes));
7
+ return new Block({
8
+ value,
9
+ bytes,
10
+ cid
11
+ });
12
+ }
13
+ export function createAttachment(value, mimeType) {
14
+ const block = createBlock(value);
15
+ return {
16
+ block,
17
+ id: new AttachmentID(block.cid, value.byteLength, mimeType)
18
+ };
19
+ }
@@ -0,0 +1,474 @@
1
+ import type { Signer } from '@enkaku/jwt';
2
+ import type { ClientTransport, CreateGraphParams, ExecuteGraphParams, LoadGraphParams, MutateGraphParams as ProtocolMutateGraphParams } from '@kubun/protocol';
3
+ import type { Attachment } from './attachments.js';
4
+ export type ClientParams = {
5
+ signer: Signer;
6
+ transport: ClientTransport;
7
+ };
8
+ export type MutateGraphInvocationParams = Omit<ProtocolMutateGraphParams, 'attachments'> & {
9
+ attachments?: Array<Attachment>;
10
+ };
11
+ export type MutateGraphParams = Omit<ExecuteGraphParams, 'attachments'> & {
12
+ attachments?: Array<Attachment>;
13
+ };
14
+ export declare class KubunClient {
15
+ #private;
16
+ constructor(params: ClientParams);
17
+ createGraph(params: CreateGraphParams): Promise<import("@enkaku/client").InvokeReturn<{
18
+ id: string;
19
+ models: {
20
+ [x: string]: {
21
+ name: string;
22
+ version: "1.0";
23
+ interfaces: string[];
24
+ schema: {
25
+ $defs: {
26
+ [x: string]: {
27
+ default?: boolean | undefined;
28
+ type: "boolean";
29
+ } | {
30
+ default?: number | undefined;
31
+ min?: number | undefined;
32
+ max?: number | undefined;
33
+ type: "integer";
34
+ } | {
35
+ default?: number | undefined;
36
+ min?: number | undefined;
37
+ max?: number | undefined;
38
+ type: "number";
39
+ } | {
40
+ default?: string | undefined;
41
+ title?: string | undefined;
42
+ maxLength?: number | undefined;
43
+ minLength?: number | undefined;
44
+ pattern?: string | undefined;
45
+ type: "string";
46
+ } | {
47
+ default?: string | undefined;
48
+ enum: string[];
49
+ title: string;
50
+ type: "string";
51
+ } | {
52
+ format: "date" | "date-time" | "duration" | "time" | "uri";
53
+ type: "string";
54
+ } | {
55
+ title: string;
56
+ type: "array";
57
+ items: {
58
+ $ref: string;
59
+ };
60
+ } | {
61
+ title: string;
62
+ type: "object";
63
+ additionalProperties: boolean;
64
+ properties: {
65
+ [x: string]: {
66
+ $ref: string;
67
+ };
68
+ };
69
+ required: string[];
70
+ };
71
+ };
72
+ type: "object";
73
+ additionalProperties: boolean;
74
+ properties: {
75
+ [x: string]: {
76
+ $ref: string;
77
+ };
78
+ };
79
+ required: string[];
80
+ };
81
+ fieldsMeta: {
82
+ [x: string]: {
83
+ relationModel?: string | null | undefined;
84
+ };
85
+ };
86
+ behavior: "interface";
87
+ } | {
88
+ name: string;
89
+ version: "1.0";
90
+ interfaces: string[];
91
+ schema: {
92
+ $defs: {
93
+ [x: string]: {
94
+ default?: boolean | undefined;
95
+ type: "boolean";
96
+ } | {
97
+ default?: number | undefined;
98
+ min?: number | undefined;
99
+ max?: number | undefined;
100
+ type: "integer";
101
+ } | {
102
+ default?: number | undefined;
103
+ min?: number | undefined;
104
+ max?: number | undefined;
105
+ type: "number";
106
+ } | {
107
+ default?: string | undefined;
108
+ title?: string | undefined;
109
+ maxLength?: number | undefined;
110
+ minLength?: number | undefined;
111
+ pattern?: string | undefined;
112
+ type: "string";
113
+ } | {
114
+ default?: string | undefined;
115
+ enum: string[];
116
+ title: string;
117
+ type: "string";
118
+ } | {
119
+ format: "date" | "date-time" | "duration" | "time" | "uri";
120
+ type: "string";
121
+ } | {
122
+ title: string;
123
+ type: "array";
124
+ items: {
125
+ $ref: string;
126
+ };
127
+ } | {
128
+ title: string;
129
+ type: "object";
130
+ additionalProperties: boolean;
131
+ properties: {
132
+ [x: string]: {
133
+ $ref: string;
134
+ };
135
+ };
136
+ required: string[];
137
+ };
138
+ };
139
+ type: "object";
140
+ additionalProperties: boolean;
141
+ properties: {
142
+ [x: string]: {
143
+ $ref: string;
144
+ };
145
+ };
146
+ required: string[];
147
+ };
148
+ fieldsMeta: {
149
+ [x: string]: {
150
+ relationModel?: string | null | undefined;
151
+ };
152
+ };
153
+ behavior: "list";
154
+ } | {
155
+ name: string;
156
+ version: "1.0";
157
+ interfaces: string[];
158
+ schema: {
159
+ $defs: {
160
+ [x: string]: {
161
+ default?: boolean | undefined;
162
+ type: "boolean";
163
+ } | {
164
+ default?: number | undefined;
165
+ min?: number | undefined;
166
+ max?: number | undefined;
167
+ type: "integer";
168
+ } | {
169
+ default?: number | undefined;
170
+ min?: number | undefined;
171
+ max?: number | undefined;
172
+ type: "number";
173
+ } | {
174
+ default?: string | undefined;
175
+ title?: string | undefined;
176
+ maxLength?: number | undefined;
177
+ minLength?: number | undefined;
178
+ pattern?: string | undefined;
179
+ type: "string";
180
+ } | {
181
+ default?: string | undefined;
182
+ enum: string[];
183
+ title: string;
184
+ type: "string";
185
+ } | {
186
+ format: "date" | "date-time" | "duration" | "time" | "uri";
187
+ type: "string";
188
+ } | {
189
+ title: string;
190
+ type: "array";
191
+ items: {
192
+ $ref: string;
193
+ };
194
+ } | {
195
+ title: string;
196
+ type: "object";
197
+ additionalProperties: boolean;
198
+ properties: {
199
+ [x: string]: {
200
+ $ref: string;
201
+ };
202
+ };
203
+ required: string[];
204
+ };
205
+ };
206
+ type: "object";
207
+ additionalProperties: boolean;
208
+ properties: {
209
+ [x: string]: {
210
+ $ref: string;
211
+ };
212
+ };
213
+ required: string[];
214
+ };
215
+ fieldsMeta: {
216
+ [x: string]: {
217
+ relationModel?: string | null | undefined;
218
+ };
219
+ };
220
+ behavior: "set";
221
+ setFields: string[];
222
+ };
223
+ };
224
+ }>>;
225
+ loadGraph(params: LoadGraphParams): Promise<import("@enkaku/client").InvokeReturn<{
226
+ aliases?: {
227
+ [x: string]: string;
228
+ } | undefined;
229
+ models: {
230
+ [x: string]: {
231
+ name: string;
232
+ version: "1.0";
233
+ interfaces: string[];
234
+ schema: {
235
+ $defs: {
236
+ [x: string]: {
237
+ default?: boolean | undefined;
238
+ type: "boolean";
239
+ } | {
240
+ default?: number | undefined;
241
+ min?: number | undefined;
242
+ max?: number | undefined;
243
+ type: "integer";
244
+ } | {
245
+ default?: number | undefined;
246
+ min?: number | undefined;
247
+ max?: number | undefined;
248
+ type: "number";
249
+ } | {
250
+ default?: string | undefined;
251
+ title?: string | undefined;
252
+ maxLength?: number | undefined;
253
+ minLength?: number | undefined;
254
+ pattern?: string | undefined;
255
+ type: "string";
256
+ } | {
257
+ default?: string | undefined;
258
+ enum: string[];
259
+ title: string;
260
+ type: "string";
261
+ } | {
262
+ format: "date" | "date-time" | "duration" | "time" | "uri";
263
+ type: "string";
264
+ } | {
265
+ title: string;
266
+ type: "array";
267
+ items: {
268
+ $ref: string;
269
+ };
270
+ } | {
271
+ title: string;
272
+ type: "object";
273
+ additionalProperties: boolean;
274
+ properties: {
275
+ [x: string]: {
276
+ $ref: string;
277
+ };
278
+ };
279
+ required: string[];
280
+ };
281
+ };
282
+ type: "object";
283
+ additionalProperties: boolean;
284
+ properties: {
285
+ [x: string]: {
286
+ $ref: string;
287
+ };
288
+ };
289
+ required: string[];
290
+ };
291
+ fieldsMeta: {
292
+ [x: string]: {
293
+ relationModel?: string | null | undefined;
294
+ };
295
+ };
296
+ behavior: "interface";
297
+ } | {
298
+ name: string;
299
+ version: "1.0";
300
+ interfaces: string[];
301
+ schema: {
302
+ $defs: {
303
+ [x: string]: {
304
+ default?: boolean | undefined;
305
+ type: "boolean";
306
+ } | {
307
+ default?: number | undefined;
308
+ min?: number | undefined;
309
+ max?: number | undefined;
310
+ type: "integer";
311
+ } | {
312
+ default?: number | undefined;
313
+ min?: number | undefined;
314
+ max?: number | undefined;
315
+ type: "number";
316
+ } | {
317
+ default?: string | undefined;
318
+ title?: string | undefined;
319
+ maxLength?: number | undefined;
320
+ minLength?: number | undefined;
321
+ pattern?: string | undefined;
322
+ type: "string";
323
+ } | {
324
+ default?: string | undefined;
325
+ enum: string[];
326
+ title: string;
327
+ type: "string";
328
+ } | {
329
+ format: "date" | "date-time" | "duration" | "time" | "uri";
330
+ type: "string";
331
+ } | {
332
+ title: string;
333
+ type: "array";
334
+ items: {
335
+ $ref: string;
336
+ };
337
+ } | {
338
+ title: string;
339
+ type: "object";
340
+ additionalProperties: boolean;
341
+ properties: {
342
+ [x: string]: {
343
+ $ref: string;
344
+ };
345
+ };
346
+ required: string[];
347
+ };
348
+ };
349
+ type: "object";
350
+ additionalProperties: boolean;
351
+ properties: {
352
+ [x: string]: {
353
+ $ref: string;
354
+ };
355
+ };
356
+ required: string[];
357
+ };
358
+ fieldsMeta: {
359
+ [x: string]: {
360
+ relationModel?: string | null | undefined;
361
+ };
362
+ };
363
+ behavior: "list";
364
+ } | {
365
+ name: string;
366
+ version: "1.0";
367
+ interfaces: string[];
368
+ schema: {
369
+ $defs: {
370
+ [x: string]: {
371
+ default?: boolean | undefined;
372
+ type: "boolean";
373
+ } | {
374
+ default?: number | undefined;
375
+ min?: number | undefined;
376
+ max?: number | undefined;
377
+ type: "integer";
378
+ } | {
379
+ default?: number | undefined;
380
+ min?: number | undefined;
381
+ max?: number | undefined;
382
+ type: "number";
383
+ } | {
384
+ default?: string | undefined;
385
+ title?: string | undefined;
386
+ maxLength?: number | undefined;
387
+ minLength?: number | undefined;
388
+ pattern?: string | undefined;
389
+ type: "string";
390
+ } | {
391
+ default?: string | undefined;
392
+ enum: string[];
393
+ title: string;
394
+ type: "string";
395
+ } | {
396
+ format: "date" | "date-time" | "duration" | "time" | "uri";
397
+ type: "string";
398
+ } | {
399
+ title: string;
400
+ type: "array";
401
+ items: {
402
+ $ref: string;
403
+ };
404
+ } | {
405
+ title: string;
406
+ type: "object";
407
+ additionalProperties: boolean;
408
+ properties: {
409
+ [x: string]: {
410
+ $ref: string;
411
+ };
412
+ };
413
+ required: string[];
414
+ };
415
+ };
416
+ type: "object";
417
+ additionalProperties: boolean;
418
+ properties: {
419
+ [x: string]: {
420
+ $ref: string;
421
+ };
422
+ };
423
+ required: string[];
424
+ };
425
+ fieldsMeta: {
426
+ [x: string]: {
427
+ relationModel?: string | null | undefined;
428
+ };
429
+ };
430
+ behavior: "set";
431
+ setFields: string[];
432
+ };
433
+ };
434
+ }>>;
435
+ queryGraph(params: ExecuteGraphParams): Promise<import("@enkaku/client").InvokeReturn<{
436
+ data?: {
437
+ [x: string]: unknown;
438
+ } | null | undefined;
439
+ errors?: {
440
+ locations?: {
441
+ line: number;
442
+ column: number;
443
+ }[] | undefined;
444
+ path?: string[] | undefined;
445
+ extensions?: {
446
+ [x: string]: unknown;
447
+ } | undefined;
448
+ message: string;
449
+ }[] | undefined;
450
+ extensions?: {
451
+ [x: string]: unknown;
452
+ } | undefined;
453
+ }>>;
454
+ mutateGraph(params: MutateGraphParams): Promise<import("@enkaku/client").InvokeReturn<{
455
+ data?: {
456
+ [x: string]: unknown;
457
+ } | null | undefined;
458
+ errors?: {
459
+ locations?: {
460
+ line: number;
461
+ column: number;
462
+ }[] | undefined;
463
+ path?: string[] | undefined;
464
+ extensions?: {
465
+ [x: string]: unknown;
466
+ } | undefined;
467
+ message: string;
468
+ }[] | undefined;
469
+ extensions?: {
470
+ [x: string]: unknown;
471
+ } | undefined;
472
+ }>>;
473
+ }
474
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACzC,OAAO,KAAK,EACV,eAAe,EACf,iBAAiB,EAEjB,kBAAkB,EAClB,eAAe,EACf,iBAAiB,IAAI,yBAAyB,EAC/C,MAAM,iBAAiB,CAAA;AAExB,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAKlD,MAAM,MAAM,YAAY,GAAG;IACzB,MAAM,EAAE,MAAM,CAAA;IACd,SAAS,EAAE,eAAe,CAAA;CAC3B,CAAA;AAED,MAAM,MAAM,2BAA2B,GAAG,IAAI,CAAC,yBAAyB,EAAE,aAAa,CAAC,GAAG;IACzF,WAAW,CAAC,EAAE,KAAK,CAAC,UAAU,CAAC,CAAA;CAChC,CAAA;AAED,MAAM,MAAM,iBAAiB,GAAG,IAAI,CAAC,kBAAkB,EAAE,aAAa,CAAC,GAAG;IACxE,WAAW,CAAC,EAAE,KAAK,CAAC,UAAU,CAAC,CAAA;CAChC,CAAA;AAED,qBAAa,WAAW;;gBAKV,MAAM,EAAE,YAAY;IAM1B,WAAW,CAAC,MAAM,EAAE,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IASrC,SAAS,CAAC,MAAM,EAAE,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IASjC,UAAU,CAAC,MAAM,EAAE,kBAAkB;;;;;;;;;;;;;;;;;;;IAIrC,WAAW,CAAC,MAAM,EAAE,iBAAiB;;;;;;;;;;;;;;;;;;;CAmC5C"}
package/lib/client.js ADDED
@@ -0,0 +1,74 @@
1
+ import { Client } from '@enkaku/client';
2
+ import { toB64 } from '@enkaku/codec';
3
+ import { GraphQLExecutionError } from './errors.js';
4
+ import { ClientSchemaBuilder } from './graphql.js';
5
+ import { MutationsRunner } from './mutations-runner.js';
6
+ export class KubunClient {
7
+ #client;
8
+ #mutationsRunner;
9
+ #signer;
10
+ constructor(params){
11
+ this.#client = new Client({
12
+ transport: params.transport
13
+ });
14
+ this.#mutationsRunner = new MutationsRunner();
15
+ this.#signer = params.signer;
16
+ }
17
+ async createGraph(params) {
18
+ const request = await this.#client.request('graph/create', params);
19
+ request.result.then((res)=>{
20
+ const builder = new ClientSchemaBuilder({
21
+ record: res.models
22
+ });
23
+ this.#mutationsRunner.setSchema(res.id, builder.build());
24
+ });
25
+ return request;
26
+ }
27
+ async loadGraph(params) {
28
+ const request = await this.#client.request('graph/load', params);
29
+ request.result.then((res)=>{
30
+ const builder = new ClientSchemaBuilder({
31
+ record: res.models
32
+ });
33
+ this.#mutationsRunner.setSchema(params.id, builder.build());
34
+ });
35
+ return request;
36
+ }
37
+ async queryGraph(params) {
38
+ return await this.#client.request('graph/query', params);
39
+ }
40
+ async mutateGraph(params) {
41
+ const { attachments, id, ...rest } = params;
42
+ if (!this.#mutationsRunner.hasSchema(id)) {
43
+ const loadGraphRequest = await this.loadGraph({
44
+ id
45
+ });
46
+ await loadGraphRequest.result;
47
+ }
48
+ const context = {
49
+ mutations: {},
50
+ signer: this.#signer
51
+ };
52
+ const { errors } = await this.#mutationsRunner.execute({
53
+ context,
54
+ schemaID: id,
55
+ source: params.text,
56
+ variables: params.variables
57
+ });
58
+ if (errors != null && errors.length !== 0) {
59
+ throw new GraphQLExecutionError(errors);
60
+ }
61
+ const attachmentsRecord = {};
62
+ if (attachments != null) {
63
+ for (const { id, block } of attachments){
64
+ attachmentsRecord[id.toString()] = toB64(block.bytes);
65
+ }
66
+ }
67
+ return await this.#client.request('graph/mutate', {
68
+ id,
69
+ attachments: attachmentsRecord,
70
+ mutations: context.mutations,
71
+ ...rest
72
+ });
73
+ }
74
+ }
@@ -0,0 +1,7 @@
1
+ import type { GraphQLError } from 'graphql';
2
+ export declare class GraphQLExecutionError extends Error {
3
+ #private;
4
+ constructor(errors: ReadonlyArray<GraphQLError>, message?: string);
5
+ get errors(): ReadonlyArray<GraphQLError>;
6
+ }
7
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAE3C,qBAAa,qBAAsB,SAAQ,KAAK;;gBAGlC,MAAM,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE,OAAO,SAA6B;IAMrF,IAAI,MAAM,IAAI,aAAa,CAAC,YAAY,CAAC,CAExC;CACF"}
package/lib/errors.js ADDED
@@ -0,0 +1,11 @@
1
+ export class GraphQLExecutionError extends Error {
2
+ #errors;
3
+ constructor(errors, message = 'GraphQL execution failed'){
4
+ super(message);
5
+ this.name = GraphQLExecutionError.name;
6
+ this.#errors = errors;
7
+ }
8
+ get errors() {
9
+ return this.#errors;
10
+ }
11
+ }
@@ -0,0 +1,10 @@
1
+ import { type Signer } from '@enkaku/jwt';
2
+ import { type DocumentType, SchemaBuilder, type SharedDefinitions } from '@kubun/graphql';
3
+ export type Context = {
4
+ mutations: Record<string, string>;
5
+ signer: Signer;
6
+ };
7
+ export declare class ClientSchemaBuilder extends SchemaBuilder<DocumentType, Context> {
8
+ buildMutations(id: string, definitions: SharedDefinitions<DocumentType, Context>): void;
9
+ }
10
+ //# sourceMappingURL=graphql.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graphql.d.ts","sourceRoot":"","sources":["../src/graphql.ts"],"names":[],"mappings":"AACA,OAAO,EAAoB,KAAK,MAAM,EAAkB,MAAM,aAAa,CAAA;AAC3E,OAAO,EAAE,KAAK,YAAY,EAAE,aAAa,EAAE,KAAK,iBAAiB,EAAE,MAAM,gBAAgB,CAAA;AAOzF,MAAM,MAAM,OAAO,GAAG;IACpB,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAEjC,MAAM,EAAE,MAAM,CAAA;CACf,CAAA;AAED,qBAAa,mBAAoB,SAAQ,aAAa,CAAC,YAAY,EAAE,OAAO,CAAC;IAC3E,cAAc,CAAC,EAAE,EAAE,MAAM,EAAE,WAAW,EAAE,iBAAiB,CAAC,YAAY,EAAE,OAAO,CAAC,GAAG,IAAI;CAkGxF"}
package/lib/graphql.js ADDED
@@ -0,0 +1,121 @@
1
+ import { fromUTF } from '@enkaku/codec';
2
+ import { stringifyToken } from '@enkaku/jwt';
3
+ import { SchemaBuilder } from '@kubun/graphql';
4
+ import { GraphQLID, GraphQLNonNull } from 'graphql';
5
+ import { mutationWithClientMutationId } from 'graphql-relay';
6
+ import { createChangeMutation, createSetMutation } from './mutations.js';
7
+ export class ClientSchemaBuilder extends SchemaBuilder {
8
+ buildMutations(id, definitions) {
9
+ const model = this._record[id];
10
+ if (model == null) {
11
+ throw new Error(`Could not find model id: ${id}`);
12
+ }
13
+ const name = this._aliases[id];
14
+ switch(model.behavior){
15
+ case 'interface':
16
+ // Interfaces don't have mutations
17
+ return;
18
+ case 'list':
19
+ {
20
+ this._mutations[`create${name}`] = mutationWithClientMutationId({
21
+ name: `Create${name}`,
22
+ inputFields: ()=>({
23
+ data: {
24
+ type: new GraphQLNonNull(this._inputObjects[id])
25
+ }
26
+ }),
27
+ outputFields: ()=>({
28
+ ...definitions.queryFields,
29
+ document: {
30
+ type: this._types[id]
31
+ }
32
+ }),
33
+ mutateAndGetPayload: async (input, ctx, info)=>{
34
+ // Create the mutation token
35
+ const unique = globalThis.crypto.getRandomValues(new Uint8Array(12));
36
+ const mutation = await createSetMutation({
37
+ signer: ctx.signer,
38
+ modelID: id,
39
+ data: input.data,
40
+ // owner: ctx.owner,
41
+ unique
42
+ });
43
+ // Add the signed mutation to context so it can be retrieved by the client
44
+ ctx.mutations[info.path.key] = stringifyToken(mutation);
45
+ // Need to return the mutation result for execution to work
46
+ return {
47
+ document: null
48
+ };
49
+ }
50
+ });
51
+ break;
52
+ }
53
+ case 'set':
54
+ {
55
+ this._mutations[`set${name}`] = mutationWithClientMutationId({
56
+ name: `Set${name}`,
57
+ inputFields: ()=>({
58
+ data: {
59
+ type: new GraphQLNonNull(this._inputObjects[id])
60
+ }
61
+ }),
62
+ outputFields: ()=>({
63
+ ...definitions.queryFields,
64
+ document: {
65
+ type: this._types[id]
66
+ }
67
+ }),
68
+ mutateAndGetPayload: async (input, ctx, info)=>{
69
+ // Create the mutation token
70
+ const unique = model.setFields.map((field)=>String(input.data[field] ?? '')).join('|');
71
+ const mutation = await createSetMutation({
72
+ signer: ctx.signer,
73
+ modelID: id,
74
+ data: input.data,
75
+ // owner: ctx.owner,
76
+ unique: fromUTF(unique)
77
+ });
78
+ // Add the signed mutation to context so it can be retrieved by the client
79
+ ctx.mutations[info.path.key] = stringifyToken(mutation);
80
+ // Need to return the mutation result for execution to work
81
+ return {
82
+ document: null
83
+ };
84
+ }
85
+ });
86
+ break;
87
+ }
88
+ }
89
+ this._mutations[`update${name}`] = mutationWithClientMutationId({
90
+ name: `Update${name}`,
91
+ inputFields: ()=>({
92
+ id: {
93
+ type: new GraphQLNonNull(GraphQLID)
94
+ },
95
+ data: {
96
+ type: this._inputObjects[`${id}-update`]
97
+ }
98
+ }),
99
+ outputFields: ()=>({
100
+ ...definitions.queryFields,
101
+ document: {
102
+ type: this._types[id]
103
+ }
104
+ }),
105
+ mutateAndGetPayload: async (input, ctx, info)=>{
106
+ // TODO: add existing doc if available
107
+ const mutation = await createChangeMutation({
108
+ signer: ctx.signer,
109
+ docID: input.id,
110
+ data: input.data
111
+ });
112
+ // Add the signed mutation to context so it can be retrieved by the client
113
+ ctx.mutations[info.path.key] = stringifyToken(mutation);
114
+ // Need to return the mutation result for execution to work
115
+ return {
116
+ document: null
117
+ };
118
+ }
119
+ });
120
+ }
121
+ }
package/lib/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export { createAttachment } from './attachments.js';
2
+ export { type ClientParams, KubunClient, type MutateGraphParams, } from './client.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAA;AACnD,OAAO,EACL,KAAK,YAAY,EACjB,WAAW,EACX,KAAK,iBAAiB,GACvB,MAAM,aAAa,CAAA"}
package/lib/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { createAttachment } from './attachments.js';
2
+ export { KubunClient } from './client.js';
@@ -0,0 +1,21 @@
1
+ import { type ExecutionResult, type GraphQLSchema } from 'graphql';
2
+ import type { Context } from './graphql.js';
3
+ type SchemasRecord = Record<string, GraphQLSchema | Promise<GraphQLSchema>>;
4
+ type MutationRunnerParams = {
5
+ schemas?: SchemasRecord;
6
+ };
7
+ type ExecuteMutationParams = {
8
+ context: Context;
9
+ schemaID: string;
10
+ source: string;
11
+ variables?: Record<string, unknown>;
12
+ };
13
+ export declare class MutationsRunner {
14
+ #private;
15
+ constructor(params?: MutationRunnerParams);
16
+ hasSchema(id: string): boolean;
17
+ setSchema(id: string, schemaOrPromise: GraphQLSchema | Promise<GraphQLSchema>): void;
18
+ execute(params: ExecuteMutationParams): Promise<ExecutionResult>;
19
+ }
20
+ export {};
21
+ //# sourceMappingURL=mutations-runner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mutations-runner.d.ts","sourceRoot":"","sources":["../src/mutations-runner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,eAAe,EAAE,KAAK,aAAa,EAAW,MAAM,SAAS,CAAA;AAE3E,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAE3C,KAAK,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC,CAAA;AAE3E,KAAK,oBAAoB,GAAG;IAC1B,OAAO,CAAC,EAAE,aAAa,CAAA;CACxB,CAAA;AAED,KAAK,qBAAqB,GAAG;IAC3B,OAAO,EAAE,OAAO,CAAA;IAChB,QAAQ,EAAE,MAAM,CAAA;IAChB,MAAM,EAAE,MAAM,CAAA;IACd,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CACpC,CAAA;AAED,qBAAa,eAAe;;gBAGd,MAAM,GAAE,oBAAyB;IAI7C,SAAS,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO;IAI9B,SAAS,CAAC,EAAE,EAAE,MAAM,EAAE,eAAe,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC,GAAG,IAAI;IAI9E,OAAO,CAAC,MAAM,EAAE,qBAAqB,GAAG,OAAO,CAAC,eAAe,CAAC;CAavE"}
@@ -0,0 +1,25 @@
1
+ import { graphql } from 'graphql';
2
+ export class MutationsRunner {
3
+ #schemas;
4
+ constructor(params = {}){
5
+ this.#schemas = params.schemas ?? {};
6
+ }
7
+ hasSchema(id) {
8
+ return this.#schemas[id] != null;
9
+ }
10
+ setSchema(id, schemaOrPromise) {
11
+ this.#schemas[id] = schemaOrPromise;
12
+ }
13
+ async execute(params) {
14
+ const schemaPromise = this.#schemas[params.schemaID];
15
+ if (schemaPromise == null) {
16
+ throw new Error(`Schema ${params.schemaID} not found`);
17
+ }
18
+ return await graphql({
19
+ contextValue: params.context,
20
+ schema: await schemaPromise,
21
+ source: params.source,
22
+ variableValues: params.variables
23
+ });
24
+ }
25
+ }
@@ -0,0 +1,21 @@
1
+ import * as A from '@automerge/automerge';
2
+ import { type SignedToken, type Signer } from '@enkaku/jwt';
3
+ import { DocumentID, type GraphModelID } from '@kubun/id';
4
+ import type { ChangeDocumentMutation, DocumentData, SetDocumentMutation } from '@kubun/protocol';
5
+ export declare function getMutationData<Data extends DocumentData = DocumentData>(data: Data | null, doc?: A.Doc<Data>): string | null;
6
+ export type SetMutationParams<Data extends DocumentData = DocumentData> = {
7
+ signer: Signer;
8
+ owner?: string;
9
+ modelID: GraphModelID | string;
10
+ data: Data | null;
11
+ unique: Uint8Array;
12
+ };
13
+ export declare function createSetMutation<Data extends DocumentData = DocumentData>(params: SetMutationParams<Data>): Promise<SignedToken<SetDocumentMutation>>;
14
+ export type CreateChangeMutationParams<Data extends DocumentData = DocumentData> = {
15
+ signer: Signer;
16
+ docID: DocumentID | string;
17
+ data: Data | null;
18
+ doc?: A.Doc<Data>;
19
+ };
20
+ export declare function createChangeMutation<Data extends DocumentData = DocumentData>(params: CreateChangeMutationParams<Data>): Promise<SignedToken<ChangeDocumentMutation>>;
21
+ //# sourceMappingURL=mutations.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mutations.d.ts","sourceRoot":"","sources":["../src/mutations.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,sBAAsB,CAAA;AAEzC,OAAO,EAAE,KAAK,WAAW,EAAE,KAAK,MAAM,EAAqB,MAAM,aAAa,CAAA;AAC9E,OAAO,EAAE,UAAU,EAAE,KAAK,YAAY,EAAE,MAAM,WAAW,CAAA;AACzD,OAAO,KAAK,EAAE,sBAAsB,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AAEhG,wBAAgB,eAAe,CAAC,IAAI,SAAS,YAAY,GAAG,YAAY,EACtE,IAAI,EAAE,IAAI,GAAG,IAAI,EACjB,GAAG,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,GAChB,MAAM,GAAG,IAAI,CAWf;AAED,MAAM,MAAM,iBAAiB,CAAC,IAAI,SAAS,YAAY,GAAG,YAAY,IAAI;IACxE,MAAM,EAAE,MAAM,CAAA;IACd,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,YAAY,GAAG,MAAM,CAAA;IAC9B,IAAI,EAAE,IAAI,GAAG,IAAI,CAAA;IACjB,MAAM,EAAE,UAAU,CAAA;CACnB,CAAA;AAED,wBAAsB,iBAAiB,CAAC,IAAI,SAAS,YAAY,GAAG,YAAY,EAC9E,MAAM,EAAE,iBAAiB,CAAC,IAAI,CAAC,GAC9B,OAAO,CAAC,WAAW,CAAC,mBAAmB,CAAC,CAAC,CAU3C;AAED,MAAM,MAAM,0BAA0B,CAAC,IAAI,SAAS,YAAY,GAAG,YAAY,IAAI;IACjF,MAAM,EAAE,MAAM,CAAA;IACd,KAAK,EAAE,UAAU,GAAG,MAAM,CAAA;IAC1B,IAAI,EAAE,IAAI,GAAG,IAAI,CAAA;IACjB,GAAG,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;CAClB,CAAA;AAED,wBAAsB,oBAAoB,CAAC,IAAI,SAAS,YAAY,GAAG,YAAY,EACjF,MAAM,EAAE,0BAA0B,CAAC,IAAI,CAAC,GACvC,OAAO,CAAC,WAAW,CAAC,sBAAsB,CAAC,CAAC,CAQ9C"}
@@ -0,0 +1,36 @@
1
+ import * as A from '@automerge/automerge';
2
+ import { toB64 } from '@enkaku/codec';
3
+ import { createSignedToken } from '@enkaku/jwt';
4
+ import { DocumentID } from '@kubun/id';
5
+ export function getMutationData(data, doc) {
6
+ if (data == null) {
7
+ return null;
8
+ }
9
+ if (doc == null) {
10
+ return toB64(A.save(A.from(data)));
11
+ }
12
+ const changedDoc = A.change(doc, (proxy)=>{
13
+ Object.assign(proxy, data);
14
+ });
15
+ return toB64(A.saveSince(changedDoc, A.getHeads(doc)));
16
+ }
17
+ export async function createSetMutation(params) {
18
+ const owner = params.owner ?? params.signer.did;
19
+ return await createSignedToken(params.signer, {
20
+ typ: 'set',
21
+ iss: params.signer.did,
22
+ aud: owner,
23
+ sub: DocumentID.create(params.modelID, owner, params.unique).toString(),
24
+ data: getMutationData(params.data),
25
+ unq: toB64(params.unique)
26
+ });
27
+ }
28
+ export async function createChangeMutation(params) {
29
+ return await createSignedToken(params.signer, {
30
+ typ: 'change',
31
+ iss: params.signer.did,
32
+ sub: DocumentID.from(params.docID).toString(),
33
+ data: getMutationData(params.data, params.doc),
34
+ inc: params.doc != null
35
+ });
36
+ }
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@kubun/client",
3
+ "version": "0.1.0",
4
+ "license": "see LICENSE.md",
5
+ "keywords": [],
6
+ "type": "module",
7
+ "main": "lib/index.js",
8
+ "types": "lib/index.d.ts",
9
+ "exports": {
10
+ ".": "./lib/index.js"
11
+ },
12
+ "files": [
13
+ "lib/*",
14
+ "LICENSE.md"
15
+ ],
16
+ "sideEffects": false,
17
+ "dependencies": {
18
+ "@automerge/automerge": "^2.2.8",
19
+ "@enkaku/client": "^0.2.2",
20
+ "@enkaku/codec": "^0.2.0",
21
+ "@enkaku/jwt": "^0.2.3",
22
+ "graphql": "^16.9.0",
23
+ "graphql-relay": "^0.10.2",
24
+ "multiformats": "^13.3.0",
25
+ "@kubun/graphql": "^0.1.0",
26
+ "@kubun/id": "^0.1.0"
27
+ },
28
+ "devDependencies": {
29
+ "@kubun/protocol": "^0.1.0"
30
+ },
31
+ "scripts": {
32
+ "build:clean": "del lib",
33
+ "build:js": "swc src -d ./lib --config-file ../../swc.json --strip-leading-paths",
34
+ "build:types": "tsc --emitDeclarationOnly --skipLibCheck",
35
+ "build:types:ci": "tsc --emitDeclarationOnly --declarationMap false",
36
+ "build": "pnpm run build:clean && pnpm run build:js && pnpm run build:types",
37
+ "test:types": "tsc --noEmit",
38
+ "test:unit": "node --experimental-vm-modules ../../node_modules/jest/bin/jest.js",
39
+ "test": "pnpm run test:types && pnpm run test:unit"
40
+ }
41
+ }