@enbox/api 0.1.0 → 0.2.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.
Files changed (63) hide show
  1. package/README.md +140 -159
  2. package/dist/browser.mjs +23 -13
  3. package/dist/browser.mjs.map +4 -4
  4. package/dist/esm/advanced.js +11 -0
  5. package/dist/esm/advanced.js.map +1 -0
  6. package/dist/esm/define-protocol.js +3 -3
  7. package/dist/esm/dwn-api.js +84 -130
  8. package/dist/esm/dwn-api.js.map +1 -1
  9. package/dist/esm/dwn-reader-api.js +128 -0
  10. package/dist/esm/dwn-reader-api.js.map +1 -0
  11. package/dist/esm/index.js +4 -2
  12. package/dist/esm/index.js.map +1 -1
  13. package/dist/esm/live-query.js +154 -0
  14. package/dist/esm/live-query.js.map +1 -0
  15. package/dist/esm/read-only-record.js +255 -0
  16. package/dist/esm/read-only-record.js.map +1 -0
  17. package/dist/esm/record.js +46 -57
  18. package/dist/esm/record.js.map +1 -1
  19. package/dist/esm/typed-web5.js +242 -0
  20. package/dist/esm/typed-web5.js.map +1 -0
  21. package/dist/esm/web5.js +71 -3
  22. package/dist/esm/web5.js.map +1 -1
  23. package/dist/types/advanced.d.ts +12 -0
  24. package/dist/types/advanced.d.ts.map +1 -0
  25. package/dist/types/define-protocol.d.ts +3 -3
  26. package/dist/types/dwn-api.d.ts +20 -104
  27. package/dist/types/dwn-api.d.ts.map +1 -1
  28. package/dist/types/dwn-reader-api.d.ts +138 -0
  29. package/dist/types/dwn-reader-api.d.ts.map +1 -0
  30. package/dist/types/index.d.ts +4 -2
  31. package/dist/types/index.d.ts.map +1 -1
  32. package/dist/types/live-query.d.ts +148 -0
  33. package/dist/types/live-query.d.ts.map +1 -0
  34. package/dist/types/protocol-types.d.ts +2 -2
  35. package/dist/types/read-only-record.d.ts +133 -0
  36. package/dist/types/read-only-record.d.ts.map +1 -0
  37. package/dist/types/record.d.ts +37 -17
  38. package/dist/types/record.d.ts.map +1 -1
  39. package/dist/types/{typed-dwn-api.d.ts → typed-web5.d.ts} +75 -76
  40. package/dist/types/typed-web5.d.ts.map +1 -0
  41. package/dist/types/web5.d.ts +79 -3
  42. package/dist/types/web5.d.ts.map +1 -1
  43. package/package.json +10 -6
  44. package/src/advanced.ts +29 -0
  45. package/src/define-protocol.ts +3 -3
  46. package/src/dwn-api.ts +141 -266
  47. package/src/dwn-reader-api.ts +255 -0
  48. package/src/index.ts +4 -2
  49. package/src/live-query.ts +261 -0
  50. package/src/protocol-types.ts +2 -2
  51. package/src/read-only-record.ts +328 -0
  52. package/src/record.ts +116 -86
  53. package/src/typed-web5.ts +445 -0
  54. package/src/web5.ts +104 -5
  55. package/dist/esm/subscription-util.js +0 -35
  56. package/dist/esm/subscription-util.js.map +0 -1
  57. package/dist/esm/typed-dwn-api.js +0 -181
  58. package/dist/esm/typed-dwn-api.js.map +0 -1
  59. package/dist/types/subscription-util.d.ts +0 -19
  60. package/dist/types/subscription-util.d.ts.map +0 -1
  61. package/dist/types/typed-dwn-api.d.ts.map +0 -1
  62. package/src/subscription-util.ts +0 -44
  63. package/src/typed-dwn-api.ts +0 -370
