@atproto/lex-client 0.1.5 → 0.2.1
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 +25 -0
- package/dist/agent.d.ts +1 -1
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js.map +1 -1
- package/dist/client.d.ts +42 -21
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +97 -16
- package/dist/client.js.map +1 -1
- package/dist/errors.d.ts +6 -4
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +3 -3
- package/dist/errors.js.map +1 -1
- package/dist/response.d.ts +3 -2
- package/dist/response.d.ts.map +1 -1
- package/dist/response.js +2 -1
- package/dist/response.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/util.d.ts +6 -2
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +25 -0
- package/dist/util.js.map +1 -1
- package/dist/write-operation-builder.d.ts +3 -2
- package/dist/write-operation-builder.d.ts.map +1 -1
- package/dist/write-operation-builder.js +2 -2
- package/dist/write-operation-builder.js.map +1 -1
- package/dist/xrpc.d.ts +8 -6
- package/dist/xrpc.d.ts.map +1 -1
- package/dist/xrpc.js +1 -1
- package/dist/xrpc.js.map +1 -1
- package/package.json +11 -14
- package/src/agent.test.ts +0 -216
- package/src/agent.ts +0 -186
- package/src/client.ts +0 -1086
- package/src/errors.test.ts +0 -626
- package/src/errors.ts +0 -570
- package/src/index.ts +0 -6
- package/src/lexicons/com/atproto/repo/applyWrites.defs.ts +0 -201
- package/src/lexicons/com/atproto/repo/applyWrites.ts +0 -6
- package/src/lexicons/com/atproto/repo/createRecord.defs.ts +0 -58
- package/src/lexicons/com/atproto/repo/createRecord.ts +0 -6
- package/src/lexicons/com/atproto/repo/defs.defs.ts +0 -28
- package/src/lexicons/com/atproto/repo/defs.ts +0 -5
- package/src/lexicons/com/atproto/repo/deleteRecord.defs.ts +0 -52
- package/src/lexicons/com/atproto/repo/deleteRecord.ts +0 -6
- package/src/lexicons/com/atproto/repo/getRecord.defs.ts +0 -37
- package/src/lexicons/com/atproto/repo/getRecord.ts +0 -6
- package/src/lexicons/com/atproto/repo/listRecords.defs.ts +0 -65
- package/src/lexicons/com/atproto/repo/listRecords.ts +0 -6
- package/src/lexicons/com/atproto/repo/putRecord.defs.ts +0 -59
- package/src/lexicons/com/atproto/repo/putRecord.ts +0 -6
- package/src/lexicons/com/atproto/repo/uploadBlob.defs.ts +0 -35
- package/src/lexicons/com/atproto/repo/uploadBlob.ts +0 -6
- package/src/lexicons/com/atproto/repo.ts +0 -12
- package/src/lexicons/com/atproto/sync/getBlob.defs.ts +0 -37
- package/src/lexicons/com/atproto/sync/getBlob.ts +0 -6
- package/src/lexicons/com/atproto/sync.ts +0 -5
- package/src/lexicons/com/atproto.ts +0 -6
- package/src/lexicons/com.ts +0 -5
- package/src/lexicons/index.ts +0 -5
- package/src/response.bench.ts +0 -113
- package/src/response.ts +0 -366
- package/src/types.ts +0 -71
- package/src/util.test.ts +0 -333
- package/src/util.ts +0 -182
- package/src/write-operation-builder.ts +0 -110
- package/src/www-authenticate.test.ts +0 -227
- package/src/www-authenticate.ts +0 -101
- package/src/xrpc.test.ts +0 -1450
- package/src/xrpc.ts +0 -446
- package/tsconfig.build.json +0 -12
- package/tsconfig.json +0 -7
- package/tsconfig.tests.json +0 -8
package/src/agent.ts
DELETED
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
import { DidString } from '@atproto/lex-schema'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* A function that performs HTTP requests towards a service endpoint.
|
|
5
|
-
*
|
|
6
|
-
* The handler is responsible for adding the origin (protocol, hostname, and
|
|
7
|
-
* port) to the provided path, typically based on authentication or service
|
|
8
|
-
* configuration. The handler can also be responsible for adding any necessary
|
|
9
|
-
* headers, such as authorization tokens.
|
|
10
|
-
*
|
|
11
|
-
* @param path - The URL path (pathname + query parameters) without the origin
|
|
12
|
-
* @param init - Standard fetch RequestInit options
|
|
13
|
-
* @returns A Promise resolving to the HTTP Response
|
|
14
|
-
*/
|
|
15
|
-
export type FetchHandler = (
|
|
16
|
-
/**
|
|
17
|
-
* The URL (pathname + query parameters) to make the request to, without the
|
|
18
|
-
* origin. The origin (protocol, hostname, and port) must be added by this
|
|
19
|
-
* {@link FetchHandler}, typically based on authentication or other factors.
|
|
20
|
-
*/
|
|
21
|
-
path: `/${string}`,
|
|
22
|
-
init: RequestInit,
|
|
23
|
-
) => Promise<Response>
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Core interface for making XRPC requests towards a specific service.
|
|
27
|
-
*
|
|
28
|
-
* An {@link Agent} encapsulates an identity and request handling for AT
|
|
29
|
-
* Protocol operations. Agents will typically handle authentication, service URL
|
|
30
|
-
* resolution, and other request configuration, allowing client code to make
|
|
31
|
-
* requests without needing to manage these details directly. The key component
|
|
32
|
-
* of an Agent is the {@link FetchHandler}, which is responsible for
|
|
33
|
-
* constructing the full request URL and adding any necessary headers or
|
|
34
|
-
* authentication tokens. The Agent's `did` property represents the
|
|
35
|
-
* authenticated user's DID, if available, and can be used for operations that
|
|
36
|
-
* require knowledge of the user's identity (such as creating AT Protocol
|
|
37
|
-
* records).
|
|
38
|
-
*
|
|
39
|
-
* @see {@link buildAgent} for creating (simple) Agent instances.
|
|
40
|
-
*
|
|
41
|
-
* @example
|
|
42
|
-
* ```typescript
|
|
43
|
-
* const agent: Agent = {
|
|
44
|
-
* did: 'did:plc:example123',
|
|
45
|
-
* fetchHandler: async (path, init) => {
|
|
46
|
-
* const url = new URL(path, 'https://bsky.social')
|
|
47
|
-
* return fetch(url, init)
|
|
48
|
-
* }
|
|
49
|
-
* }
|
|
50
|
-
* ```
|
|
51
|
-
*/
|
|
52
|
-
export interface Agent {
|
|
53
|
-
/** The DID of the authenticated user, or `undefined` if unauthenticated. */
|
|
54
|
-
readonly did?: DidString
|
|
55
|
-
/** The fetch handler used to make HTTP requests. */
|
|
56
|
-
fetchHandler: FetchHandler
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function isAgent(value: unknown): value is Agent {
|
|
60
|
-
return (
|
|
61
|
-
typeof value === 'object' &&
|
|
62
|
-
value !== null &&
|
|
63
|
-
'fetchHandler' in value &&
|
|
64
|
-
typeof value.fetchHandler === 'function' &&
|
|
65
|
-
(!('did' in value) ||
|
|
66
|
-
value.did === undefined ||
|
|
67
|
-
typeof value.did === 'string')
|
|
68
|
-
)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export type AgentConfig = {
|
|
72
|
-
/**
|
|
73
|
-
* The identifier (DID) of the user represented by this agent.
|
|
74
|
-
*/
|
|
75
|
-
did?: DidString
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* The service URL to make requests to. This can be a string, URL, or a
|
|
79
|
-
* function that returns a string or URL. This is useful for dynamic URLs,
|
|
80
|
-
* such as a service URL that changes based on authentication.
|
|
81
|
-
*/
|
|
82
|
-
service: string | URL
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Optional headers to include with every request made by this agent, unless
|
|
86
|
-
* overridden by the request-specific headers provided to the fetch handler.
|
|
87
|
-
*/
|
|
88
|
-
headers?: HeadersInit
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Bring your own fetch implementation. Typically useful for testing, logging,
|
|
92
|
-
* mocking, or adding retries, session management, signatures, proof of
|
|
93
|
-
* possession (DPoP), SSRF protection, etc. Defaults to the global `fetch`
|
|
94
|
-
* function.
|
|
95
|
-
*/
|
|
96
|
-
fetch?: typeof globalThis.fetch
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Options for creating an Agent.
|
|
101
|
-
*
|
|
102
|
-
* Can be a full {@link AgentConfig} object, or a simple service URL string/{@link URL}.
|
|
103
|
-
*/
|
|
104
|
-
export type AgentOptions = AgentConfig | FetchHandler | string | URL
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Creates an {@link Agent} from various input types.
|
|
108
|
-
*
|
|
109
|
-
* This factory function accepts an existing Agent (returned as-is), a service
|
|
110
|
-
* URL, or a full configuration object. It handles the common case of creating
|
|
111
|
-
* an unauthenticated agent from just a service URL.
|
|
112
|
-
*
|
|
113
|
-
* @param options - Agent instance, configuration object, or service URL
|
|
114
|
-
* @returns A configured Agent ready for making requests
|
|
115
|
-
* @throws {TypeError} If fetch() is not available in the environment
|
|
116
|
-
*
|
|
117
|
-
* @example // From URL string
|
|
118
|
-
* ```typescript
|
|
119
|
-
* const agent = buildAgent('https://public.api.bsky.app')
|
|
120
|
-
* ```
|
|
121
|
-
*
|
|
122
|
-
* @example // From configuration
|
|
123
|
-
* ```typescript
|
|
124
|
-
* const agent = buildAgent({
|
|
125
|
-
* did: 'did:plc:example',
|
|
126
|
-
* service: 'https://bsky.social',
|
|
127
|
-
* headers: { 'Authorization': 'Bearer ...' }
|
|
128
|
-
* })
|
|
129
|
-
* ```
|
|
130
|
-
*/
|
|
131
|
-
export function buildAgent<O extends Agent | AgentOptions>(
|
|
132
|
-
options: O,
|
|
133
|
-
): O extends Agent ? O : Agent
|
|
134
|
-
export function buildAgent(options: Agent | AgentOptions): Agent {
|
|
135
|
-
const config: Agent | AgentConfig =
|
|
136
|
-
typeof options === 'function'
|
|
137
|
-
? { did: undefined, fetchHandler: options }
|
|
138
|
-
: typeof options === 'string' || options instanceof URL
|
|
139
|
-
? { did: undefined, service: options }
|
|
140
|
-
: options
|
|
141
|
-
|
|
142
|
-
if (isAgent(config)) return config
|
|
143
|
-
|
|
144
|
-
const { service, fetch = globalThis.fetch } = config
|
|
145
|
-
|
|
146
|
-
if (typeof fetch !== 'function') {
|
|
147
|
-
throw new TypeError('fetch() is not available in this environment')
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
return {
|
|
151
|
-
get did() {
|
|
152
|
-
return config.did
|
|
153
|
-
},
|
|
154
|
-
|
|
155
|
-
async fetchHandler(path, init) {
|
|
156
|
-
const headers =
|
|
157
|
-
config.headers != null && init.headers != null
|
|
158
|
-
? mergeHeaders(config.headers, init.headers)
|
|
159
|
-
: config.headers || init.headers
|
|
160
|
-
|
|
161
|
-
return fetch(
|
|
162
|
-
new URL(path, service),
|
|
163
|
-
headers !== init.headers ? { ...init, headers } : init,
|
|
164
|
-
)
|
|
165
|
-
},
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function mergeHeaders(
|
|
170
|
-
defaultHeaders: HeadersInit,
|
|
171
|
-
requestHeaders: HeadersInit,
|
|
172
|
-
): Headers {
|
|
173
|
-
// We don't want to alter the original Headers objects, so we create a new one
|
|
174
|
-
const result = new Headers(defaultHeaders)
|
|
175
|
-
|
|
176
|
-
const overrides =
|
|
177
|
-
requestHeaders instanceof Headers
|
|
178
|
-
? requestHeaders
|
|
179
|
-
: new Headers(requestHeaders)
|
|
180
|
-
|
|
181
|
-
for (const [key, value] of overrides.entries()) {
|
|
182
|
-
result.set(key, value)
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
return result
|
|
186
|
-
}
|