@atproto/lex-client 0.2.0 → 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 +15 -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 +10 -9
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +3 -2
- 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 +3 -2
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +1 -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 -1179
- 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 -215
- 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/package.json
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atproto/lex-client",
|
|
3
|
-
"version": "0.2.
|
|
4
|
-
"engines": {
|
|
5
|
-
"node": ">=22"
|
|
6
|
-
},
|
|
3
|
+
"version": "0.2.1",
|
|
7
4
|
"license": "MIT",
|
|
8
5
|
"description": "HTTP client for interacting with Lexicon based APIs",
|
|
9
6
|
"keywords": [
|
|
@@ -19,10 +16,6 @@
|
|
|
19
16
|
"directory": "packages/lex/lex-client"
|
|
20
17
|
},
|
|
21
18
|
"files": [
|
|
22
|
-
"./src",
|
|
23
|
-
"./tsconfig.build.json",
|
|
24
|
-
"./tsconfig.tests.json",
|
|
25
|
-
"./tsconfig.json",
|
|
26
19
|
"./dist",
|
|
27
20
|
"./CHANGELOG.md"
|
|
28
21
|
],
|
|
@@ -34,19 +27,23 @@
|
|
|
34
27
|
"default": "./dist/index.js"
|
|
35
28
|
}
|
|
36
29
|
},
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=22"
|
|
32
|
+
},
|
|
37
33
|
"dependencies": {
|
|
38
34
|
"tslib": "^2.8.1",
|
|
39
|
-
"@atproto/lex-data": "^0.1.
|
|
40
|
-
"@atproto/lex-
|
|
41
|
-
"@atproto/lex-
|
|
35
|
+
"@atproto/lex-data": "^0.1.4",
|
|
36
|
+
"@atproto/lex-schema": "^0.1.6",
|
|
37
|
+
"@atproto/lex-json": "^0.1.3"
|
|
42
38
|
},
|
|
43
39
|
"devDependencies": {
|
|
44
40
|
"vitest": "^4.0.16",
|
|
45
|
-
"@atproto/lex-cbor": "^0.1.
|
|
46
|
-
"@atproto/lex-builder": "^0.1.
|
|
41
|
+
"@atproto/lex-cbor": "^0.1.3",
|
|
42
|
+
"@atproto/lex-builder": "^0.1.5"
|
|
47
43
|
},
|
|
48
44
|
"scripts": {
|
|
49
|
-
"
|
|
45
|
+
"codegen:lex": "node ./scripts/lex-build.mjs",
|
|
46
|
+
"prebuild": "pnpm run '/^(codegen:.+)$/'",
|
|
50
47
|
"build": "tsgo --build tsconfig.build.json",
|
|
51
48
|
"test": "vitest run"
|
|
52
49
|
}
|
package/src/agent.test.ts
DELETED
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi } from 'vitest'
|
|
2
|
-
import {
|
|
3
|
-
type Agent,
|
|
4
|
-
type AgentConfig,
|
|
5
|
-
type FetchHandler,
|
|
6
|
-
buildAgent,
|
|
7
|
-
isAgent,
|
|
8
|
-
} from './agent.js'
|
|
9
|
-
|
|
10
|
-
// ============================================================================
|
|
11
|
-
// isAgent
|
|
12
|
-
// ============================================================================
|
|
13
|
-
|
|
14
|
-
describe(isAgent, () => {
|
|
15
|
-
it('returns true for a valid agent with did', () => {
|
|
16
|
-
const agent: Agent = {
|
|
17
|
-
did: 'did:plc:example',
|
|
18
|
-
fetchHandler: async () => new Response(),
|
|
19
|
-
}
|
|
20
|
-
expect(isAgent(agent)).toBe(true)
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
it('returns true for an agent without did', () => {
|
|
24
|
-
const agent = { fetchHandler: async () => new Response() }
|
|
25
|
-
expect(isAgent(agent)).toBe(true)
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
it('returns true when did is undefined', () => {
|
|
29
|
-
const agent = { did: undefined, fetchHandler: async () => new Response() }
|
|
30
|
-
expect(isAgent(agent)).toBe(true)
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
it('returns false for null', () => {
|
|
34
|
-
expect(isAgent(null)).toBe(false)
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
it('returns false for non-objects', () => {
|
|
38
|
-
expect(isAgent('string')).toBe(false)
|
|
39
|
-
expect(isAgent(42)).toBe(false)
|
|
40
|
-
expect(isAgent(undefined)).toBe(false)
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
it('returns false when fetchHandler is not a function', () => {
|
|
44
|
-
expect(isAgent({ fetchHandler: 'not-a-function' })).toBe(false)
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
it('returns false when did is not a string', () => {
|
|
48
|
-
expect(isAgent({ did: 42, fetchHandler: async () => new Response() })).toBe(
|
|
49
|
-
false,
|
|
50
|
-
)
|
|
51
|
-
})
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
// ============================================================================
|
|
55
|
-
// buildAgent
|
|
56
|
-
// ============================================================================
|
|
57
|
-
|
|
58
|
-
describe(buildAgent, () => {
|
|
59
|
-
describe('from Agent', () => {
|
|
60
|
-
it('returns the same agent instance', () => {
|
|
61
|
-
const agent: Agent = {
|
|
62
|
-
did: 'did:plc:example',
|
|
63
|
-
fetchHandler: async () => new Response(),
|
|
64
|
-
}
|
|
65
|
-
expect(buildAgent(agent)).toBe(agent)
|
|
66
|
-
})
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
describe('from FetchHandler', () => {
|
|
70
|
-
it('wraps a function as an agent', () => {
|
|
71
|
-
const handler: FetchHandler = async () => new Response()
|
|
72
|
-
const agent = buildAgent(handler)
|
|
73
|
-
|
|
74
|
-
expect(agent.did).toBeUndefined()
|
|
75
|
-
expect(typeof agent.fetchHandler).toBe('function')
|
|
76
|
-
})
|
|
77
|
-
})
|
|
78
|
-
|
|
79
|
-
describe('from string URL', () => {
|
|
80
|
-
it('creates an agent that prepends the service URL', async () => {
|
|
81
|
-
const fetchFn = vi.fn<typeof globalThis.fetch>(async () =>
|
|
82
|
-
Response.json({ ok: true }),
|
|
83
|
-
)
|
|
84
|
-
const agent = buildAgent({
|
|
85
|
-
service: 'https://example.com',
|
|
86
|
-
fetch: fetchFn,
|
|
87
|
-
})
|
|
88
|
-
|
|
89
|
-
await agent.fetchHandler('/xrpc/io.example.test', { method: 'GET' })
|
|
90
|
-
|
|
91
|
-
expect(fetchFn).toHaveBeenCalledOnce()
|
|
92
|
-
const [url, init] = fetchFn.mock.calls[0]
|
|
93
|
-
expect(url).toEqual(
|
|
94
|
-
new URL('/xrpc/io.example.test', 'https://example.com'),
|
|
95
|
-
)
|
|
96
|
-
expect(init?.method).toBe('GET')
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
it('has undefined did', () => {
|
|
100
|
-
const agent = buildAgent('https://example.com')
|
|
101
|
-
expect(agent.did).toBeUndefined()
|
|
102
|
-
})
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
describe('from URL instance', () => {
|
|
106
|
-
it('creates an agent with the URL as service', async () => {
|
|
107
|
-
const fetchFn = vi.fn<typeof globalThis.fetch>(async () =>
|
|
108
|
-
Response.json({ ok: true }),
|
|
109
|
-
)
|
|
110
|
-
const agent = buildAgent({
|
|
111
|
-
service: new URL('https://example.com'),
|
|
112
|
-
fetch: fetchFn,
|
|
113
|
-
})
|
|
114
|
-
|
|
115
|
-
await agent.fetchHandler('/xrpc/io.example.test', { method: 'GET' })
|
|
116
|
-
|
|
117
|
-
expect(fetchFn).toHaveBeenCalledOnce()
|
|
118
|
-
const [url] = fetchFn.mock.calls[0]
|
|
119
|
-
expect(url).toEqual(
|
|
120
|
-
new URL('/xrpc/io.example.test', 'https://example.com'),
|
|
121
|
-
)
|
|
122
|
-
})
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
describe('from AgentConfig', () => {
|
|
126
|
-
it('exposes did from config', () => {
|
|
127
|
-
const agent = buildAgent({
|
|
128
|
-
did: 'did:plc:test123',
|
|
129
|
-
service: 'https://example.com',
|
|
130
|
-
})
|
|
131
|
-
expect(agent.did).toBe('did:plc:test123')
|
|
132
|
-
})
|
|
133
|
-
|
|
134
|
-
it('reflects did changes on the config object', () => {
|
|
135
|
-
const config: AgentConfig = {
|
|
136
|
-
did: 'did:plc:original',
|
|
137
|
-
service: 'https://example.com',
|
|
138
|
-
}
|
|
139
|
-
const agent = buildAgent(config)
|
|
140
|
-
expect(agent.did).toBe('did:plc:original')
|
|
141
|
-
|
|
142
|
-
config.did = 'did:plc:updated'
|
|
143
|
-
expect(agent.did).toBe('did:plc:updated')
|
|
144
|
-
})
|
|
145
|
-
|
|
146
|
-
it('throws TypeError when fetch is not available', () => {
|
|
147
|
-
const originalFetch = globalThis.fetch
|
|
148
|
-
try {
|
|
149
|
-
// @ts-expect-error removing fetch to simulate missing environment
|
|
150
|
-
globalThis.fetch = undefined
|
|
151
|
-
expect(() => buildAgent({ service: 'https://example.com' })).toThrow(
|
|
152
|
-
TypeError,
|
|
153
|
-
)
|
|
154
|
-
} finally {
|
|
155
|
-
globalThis.fetch = originalFetch
|
|
156
|
-
}
|
|
157
|
-
})
|
|
158
|
-
})
|
|
159
|
-
|
|
160
|
-
describe('headers', () => {
|
|
161
|
-
it('sends config headers when no request headers', async () => {
|
|
162
|
-
const fetchFn = vi.fn<typeof globalThis.fetch>(async () =>
|
|
163
|
-
Response.json({}),
|
|
164
|
-
)
|
|
165
|
-
const agent = buildAgent({
|
|
166
|
-
service: 'https://example.com',
|
|
167
|
-
headers: { Authorization: 'Bearer token123' },
|
|
168
|
-
fetch: fetchFn,
|
|
169
|
-
})
|
|
170
|
-
|
|
171
|
-
await agent.fetchHandler('/xrpc/test', { method: 'GET' })
|
|
172
|
-
|
|
173
|
-
const [, init] = fetchFn.mock.calls[0]
|
|
174
|
-
expect(init?.headers).toEqual({ Authorization: 'Bearer token123' })
|
|
175
|
-
})
|
|
176
|
-
|
|
177
|
-
it('sends request headers when no config headers', async () => {
|
|
178
|
-
const fetchFn = vi.fn<typeof globalThis.fetch>(async () =>
|
|
179
|
-
Response.json({}),
|
|
180
|
-
)
|
|
181
|
-
const agent = buildAgent({
|
|
182
|
-
service: 'https://example.com',
|
|
183
|
-
fetch: fetchFn,
|
|
184
|
-
})
|
|
185
|
-
|
|
186
|
-
await agent.fetchHandler('/xrpc/test', {
|
|
187
|
-
method: 'GET',
|
|
188
|
-
headers: { 'X-Custom': 'value' },
|
|
189
|
-
})
|
|
190
|
-
|
|
191
|
-
const [, init] = fetchFn.mock.calls[0]
|
|
192
|
-
expect(init?.headers).toEqual({ 'X-Custom': 'value' })
|
|
193
|
-
})
|
|
194
|
-
|
|
195
|
-
it('merges config and request headers, with request taking priority', async () => {
|
|
196
|
-
const fetchFn = vi.fn<typeof globalThis.fetch>(async () =>
|
|
197
|
-
Response.json({}),
|
|
198
|
-
)
|
|
199
|
-
const agent = buildAgent({
|
|
200
|
-
service: 'https://example.com',
|
|
201
|
-
headers: { Authorization: 'Bearer default', 'X-Default': 'yes' },
|
|
202
|
-
fetch: fetchFn,
|
|
203
|
-
})
|
|
204
|
-
|
|
205
|
-
await agent.fetchHandler('/xrpc/test', {
|
|
206
|
-
method: 'GET',
|
|
207
|
-
headers: { Authorization: 'Bearer override' },
|
|
208
|
-
})
|
|
209
|
-
|
|
210
|
-
const [, init] = fetchFn.mock.calls[0]
|
|
211
|
-
const headers = new Headers(init?.headers)
|
|
212
|
-
expect(headers.get('Authorization')).toBe('Bearer override')
|
|
213
|
-
expect(headers.get('X-Default')).toBe('yes')
|
|
214
|
-
})
|
|
215
|
-
})
|
|
216
|
-
})
|
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
|
-
}
|