@atproto/lex-resolver 0.0.13 → 0.0.14
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/CHANGELOG.md +14 -0
- package/dist/lex-resolver-error.d.ts +99 -2
- package/dist/lex-resolver-error.d.ts.map +1 -1
- package/dist/lex-resolver-error.js +94 -3
- package/dist/lex-resolver-error.js.map +1 -1
- package/dist/lex-resolver.d.ts +264 -1
- package/dist/lex-resolver.d.ts.map +1 -1
- package/dist/lex-resolver.js +163 -0
- package/dist/lex-resolver.js.map +1 -1
- package/package.json +8 -7
- package/src/lex-resolver-error.ts +91 -2
- package/src/lex-resolver.ts +269 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @atproto/lex-resolver
|
|
2
2
|
|
|
3
|
+
## 0.0.14
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#4601](https://github.com/bluesky-social/atproto/pull/4601) [`ed61c62`](https://github.com/bluesky-social/atproto/commit/ed61c62f3161fcde85ee9a93f8ed339c7e06c015) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Fix `exports` field in package.json
|
|
8
|
+
|
|
9
|
+
- [#4601](https://github.com/bluesky-social/atproto/pull/4601) [`ed61c62`](https://github.com/bluesky-social/atproto/commit/ed61c62f3161fcde85ee9a93f8ed339c7e06c015) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Add JSDoc
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [[`7b9a98a`](https://github.com/bluesky-social/atproto/commit/7b9a98a763636c5f66a06da11fe6013f29dd9157), [`7b9a98a`](https://github.com/bluesky-social/atproto/commit/7b9a98a763636c5f66a06da11fe6013f29dd9157), [`7b9a98a`](https://github.com/bluesky-social/atproto/commit/7b9a98a763636c5f66a06da11fe6013f29dd9157), [`ed61c62`](https://github.com/bluesky-social/atproto/commit/ed61c62f3161fcde85ee9a93f8ed339c7e06c015), [`7b9a98a`](https://github.com/bluesky-social/atproto/commit/7b9a98a763636c5f66a06da11fe6013f29dd9157), [`ed61c62`](https://github.com/bluesky-social/atproto/commit/ed61c62f3161fcde85ee9a93f8ed339c7e06c015), [`7b9a98a`](https://github.com/bluesky-social/atproto/commit/7b9a98a763636c5f66a06da11fe6013f29dd9157), [`7b9a98a`](https://github.com/bluesky-social/atproto/commit/7b9a98a763636c5f66a06da11fe6013f29dd9157), [`ed61c62`](https://github.com/bluesky-social/atproto/commit/ed61c62f3161fcde85ee9a93f8ed339c7e06c015), [`ed61c62`](https://github.com/bluesky-social/atproto/commit/ed61c62f3161fcde85ee9a93f8ed339c7e06c015)]:
|
|
12
|
+
- @atproto/lex-schema@0.0.12
|
|
13
|
+
- @atproto/lex-document@0.0.13
|
|
14
|
+
- @atproto/lex-client@0.0.12
|
|
15
|
+
- @atproto/lex-data@0.0.11
|
|
16
|
+
|
|
3
17
|
## 0.0.13
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
|
@@ -1,9 +1,106 @@
|
|
|
1
|
+
import { LexError } from '@atproto/lex-data';
|
|
1
2
|
import { NSID } from '@atproto/syntax';
|
|
2
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Error class for lexicon resolution failures.
|
|
5
|
+
*
|
|
6
|
+
* This error is thrown when the {@link LexResolver} encounters issues during
|
|
7
|
+
* the resolution process, such as DNS lookup failures, DID resolution errors,
|
|
8
|
+
* invalid lexicon documents, or network failures.
|
|
9
|
+
*
|
|
10
|
+
* @example Catching resolution errors
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { LexResolver, LexResolverError } from '@atproto/lex-resolver'
|
|
13
|
+
*
|
|
14
|
+
* const resolver = new LexResolver({})
|
|
15
|
+
*
|
|
16
|
+
* try {
|
|
17
|
+
* const result = await resolver.get('com.example.myLexicon')
|
|
18
|
+
* } catch (error) {
|
|
19
|
+
* if (error instanceof LexResolverError) {
|
|
20
|
+
* console.error(`Failed to resolve ${error.nsid}: ${error.description}`)
|
|
21
|
+
* // Access the original cause if available
|
|
22
|
+
* if (error.cause) {
|
|
23
|
+
* console.error('Caused by:', error.cause)
|
|
24
|
+
* }
|
|
25
|
+
* }
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @example Creating errors with the factory method
|
|
30
|
+
* ```typescript
|
|
31
|
+
* import { LexResolverError } from '@atproto/lex-resolver'
|
|
32
|
+
*
|
|
33
|
+
* // Create from string NSID
|
|
34
|
+
* const error = LexResolverError.from(
|
|
35
|
+
* 'com.example.myLexicon',
|
|
36
|
+
* 'Custom error description'
|
|
37
|
+
* )
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export declare class LexResolverError extends LexError {
|
|
41
|
+
/**
|
|
42
|
+
* The NSID that failed to resolve.
|
|
43
|
+
*/
|
|
3
44
|
readonly nsid: NSID;
|
|
45
|
+
/**
|
|
46
|
+
* Human-readable description of what went wrong during resolution.
|
|
47
|
+
*/
|
|
4
48
|
readonly description: string;
|
|
5
49
|
name: string;
|
|
6
|
-
|
|
50
|
+
/**
|
|
51
|
+
* Creates a new LexResolverError instance.
|
|
52
|
+
*
|
|
53
|
+
* @param nsid - The NSID that failed to resolve
|
|
54
|
+
* @param description - Human-readable description of the error. Defaults to
|
|
55
|
+
* a generic message if not provided.
|
|
56
|
+
* @param options - Standard error options including `cause` for error chaining
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* import { NSID } from '@atproto/syntax'
|
|
61
|
+
* import { LexResolverError } from '@atproto/lex-resolver'
|
|
62
|
+
*
|
|
63
|
+
* const nsid = NSID.from('com.example.myLexicon')
|
|
64
|
+
* const error = new LexResolverError(
|
|
65
|
+
* nsid,
|
|
66
|
+
* 'DNS lookup failed',
|
|
67
|
+
* { cause: originalError }
|
|
68
|
+
* )
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
constructor(
|
|
72
|
+
/**
|
|
73
|
+
* The NSID that failed to resolve.
|
|
74
|
+
*/
|
|
75
|
+
nsid: NSID,
|
|
76
|
+
/**
|
|
77
|
+
* Human-readable description of what went wrong during resolution.
|
|
78
|
+
*/
|
|
79
|
+
description?: string, options?: ErrorOptions);
|
|
80
|
+
/**
|
|
81
|
+
* Factory method to create a LexResolverError from a string or NSID.
|
|
82
|
+
*
|
|
83
|
+
* This is a convenience method that handles the conversion of string NSIDs
|
|
84
|
+
* to NSID objects automatically.
|
|
85
|
+
*
|
|
86
|
+
* @param nsid - The NSID as a string or NSID object
|
|
87
|
+
* @param description - Optional human-readable description of the error
|
|
88
|
+
* @returns A new LexResolverError instance
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```typescript
|
|
92
|
+
* import { LexResolverError } from '@atproto/lex-resolver'
|
|
93
|
+
*
|
|
94
|
+
* // Create from string
|
|
95
|
+
* const error1 = LexResolverError.from('com.example.myLexicon')
|
|
96
|
+
*
|
|
97
|
+
* // Create with description
|
|
98
|
+
* const error2 = LexResolverError.from(
|
|
99
|
+
* 'com.example.myLexicon',
|
|
100
|
+
* 'Authority not found in DNS'
|
|
101
|
+
* )
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
7
104
|
static from(nsid: NSID | string, description?: string): LexResolverError;
|
|
8
105
|
}
|
|
9
106
|
//# sourceMappingURL=lex-resolver-error.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lex-resolver-error.d.ts","sourceRoot":"","sources":["../src/lex-resolver-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAA;AAEtC,qBAAa,gBAAiB,SAAQ,
|
|
1
|
+
{"version":3,"file":"lex-resolver-error.d.ts","sourceRoot":"","sources":["../src/lex-resolver-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAC5C,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAA;AAEtC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,qBAAa,gBAAiB,SAAQ,QAAQ;IAyB1C;;OAEG;aACa,IAAI,EAAE,IAAI;IAC1B;;OAEG;aACa,WAAW;IA/B7B,IAAI,SAAqB;IAEzB;;;;;;;;;;;;;;;;;;;;OAoBG;;IAED;;OAEG;IACa,IAAI,EAAE,IAAI;IAC1B;;OAEG;IACa,WAAW,SAAuC,EAClE,OAAO,CAAC,EAAE,YAAY;IAKxB;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM;CAMtD"}
|
|
@@ -1,16 +1,107 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LexResolverError = void 0;
|
|
4
|
+
const lex_data_1 = require("@atproto/lex-data");
|
|
4
5
|
const syntax_1 = require("@atproto/syntax");
|
|
5
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Error class for lexicon resolution failures.
|
|
8
|
+
*
|
|
9
|
+
* This error is thrown when the {@link LexResolver} encounters issues during
|
|
10
|
+
* the resolution process, such as DNS lookup failures, DID resolution errors,
|
|
11
|
+
* invalid lexicon documents, or network failures.
|
|
12
|
+
*
|
|
13
|
+
* @example Catching resolution errors
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { LexResolver, LexResolverError } from '@atproto/lex-resolver'
|
|
16
|
+
*
|
|
17
|
+
* const resolver = new LexResolver({})
|
|
18
|
+
*
|
|
19
|
+
* try {
|
|
20
|
+
* const result = await resolver.get('com.example.myLexicon')
|
|
21
|
+
* } catch (error) {
|
|
22
|
+
* if (error instanceof LexResolverError) {
|
|
23
|
+
* console.error(`Failed to resolve ${error.nsid}: ${error.description}`)
|
|
24
|
+
* // Access the original cause if available
|
|
25
|
+
* if (error.cause) {
|
|
26
|
+
* console.error('Caused by:', error.cause)
|
|
27
|
+
* }
|
|
28
|
+
* }
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @example Creating errors with the factory method
|
|
33
|
+
* ```typescript
|
|
34
|
+
* import { LexResolverError } from '@atproto/lex-resolver'
|
|
35
|
+
*
|
|
36
|
+
* // Create from string NSID
|
|
37
|
+
* const error = LexResolverError.from(
|
|
38
|
+
* 'com.example.myLexicon',
|
|
39
|
+
* 'Custom error description'
|
|
40
|
+
* )
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
class LexResolverError extends lex_data_1.LexError {
|
|
6
44
|
nsid;
|
|
7
45
|
description;
|
|
8
46
|
name = 'LexResolverError';
|
|
9
|
-
|
|
10
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Creates a new LexResolverError instance.
|
|
49
|
+
*
|
|
50
|
+
* @param nsid - The NSID that failed to resolve
|
|
51
|
+
* @param description - Human-readable description of the error. Defaults to
|
|
52
|
+
* a generic message if not provided.
|
|
53
|
+
* @param options - Standard error options including `cause` for error chaining
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* import { NSID } from '@atproto/syntax'
|
|
58
|
+
* import { LexResolverError } from '@atproto/lex-resolver'
|
|
59
|
+
*
|
|
60
|
+
* const nsid = NSID.from('com.example.myLexicon')
|
|
61
|
+
* const error = new LexResolverError(
|
|
62
|
+
* nsid,
|
|
63
|
+
* 'DNS lookup failed',
|
|
64
|
+
* { cause: originalError }
|
|
65
|
+
* )
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
constructor(
|
|
69
|
+
/**
|
|
70
|
+
* The NSID that failed to resolve.
|
|
71
|
+
*/
|
|
72
|
+
nsid,
|
|
73
|
+
/**
|
|
74
|
+
* Human-readable description of what went wrong during resolution.
|
|
75
|
+
*/
|
|
76
|
+
description = `Could not resolve Lexicon for NSID`, options) {
|
|
77
|
+
super('LexiconResolutionFailure', `${description} (${nsid})`, options);
|
|
11
78
|
this.nsid = nsid;
|
|
12
79
|
this.description = description;
|
|
13
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* Factory method to create a LexResolverError from a string or NSID.
|
|
83
|
+
*
|
|
84
|
+
* This is a convenience method that handles the conversion of string NSIDs
|
|
85
|
+
* to NSID objects automatically.
|
|
86
|
+
*
|
|
87
|
+
* @param nsid - The NSID as a string or NSID object
|
|
88
|
+
* @param description - Optional human-readable description of the error
|
|
89
|
+
* @returns A new LexResolverError instance
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* import { LexResolverError } from '@atproto/lex-resolver'
|
|
94
|
+
*
|
|
95
|
+
* // Create from string
|
|
96
|
+
* const error1 = LexResolverError.from('com.example.myLexicon')
|
|
97
|
+
*
|
|
98
|
+
* // Create with description
|
|
99
|
+
* const error2 = LexResolverError.from(
|
|
100
|
+
* 'com.example.myLexicon',
|
|
101
|
+
* 'Authority not found in DNS'
|
|
102
|
+
* )
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
14
105
|
static from(nsid, description) {
|
|
15
106
|
return new LexResolverError(typeof nsid === 'string' ? syntax_1.NSID.from(nsid) : nsid, description);
|
|
16
107
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lex-resolver-error.js","sourceRoot":"","sources":["../src/lex-resolver-error.ts"],"names":[],"mappings":";;;AAAA,4CAAsC;AAEtC,MAAa,gBAAiB,SAAQ,
|
|
1
|
+
{"version":3,"file":"lex-resolver-error.js","sourceRoot":"","sources":["../src/lex-resolver-error.ts"],"names":[],"mappings":";;;AAAA,gDAA4C;AAC5C,4CAAsC;AAEtC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,MAAa,gBAAiB,SAAQ,mBAAQ;IA4B1B;IAIA;IA/BlB,IAAI,GAAG,kBAAkB,CAAA;IAEzB;;;;;;;;;;;;;;;;;;;;OAoBG;IACH;IACE;;OAEG;IACa,IAAU;IAC1B;;OAEG;IACa,cAAc,oCAAoC,EAClE,OAAsB;QAEtB,KAAK,CAAC,0BAA0B,EAAE,GAAG,WAAW,KAAK,IAAI,GAAG,EAAE,OAAO,CAAC,CAAA;QAPtD,SAAI,GAAJ,IAAI,CAAM;QAIV,gBAAW,GAAX,WAAW,CAAuC;IAIpE,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,MAAM,CAAC,IAAI,CAAC,IAAmB,EAAE,WAAoB;QACnD,OAAO,IAAI,gBAAgB,CACzB,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,aAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,EACjD,WAAW,CACZ,CAAA;IACH,CAAC;CACF;AApED,4CAoEC","sourcesContent":["import { LexError } from '@atproto/lex-data'\nimport { NSID } from '@atproto/syntax'\n\n/**\n * Error class for lexicon resolution failures.\n *\n * This error is thrown when the {@link LexResolver} encounters issues during\n * the resolution process, such as DNS lookup failures, DID resolution errors,\n * invalid lexicon documents, or network failures.\n *\n * @example Catching resolution errors\n * ```typescript\n * import { LexResolver, LexResolverError } from '@atproto/lex-resolver'\n *\n * const resolver = new LexResolver({})\n *\n * try {\n * const result = await resolver.get('com.example.myLexicon')\n * } catch (error) {\n * if (error instanceof LexResolverError) {\n * console.error(`Failed to resolve ${error.nsid}: ${error.description}`)\n * // Access the original cause if available\n * if (error.cause) {\n * console.error('Caused by:', error.cause)\n * }\n * }\n * }\n * ```\n *\n * @example Creating errors with the factory method\n * ```typescript\n * import { LexResolverError } from '@atproto/lex-resolver'\n *\n * // Create from string NSID\n * const error = LexResolverError.from(\n * 'com.example.myLexicon',\n * 'Custom error description'\n * )\n * ```\n */\nexport class LexResolverError extends LexError {\n name = 'LexResolverError'\n\n /**\n * Creates a new LexResolverError instance.\n *\n * @param nsid - The NSID that failed to resolve\n * @param description - Human-readable description of the error. Defaults to\n * a generic message if not provided.\n * @param options - Standard error options including `cause` for error chaining\n *\n * @example\n * ```typescript\n * import { NSID } from '@atproto/syntax'\n * import { LexResolverError } from '@atproto/lex-resolver'\n *\n * const nsid = NSID.from('com.example.myLexicon')\n * const error = new LexResolverError(\n * nsid,\n * 'DNS lookup failed',\n * { cause: originalError }\n * )\n * ```\n */\n constructor(\n /**\n * The NSID that failed to resolve.\n */\n public readonly nsid: NSID,\n /**\n * Human-readable description of what went wrong during resolution.\n */\n public readonly description = `Could not resolve Lexicon for NSID`,\n options?: ErrorOptions,\n ) {\n super('LexiconResolutionFailure', `${description} (${nsid})`, options)\n }\n\n /**\n * Factory method to create a LexResolverError from a string or NSID.\n *\n * This is a convenience method that handles the conversion of string NSIDs\n * to NSID objects automatically.\n *\n * @param nsid - The NSID as a string or NSID object\n * @param description - Optional human-readable description of the error\n * @returns A new LexResolverError instance\n *\n * @example\n * ```typescript\n * import { LexResolverError } from '@atproto/lex-resolver'\n *\n * // Create from string\n * const error1 = LexResolverError.from('com.example.myLexicon')\n *\n * // Create with description\n * const error2 = LexResolverError.from(\n * 'com.example.myLexicon',\n * 'Authority not found in DNS'\n * )\n * ```\n */\n static from(nsid: NSID | string, description?: string) {\n return new LexResolverError(\n typeof nsid === 'string' ? NSID.from(nsid) : nsid,\n description,\n )\n }\n}\n"]}
|
package/dist/lex-resolver.d.ts
CHANGED
|
@@ -2,30 +2,105 @@ import { Cid } from '@atproto/lex-data';
|
|
|
2
2
|
import { LexiconDocument } from '@atproto/lex-document';
|
|
3
3
|
import { AtUri, NSID } from '@atproto/syntax';
|
|
4
4
|
import { CreateDidResolverOptions, Did, DidResolver, ResolveDidOptions } from '@atproto-labs/did-resolver';
|
|
5
|
+
/**
|
|
6
|
+
* Result returned when successfully resolving a lexicon document.
|
|
7
|
+
*
|
|
8
|
+
* Contains the full AT URI where the lexicon was found, the content-addressed
|
|
9
|
+
* identifier (CID) for integrity verification, and the parsed lexicon document.
|
|
10
|
+
*/
|
|
5
11
|
export type LexResolverResult = {
|
|
12
|
+
/** The AT URI where the lexicon document was found */
|
|
6
13
|
uri: AtUri;
|
|
14
|
+
/** Content identifier (CID) of the lexicon record for integrity verification */
|
|
7
15
|
cid: Cid;
|
|
16
|
+
/** The parsed and validated lexicon document */
|
|
8
17
|
lexicon: LexiconDocument;
|
|
9
18
|
};
|
|
19
|
+
/**
|
|
20
|
+
* Result returned when fetching a lexicon document from a specific URI.
|
|
21
|
+
*
|
|
22
|
+
* This is a subset of {@link LexResolverResult} used internally and by hooks,
|
|
23
|
+
* containing only the CID and lexicon document (without the URI, which is
|
|
24
|
+
* already known from the fetch request).
|
|
25
|
+
*/
|
|
10
26
|
export type LexResolverFetchResult = {
|
|
27
|
+
/** Content identifier (CID) of the lexicon record */
|
|
11
28
|
cid: Cid;
|
|
29
|
+
/** The parsed and validated lexicon document */
|
|
12
30
|
lexicon: LexiconDocument;
|
|
13
31
|
};
|
|
14
|
-
type Awaitable<T> = T | Promise<T>;
|
|
32
|
+
export type Awaitable<T> = T | Promise<T>;
|
|
33
|
+
/**
|
|
34
|
+
* Callback hooks for customizing the lexicon resolution process.
|
|
35
|
+
*
|
|
36
|
+
* Hooks allow you to intercept, cache, or override the default resolution
|
|
37
|
+
* behavior at various stages. Each hook can be synchronous or asynchronous.
|
|
38
|
+
*
|
|
39
|
+
* @example Implementing a cache with hooks
|
|
40
|
+
* ```typescript
|
|
41
|
+
* import { LexResolver, LexResolverHooks, LexResolverFetchResult } from '@atproto/lex-resolver'
|
|
42
|
+
* import { AtUri } from '@atproto/syntax'
|
|
43
|
+
*
|
|
44
|
+
* const cache = new Map<string, LexResolverFetchResult>()
|
|
45
|
+
*
|
|
46
|
+
* const hooks: LexResolverHooks = {
|
|
47
|
+
* // Return cached result if available, bypassing network fetch
|
|
48
|
+
* onFetch({ uri }) {
|
|
49
|
+
* return cache.get(uri.toString())
|
|
50
|
+
* },
|
|
51
|
+
* // Cache successful fetches
|
|
52
|
+
* onFetchResult({ uri, cid, lexicon }) {
|
|
53
|
+
* cache.set(uri.toString(), { cid, lexicon })
|
|
54
|
+
* },
|
|
55
|
+
* // Log errors for monitoring
|
|
56
|
+
* onFetchError({ uri, err }) {
|
|
57
|
+
* console.error(`Failed to fetch ${uri}:`, err)
|
|
58
|
+
* }
|
|
59
|
+
* }
|
|
60
|
+
*
|
|
61
|
+
* const resolver = new LexResolver({ hooks })
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* @example Overriding authority resolution for testing
|
|
65
|
+
* ```typescript
|
|
66
|
+
* const hooks: LexResolverHooks = {
|
|
67
|
+
* // Always resolve to a test DID
|
|
68
|
+
* onResolveAuthority({ nsid }) {
|
|
69
|
+
* if (nsid.authority === 'test.example') {
|
|
70
|
+
* return 'did:plc:test123'
|
|
71
|
+
* }
|
|
72
|
+
* // Return undefined to use default resolution
|
|
73
|
+
* }
|
|
74
|
+
* }
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
15
77
|
export type LexResolverHooks = {
|
|
16
78
|
/**
|
|
17
79
|
* Hook called before resolving a lexicon authority DID. If a DID is returned,
|
|
18
80
|
* it will be used instead of performing the default resolution. In that case,
|
|
19
81
|
* the `onResolveAuthorityResult` and `onResolveAuthorityError` hooks will
|
|
20
82
|
* not be called.
|
|
83
|
+
*
|
|
84
|
+
* @param data - Object containing the NSID being resolved
|
|
85
|
+
* @returns A DID to use instead of default resolution, or void/undefined to proceed normally
|
|
21
86
|
*/
|
|
22
87
|
onResolveAuthority?(data: {
|
|
23
88
|
nsid: NSID;
|
|
24
89
|
}): Awaitable<void | Did>;
|
|
90
|
+
/**
|
|
91
|
+
* Hook called after successfully resolving a lexicon authority DID.
|
|
92
|
+
*
|
|
93
|
+
* @param data - Object containing the NSID and resolved DID
|
|
94
|
+
*/
|
|
25
95
|
onResolveAuthorityResult?(data: {
|
|
26
96
|
nsid: NSID;
|
|
27
97
|
did: Did;
|
|
28
98
|
}): Awaitable<void>;
|
|
99
|
+
/**
|
|
100
|
+
* Hook called when authority resolution fails.
|
|
101
|
+
*
|
|
102
|
+
* @param data - Object containing the NSID and error that occurred
|
|
103
|
+
*/
|
|
29
104
|
onResolveAuthorityError?(data: {
|
|
30
105
|
nsid: NSID;
|
|
31
106
|
err: unknown;
|
|
@@ -34,32 +109,220 @@ export type LexResolverHooks = {
|
|
|
34
109
|
* Hook called before fetching a lexicon URI. If a result is returned, it will
|
|
35
110
|
* be used instead of performing the default fetch. In that case, the
|
|
36
111
|
* `onFetchResult` and `onFetchError` hooks will not be called.
|
|
112
|
+
*
|
|
113
|
+
* @param data - Object containing the URI being fetched
|
|
114
|
+
* @returns A fetch result to use instead of default fetch, or void/undefined to proceed normally
|
|
37
115
|
*/
|
|
38
116
|
onFetch?(data: {
|
|
39
117
|
uri: AtUri;
|
|
40
118
|
}): Awaitable<void | LexResolverFetchResult>;
|
|
119
|
+
/**
|
|
120
|
+
* Hook called after successfully fetching a lexicon document.
|
|
121
|
+
*
|
|
122
|
+
* @param data - Object containing the URI, CID, and parsed lexicon document
|
|
123
|
+
*/
|
|
41
124
|
onFetchResult?(data: {
|
|
42
125
|
uri: AtUri;
|
|
43
126
|
cid: Cid;
|
|
44
127
|
lexicon: LexiconDocument;
|
|
45
128
|
}): Awaitable<void>;
|
|
129
|
+
/**
|
|
130
|
+
* Hook called when fetching fails.
|
|
131
|
+
*
|
|
132
|
+
* @param data - Object containing the URI and error that occurred
|
|
133
|
+
*/
|
|
46
134
|
onFetchError?(data: {
|
|
47
135
|
uri: AtUri;
|
|
48
136
|
err: unknown;
|
|
49
137
|
}): Awaitable<void>;
|
|
50
138
|
};
|
|
139
|
+
/**
|
|
140
|
+
* Configuration options for the {@link LexResolver}.
|
|
141
|
+
*
|
|
142
|
+
* Extends DID resolver options with lexicon-specific hooks for customizing
|
|
143
|
+
* the resolution process.
|
|
144
|
+
*
|
|
145
|
+
* @see {@link CreateDidResolverOptions} for DID resolver configuration
|
|
146
|
+
*/
|
|
51
147
|
export type LexResolverOptions = CreateDidResolverOptions & {
|
|
148
|
+
/**
|
|
149
|
+
* Optional hooks for customizing the resolution process.
|
|
150
|
+
* See {@link LexResolverHooks} for available callbacks.
|
|
151
|
+
*/
|
|
52
152
|
hooks?: LexResolverHooks;
|
|
53
153
|
};
|
|
54
154
|
export { AtUri, type Cid, NSID };
|
|
55
155
|
export type { LexiconDocument, ResolveDidOptions };
|
|
156
|
+
/**
|
|
157
|
+
* Resolves Lexicon documents from the AT Protocol network.
|
|
158
|
+
*
|
|
159
|
+
* The {@link LexResolver} handles the complete process of resolving a lexicon
|
|
160
|
+
* by NSID:
|
|
161
|
+
* 1. **Authority Resolution**: Looks up the `_lexicon.<authority>` DNS TXT record
|
|
162
|
+
* to find the DID that controls lexicons for that namespace
|
|
163
|
+
* 2. **DID Resolution**: Resolves the DID document to find the PDS endpoint and
|
|
164
|
+
* signing key
|
|
165
|
+
* 3. **Record Fetch**: Fetches the lexicon record from the PDS with cryptographic
|
|
166
|
+
* proof verification
|
|
167
|
+
* 4. **Validation**: Validates the lexicon document structure
|
|
168
|
+
*
|
|
169
|
+
* @example Basic usage - resolve a lexicon by NSID
|
|
170
|
+
* ```typescript
|
|
171
|
+
* import { LexResolver } from '@atproto/lex-resolver'
|
|
172
|
+
*
|
|
173
|
+
* const resolver = new LexResolver({})
|
|
174
|
+
*
|
|
175
|
+
* // Get a lexicon document by its NSID
|
|
176
|
+
* const result = await resolver.get('app.bsky.feed.post')
|
|
177
|
+
* console.log(result.lexicon) // The parsed lexicon document
|
|
178
|
+
* console.log(result.uri) // AT URI where it was found
|
|
179
|
+
* console.log(result.cid) // Content identifier for verification
|
|
180
|
+
* ```
|
|
181
|
+
*
|
|
182
|
+
* @example Two-step resolution for more control
|
|
183
|
+
* ```typescript
|
|
184
|
+
* import { LexResolver } from '@atproto/lex-resolver'
|
|
185
|
+
*
|
|
186
|
+
* const resolver = new LexResolver({})
|
|
187
|
+
*
|
|
188
|
+
* // Step 1: Resolve the authority to get the AT URI
|
|
189
|
+
* const uri = await resolver.resolve('app.bsky.feed.post')
|
|
190
|
+
* console.log(uri.toString()) // 'at://did:plc:xxx/com.atproto.lexicon.schema/app.bsky.feed.post'
|
|
191
|
+
*
|
|
192
|
+
* // Step 2: Fetch the lexicon from the URI
|
|
193
|
+
* const result = await resolver.fetch(uri)
|
|
194
|
+
* console.log(result.lexicon)
|
|
195
|
+
* ```
|
|
196
|
+
*
|
|
197
|
+
* @example Using hooks for caching
|
|
198
|
+
* ```typescript
|
|
199
|
+
* import { LexResolver, LexResolverFetchResult } from '@atproto/lex-resolver'
|
|
200
|
+
*
|
|
201
|
+
* const cache = new Map<string, LexResolverFetchResult>()
|
|
202
|
+
*
|
|
203
|
+
* const resolver = new LexResolver({
|
|
204
|
+
* hooks: {
|
|
205
|
+
* onFetch({ uri }) {
|
|
206
|
+
* return cache.get(uri.toString())
|
|
207
|
+
* },
|
|
208
|
+
* onFetchResult({ uri, cid, lexicon }) {
|
|
209
|
+
* cache.set(uri.toString(), { cid, lexicon })
|
|
210
|
+
* }
|
|
211
|
+
* }
|
|
212
|
+
* })
|
|
213
|
+
* ```
|
|
214
|
+
*
|
|
215
|
+
* @example Error handling
|
|
216
|
+
* ```typescript
|
|
217
|
+
* import { LexResolver, LexResolverError } from '@atproto/lex-resolver'
|
|
218
|
+
*
|
|
219
|
+
* const resolver = new LexResolver({})
|
|
220
|
+
*
|
|
221
|
+
* try {
|
|
222
|
+
* const result = await resolver.get('com.example.unknown')
|
|
223
|
+
* } catch (error) {
|
|
224
|
+
* if (error instanceof LexResolverError) {
|
|
225
|
+
* console.error(`Failed to resolve ${error.nsid}: ${error.description}`)
|
|
226
|
+
* }
|
|
227
|
+
* }
|
|
228
|
+
* ```
|
|
229
|
+
*/
|
|
56
230
|
export declare class LexResolver {
|
|
57
231
|
protected readonly options: LexResolverOptions;
|
|
58
232
|
protected readonly didResolver: DidResolver<'plc' | 'web'>;
|
|
59
233
|
constructor(options: LexResolverOptions);
|
|
234
|
+
/**
|
|
235
|
+
* Gets a lexicon document by its NSID.
|
|
236
|
+
*
|
|
237
|
+
* This is the primary method for resolving lexicons. It combines
|
|
238
|
+
* {@link resolve} and {@link fetch} into a single operation, handling
|
|
239
|
+
* authority resolution, DID lookup, and record fetching.
|
|
240
|
+
*
|
|
241
|
+
* @param nsidStr - The NSID to resolve, either as a string or NSID object
|
|
242
|
+
* @param options - Optional DID resolution options (e.g., signal for cancellation)
|
|
243
|
+
* @returns The resolved lexicon result containing URI, CID, and lexicon document
|
|
244
|
+
* @throws {LexResolverError} If resolution fails at any stage
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* ```typescript
|
|
248
|
+
* // Resolve using string NSID
|
|
249
|
+
* const result = await resolver.get('app.bsky.feed.post')
|
|
250
|
+
*
|
|
251
|
+
* // Resolve using NSID object
|
|
252
|
+
* import { NSID } from '@atproto/syntax'
|
|
253
|
+
* const nsid = NSID.from('app.bsky.feed.post')
|
|
254
|
+
* const result = await resolver.get(nsid)
|
|
255
|
+
*
|
|
256
|
+
* // With abort signal for cancellation
|
|
257
|
+
* const controller = new AbortController()
|
|
258
|
+
* const result = await resolver.get('app.bsky.feed.post', {
|
|
259
|
+
* signal: controller.signal
|
|
260
|
+
* })
|
|
261
|
+
* ```
|
|
262
|
+
*/
|
|
60
263
|
get(nsidStr: NSID | string, options?: ResolveDidOptions): Promise<LexResolverResult>;
|
|
264
|
+
/**
|
|
265
|
+
* Resolves the authority for an NSID and returns the AT URI for the lexicon.
|
|
266
|
+
*
|
|
267
|
+
* This method performs the first stage of lexicon resolution:
|
|
268
|
+
* 1. Parses the NSID to extract the authority domain
|
|
269
|
+
* 2. Looks up the `_lexicon.<authority>` DNS TXT record
|
|
270
|
+
* 3. Extracts the DID from the TXT record (format: `did=<did>`)
|
|
271
|
+
* 4. Constructs the AT URI for the lexicon record
|
|
272
|
+
*
|
|
273
|
+
* Use this when you need the URI without fetching the actual document,
|
|
274
|
+
* or when you want to implement custom fetching logic.
|
|
275
|
+
*
|
|
276
|
+
* @param nsidStr - The NSID to resolve, either as a string or NSID object
|
|
277
|
+
* @returns The AT URI pointing to the lexicon record
|
|
278
|
+
* @throws {LexResolverError} If authority resolution fails (e.g., DNS lookup fails)
|
|
279
|
+
*
|
|
280
|
+
* @example
|
|
281
|
+
* ```typescript
|
|
282
|
+
* // Resolve to get the AT URI
|
|
283
|
+
* const uri = await resolver.resolve('app.bsky.feed.post')
|
|
284
|
+
* console.log(uri.toString())
|
|
285
|
+
* // Output: 'at://did:plc:z72i7hdynmk6r22z27h6tvur/com.atproto.lexicon.schema/app.bsky.feed.post'
|
|
286
|
+
*
|
|
287
|
+
* // The URI can then be used with fetch() or stored for later use
|
|
288
|
+
* const result = await resolver.fetch(uri)
|
|
289
|
+
* ```
|
|
290
|
+
*/
|
|
61
291
|
resolve(nsidStr: NSID | string): Promise<AtUri>;
|
|
62
292
|
protected resolveLexiconAuthority(nsid: NSID): Promise<Did>;
|
|
293
|
+
/**
|
|
294
|
+
* Fetches a lexicon document from a specific AT URI.
|
|
295
|
+
*
|
|
296
|
+
* This method performs the second stage of lexicon resolution:
|
|
297
|
+
* 1. Resolves the DID from the URI to find the PDS endpoint
|
|
298
|
+
* 2. Fetches the record from the PDS using `com.atproto.sync.getRecord`
|
|
299
|
+
* 3. Verifies the cryptographic proof (commit signature)
|
|
300
|
+
* 4. Validates the lexicon document structure
|
|
301
|
+
* 5. Ensures the document ID matches the URI rkey
|
|
302
|
+
*
|
|
303
|
+
* Use this when you already have an AT URI (e.g., from {@link resolve})
|
|
304
|
+
* and want to fetch the lexicon document.
|
|
305
|
+
*
|
|
306
|
+
* @param uriStr - The AT URI to fetch, either as a string or AtUri object
|
|
307
|
+
* @param options - Optional DID resolution options (e.g., signal for cancellation, noCache)
|
|
308
|
+
* @returns The resolved lexicon result containing URI, CID, and lexicon document
|
|
309
|
+
* @throws {LexResolverError} If fetching or validation fails
|
|
310
|
+
*
|
|
311
|
+
* @example
|
|
312
|
+
* ```typescript
|
|
313
|
+
* // Fetch from a known URI
|
|
314
|
+
* const result = await resolver.fetch(
|
|
315
|
+
* 'at://did:plc:xyz/com.atproto.lexicon.schema/app.bsky.feed.post'
|
|
316
|
+
* )
|
|
317
|
+
*
|
|
318
|
+
* // Fetch with no-cache to bypass any upstream caching
|
|
319
|
+
* const result = await resolver.fetch(uri, { noCache: true })
|
|
320
|
+
*
|
|
321
|
+
* // Fetch with abort signal
|
|
322
|
+
* const controller = new AbortController()
|
|
323
|
+
* const result = await resolver.fetch(uri, { signal: controller.signal })
|
|
324
|
+
* ```
|
|
325
|
+
*/
|
|
63
326
|
fetch(uriStr: AtUri | string, options?: ResolveDidOptions): Promise<LexResolverResult>;
|
|
64
327
|
protected fetchLexiconUri(uri: AtUri, options?: ResolveDidOptions): Promise<LexResolverFetchResult>;
|
|
65
328
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lex-resolver.d.ts","sourceRoot":"","sources":["../src/lex-resolver.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAA;AACvC,OAAO,EAAE,eAAe,EAAyB,MAAM,uBAAuB,CAAA;AAQ9E,OAAO,EAAE,KAAK,EAAE,IAAI,EAAc,MAAM,iBAAiB,CAAA;AACzD,OAAO,EAEL,wBAAwB,EACxB,GAAG,EACH,WAAW,EACX,iBAAiB,EAIlB,MAAM,4BAA4B,CAAA;AAInC,MAAM,MAAM,iBAAiB,GAAG;IAC9B,GAAG,EAAE,KAAK,CAAA;IACV,GAAG,EAAE,GAAG,CAAA;IACR,OAAO,EAAE,eAAe,CAAA;CACzB,CAAA;AAED,MAAM,MAAM,sBAAsB,GAAG;IACnC,GAAG,EAAE,GAAG,CAAA;IACR,OAAO,EAAE,eAAe,CAAA;CACzB,CAAA;AAED,
|
|
1
|
+
{"version":3,"file":"lex-resolver.d.ts","sourceRoot":"","sources":["../src/lex-resolver.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAA;AACvC,OAAO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