@@ -1,181 +0,0 @@
1
- /**
2
- * A type-safe wrapper around {@link DwnApi} scoped to a single protocol.
3
- *
4
- * `TypedDwnApi` is created via `dwn.using(typedProtocol)` and provides
5
- * autocompletion for protocol paths, typed data payloads, and tag shapes.
6
- *
7
- * Every method delegates to the corresponding `dwn.records.*` method,
8
- * injecting the protocol URI, protocolPath, and schema automatically.
9
- */
10
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
11
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
12
- return new (P || (P = Promise))(function (resolve, reject) {
13
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
14
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
15
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
16
- step((generator = generator.apply(thisArg, _arguments || [])).next());
17
- });
18
- };
19
- // ---------------------------------------------------------------------------
20
- // TypedDwnApi class
21
- // ---------------------------------------------------------------------------
22
- /**
23
- * A protocol-scoped wrapper around `DwnApi` that automatically injects
24
- * the `protocol` URI, `protocolPath`, and `schema` into every DWN operation.
25
- *
26
- * Obtain an instance via `dwn.using(typedProtocol)`.
27
- *
28
- * @example
29
- * ```ts
30
- * const social = dwn.using(SocialGraphProtocol);
31
- *
32
- * // Write — path and data type are checked at compile time
33
- * const { record } = await social.write('friend', {
34
- * data: { did: 'did:example:alice', alias: 'Alice' },
35
- * });
36
- *
37
- * // Query — protocol and protocolPath are auto-injected
38
- * const { records } = await social.query('friend', {
39
- * filter: { tags: { did: 'did:example:alice' } },
40
- * });
41
- * ```
42
- */
43
- export class TypedDwnApi {
44
- constructor(dwn, protocol) {
45
- this._dwn = dwn;
46
- this._definition = protocol.definition;
47
- }
48
- /** The protocol URI. */
49
- get protocol() {
50
- return this._definition.protocol;
51
- }
52
- /** The raw protocol definition. */
53
- get definition() {
54
- return this._definition;
55
- }
56
- /**
57
- * Configures (installs) this protocol on the local DWN.
58
- *
59
- * @param options - Optional overrides like `encryption`.
60
- */
61
- configure(options) {
62
- return __awaiter(this, void 0, void 0, function* () {
63
- return this._dwn.protocols.configure({
64
- message: { definition: this._definition },
65
- encryption: options === null || options === void 0 ? void 0 : options.encryption,
66
- });
67
- });
68
- }
69
- /**
70
- * Write a record at the given protocol path.
71
- *
72
- * @param path - The protocol path (e.g. `'friend'`, `'group/member'`).
73
- * @param request - Write options including typed `data`.
74
- */
75
- write(path, request) {
76
- return __awaiter(this, void 0, void 0, function* () {
77
- var _a, _b, _c;
78
- const typeName = lastSegment(path);
79
- const typeEntry = this._definition.types[typeName];
80
- return this._dwn.records.write({
81
- data: request.data,
82
- store: request.store,
83
- encryption: request.encryption,
84
- message: Object.assign(Object.assign({}, request.message), { protocol: this._definition.protocol, protocolPath: path, schema: typeEntry === null || typeEntry === void 0 ? void 0 : typeEntry.schema, dataFormat: (_b = (_a = request.message) === null || _a === void 0 ? void 0 : _a.dataFormat) !== null && _b !== void 0 ? _b : (_c = typeEntry === null || typeEntry === void 0 ? void 0 : typeEntry.dataFormats) === null || _c === void 0 ? void 0 : _c[0] }),
85
- });
86
- });
87
- }
88
- /**
89
- * Query records at the given protocol path.
90
- *
91
- * @param path - The protocol path to query.
92
- * @param request - Query options including optional filter, sort, and pagination.
93
- */
94
- query(path, request) {
95
- return __awaiter(this, void 0, void 0, function* () {
96
- const typeName = lastSegment(path);
97
- const typeEntry = this._definition.types[typeName];
98
- return this._dwn.records.query({
99
- from: request === null || request === void 0 ? void 0 : request.from,
100
- protocol: this._definition.protocol,
101
- encryption: request === null || request === void 0 ? void 0 : request.encryption,
102
- message: {
103
- filter: Object.assign(Object.assign({}, request === null || request === void 0 ? void 0 : request.filter), { protocol: this._definition.protocol, protocolPath: path, schema: typeEntry === null || typeEntry === void 0 ? void 0 : typeEntry.schema }),
104
- dateSort: request === null || request === void 0 ? void 0 : request.dateSort,
105
- pagination: request === null || request === void 0 ? void 0 : request.pagination,
106
- protocolRole: request === null || request === void 0 ? void 0 : request.protocolRole,
107
- },
108
- });
109
- });
110
- }
111
- /**
112
- * Read a single record at the given protocol path.
113
- *
114
- * @param path - The protocol path to read from.
115
- * @param request - Read options including a filter to identify the record.
116
- */
117
- read(path, request) {
118
- return __awaiter(this, void 0, void 0, function* () {
119
- const typeName = lastSegment(path);
120
- const typeEntry = this._definition.types[typeName];
121
- return this._dwn.records.read({
122
- from: request.from,
123
- protocol: this._definition.protocol,
124
- encryption: request.encryption,
125
- message: {
126
- filter: Object.assign(Object.assign({}, request.filter), { protocol: this._definition.protocol, protocolPath: path, schema: typeEntry === null || typeEntry === void 0 ? void 0 : typeEntry.schema }),
127
- },
128
- });
129
- });
130
- }
131
- /**
132
- * Delete a record at the given protocol path.
133
- *
134
- * @param path - The protocol path (used for permission scoping).
135
- * @param request - Delete options including the `recordId`.
136
- */
137
- delete(_path, request) {
138
- return __awaiter(this, void 0, void 0, function* () {
139
- return this._dwn.records.delete({
140
- from: request.from,
141
- protocol: this._definition.protocol,
142
- message: {
143
- recordId: request.recordId,
144
- },
145
- });
146
- });
147
- }
148
- /**
149
- * Subscribe to records at the given protocol path.
150
- *
151
- * @param path - The protocol path to subscribe to.
152
- * @param request - Subscribe options including the subscription handler.
153
- */
154
- subscribe(path, request) {
155
- return __awaiter(this, void 0, void 0, function* () {
156
- const typeName = lastSegment(path);
157
- const typeEntry = this._definition.types[typeName];
158
- return this._dwn.records.subscribe({
159
- from: request.from,
160
- protocol: this._definition.protocol,
161
- encryption: request.encryption,
162
- subscriptionHandler: request.subscriptionHandler,
163
- message: {
164
- filter: Object.assign(Object.assign({}, request.filter), { protocol: this._definition.protocol, protocolPath: path, schema: typeEntry === null || typeEntry === void 0 ? void 0 : typeEntry.schema }),
165
- protocolRole: request.protocolRole,
166
- },
167
- });
168
- });
169
- }
170
- }
171
- // ---------------------------------------------------------------------------
172
- // Helpers
173
- // ---------------------------------------------------------------------------
174
- /**
175
- * Returns the last segment of a slash-delimited path.
176
- */
177
- function lastSegment(path) {
178
- const parts = path.split('/');
179
- return parts[parts.length - 1];
180
- }
181
- //# sourceMappingURL=typed-dwn-api.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"typed-dwn-api.js","sourceRoot":"","sources":["../../src/typed-dwn-api.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;;;;;;;;;;AA4JH,8EAA8E;AAC9E,oBAAoB;AACpB,8EAA8E;AAE9E;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,OAAO,WAAW;IAOtB,YAAY,GAAW,EAAE,QAA6B;QACpD,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;QAChB,IAAI,CAAC,WAAW,GAAG,QAAQ,CAAC,UAAU,CAAC;IACzC,CAAC;IAED,wBAAwB;IACxB,IAAW,QAAQ;QACjB,OAAO,IAAI,CAAC,WAAW,CAAC,QAAQ,CAA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,KAAK,CAAC,GAAG,CAAC,CAAC;IAC9B,OAAO,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AACjC,CAAC"}
@@ -1,19 +0,0 @@
1
- import type { DwnRecordSubscriptionHandler, PermissionsApi, Web5Agent } from '@enbox/agent';
2
- import type { RecordsSubscribeRequest } from './dwn-api.js';
3
- /**
4
- * Utility class for dealing with subscriptions.
5
- */
6
- export declare class SubscriptionUtil {
7
- /**
8
- * Creates a record subscription handler that can be used to process incoming {Record} messages.
9
- */
10
- static recordSubscriptionHandler({ agent, connectedDid, request, delegateDid, protocolRole, permissionsApi }: {
11
- agent: Web5Agent;
12
- connectedDid: string;
13
- delegateDid?: string;
14
- protocolRole?: string;
15
- permissionsApi?: PermissionsApi;
16
- request: RecordsSubscribeRequest;
17
- }): DwnRecordSubscriptionHandler;
18
- }
19
- //# sourceMappingURL=subscription-util.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"subscription-util.d.ts","sourceRoot":"","sources":["../../src/subscription-util.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,4BAA4B,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAG5F,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AAE5D;;GAEG;AACH,qBAAa,gBAAgB;IAC3B;;OAEG;IACH,MAAM,CAAC,yBAAyB,CAAC,EAAE,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,cAAc,EAAE,EAAC;QAC3G,KAAK,EAAE,SAAS,CAAC;QACjB,YAAY,EAAE,MAAM,CAAC;QACrB,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,cAAc,CAAC,EAAE,cAAc,CAAC;QAChC,OAAO,EAAE,uBAAuB,CAAC;KAClC,GAAG,4BAA4B;CAuBjC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"typed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,CAAC;IAsB9B;;;;;OAKG;IACU,IAAI,CAAC,IAAI,SAAS,aAAa,CAAC,CAAC,CAAC,GAAG,MAAM,EACtD,IAAI,EAAG,IAAI,EACX,OAAO,EAAG,gBAAgB,GACzB,OAAO,CAAC,iBAAiB,CAAC;IAmB7B;;;;;OAKG;IACU,MAAM,CAAC,IAAI,SAAS,aAAa,CAAC,CAAC,CAAC,GAAG,MAAM,EACxD,KAAK,EAAG,IAAI,EACZ,OAAO,EAAG,kBAAkB,GAC3B,OAAO,CAAC,iBAAiB,CAAC;IAU7B;;;;;OAKG;IACU,SAAS,CAAC,IAAI,SAAS,aAAa,CAAC,CAAC,CAAC,GAAG,MAAM,EAC3D,IAAI,EAAG,IAAI,EACX,OAAO,EAAG,qBAAqB,GAC9B,OAAO,CAAC,sBAAsB,CAAC;CAoBnC"}
@@ -1,44 +0,0 @@
1
- import { getRecordAuthor } from '@enbox/agent';
2
- import type { DwnRecordSubscriptionHandler, PermissionsApi, Web5Agent } from '@enbox/agent';
3
-
4
- import { Record } from './record.js';
5
- import type { RecordsSubscribeRequest } from './dwn-api.js';
6
-
7
- /**
8
- * Utility class for dealing with subscriptions.
9
- */
10
- export class SubscriptionUtil {
11
- /**
12
- * Creates a record subscription handler that can be used to process incoming {Record} messages.
13
- */
14
- static recordSubscriptionHandler({ agent, connectedDid, request, delegateDid, protocolRole, permissionsApi }:{
15
- agent: Web5Agent;
16
- connectedDid: string;
17
- delegateDid?: string;
18
- protocolRole?: string;
19
- permissionsApi?: PermissionsApi;
20
- request: RecordsSubscribeRequest;
21
- }): DwnRecordSubscriptionHandler {
22
- const { subscriptionHandler, from: remoteOrigin } = request;
23
-
24
- return async (event) => {
25
- const { message, initialWrite } = event;
26
- const author = getRecordAuthor(message);
27
- const recordOptions = {
28
- author,
29
- connectedDid,
30
- remoteOrigin,
31
- initialWrite
32
- };
33
-
34
- const record = new Record(agent, {
35
- ...message,
36
- ...recordOptions,
37
- protocolRole,
38
- delegateDid: delegateDid,
39
- }, permissionsApi);
40
-
41
- subscriptionHandler(record);
42
- };
43
- }
44
- }
@@ -1,370 +0,0 @@
1
- /**
2
- * A type-safe wrapper around {@link DwnApi} scoped to a single protocol.
3
- *
4
- * `TypedDwnApi` is created via `dwn.using(typedProtocol)` and provides
5
- * autocompletion for protocol paths, typed data payloads, and tag shapes.
6
- *
7
- * Every method delegates to the corresponding `dwn.records.*` method,
8
- * injecting the protocol URI, protocolPath, and schema automatically.
9
- */
10
-
11
- import type { Protocol } from './protocol.js';
12
- import type { Record } from './record.js';
13
- import type { DateSort, ProtocolDefinition, ProtocolType, RecordsFilter } from '@enbox/dwn-sdk-js';
14
- import type { DwnApi, RecordsSubscriptionHandler } from './dwn-api.js';
15
- import type { DwnMessageSubscription, DwnPaginationCursor, DwnResponseStatus } from '@enbox/agent';
16
- import type { ProtocolPaths, SchemaMap, TypedProtocol, TypeNameAtPath } from './protocol-types.js';
17
-
18
- // ---------------------------------------------------------------------------
19
- // Helper types
20
- // ---------------------------------------------------------------------------
21
-
22
- /**
23
- * Resolves the TypeScript data type for a given protocol path.
24
- *
25
- * If the schema map contains a mapping for the type name at the given path,
26
- * that type is returned. Otherwise falls back to `unknown`.
27
- */
28
- type DataForPath<
29
- _D extends ProtocolDefinition,
30
- M extends SchemaMap,
31
- Path extends string,
32
- > = TypeNameAtPath<Path> extends keyof M ? M[TypeNameAtPath<Path>] : unknown;
33
-
34
- /**
35
- * Resolves the `ProtocolType` entry for a given protocol path.
36
- */
37
- type ProtocolTypeForPath<
38
- D extends ProtocolDefinition,
39
- Path extends string,
40
- > = TypeNameAtPath<Path> extends keyof D['types']
41
- ? D['types'][TypeNameAtPath<Path>] extends ProtocolType
42
- ? D['types'][TypeNameAtPath<Path>]
43
- : undefined
44
- : undefined;
45
-
46
- /**
47
- * Resolves a `dataFormat` string literal union for a path, or `string` if none.
48
- */
49
- type DataFormatForPath<
50
- D extends ProtocolDefinition,
51
- Path extends string,
52
- > = ProtocolTypeForPath<D, Path> extends { dataFormats: infer F }
53
- ? F extends readonly string[]
54
- ? F[number]
55
- : string
56
- : string;
57
-
58
- // ---------------------------------------------------------------------------
59
- // Request / response types
60
- // ---------------------------------------------------------------------------
61
-
62
- /** Options for `TypedDwnApi.write()`. */
63
- export type TypedWriteRequest<
64
- D extends ProtocolDefinition,
65
- M extends SchemaMap,
66
- Path extends string,
67
- > = {
68
- /** The data payload. Type-checked against the schema map. */
69
- data: DataForPath<D, M, Path>;
70
-
71
- /** Additional message parameters (protocolPath and protocol are injected). */
72
- message?: {
73
- parentContextId? : string;
74
- published? : boolean;
75
- datePublished? : string;
76
- recipient? : string;
77
- protocolRole? : string;
78
- dataFormat? : DataFormatForPath<D, Path>;
79
- tags? : globalThis.Record<string, string | number | boolean | string[] | number[]>;
80
- };
81
-
82
- /** Whether to persist immediately (defaults to `true`). */
83
- store?: boolean;
84
-
85
- /** Whether to auto-encrypt (follows protocol definition if omitted). */
86
- encryption?: boolean;
87
- };
88
-
89
- /** Response from `TypedDwnApi.write()`. */
90
- export type TypedWriteResponse = DwnResponseStatus & {
91
- record?: Record;
92
- };
93
-
94
- /** Filter options for `TypedDwnApi.query()`. */
95
- export type TypedQueryFilter = Omit<RecordsFilter, 'protocol' | 'protocolPath' | 'schema'> & {
96
- tags?: globalThis.Record<string, string | number | boolean | (string | number)[]>;
97
- };
98
-
99
- /** Options for `TypedDwnApi.query()`. */
100
- export type TypedQueryRequest = {
101
- /** Optional remote DWN DID to query from. */
102
- from?: string;
103
-
104
- /** Query filter (protocol, protocolPath, schema are injected). */
105
- filter? : TypedQueryFilter;
106
- dateSort? : DateSort;
107
- pagination? : { limit?: number; cursor?: DwnPaginationCursor };
108
- protocolRole? : string;
109
-
110
- /** When true, automatically decrypts encrypted records. */
111
- encryption?: boolean;
112
- };
113
-
114
- /** Response from `TypedDwnApi.query()`. */
115
- export type TypedQueryResponse = DwnResponseStatus & {
116
- records?: Record[];
117
- cursor? : DwnPaginationCursor;
118
- };
119
-
120
- /** Options for `TypedDwnApi.read()`. */
121
- export type TypedReadRequest = {
122
- /** Optional remote DWN DID to read from. */
123
- from?: string;
124
-
125
- /** Filter to identify the record (protocol and protocolPath are injected). */
126
- filter: Omit<RecordsFilter, 'protocol' | 'protocolPath' | 'schema'>;
127
-
128
- /** When true, automatically decrypts the record. */
129
- encryption?: boolean;
130
- };
131
-
132
- /** Response from `TypedDwnApi.read()`. */
133
- export type TypedReadResponse = DwnResponseStatus & {
134
- record: Record;
135
- };
136
-
137
- /** Options for `TypedDwnApi.delete()`. */
138
- export type TypedDeleteRequest = {
139
- /** Optional remote DWN DID to delete from. */
140
- from?: string;
141
-
142
- /** The `recordId` of the record to delete. */
143
- recordId: string;
144
- };
145
-
146
- /** Options for `TypedDwnApi.subscribe()`. */
147
- export type TypedSubscribeRequest = {
148
- /** Optional remote DWN DID to subscribe to. */
149
- from?: string;
150
-
151
- /** Subscription filter (protocol, protocolPath, schema are injected). */
152
- filter? : TypedQueryFilter;
153
- protocolRole? : string;
154
- subscriptionHandler : RecordsSubscriptionHandler;
155
-
156
- /** When true, indicates encryption is active. */
157
- encryption?: boolean;
158
- };
159
-
160
- /** Response from `TypedDwnApi.subscribe()`. */
161
- export type TypedSubscribeResponse = DwnResponseStatus & {
162
- subscription?: DwnMessageSubscription;
163
- };
164
-
165
- // ---------------------------------------------------------------------------
166
- // TypedDwnApi class
167
- // ---------------------------------------------------------------------------
168
-
169
- /**
170
- * A protocol-scoped wrapper around `DwnApi` that automatically injects
171
- * the `protocol` URI, `protocolPath`, and `schema` into every DWN operation.
172
- *
173
- * Obtain an instance via `dwn.using(typedProtocol)`.
174
- *
175
- * @example
176
- * ```ts
177
- * const social = dwn.using(SocialGraphProtocol);
178
- *
179
- * // Write — path and data type are checked at compile time
180
- * const { record } = await social.write('friend', {
181
- * data: { did: 'did:example:alice', alias: 'Alice' },
182
- * });
183
- *
184
- * // Query — protocol and protocolPath are auto-injected
185
- * const { records } = await social.query('friend', {
186
- * filter: { tags: { did: 'did:example:alice' } },
187
- * });
188
- * ```
189
- */
190
- export class TypedDwnApi<
191
- D extends ProtocolDefinition = ProtocolDefinition,
192
- M extends SchemaMap = SchemaMap,
193
- > {
194
- private _dwn: DwnApi;
195
- private _definition: D;
196
-
197
- constructor(dwn: DwnApi, protocol: TypedProtocol<D, M>) {
198
- this._dwn = dwn;
199
- this._definition = protocol.definition;
200
- }
201
-
202
- /** The protocol URI. */
203
- public get protocol(): string {
204
- return this._definition.protocol;
205
- }
206
-
207
- /** The raw protocol definition. */
208
- public get definition(): D {
209
- return this._definition;
210
- }
211
-
212
- /**
213
- * Configures (installs) this protocol on the local DWN.
214
- *
215
- * @param options - Optional overrides like `encryption`.
216
- */
217
- public async configure(options?: { encryption?: boolean }): Promise<DwnResponseStatus & { protocol?: Protocol }> {
218
- return this._dwn.protocols.configure({
219
- message : { definition: this._definition },
220
- encryption : options?.encryption,
221
- });
222
- }
223
-
224
- /**
225
- * Write a record at the given protocol path.
226
- *
227
- * @param path - The protocol path (e.g. `'friend'`, `'group/member'`).
228
- * @param request - Write options including typed `data`.
229
- */
230
- public async write<Path extends ProtocolPaths<D> & string>(
231
- path : Path,
232
- request : TypedWriteRequest<D, M, Path>,
233
- ): Promise<TypedWriteResponse> {
234
- const typeName = lastSegment(path);
235
- const typeEntry = this._definition.types[typeName] as ProtocolType | undefined;
236
-
237
- return this._dwn.records.write({
238
- data : request.data,
239
- store : request.store,
240
- encryption : request.encryption,
241
- message : {
242
- ...request.message,
243
- protocol : this._definition.protocol,
244
- protocolPath : path,
245
- schema : typeEntry?.schema,
246
- dataFormat : request.message?.dataFormat ?? typeEntry?.dataFormats?.[0],
247
- },
248
- });
249
- }
250
-
251
- /**
252
- * Query records at the given protocol path.
253
- *
254
- * @param path - The protocol path to query.
255
- * @param request - Query options including optional filter, sort, and pagination.
256
- */
257
- public async query<Path extends ProtocolPaths<D> & string>(
258
- path : Path,
259
- request? : TypedQueryRequest,
260
- ): Promise<TypedQueryResponse> {
261
- const typeName = lastSegment(path);
262
- const typeEntry = this._definition.types[typeName] as ProtocolType | undefined;
263
-
264
- return this._dwn.records.query({
265
- from : request?.from,
266
- protocol : this._definition.protocol,
267
- encryption : request?.encryption,
268
- message : {
269
- filter: {
270
- ...request?.filter,
271
- protocol : this._definition.protocol,
272
- protocolPath : path,
273
- schema : typeEntry?.schema,
274
- },
275
- dateSort : request?.dateSort,
276
- pagination : request?.pagination,
277
- protocolRole : request?.protocolRole,
278
- },
279
- });
280
- }
281
-
282
- /**
283
- * Read a single record at the given protocol path.
284
- *
285
- * @param path - The protocol path to read from.
286
- * @param request - Read options including a filter to identify the record.
287
- */
288
- public async read<Path extends ProtocolPaths<D> & string>(
289
- path : Path,
290
- request : TypedReadRequest,
291
- ): Promise<TypedReadResponse> {
292
- const typeName = lastSegment(path);
293
- const typeEntry = this._definition.types[typeName] as ProtocolType | undefined;
294
-
295
- return this._dwn.records.read({
296
- from : request.from,
297
- protocol : this._definition.protocol,
298
- encryption : request.encryption,
299
- message : {
300
- filter: {
301
- ...request.filter,
302
- protocol : this._definition.protocol,
303
- protocolPath : path,
304
- schema : typeEntry?.schema,
305
- },
306
- },
307
- });
308
- }
309
-
310
- /**
311
- * Delete a record at the given protocol path.
312
- *
313
- * @param path - The protocol path (used for permission scoping).
314
- * @param request - Delete options including the `recordId`.
315
- */
316
- public async delete<Path extends ProtocolPaths<D> & string>(
317
- _path : Path,
318
- request : TypedDeleteRequest,
319
- ): Promise<DwnResponseStatus> {
320
- return this._dwn.records.delete({
321
- from : request.from,
322
- protocol : this._definition.protocol,
323
- message : {
324
- recordId: request.recordId,
325
- },
326
- });
327
- }
328
-
329
- /**
330
- * Subscribe to records at the given protocol path.
331
- *
332
- * @param path - The protocol path to subscribe to.
333
- * @param request - Subscribe options including the subscription handler.
334
- */
335
- public async subscribe<Path extends ProtocolPaths<D> & string>(
336
- path : Path,
337
- request : TypedSubscribeRequest,
338
- ): Promise<TypedSubscribeResponse> {
339
- const typeName = lastSegment(path);
340
- const typeEntry = this._definition.types[typeName] as ProtocolType | undefined;
341
-
342
- return this._dwn.records.subscribe({
343
- from : request.from,
344
- protocol : this._definition.protocol,
345
- encryption : request.encryption,
346
- subscriptionHandler : request.subscriptionHandler,
347
- message : {
348
- filter: {
349
- ...request.filter,
350
- protocol : this._definition.protocol,
351
- protocolPath : path,
352
- schema : typeEntry?.schema,
353
- },
354
- protocolRole: request.protocolRole,
355
- },
356
- });
357
- }
358
- }
359
-
360
- // ---------------------------------------------------------------------------
361
- // Helpers
362
- // ---------------------------------------------------------------------------
363
-
364
- /**
365
- * Returns the last segment of a slash-delimited path.
366
- */
367
- function lastSegment(path: string): string {
368
- const parts = path.split('/');
369
- return parts[parts.length - 1];
370
- }