@enbox/api 0.1.1 → 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.
- package/README.md +140 -159
- package/dist/browser.mjs +23 -13
- package/dist/browser.mjs.map +4 -4
- package/dist/esm/advanced.js +11 -0
- package/dist/esm/advanced.js.map +1 -0
- package/dist/esm/define-protocol.js +3 -3
- package/dist/esm/dwn-api.js +55 -107
- package/dist/esm/dwn-api.js.map +1 -1
- package/dist/esm/dwn-reader-api.js +128 -0
- package/dist/esm/dwn-reader-api.js.map +1 -0
- package/dist/esm/index.js +3 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/live-query.js +5 -4
- package/dist/esm/live-query.js.map +1 -1
- package/dist/esm/read-only-record.js +255 -0
- package/dist/esm/read-only-record.js.map +1 -0
- package/dist/esm/record.js +46 -57
- package/dist/esm/record.js.map +1 -1
- package/dist/esm/typed-web5.js +242 -0
- package/dist/esm/typed-web5.js.map +1 -0
- package/dist/esm/web5.js +71 -3
- package/dist/esm/web5.js.map +1 -1
- package/dist/types/advanced.d.ts +12 -0
- package/dist/types/advanced.d.ts.map +1 -0
- package/dist/types/define-protocol.d.ts +3 -3
- package/dist/types/dwn-api.d.ts +12 -89
- package/dist/types/dwn-api.d.ts.map +1 -1
- package/dist/types/dwn-reader-api.d.ts +138 -0
- package/dist/types/dwn-reader-api.d.ts.map +1 -0
- package/dist/types/index.d.ts +3 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/live-query.d.ts +13 -1
- package/dist/types/live-query.d.ts.map +1 -1
- package/dist/types/protocol-types.d.ts +2 -2
- package/dist/types/read-only-record.d.ts +133 -0
- package/dist/types/read-only-record.d.ts.map +1 -0
- package/dist/types/record.d.ts +37 -17
- package/dist/types/record.d.ts.map +1 -1
- package/dist/types/{typed-dwn-api.d.ts → typed-web5.d.ts} +70 -73
- package/dist/types/typed-web5.d.ts.map +1 -0
- package/dist/types/web5.d.ts +79 -3
- package/dist/types/web5.d.ts.map +1 -1
- package/package.json +9 -5
- package/src/advanced.ts +29 -0
- package/src/define-protocol.ts +3 -3
- package/src/dwn-api.ts +88 -222
- package/src/dwn-reader-api.ts +255 -0
- package/src/index.ts +3 -2
- package/src/live-query.ts +20 -4
- package/src/protocol-types.ts +2 -2
- package/src/read-only-record.ts +328 -0
- package/src/record.ts +116 -86
- package/src/typed-web5.ts +445 -0
- package/src/web5.ts +104 -5
- package/dist/esm/typed-dwn-api.js +0 -182
- package/dist/esm/typed-dwn-api.js.map +0 -1
- package/dist/types/typed-dwn-api.d.ts.map +0 -1
- package/src/typed-dwn-api.ts +0 -370
|
@@ -1,182 +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
|
-
* Returns a {@link LiveQuery} that atomically provides an initial snapshot
|
|
152
|
-
* and a real-time stream of deduplicated change events.
|
|
153
|
-
*
|
|
154
|
-
* @param path - The protocol path to subscribe to.
|
|
155
|
-
* @param request - Subscribe options including optional filter and role.
|
|
156
|
-
*/
|
|
157
|
-
subscribe(path, request) {
|
|
158
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
159
|
-
const typeName = lastSegment(path);
|
|
160
|
-
const typeEntry = this._definition.types[typeName];
|
|
161
|
-
return this._dwn.records.subscribe({
|
|
162
|
-
from: request === null || request === void 0 ? void 0 : request.from,
|
|
163
|
-
protocol: this._definition.protocol,
|
|
164
|
-
message: {
|
|
165
|
-
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 }),
|
|
166
|
-
protocolRole: request === null || request === void 0 ? void 0 : request.protocolRole,
|
|
167
|
-
},
|
|
168
|
-
});
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
// ---------------------------------------------------------------------------
|
|
173
|
-
// Helpers
|
|
174
|
-
// ---------------------------------------------------------------------------
|
|
175
|
-
/**
|
|
176
|
-
* Returns the last segment of a slash-delimited path.
|
|
177
|
-
*/
|
|
178
|
-
function lastSegment(path) {
|
|
179
|
-
const parts = path.split('/');
|
|
180
|
-
return parts[parts.length - 1];
|
|
181
|
-
}
|
|
182
|
-
//# 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;;;;;;;;;;AA2JH,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,QA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,EAAE,CAAC,EAAE,IAAI,CAAC,GACtC,OAAO,CAAC,kBAAkB,CAAC;IAkB9B;;;;;OAKG;IACU,KAAK,CAAC,IAAI,SAAS,aAAa,CAAC,CAAC,CAAC,GAAG,MAAM,EACvD,IAAI,EAAG,IAAI,EACX,OAAO,CAAC,EAAG,iBAAiB,GAC3B,OAAO,CAAC,kBAAkB,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;;;;;;;;OAQG;IACU,SAAS,CAAC,IAAI,SAAS,aAAa,CAAC,CAAC,CAAC,GAAG,MAAM,EAC3D,IAAI,EAAG,IAAI,EACX,OAAO,CAAC,EAAG,qBAAqB,GAC/B,OAAO,CAAC,sBAAsB,CAAC;CAkBnC"}
|
package/src/typed-dwn-api.ts
DELETED
|
@@ -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 { DwnApi } from './dwn-api.js';
|
|
12
|
-
import type { LiveQuery } from './live-query.js';
|
|
13
|
-
import type { Protocol } from './protocol.js';
|
|
14
|
-
import type { Record } from './record.js';
|
|
15
|
-
|
|
16
|
-
import type { DateSort, ProtocolDefinition, ProtocolType, RecordsFilter } from '@enbox/dwn-sdk-js';
|
|
17
|
-
import type { DwnPaginationCursor, DwnResponseStatus } from '@enbox/agent';
|
|
18
|
-
import type { ProtocolPaths, SchemaMap, TypedProtocol, TypeNameAtPath } from './protocol-types.js';
|
|
19
|
-
|
|
20
|
-
// ---------------------------------------------------------------------------
|
|
21
|
-
// Helper types
|
|
22
|
-
// ---------------------------------------------------------------------------
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Resolves the TypeScript data type for a given protocol path.
|
|
26
|
-
*
|
|
27
|
-
* If the schema map contains a mapping for the type name at the given path,
|
|
28
|
-
* that type is returned. Otherwise falls back to `unknown`.
|
|
29
|
-
*/
|
|
30
|
-
type DataForPath<
|
|
31
|
-
_D extends ProtocolDefinition,
|
|
32
|
-
M extends SchemaMap,
|
|
33
|
-
Path extends string,
|
|
34
|
-
> = TypeNameAtPath<Path> extends keyof M ? M[TypeNameAtPath<Path>] : unknown;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Resolves the `ProtocolType` entry for a given protocol path.
|
|
38
|
-
*/
|
|
39
|
-
type ProtocolTypeForPath<
|
|
40
|
-
D extends ProtocolDefinition,
|
|
41
|
-
Path extends string,
|
|
42
|
-
> = TypeNameAtPath<Path> extends keyof D['types']
|
|
43
|
-
? D['types'][TypeNameAtPath<Path>] extends ProtocolType
|
|
44
|
-
? D['types'][TypeNameAtPath<Path>]
|
|
45
|
-
: undefined
|
|
46
|
-
: undefined;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Resolves a `dataFormat` string literal union for a path, or `string` if none.
|
|
50
|
-
*/
|
|
51
|
-
type DataFormatForPath<
|
|
52
|
-
D extends ProtocolDefinition,
|
|
53
|
-
Path extends string,
|
|
54
|
-
> = ProtocolTypeForPath<D, Path> extends { dataFormats: infer F }
|
|
55
|
-
? F extends readonly string[]
|
|
56
|
-
? F[number]
|
|
57
|
-
: string
|
|
58
|
-
: string;
|
|
59
|
-
|
|
60
|
-
// ---------------------------------------------------------------------------
|
|
61
|
-
// Request / response types
|
|
62
|
-
// ---------------------------------------------------------------------------
|
|
63
|
-
|
|
64
|
-
/** Options for `TypedDwnApi.write()`. */
|
|
65
|
-
export type TypedWriteRequest<
|
|
66
|
-
D extends ProtocolDefinition,
|
|
67
|
-
M extends SchemaMap,
|
|
68
|
-
Path extends string,
|
|
69
|
-
> = {
|
|
70
|
-
/** The data payload. Type-checked against the schema map. */
|
|
71
|
-
data: DataForPath<D, M, Path>;
|
|
72
|
-
|
|
73
|
-
/** Additional message parameters (protocolPath and protocol are injected). */
|
|
74
|
-
message?: {
|
|
75
|
-
parentContextId? : string;
|
|
76
|
-
published? : boolean;
|
|
77
|
-
datePublished? : string;
|
|
78
|
-
recipient? : string;
|
|
79
|
-
protocolRole? : string;
|
|
80
|
-
dataFormat? : DataFormatForPath<D, Path>;
|
|
81
|
-
tags? : globalThis.Record<string, string | number | boolean | string[] | number[]>;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
/** Whether to persist immediately (defaults to `true`). */
|
|
85
|
-
store?: boolean;
|
|
86
|
-
|
|
87
|
-
/** Whether to auto-encrypt (follows protocol definition if omitted). */
|
|
88
|
-
encryption?: boolean;
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
/** Response from `TypedDwnApi.write()`. */
|
|
92
|
-
export type TypedWriteResponse = DwnResponseStatus & {
|
|
93
|
-
record?: Record;
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
/** Filter options for `TypedDwnApi.query()`. */
|
|
97
|
-
export type TypedQueryFilter = Omit<RecordsFilter, 'protocol' | 'protocolPath' | 'schema'> & {
|
|
98
|
-
tags?: globalThis.Record<string, string | number | boolean | (string | number)[]>;
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
/** Options for `TypedDwnApi.query()`. */
|
|
102
|
-
export type TypedQueryRequest = {
|
|
103
|
-
/** Optional remote DWN DID to query from. */
|
|
104
|
-
from?: string;
|
|
105
|
-
|
|
106
|
-
/** Query filter (protocol, protocolPath, schema are injected). */
|
|
107
|
-
filter? : TypedQueryFilter;
|
|
108
|
-
dateSort? : DateSort;
|
|
109
|
-
pagination? : { limit?: number; cursor?: DwnPaginationCursor };
|
|
110
|
-
protocolRole? : string;
|
|
111
|
-
|
|
112
|
-
/** When true, automatically decrypts encrypted records. */
|
|
113
|
-
encryption?: boolean;
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
/** Response from `TypedDwnApi.query()`. */
|
|
117
|
-
export type TypedQueryResponse = DwnResponseStatus & {
|
|
118
|
-
records?: Record[];
|
|
119
|
-
cursor? : DwnPaginationCursor;
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
/** Options for `TypedDwnApi.read()`. */
|
|
123
|
-
export type TypedReadRequest = {
|
|
124
|
-
/** Optional remote DWN DID to read from. */
|
|
125
|
-
from?: string;
|
|
126
|
-
|
|
127
|
-
/** Filter to identify the record (protocol and protocolPath are injected). */
|
|
128
|
-
filter: Omit<RecordsFilter, 'protocol' | 'protocolPath' | 'schema'>;
|
|
129
|
-
|
|
130
|
-
/** When true, automatically decrypts the record. */
|
|
131
|
-
encryption?: boolean;
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
/** Response from `TypedDwnApi.read()`. */
|
|
135
|
-
export type TypedReadResponse = DwnResponseStatus & {
|
|
136
|
-
record: Record;
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
/** Options for `TypedDwnApi.delete()`. */
|
|
140
|
-
export type TypedDeleteRequest = {
|
|
141
|
-
/** Optional remote DWN DID to delete from. */
|
|
142
|
-
from?: string;
|
|
143
|
-
|
|
144
|
-
/** The `recordId` of the record to delete. */
|
|
145
|
-
recordId: string;
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
/** Options for `TypedDwnApi.subscribe()`. */
|
|
149
|
-
export type TypedSubscribeRequest = {
|
|
150
|
-
/** Optional remote DWN DID to subscribe to. */
|
|
151
|
-
from?: string;
|
|
152
|
-
|
|
153
|
-
/** Subscription filter (protocol, protocolPath, schema are injected). */
|
|
154
|
-
filter? : TypedQueryFilter;
|
|
155
|
-
protocolRole? : string;
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
/** Response from `TypedDwnApi.subscribe()`. */
|
|
159
|
-
export type TypedSubscribeResponse = DwnResponseStatus & {
|
|
160
|
-
/** The live query instance, or `undefined` if the request failed. */
|
|
161
|
-
liveQuery?: LiveQuery;
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
// ---------------------------------------------------------------------------
|
|
165
|
-
// TypedDwnApi class
|
|
166
|
-
// ---------------------------------------------------------------------------
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* A protocol-scoped wrapper around `DwnApi` that automatically injects
|
|
170
|
-
* the `protocol` URI, `protocolPath`, and `schema` into every DWN operation.
|
|
171
|
-
*
|
|
172
|
-
* Obtain an instance via `dwn.using(typedProtocol)`.
|
|
173
|
-
*
|
|
174
|
-
* @example
|
|
175
|
-
* ```ts
|
|
176
|
-
* const social = dwn.using(SocialGraphProtocol);
|
|
177
|
-
*
|
|
178
|
-
* // Write — path and data type are checked at compile time
|
|
179
|
-
* const { record } = await social.write('friend', {
|
|
180
|
-
* data: { did: 'did:example:alice', alias: 'Alice' },
|
|
181
|
-
* });
|
|
182
|
-
*
|
|
183
|
-
* // Query — protocol and protocolPath are auto-injected
|
|
184
|
-
* const { records } = await social.query('friend', {
|
|
185
|
-
* filter: { tags: { did: 'did:example:alice' } },
|
|
186
|
-
* });
|
|
187
|
-
* ```
|
|
188
|
-
*/
|
|
189
|
-
export class TypedDwnApi<
|
|
190
|
-
D extends ProtocolDefinition = ProtocolDefinition,
|
|
191
|
-
M extends SchemaMap = SchemaMap,
|
|
192
|
-
> {
|
|
193
|
-
private _dwn: DwnApi;
|
|
194
|
-
private _definition: D;
|
|
195
|
-
|
|
196
|
-
constructor(dwn: DwnApi, protocol: TypedProtocol<D, M>) {
|
|
197
|
-
this._dwn = dwn;
|
|
198
|
-
this._definition = protocol.definition;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/** The protocol URI. */
|
|
202
|
-
public get protocol(): string {
|
|
203
|
-
return this._definition.protocol;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/** The raw protocol definition. */
|
|
207
|
-
public get definition(): D {
|
|
208
|
-
return this._definition;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Configures (installs) this protocol on the local DWN.
|
|
213
|
-
*
|
|
214
|
-
* @param options - Optional overrides like `encryption`.
|
|
215
|
-
*/
|
|
216
|
-
public async configure(options?: { encryption?: boolean }): Promise<DwnResponseStatus & { protocol?: Protocol }> {
|
|
217
|
-
return this._dwn.protocols.configure({
|
|
218
|
-
message : { definition: this._definition },
|
|
219
|
-
encryption : options?.encryption,
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* Write a record at the given protocol path.
|
|
225
|
-
*
|
|
226
|
-
* @param path - The protocol path (e.g. `'friend'`, `'group/member'`).
|
|
227
|
-
* @param request - Write options including typed `data`.
|
|
228
|
-
*/
|
|
229
|
-
public async write<Path extends ProtocolPaths<D> & string>(
|
|
230
|
-
path : Path,
|
|
231
|
-
request : TypedWriteRequest<D, M, Path>,
|
|
232
|
-
): Promise<TypedWriteResponse> {
|
|
233
|
-
const typeName = lastSegment(path);
|
|
234
|
-
const typeEntry = this._definition.types[typeName] as ProtocolType | undefined;
|
|
235
|
-
|
|
236
|
-
return this._dwn.records.write({
|
|
237
|
-
data : request.data,
|
|
238
|
-
store : request.store,
|
|
239
|
-
encryption : request.encryption,
|
|
240
|
-
message : {
|
|
241
|
-
...request.message,
|
|
242
|
-
protocol : this._definition.protocol,
|
|
243
|
-
protocolPath : path,
|
|
244
|
-
schema : typeEntry?.schema,
|
|
245
|
-
dataFormat : request.message?.dataFormat ?? typeEntry?.dataFormats?.[0],
|
|
246
|
-
},
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* Query records at the given protocol path.
|
|
252
|
-
*
|
|
253
|
-
* @param path - The protocol path to query.
|
|
254
|
-
* @param request - Query options including optional filter, sort, and pagination.
|
|
255
|
-
*/
|
|
256
|
-
public async query<Path extends ProtocolPaths<D> & string>(
|
|
257
|
-
path : Path,
|
|
258
|
-
request? : TypedQueryRequest,
|
|
259
|
-
): Promise<TypedQueryResponse> {
|
|
260
|
-
const typeName = lastSegment(path);
|
|
261
|
-
const typeEntry = this._definition.types[typeName] as ProtocolType | undefined;
|
|
262
|
-
|
|
263
|
-
return this._dwn.records.query({
|
|
264
|
-
from : request?.from,
|
|
265
|
-
protocol : this._definition.protocol,
|
|
266
|
-
encryption : request?.encryption,
|
|
267
|
-
message : {
|
|
268
|
-
filter: {
|
|
269
|
-
...request?.filter,
|
|
270
|
-
protocol : this._definition.protocol,
|
|
271
|
-
protocolPath : path,
|
|
272
|
-
schema : typeEntry?.schema,
|
|
273
|
-
},
|
|
274
|
-
dateSort : request?.dateSort,
|
|
275
|
-
pagination : request?.pagination,
|
|
276
|
-
protocolRole : request?.protocolRole,
|
|
277
|
-
},
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
/**
|
|
282
|
-
* Read a single record at the given protocol path.
|
|
283
|
-
*
|
|
284
|
-
* @param path - The protocol path to read from.
|
|
285
|
-
* @param request - Read options including a filter to identify the record.
|
|
286
|
-
*/
|
|
287
|
-
public async read<Path extends ProtocolPaths<D> & string>(
|
|
288
|
-
path : Path,
|
|
289
|
-
request : TypedReadRequest,
|
|
290
|
-
): Promise<TypedReadResponse> {
|
|
291
|
-
const typeName = lastSegment(path);
|
|
292
|
-
const typeEntry = this._definition.types[typeName] as ProtocolType | undefined;
|
|
293
|
-
|
|
294
|
-
return this._dwn.records.read({
|
|
295
|
-
from : request.from,
|
|
296
|
-
protocol : this._definition.protocol,
|
|
297
|
-
encryption : request.encryption,
|
|
298
|
-
message : {
|
|
299
|
-
filter: {
|
|
300
|
-
...request.filter,
|
|
301
|
-
protocol : this._definition.protocol,
|
|
302
|
-
protocolPath : path,
|
|
303
|
-
schema : typeEntry?.schema,
|
|
304
|
-
},
|
|
305
|
-
},
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
/**
|
|
310
|
-
* Delete a record at the given protocol path.
|
|
311
|
-
*
|
|
312
|
-
* @param path - The protocol path (used for permission scoping).
|
|
313
|
-
* @param request - Delete options including the `recordId`.
|
|
314
|
-
*/
|
|
315
|
-
public async delete<Path extends ProtocolPaths<D> & string>(
|
|
316
|
-
_path : Path,
|
|
317
|
-
request : TypedDeleteRequest,
|
|
318
|
-
): Promise<DwnResponseStatus> {
|
|
319
|
-
return this._dwn.records.delete({
|
|
320
|
-
from : request.from,
|
|
321
|
-
protocol : this._definition.protocol,
|
|
322
|
-
message : {
|
|
323
|
-
recordId: request.recordId,
|
|
324
|
-
},
|
|
325
|
-
});
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
/**
|
|
329
|
-
* Subscribe to records at the given protocol path.
|
|
330
|
-
*
|
|
331
|
-
* Returns a {@link LiveQuery} that atomically provides an initial snapshot
|
|
332
|
-
* and a real-time stream of deduplicated change events.
|
|
333
|
-
*
|
|
334
|
-
* @param path - The protocol path to subscribe to.
|
|
335
|
-
* @param request - Subscribe options including optional filter and role.
|
|
336
|
-
*/
|
|
337
|
-
public async subscribe<Path extends ProtocolPaths<D> & string>(
|
|
338
|
-
path : Path,
|
|
339
|
-
request? : TypedSubscribeRequest,
|
|
340
|
-
): Promise<TypedSubscribeResponse> {
|
|
341
|
-
const typeName = lastSegment(path);
|
|
342
|
-
const typeEntry = this._definition.types[typeName] as ProtocolType | undefined;
|
|
343
|
-
|
|
344
|
-
return this._dwn.records.subscribe({
|
|
345
|
-
from : request?.from,
|
|
346
|
-
protocol : this._definition.protocol,
|
|
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
|
-
}
|