@atproto/lex-client 0.0.0-spaces-alpha-20260818021221
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 +456 -0
- package/LICENSE.txt +7 -0
- package/README.md +19 -0
- package/dist/agent.d.ts +127 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +41 -0
- package/dist/agent.js.map +1 -0
- package/dist/client.d.ts +685 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +511 -0
- package/dist/client.js.map +1 -0
- package/dist/errors.d.ts +277 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +388 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/lexicons/com/atproto/repo/applyWrites.d.ts +3 -0
- package/dist/lexicons/com/atproto/repo/applyWrites.d.ts.map +1 -0
- package/dist/lexicons/com/atproto/repo/applyWrites.defs.d.ts +103 -0
- package/dist/lexicons/com/atproto/repo/applyWrites.defs.d.ts.map +1 -0
- package/dist/lexicons/com/atproto/repo/applyWrites.defs.js +82 -0
- package/dist/lexicons/com/atproto/repo/applyWrites.defs.js.map +1 -0
- package/dist/lexicons/com/atproto/repo/applyWrites.js +6 -0
- package/dist/lexicons/com/atproto/repo/applyWrites.js.map +1 -0
- package/dist/lexicons/com/atproto/repo/createRecord.d.ts +3 -0
- package/dist/lexicons/com/atproto/repo/createRecord.d.ts.map +1 -0
- package/dist/lexicons/com/atproto/repo/createRecord.defs.d.ts +74 -0
- package/dist/lexicons/com/atproto/repo/createRecord.defs.d.ts.map +1 -0
- package/dist/lexicons/com/atproto/repo/createRecord.defs.js +34 -0
- package/dist/lexicons/com/atproto/repo/createRecord.defs.js.map +1 -0
- package/dist/lexicons/com/atproto/repo/createRecord.js +6 -0
- package/dist/lexicons/com/atproto/repo/createRecord.js.map +1 -0
- package/dist/lexicons/com/atproto/repo/defs.d.ts +2 -0
- package/dist/lexicons/com/atproto/repo/defs.d.ts.map +1 -0
- package/dist/lexicons/com/atproto/repo/defs.defs.d.ts +13 -0
- package/dist/lexicons/com/atproto/repo/defs.defs.d.ts.map +1 -0
- package/dist/lexicons/com/atproto/repo/defs.defs.js +13 -0
- package/dist/lexicons/com/atproto/repo/defs.defs.js.map +1 -0
- package/dist/lexicons/com/atproto/repo/defs.js +5 -0
- package/dist/lexicons/com/atproto/repo/defs.js.map +1 -0
- package/dist/lexicons/com/atproto/repo/deleteRecord.d.ts +3 -0
- package/dist/lexicons/com/atproto/repo/deleteRecord.d.ts.map +1 -0
- package/dist/lexicons/com/atproto/repo/deleteRecord.defs.d.ts +56 -0
- package/dist/lexicons/com/atproto/repo/deleteRecord.defs.d.ts.map +1 -0
- package/dist/lexicons/com/atproto/repo/deleteRecord.defs.js +29 -0
- package/dist/lexicons/com/atproto/repo/deleteRecord.defs.js.map +1 -0
- package/dist/lexicons/com/atproto/repo/deleteRecord.js +6 -0
- package/dist/lexicons/com/atproto/repo/deleteRecord.js.map +1 -0
- package/dist/lexicons/com/atproto/repo/getRecord.d.ts +3 -0
- package/dist/lexicons/com/atproto/repo/getRecord.d.ts.map +1 -0
- package/dist/lexicons/com/atproto/repo/getRecord.defs.d.ts +58 -0
- package/dist/lexicons/com/atproto/repo/getRecord.defs.d.ts.map +1 -0
- package/dist/lexicons/com/atproto/repo/getRecord.defs.js +23 -0
- package/dist/lexicons/com/atproto/repo/getRecord.defs.js.map +1 -0
- package/dist/lexicons/com/atproto/repo/getRecord.js +6 -0
- package/dist/lexicons/com/atproto/repo/getRecord.js.map +1 -0
- package/dist/lexicons/com/atproto/repo/listRecords.d.ts +3 -0
- package/dist/lexicons/com/atproto/repo/listRecords.d.ts.map +1 -0
- package/dist/lexicons/com/atproto/repo/listRecords.defs.d.ts +51 -0
- package/dist/lexicons/com/atproto/repo/listRecords.defs.d.ts.map +1 -0
- package/dist/lexicons/com/atproto/repo/listRecords.defs.js +33 -0
- package/dist/lexicons/com/atproto/repo/listRecords.defs.js.map +1 -0
- package/dist/lexicons/com/atproto/repo/listRecords.js +6 -0
- package/dist/lexicons/com/atproto/repo/listRecords.js.map +1 -0
- package/dist/lexicons/com/atproto/repo/putRecord.d.ts +3 -0
- package/dist/lexicons/com/atproto/repo/putRecord.d.ts.map +1 -0
- package/dist/lexicons/com/atproto/repo/putRecord.defs.d.ts +80 -0
- package/dist/lexicons/com/atproto/repo/putRecord.defs.d.ts.map +1 -0
- package/dist/lexicons/com/atproto/repo/putRecord.defs.js +35 -0
- package/dist/lexicons/com/atproto/repo/putRecord.defs.js.map +1 -0
- package/dist/lexicons/com/atproto/repo/putRecord.js +6 -0
- package/dist/lexicons/com/atproto/repo/putRecord.js.map +1 -0
- package/dist/lexicons/com/atproto/repo/uploadBlob.d.ts +3 -0
- package/dist/lexicons/com/atproto/repo/uploadBlob.d.ts.map +1 -0
- package/dist/lexicons/com/atproto/repo/uploadBlob.defs.d.ts +23 -0
- package/dist/lexicons/com/atproto/repo/uploadBlob.defs.d.ts.map +1 -0
- package/dist/lexicons/com/atproto/repo/uploadBlob.defs.js +17 -0
- package/dist/lexicons/com/atproto/repo/uploadBlob.defs.js.map +1 -0
- package/dist/lexicons/com/atproto/repo/uploadBlob.js +6 -0
- package/dist/lexicons/com/atproto/repo/uploadBlob.js.map +1 -0
- package/dist/lexicons/com/atproto/repo.d.ts +9 -0
- package/dist/lexicons/com/atproto/repo.d.ts.map +1 -0
- package/dist/lexicons/com/atproto/repo.js +12 -0
- package/dist/lexicons/com/atproto/repo.js.map +1 -0
- package/dist/lexicons/com/atproto/sync/getBlob.d.ts +3 -0
- package/dist/lexicons/com/atproto/sync/getBlob.d.ts.map +1 -0
- package/dist/lexicons/com/atproto/sync/getBlob.defs.d.ts +30 -0
- package/dist/lexicons/com/atproto/sync/getBlob.defs.d.ts.map +1 -0
- package/dist/lexicons/com/atproto/sync/getBlob.defs.js +23 -0
- package/dist/lexicons/com/atproto/sync/getBlob.defs.js.map +1 -0
- package/dist/lexicons/com/atproto/sync/getBlob.js +6 -0
- package/dist/lexicons/com/atproto/sync/getBlob.js.map +1 -0
- package/dist/lexicons/com/atproto/sync.d.ts +2 -0
- package/dist/lexicons/com/atproto/sync.d.ts.map +1 -0
- package/dist/lexicons/com/atproto/sync.js +5 -0
- package/dist/lexicons/com/atproto/sync.js.map +1 -0
- package/dist/lexicons/com/atproto.d.ts +3 -0
- package/dist/lexicons/com/atproto.d.ts.map +1 -0
- package/dist/lexicons/com/atproto.js +6 -0
- package/dist/lexicons/com/atproto.js.map +1 -0
- package/dist/lexicons/com.d.ts +2 -0
- package/dist/lexicons/com.d.ts.map +1 -0
- package/dist/lexicons/com.js +5 -0
- package/dist/lexicons/com.js.map +1 -0
- package/dist/lexicons/index.d.ts +2 -0
- package/dist/lexicons/index.d.ts.map +1 -0
- package/dist/lexicons/index.js +5 -0
- package/dist/lexicons/index.js.map +1 -0
- package/dist/response.d.ts +124 -0
- package/dist/response.d.ts.map +1 -0
- package/dist/response.js +178 -0
- package/dist/response.js.map +1 -0
- package/dist/types.d.ts +54 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +16 -0
- package/dist/types.js.map +1 -0
- package/dist/util.d.ts +85 -0
- package/dist/util.d.ts.map +1 -0
- package/dist/util.js +194 -0
- package/dist/util.js.map +1 -0
- package/dist/write-operation-builder.d.ts +19 -0
- package/dist/write-operation-builder.d.ts.map +1 -0
- package/dist/write-operation-builder.js +35 -0
- package/dist/write-operation-builder.js.map +1 -0
- package/dist/www-authenticate.d.ts +35 -0
- package/dist/www-authenticate.d.ts.map +1 -0
- package/dist/www-authenticate.js +54 -0
- package/dist/www-authenticate.js.map +1 -0
- package/dist/xrpc.d.ts +194 -0
- package/dist/xrpc.d.ts.map +1 -0
- package/dist/xrpc.js +235 -0
- package/dist/xrpc.js.map +1 -0
- package/package.json +51 -0
package/dist/agent.d.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type { 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 can also be 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
|
+
*/
|
|
14
|
+
export type FetchHandler = (
|
|
15
|
+
/**
|
|
16
|
+
* The URL (pathname + query parameters) to make the request to, without the
|
|
17
|
+
* origin. The origin (protocol, hostname, and port) must be added by this
|
|
18
|
+
* {@link FetchHandler}, typically based on authentication or other factors.
|
|
19
|
+
*/
|
|
20
|
+
path: `/${string}`, init: RequestInit) => Promise<Response>;
|
|
21
|
+
/**
|
|
22
|
+
* Core interface for making XRPC requests towards a specific service.
|
|
23
|
+
*
|
|
24
|
+
* An {@link Agent} encapsulates an identity and request handling for AT
|
|
25
|
+
* Protocol operations. Agents will typically handle authentication, service URL
|
|
26
|
+
* resolution, and other request configuration, allowing client code to make
|
|
27
|
+
* requests without needing to manage these details directly. The key component
|
|
28
|
+
* of an Agent is the {@link FetchHandler}, which is responsible for
|
|
29
|
+
* constructing the full request URL and adding any necessary headers or
|
|
30
|
+
* authentication tokens. The Agent's `did` property represents the
|
|
31
|
+
* authenticated user's DID, if available, and can be used for operations that
|
|
32
|
+
* require knowledge of the user's identity (such as creating AT Protocol
|
|
33
|
+
* records).
|
|
34
|
+
*
|
|
35
|
+
* @see {@link buildAgent} for creating (simple) Agent instances.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* const agent: Agent = {
|
|
40
|
+
* did: 'did:plc:example123',
|
|
41
|
+
* fetchHandler: async (path, init) => {
|
|
42
|
+
* const url = new URL(path, 'https://bsky.social')
|
|
43
|
+
* return fetch(url, init)
|
|
44
|
+
* }
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export interface Agent {
|
|
49
|
+
/** The DID of the authenticated user, or `undefined` if unauthenticated. */
|
|
50
|
+
readonly did?: DidString;
|
|
51
|
+
/** The fetch handler used to make HTTP requests. */
|
|
52
|
+
fetchHandler: FetchHandler;
|
|
53
|
+
}
|
|
54
|
+
export declare function isAgent(value: unknown): value is Agent;
|
|
55
|
+
export type AgentConfig = {
|
|
56
|
+
/**
|
|
57
|
+
* The identifier (DID) of the user represented by this agent.
|
|
58
|
+
*/
|
|
59
|
+
did?: DidString;
|
|
60
|
+
/**
|
|
61
|
+
* The service URL to make requests to. This can be a string, URL, or a
|
|
62
|
+
* function that returns a string or URL. This is useful for dynamic URLs,
|
|
63
|
+
* such as a service URL that changes based on authentication.
|
|
64
|
+
*/
|
|
65
|
+
service: string | URL;
|
|
66
|
+
/**
|
|
67
|
+
* Optional headers to include with every request made by this agent, unless
|
|
68
|
+
* overridden by the request-specific headers provided to the fetch handler.
|
|
69
|
+
*/
|
|
70
|
+
headers?: HeadersInit;
|
|
71
|
+
/**
|
|
72
|
+
* Bring your own fetch implementation. Typically useful for testing, logging,
|
|
73
|
+
* mocking, or adding retries, session management, signatures, proof of
|
|
74
|
+
* possession (DPoP), SSRF protection, etc. Defaults to the global `fetch`
|
|
75
|
+
* function.
|
|
76
|
+
*/
|
|
77
|
+
fetch?: typeof globalThis.fetch;
|
|
78
|
+
fetchHandler?: never;
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Options for creating an Agent.
|
|
82
|
+
*
|
|
83
|
+
* Can be a full {@link AgentConfig} object, or a simple service URL string/{@link URL}.
|
|
84
|
+
*/
|
|
85
|
+
export type AgentOptions = AgentConfig | FetchHandler | string | URL;
|
|
86
|
+
/**
|
|
87
|
+
* Creates an {@link Agent} from various input types.
|
|
88
|
+
*
|
|
89
|
+
* This factory function accepts an existing Agent (returned as-is), a service
|
|
90
|
+
* URL, or a full configuration object. It handles the common case of creating
|
|
91
|
+
* an unauthenticated agent from just a service URL.
|
|
92
|
+
*
|
|
93
|
+
* @param options - Agent instance, configuration object, or service URL
|
|
94
|
+
* @returns A configured Agent ready for making requests
|
|
95
|
+
* @throws {TypeError} If fetch() is not available in the environment
|
|
96
|
+
*
|
|
97
|
+
* @example // From URL string
|
|
98
|
+
* ```typescript
|
|
99
|
+
* const agent = buildAgent('https://public.api.bsky.app')
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
102
|
+
* @example // From configuration
|
|
103
|
+
* ```typescript
|
|
104
|
+
* const agent = buildAgent({
|
|
105
|
+
* did: 'did:plc:example',
|
|
106
|
+
* service: 'https://bsky.social',
|
|
107
|
+
* headers: { 'Authorization': 'Bearer ...' }
|
|
108
|
+
* })
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* @example // From existing Agent
|
|
112
|
+
* ```typescript
|
|
113
|
+
* const existingAgent: Agent = { ... }
|
|
114
|
+
* const agent = buildAgent(existingAgent)
|
|
115
|
+
* ```
|
|
116
|
+
*
|
|
117
|
+
* @example // From agent like object with fetchHandler
|
|
118
|
+
* ```typescript
|
|
119
|
+
* const agent = buildAgent({
|
|
120
|
+
* did: 'did:plc:example',
|
|
121
|
+
* fetchHandler: async (path, init) => { ... }
|
|
122
|
+
* // @NOTE "headers" and "service" are not allowed when providing a fetchHandler, because the input will returned "as is" if it matched the "Agent" interface.
|
|
123
|
+
* })
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
export declare function buildAgent<O extends Agent | AgentOptions>(options: O): O extends Agent ? O : Agent;
|
|
127
|
+
//# sourceMappingURL=agent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AAGpD;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,YAAY,GAAG;AACzB;;;;GAIG;AACH,IAAI,EAAE,IAAI,MAAM,EAAE,EAClB,IAAI,EAAE,WAAW,KACd,OAAO,CAAC,QAAQ,CAAC,CAAA;AAEtB;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,WAAW,KAAK;IACpB,4EAA4E;IAC5E,QAAQ,CAAC,GAAG,CAAC,EAAE,SAAS,CAAA;IACxB,oDAAoD;IACpD,YAAY,EAAE,YAAY,CAAA;CAC3B;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,KAAK,CAUtD;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;IAK/B,YAAY,CAAC,EAAE,KAAK,CAAA;CACrB,CAAA;AAED;;;;GAIG;AACH,MAAM,MAAM,YAAY,GAAG,WAAW,GAAG,YAAY,GAAG,MAAM,GAAG,GAAG,CAAA;AAEpE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAS,KAAK,GAAG,YAAY,EACvD,OAAO,EAAE,CAAC,GACT,CAAC,SAAS,KAAK,GAAG,CAAC,GAAG,KAAK,CAAA"}
|
package/dist/agent.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { mergeHeaders } from './util.js';
|
|
2
|
+
export function isAgent(value) {
|
|
3
|
+
return (typeof value === 'object' &&
|
|
4
|
+
value !== null &&
|
|
5
|
+
'fetchHandler' in value &&
|
|
6
|
+
typeof value.fetchHandler === 'function' &&
|
|
7
|
+
(!('did' in value) ||
|
|
8
|
+
value.did === undefined ||
|
|
9
|
+
typeof value.did === 'string'));
|
|
10
|
+
}
|
|
11
|
+
export function buildAgent(options) {
|
|
12
|
+
if (typeof options === 'function') {
|
|
13
|
+
return { did: undefined, fetchHandler: options };
|
|
14
|
+
}
|
|
15
|
+
const config = typeof options === 'string' || options instanceof URL
|
|
16
|
+
? { did: undefined, service: options }
|
|
17
|
+
: options;
|
|
18
|
+
// Prevent mismatch between `fetchHandler` and `headers` in the config, since
|
|
19
|
+
// `headers` would be ignored if `fetchHandler` is provided.
|
|
20
|
+
if ('fetchHandler' in config) {
|
|
21
|
+
if (isAgent(config))
|
|
22
|
+
return config;
|
|
23
|
+
throw new TypeError('fetchHandler must not be provided when using AgentConfig');
|
|
24
|
+
}
|
|
25
|
+
const { did, service, fetch = globalThis.fetch, headers: defaultHeaders, } = config;
|
|
26
|
+
if (typeof fetch !== 'function') {
|
|
27
|
+
throw new TypeError('fetch() is not available in this environment');
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
get did() {
|
|
31
|
+
return did;
|
|
32
|
+
},
|
|
33
|
+
async fetchHandler(path, init) {
|
|
34
|
+
const headers = defaultHeaders != null && init.headers != null
|
|
35
|
+
? mergeHeaders(defaultHeaders, init.headers)
|
|
36
|
+
: defaultHeaders || init.headers;
|
|
37
|
+
return fetch(new URL(path, service), headers !== init.headers ? { ...init, headers } : init);
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=agent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AA0DxC,MAAM,UAAU,OAAO,CAAC,KAAc;IACpC,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,cAAc,IAAI,KAAK;QACvB,OAAO,KAAK,CAAC,YAAY,KAAK,UAAU;QACxC,CAAC,CAAC,CAAC,KAAK,IAAI,KAAK,CAAC;YAChB,KAAK,CAAC,GAAG,KAAK,SAAS;YACvB,OAAO,KAAK,CAAC,GAAG,KAAK,QAAQ,CAAC,CACjC,CAAA;AACH,CAAC;AAqFD,MAAM,UAAU,UAAU,CAAC,OAA6B;IACtD,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE,CAAC;QAClC,OAAO,EAAE,GAAG,EAAE,SAAS,EAAE,YAAY,EAAE,OAAO,EAAE,CAAA;IAClD,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,6EAA6E;IAC7E,4DAA4D;IAC5D,IAAI,cAAc,IAAI,MAAM,EAAE,CAAC;QAC7B,IAAI,OAAO,CAAC,MAAM,CAAC;YAAE,OAAO,MAAM,CAAA;QAElC,MAAM,IAAI,SAAS,CACjB,0DAA0D,CAC3D,CAAA;IACH,CAAC;IAED,MAAM,EACJ,GAAG,EACH,OAAO,EACP,KAAK,GAAG,UAAU,CAAC,KAAK,EACxB,OAAO,EAAE,cAAc,GACxB,GAAG,MAAM,CAAA;IAEV,IAAI,OAAO,KAAK,KAAK,UAAU,EAAE,CAAC;QAChC,MAAM,IAAI,SAAS,CAAC,8CAA8C,CAAC,CAAA;IACrE,CAAC;IAED,OAAO;QACL,IAAI,GAAG;YACL,OAAO,GAAG,CAAA;QACZ,CAAC;QAED,KAAK,CAAC,YAAY,CAAC,IAAI,EAAE,IAAI;YAC3B,MAAM,OAAO,GACX,cAAc,IAAI,IAAI,IAAI,IAAI,CAAC,OAAO,IAAI,IAAI;gBAC5C,CAAC,CAAC,YAAY,CAAC,cAAc,EAAE,IAAI,CAAC,OAAO,CAAC;gBAC5C,CAAC,CAAC,cAAc,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","sourcesContent":["import type { DidString } from '@atproto/lex-schema'\nimport { mergeHeaders } from './util.js'\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 can also be 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 towards a specific service.\n *\n * An {@link Agent} encapsulates an identity and request handling for AT\n * Protocol operations. Agents will typically handle authentication, service URL\n * resolution, and other request configuration, allowing client code to make\n * requests without needing to manage these details directly. The key component\n * of an Agent is the {@link FetchHandler}, which is responsible for\n * constructing the full request URL and adding any necessary headers or\n * authentication tokens. The Agent's `did` property represents the\n * authenticated user's DID, if available, and can be used for operations that\n * require knowledge of the user's identity (such as creating AT Protocol\n * records).\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: async (path, init) => {\n * const url = new URL(path, 'https://bsky.social')\n * return fetch(url, init)\n * }\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 function isAgent(value: unknown): value is Agent {\n return (\n typeof value === 'object' &&\n value !== null &&\n 'fetchHandler' in value &&\n typeof value.fetchHandler === 'function' &&\n (!('did' in value) ||\n value.did === undefined ||\n typeof value.did === 'string')\n )\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 // Because this type get unioned with `Agent`, we need to prevent TypeScript\n // from accepting `headers` when `fetchHandler` is provided, since `headers`\n // would not be used (while the user might expect it to be).\n fetchHandler?: never\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 | FetchHandler | 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\n * URL, or a full configuration object. It handles the common case of creating\n * an 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 // From existing Agent\n * ```typescript\n * const existingAgent: Agent = { ... }\n * const agent = buildAgent(existingAgent)\n * ```\n *\n * @example // From agent like object with fetchHandler\n * ```typescript\n * const agent = buildAgent({\n * did: 'did:plc:example',\n * fetchHandler: async (path, init) => { ... }\n * // @NOTE \"headers\" and \"service\" are not allowed when providing a fetchHandler, because the input will returned \"as is\" if it matched the \"Agent\" interface.\n * })\n * ```\n */\nexport function buildAgent<O extends Agent | AgentOptions>(\n options: O,\n): O extends Agent ? O : Agent\nexport function buildAgent(options: Agent | AgentOptions): Agent {\n if (typeof options === 'function') {\n return { did: undefined, fetchHandler: options }\n }\n\n const config: Agent | AgentConfig =\n typeof options === 'string' || options instanceof URL\n ? { did: undefined, service: options }\n : options\n\n // Prevent mismatch between `fetchHandler` and `headers` in the config, since\n // `headers` would be ignored if `fetchHandler` is provided.\n if ('fetchHandler' in config) {\n if (isAgent(config)) return config\n\n throw new TypeError(\n 'fetchHandler must not be provided when using AgentConfig',\n )\n }\n\n const {\n did,\n service,\n fetch = globalThis.fetch,\n headers: defaultHeaders,\n } = 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 did\n },\n\n async fetchHandler(path, init) {\n const headers =\n defaultHeaders != null && init.headers != null\n ? mergeHeaders(defaultHeaders, init.headers)\n : defaultHeaders || init.headers\n\n return fetch(\n new URL(path, service),\n headers !== init.headers ? { ...init, headers } : init,\n )\n },\n }\n}\n"]}
|