@atproto/lex-client 0.0.11 → 0.0.13
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 +26 -0
- package/dist/agent.d.ts +67 -0
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +31 -0
- package/dist/agent.js.map +1 -1
- package/dist/client.d.ts +443 -45
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +145 -1
- package/dist/client.js.map +1 -1
- package/dist/errors.d.ts +162 -9
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +132 -8
- package/dist/errors.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/createRecord.defs.d.ts +29 -25
- package/dist/lexicons/com/atproto/repo/createRecord.defs.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/createRecord.defs.js +3 -2
- package/dist/lexicons/com/atproto/repo/createRecord.defs.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/deleteRecord.defs.d.ts +17 -17
- package/dist/lexicons/com/atproto/repo/deleteRecord.defs.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/deleteRecord.defs.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/getRecord.defs.d.ts +9 -9
- package/dist/lexicons/com/atproto/repo/getRecord.defs.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/getRecord.defs.js +1 -1
- package/dist/lexicons/com/atproto/repo/getRecord.defs.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/listRecords.defs.d.ts +8 -8
- package/dist/lexicons/com/atproto/repo/listRecords.defs.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/listRecords.defs.js +1 -1
- package/dist/lexicons/com/atproto/repo/listRecords.defs.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/putRecord.defs.d.ts +31 -27
- package/dist/lexicons/com/atproto/repo/putRecord.defs.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/putRecord.defs.js +3 -2
- package/dist/lexicons/com/atproto/repo/putRecord.defs.js.map +1 -1
- package/dist/lexicons/com/atproto/repo/uploadBlob.defs.d.ts +7 -7
- package/dist/lexicons/com/atproto/repo/uploadBlob.defs.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/repo/uploadBlob.defs.js.map +1 -1
- package/dist/lexicons/com/atproto/sync/getBlob.defs.d.ts +3 -3
- package/dist/lexicons/com/atproto/sync/getBlob.defs.d.ts.map +1 -1
- package/dist/lexicons/com/atproto/sync/getBlob.defs.js.map +1 -1
- package/dist/response.d.ts +14 -4
- package/dist/response.d.ts.map +1 -1
- package/dist/response.js +19 -9
- package/dist/response.js.map +1 -1
- package/dist/types.d.ts +51 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/util.d.ts +40 -5
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +22 -0
- package/dist/util.js.map +1 -1
- package/dist/www-authenticate.d.ts +23 -0
- package/dist/www-authenticate.d.ts.map +1 -1
- package/dist/www-authenticate.js.map +1 -1
- package/dist/xrpc.d.ts +81 -1
- package/dist/xrpc.d.ts.map +1 -1
- package/dist/xrpc.js.map +1 -1
- package/package.json +7 -7
- package/src/agent.ts +67 -0
- package/src/client.ts +424 -11
- package/src/errors.ts +165 -12
- package/src/lexicons/com/atproto/repo/createRecord.defs.ts +15 -7
- package/src/lexicons/com/atproto/repo/deleteRecord.defs.ts +11 -5
- package/src/lexicons/com/atproto/repo/getRecord.defs.ts +7 -4
- package/src/lexicons/com/atproto/repo/listRecords.defs.ts +8 -5
- package/src/lexicons/com/atproto/repo/putRecord.defs.ts +15 -7
- package/src/lexicons/com/atproto/repo/uploadBlob.defs.ts +11 -5
- package/src/lexicons/com/atproto/sync/getBlob.defs.ts +6 -3
- package/src/response.ts +22 -19
- package/src/types.ts +52 -0
- package/src/util.ts +50 -5
- package/src/www-authenticate.ts +24 -0
- package/src/xrpc.ts +83 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# @atproto/lex-client
|
|
2
2
|
|
|
3
|
+
## 0.0.13
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [[`39dea03`](https://github.com/bluesky-social/atproto/commit/39dea03c417a1da069962560505427a7aa25ad7a), [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df), [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df), [`39dea03`](https://github.com/bluesky-social/atproto/commit/39dea03c417a1da069962560505427a7aa25ad7a), [`39dea03`](https://github.com/bluesky-social/atproto/commit/39dea03c417a1da069962560505427a7aa25ad7a), [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df), [`39dea03`](https://github.com/bluesky-social/atproto/commit/39dea03c417a1da069962560505427a7aa25ad7a), [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df), [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df), [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df), [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df), [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df), [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df), [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df), [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df)]:
|
|
8
|
+
- @atproto/lex-schema@0.0.13
|
|
9
|
+
- @atproto/lex-data@0.0.12
|
|
10
|
+
- @atproto/lex-json@0.0.12
|
|
11
|
+
|
|
12
|
+
## 0.0.12
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- [#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 missing exports
|
|
17
|
+
|
|
18
|
+
- [#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
|
|
19
|
+
|
|
20
|
+
- [#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
|
|
21
|
+
|
|
22
|
+
- [#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 to `Client` class properties and methods
|
|
23
|
+
|
|
24
|
+
- Updated dependencies [[`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), [`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)]:
|
|
25
|
+
- @atproto/lex-schema@0.0.12
|
|
26
|
+
- @atproto/lex-json@0.0.11
|
|
27
|
+
- @atproto/lex-data@0.0.11
|
|
28
|
+
|
|
3
29
|
## 0.0.11
|
|
4
30
|
|
|
5
31
|
### Patch Changes
|
package/dist/agent.d.ts
CHANGED
|
@@ -1,4 +1,16 @@
|
|
|
1
1
|
import { DidString } from '@atproto/lex-schema';
|
|
2
|
+
/**
|
|
3
|
+
* A function that performs HTTP requests towards a service endpoint.
|
|
4
|
+
*
|
|
5
|
+
* The handler is responsible for adding the origin (protocol, hostname, and
|
|
6
|
+
* port) to the provided path, typically based on authentication or service
|
|
7
|
+
* configuration. The handler are also responsible for adding any necessary
|
|
8
|
+
* headers, such as authorization tokens.
|
|
9
|
+
*
|
|
10
|
+
* @param path - The URL path (pathname + query parameters) without the origin
|
|
11
|
+
* @param init - Standard fetch RequestInit options
|
|
12
|
+
* @returns A Promise resolving to the HTTP Response
|
|
13
|
+
*/
|
|
2
14
|
export type FetchHandler = (
|
|
3
15
|
/**
|
|
4
16
|
* The URL (pathname + query parameters) to make the request to, without the
|
|
@@ -6,8 +18,27 @@ export type FetchHandler = (
|
|
|
6
18
|
* {@link FetchHandler}, typically based on authentication or other factors.
|
|
7
19
|
*/
|
|
8
20
|
path: string, init: RequestInit) => Promise<Response>;
|
|
21
|
+
/**
|
|
22
|
+
* Core interface for making XRPC requests.
|
|
23
|
+
*
|
|
24
|
+
* An Agent encapsulates an identity and request handling for AT Protocol
|
|
25
|
+
* operations. It can represent an authenticated user session or an
|
|
26
|
+
* unauthenticated service client.
|
|
27
|
+
*
|
|
28
|
+
* @see {@link buildAgent} for creating (simple) Agent instances.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* const agent: Agent = {
|
|
33
|
+
* did: 'did:plc:example123',
|
|
34
|
+
* fetchHandler: (path, init) => fetch(new URL(path, 'https://bsky.social'), init)
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
9
38
|
export interface Agent {
|
|
39
|
+
/** The DID of the authenticated user, or `undefined` if unauthenticated. */
|
|
10
40
|
readonly did?: DidString;
|
|
41
|
+
/** The fetch handler used to make HTTP requests. */
|
|
11
42
|
fetchHandler: FetchHandler;
|
|
12
43
|
}
|
|
13
44
|
export type AgentConfig = {
|
|
@@ -34,6 +65,42 @@ export type AgentConfig = {
|
|
|
34
65
|
*/
|
|
35
66
|
fetch?: typeof globalThis.fetch;
|
|
36
67
|
};
|
|
68
|
+
/**
|
|
69
|
+
* Options for creating an Agent.
|
|
70
|
+
*
|
|
71
|
+
* Can be a full {@link AgentConfig} object, or a simple service URL string/{@link URL}.
|
|
72
|
+
*/
|
|
37
73
|
export type AgentOptions = AgentConfig | string | URL;
|
|
74
|
+
/**
|
|
75
|
+
* Creates an {@link Agent} from various input types.
|
|
76
|
+
*
|
|
77
|
+
* This factory function accepts an existing Agent (returned as-is), a service URL,
|
|
78
|
+
* or a full configuration object. It handles the common case of creating an
|
|
79
|
+
* unauthenticated agent from just a service URL.
|
|
80
|
+
*
|
|
81
|
+
* @param options - Agent instance, configuration object, or service URL
|
|
82
|
+
* @returns A configured Agent ready for making requests
|
|
83
|
+
* @throws {TypeError} If fetch() is not available in the environment
|
|
84
|
+
*
|
|
85
|
+
* @example From URL string
|
|
86
|
+
* ```typescript
|
|
87
|
+
* const agent = buildAgent('https://public.api.bsky.app')
|
|
88
|
+
* ```
|
|
89
|
+
*
|
|
90
|
+
* @example From configuration
|
|
91
|
+
* ```typescript
|
|
92
|
+
* const agent = buildAgent({
|
|
93
|
+
* did: 'did:plc:example',
|
|
94
|
+
* service: 'https://bsky.social',
|
|
95
|
+
* headers: { 'Authorization': 'Bearer ...' }
|
|
96
|
+
* })
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* @example Pass-through existing agent
|
|
100
|
+
* ```typescript
|
|
101
|
+
* const existing: Agent = { ... }
|
|
102
|
+
* const agent = buildAgent(existing) // Returns existing unchanged
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
38
105
|
export declare function buildAgent(options: Agent | AgentOptions): Agent;
|
|
39
106
|
//# sourceMappingURL=agent.d.ts.map
|
package/dist/agent.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AAE/C,MAAM,MAAM,YAAY,GAAG;AACzB;;;;GAIG;AACH,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,WAAW,KACd,OAAO,CAAC,QAAQ,CAAC,CAAA;AAEtB,MAAM,WAAW,KAAK;IACpB,QAAQ,CAAC,GAAG,CAAC,EAAE,SAAS,CAAA;IACxB,YAAY,EAAE,YAAY,CAAA;CAC3B;AAED,MAAM,MAAM,WAAW,GAAG;IACxB;;OAEG;IACH,GAAG,CAAC,EAAE,SAAS,CAAA;IAEf;;;;OAIG;IACH,OAAO,EAAE,MAAM,GAAG,GAAG,CAAA;IAErB;;;OAGG;IACH,OAAO,CAAC,EAAE,WAAW,CAAA;IAErB;;;;;OAKG;IACH,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAA;CAChC,CAAA;AAED,MAAM,MAAM,YAAY,GAAG,WAAW,GAAG,MAAM,GAAG,GAAG,CAAA;AAErD,wBAAgB,UAAU,CAAC,OAAO,EAAE,KAAK,GAAG,YAAY,GAAG,KAAK,CAiC/D"}
|
|
1
|
+
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AAE/C;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,YAAY,GAAG;AACzB;;;;GAIG;AACH,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,WAAW,KACd,OAAO,CAAC,QAAQ,CAAC,CAAA;AAEtB;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,WAAW,KAAK;IACpB,4EAA4E;IAC5E,QAAQ,CAAC,GAAG,CAAC,EAAE,SAAS,CAAA;IACxB,oDAAoD;IACpD,YAAY,EAAE,YAAY,CAAA;CAC3B;AAED,MAAM,MAAM,WAAW,GAAG;IACxB;;OAEG;IACH,GAAG,CAAC,EAAE,SAAS,CAAA;IAEf;;;;OAIG;IACH,OAAO,EAAE,MAAM,GAAG,GAAG,CAAA;IAErB;;;OAGG;IACH,OAAO,CAAC,EAAE,WAAW,CAAA;IAErB;;;;;OAKG;IACH,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAA;CAChC,CAAA;AAED;;;;GAIG;AACH,MAAM,MAAM,YAAY,GAAG,WAAW,GAAG,MAAM,GAAG,GAAG,CAAA;AAErD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,KAAK,GAAG,YAAY,GAAG,KAAK,CAiC/D"}
|
package/dist/agent.js
CHANGED
|
@@ -1,6 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.buildAgent = buildAgent;
|
|
4
|
+
/**
|
|
5
|
+
* Creates an {@link Agent} from various input types.
|
|
6
|
+
*
|
|
7
|
+
* This factory function accepts an existing Agent (returned as-is), a service URL,
|
|
8
|
+
* or a full configuration object. It handles the common case of creating an
|
|
9
|
+
* unauthenticated agent from just a service URL.
|
|
10
|
+
*
|
|
11
|
+
* @param options - Agent instance, configuration object, or service URL
|
|
12
|
+
* @returns A configured Agent ready for making requests
|
|
13
|
+
* @throws {TypeError} If fetch() is not available in the environment
|
|
14
|
+
*
|
|
15
|
+
* @example From URL string
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const agent = buildAgent('https://public.api.bsky.app')
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @example From configuration
|
|
21
|
+
* ```typescript
|
|
22
|
+
* const agent = buildAgent({
|
|
23
|
+
* did: 'did:plc:example',
|
|
24
|
+
* service: 'https://bsky.social',
|
|
25
|
+
* headers: { 'Authorization': 'Bearer ...' }
|
|
26
|
+
* })
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @example Pass-through existing agent
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const existing: Agent = { ... }
|
|
32
|
+
* const agent = buildAgent(existing) // Returns existing unchanged
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
4
35
|
function buildAgent(options) {
|
|
5
36
|
if (typeof options === 'object' && 'fetchHandler' in options) {
|
|
6
37
|
return options;
|
package/dist/agent.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":";;AAkHA,gCAiCC;AAhED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,SAAgB,UAAU,CAAC,OAA6B;IACtD,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,cAAc,IAAI,OAAO,EAAE,CAAC;QAC7D,OAAO,OAAO,CAAA;IAChB,CAAC;IAED,MAAM,MAAM,GACV,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,YAAY,GAAG;QACnD,CAAC,CAAC,EAAE,GAAG,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAE;QACtC,CAAC,CAAC,OAAO,CAAA;IAEb,MAAM,EAAE,OAAO,EAAE,KAAK,GAAG,UAAU,CAAC,KAAK,EAAE,GAAG,MAAM,CAAA;IAEpD,IAAI,OAAO,KAAK,KAAK,UAAU,EAAE,CAAC;QAChC,MAAM,IAAI,SAAS,CAAC,8CAA8C,CAAC,CAAA;IACrE,CAAC;IAED,OAAO;QACL,IAAI,GAAG;YACL,OAAO,MAAM,CAAC,GAAG,CAAA;QACnB,CAAC;QAED,KAAK,CAAC,YAAY,CAAC,IAAI,EAAE,IAAI;YAC3B,MAAM,OAAO,GACX,MAAM,CAAC,OAAO,IAAI,IAAI,IAAI,IAAI,CAAC,OAAO,IAAI,IAAI;gBAC5C,CAAC,CAAC,YAAY,CAAC,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,OAAO,CAAC;gBAC5C,CAAC,CAAC,MAAM,CAAC,OAAO,IAAI,IAAI,CAAC,OAAO,CAAA;YAEpC,OAAO,KAAK,CACV,IAAI,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,EACtB,OAAO,KAAK,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,IAAI,CACvD,CAAA;QACH,CAAC;KACF,CAAA;AACH,CAAC;AAED,SAAS,YAAY,CACnB,cAA2B,EAC3B,cAA2B;IAE3B,8EAA8E;IAC9E,MAAM,MAAM,GAAG,IAAI,OAAO,CAAC,cAAc,CAAC,CAAA;IAE1C,MAAM,SAAS,GACb,cAAc,YAAY,OAAO;QAC/B,CAAC,CAAC,cAAc;QAChB,CAAC,CAAC,IAAI,OAAO,CAAC,cAAc,CAAC,CAAA;IAEjC,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,SAAS,CAAC,OAAO,EAAE,EAAE,CAAC;QAC/C,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;IACxB,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC","sourcesContent":["import { DidString } from '@atproto/lex-schema'\n\n/**\n * A function that performs HTTP requests towards a service endpoint.\n *\n * The handler is responsible for adding the origin (protocol, hostname, and\n * port) to the provided path, typically based on authentication or service\n * configuration. The handler are also responsible for adding any necessary\n * headers, such as authorization tokens.\n *\n * @param path - The URL path (pathname + query parameters) without the origin\n * @param init - Standard fetch RequestInit options\n * @returns A Promise resolving to the HTTP Response\n */\nexport type FetchHandler = (\n /**\n * The URL (pathname + query parameters) to make the request to, without the\n * origin. The origin (protocol, hostname, and port) must be added by this\n * {@link FetchHandler}, typically based on authentication or other factors.\n */\n path: string,\n init: RequestInit,\n) => Promise<Response>\n\n/**\n * Core interface for making XRPC requests.\n *\n * An Agent encapsulates an identity and request handling for AT Protocol\n * operations. It can represent an authenticated user session or an\n * unauthenticated service client.\n *\n * @see {@link buildAgent} for creating (simple) Agent instances.\n *\n * @example\n * ```typescript\n * const agent: Agent = {\n * did: 'did:plc:example123',\n * fetchHandler: (path, init) => fetch(new URL(path, 'https://bsky.social'), init)\n * }\n * ```\n */\nexport interface Agent {\n /** The DID of the authenticated user, or `undefined` if unauthenticated. */\n readonly did?: DidString\n /** The fetch handler used to make HTTP requests. */\n fetchHandler: FetchHandler\n}\n\nexport type AgentConfig = {\n /**\n * The identifier (DID) of the user represented by this agent.\n */\n did?: DidString\n\n /**\n * The service URL to make requests to. This can be a string, URL, or a\n * function that returns a string or URL. This is useful for dynamic URLs,\n * such as a service URL that changes based on authentication.\n */\n service: string | URL\n\n /**\n * Optional headers to include with every request made by this agent, unless\n * overridden by the request-specific headers provided to the fetch handler.\n */\n headers?: HeadersInit\n\n /**\n * Bring your own fetch implementation. Typically useful for testing, logging,\n * mocking, or adding retries, session management, signatures, proof of\n * possession (DPoP), SSRF protection, etc. Defaults to the global `fetch`\n * function.\n */\n fetch?: typeof globalThis.fetch\n}\n\n/**\n * Options for creating an Agent.\n *\n * Can be a full {@link AgentConfig} object, or a simple service URL string/{@link URL}.\n */\nexport type AgentOptions = AgentConfig | string | URL\n\n/**\n * Creates an {@link Agent} from various input types.\n *\n * This factory function accepts an existing Agent (returned as-is), a service URL,\n * or a full configuration object. It handles the common case of creating an\n * unauthenticated agent from just a service URL.\n *\n * @param options - Agent instance, configuration object, or service URL\n * @returns A configured Agent ready for making requests\n * @throws {TypeError} If fetch() is not available in the environment\n *\n * @example From URL string\n * ```typescript\n * const agent = buildAgent('https://public.api.bsky.app')\n * ```\n *\n * @example From configuration\n * ```typescript\n * const agent = buildAgent({\n * did: 'did:plc:example',\n * service: 'https://bsky.social',\n * headers: { 'Authorization': 'Bearer ...' }\n * })\n * ```\n *\n * @example Pass-through existing agent\n * ```typescript\n * const existing: Agent = { ... }\n * const agent = buildAgent(existing) // Returns existing unchanged\n * ```\n */\nexport function buildAgent(options: Agent | AgentOptions): Agent {\n if (typeof options === 'object' && 'fetchHandler' in options) {\n return options\n }\n\n const config: Agent | AgentConfig =\n typeof options === 'string' || options instanceof URL\n ? { did: undefined, service: options }\n : options\n\n const { service, fetch = globalThis.fetch } = config\n\n if (typeof fetch !== 'function') {\n throw new TypeError('fetch() is not available in this environment')\n }\n\n return {\n get did() {\n return config.did\n },\n\n async fetchHandler(path, init) {\n const headers =\n config.headers != null && init.headers != null\n ? mergeHeaders(config.headers, init.headers)\n : config.headers || init.headers\n\n return fetch(\n new URL(path, service),\n headers !== init.headers ? { ...init, headers } : init,\n )\n },\n }\n}\n\nfunction mergeHeaders(\n defaultHeaders: HeadersInit,\n requestHeaders: HeadersInit,\n): Headers {\n // We don't want to alter the original Headers objects, so we create a new one\n const result = new Headers(defaultHeaders)\n\n const overrides =\n requestHeaders instanceof Headers\n ? requestHeaders\n : new Headers(requestHeaders)\n\n for (const [key, value] of overrides.entries()) {\n result.set(key, value)\n }\n\n return result\n}\n"]}
|