@atproto/api 0.13.0-rc.2 → 0.13.0

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 CHANGED
@@ -1,5 +1,577 @@
1
1
  # @atproto/api
2
2
 
3
+ ## 0.13.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#2483](https://github.com/bluesky-social/atproto/pull/2483) [`b934b396b`](https://github.com/bluesky-social/atproto/commit/b934b396b13ba32bf2bf7e75ecdf6871e5f310dd) Thanks [@matthieusieben](https://github.com/matthieusieben)!
8
+
9
+ #### Motivation
10
+
11
+ The motivation for these changes is the need to make the `@atproto/api` package
12
+ compatible with OAuth session management. We don't have OAuth client support
13
+ "launched" and documented quite yet, so you can keep using the current app
14
+ password authentication system. When we do "launch" OAuth support and begin
15
+ encouraging its usage in the near future (see the [OAuth
16
+ Roadmap](https://github.com/bluesky-social/atproto/discussions/2656)), these
17
+ changes will make it easier to migrate.
18
+
19
+ In addition, the redesigned session management system fixes a bug that could
20
+ cause the session data to become invalid when Agent clones are created (e.g.
21
+ using `agent.withProxy()`).
22
+
23
+ #### New Features
24
+
25
+ We've restructured the `XrpcClient` HTTP fetch handler to be specified during
26
+ the instantiation of the XRPC client, through the constructor, instead of using
27
+ a default implementation (which was statically defined).
28
+
29
+ With this refactor, the XRPC client is now more modular and reusable. Session
30
+ management, retries, cryptographic signing, and other request-specific logic can
31
+ be implemented in the fetch handler itself rather than by the calling code.
32
+
33
+ A new abstract class named `Agent`, has been added to `@atproto/api`. This class
34
+ will be the base class for all Bluesky agents classes in the `@atproto`
35
+ ecosystem. It is meant to be extended by implementations that provide session
36
+ management and fetch handling.
37
+
38
+ As you adapt your code to these changes, make sure to use the `Agent` type
39
+ wherever you expect to receive an agent, and use the `AtpAgent` type (class)
40
+ only to instantiate your client. The reason for this is to be forward compatible
41
+ with the OAuth agent implementation that will also extend `Agent`, and not
42
+ `AtpAgent`.
43
+
44
+ ```ts
45
+ import { Agent, AtpAgent } from '@atproto/api'
46
+
47
+ async function setupAgent(
48
+ service: string,
49
+ username: string,
50
+ password: string,
51
+ ): Promise<Agent> {
52
+ const agent = new AtpAgent({
53
+ service,
54
+ persistSession: (evt, session) => {
55
+ // handle session update
56
+ },
57
+ })
58
+
59
+ await agent.login(username, password)
60
+
61
+ return agent
62
+ }
63
+ ```
64
+
65
+ ```ts
66
+ import { Agent } from '@atproto/api'
67
+
68
+ async function doStuffWithAgent(agent: Agent, arg: string) {
69
+ return agent.resolveHandle(arg)
70
+ }
71
+ ```
72
+
73
+ ```ts
74
+ import { Agent, AtpAgent } from '@atproto/api'
75
+
76
+ class MyClass {
77
+ agent: Agent
78
+
79
+ constructor() {
80
+ this.agent = new AtpAgent()
81
+ }
82
+ }
83
+ ```
84
+
85
+ #### Breaking changes
86
+
87
+ Most of the changes introduced in this version are backward-compatible. However,
88
+ there are a couple of breaking changes you should be aware of:
89
+
90
+ - Customizing `fetch`: The ability to customize the `fetch: FetchHandler`
91
+ property of `@atproto/xrpc`'s `Client` and `@atproto/api`'s `AtpAgent` classes
92
+ has been removed. Previously, the `fetch` property could be set to a function
93
+ that would be used as the fetch handler for that instance, and was initialized
94
+ to a default fetch handler. That property is still accessible in a read-only
95
+ fashion through the `fetchHandler` property and can only be set during the
96
+ instance creation. Attempting to set/get the `fetch` property will now result
97
+ in an error.
98
+ - The `fetch()` method, as well as WhatWG compliant `Request` and `Headers`
99
+ constructors, must be globally available in your environment. Use a polyfill
100
+ if necessary.
101
+ - The `AtpBaseClient` has been removed. The `AtpServiceClient` has been renamed
102
+ `AtpBaseClient`. Any code using either of these classes will need to be
103
+ updated.
104
+ - Instead of _wrapping_ an `XrpcClient` in its `xrpc` property, the
105
+ `AtpBaseClient` (formerly `AtpServiceClient`) class - created through
106
+ `lex-cli` - now _extends_ the `XrpcClient` class. This means that a client
107
+ instance now passes the `instanceof XrpcClient` check. The `xrpc` property now
108
+ returns the instance itself and has been deprecated.
109
+ - `setSessionPersistHandler` is no longer available on the `AtpAgent` or
110
+ `BskyAgent` classes. The session handler can only be set though the
111
+ `persistSession` options of the `AtpAgent` constructor.
112
+ - The new class hierarchy is as follows:
113
+ - `BskyAgent` extends `AtpAgent`: but add no functionality (hence its
114
+ deprecation).
115
+ - `AtpAgent` extends `Agent`: adds password based session management.
116
+ - `Agent` extends `AtpBaseClient`: this abstract class that adds syntactic sugar
117
+ methods `app.bsky` lexicons. It also adds abstract session management
118
+ methods and adds atproto specific utilities
119
+ (`labelers` & `proxy` headers, cloning capability)
120
+ - `AtpBaseClient` extends `XrpcClient`: automatically code that adds fully
121
+ typed lexicon defined namespaces (`instance.app.bsky.feed.getPosts()`) to
122
+ the `XrpcClient`.
123
+ - `XrpcClient` is the base class.
124
+
125
+ #### Non-breaking changes
126
+
127
+ - The `com.*` and `app.*` namespaces have been made directly available to every
128
+ `Agent` instances.
129
+
130
+ #### Deprecations
131
+
132
+ - The default export of the `@atproto/xrpc` package has been deprecated. Use
133
+ named exports instead.
134
+ - The `Client` and `ServiceClient` classes are now deprecated. They are replaced by a single `XrpcClient` class.
135
+ - The default export of the `@atproto/api` package has been deprecated. Use
136
+ named exports instead.
137
+ - The `BskyAgent` has been deprecated. Use the `AtpAgent` class instead.
138
+ - The `xrpc` property of the `AtpClient` instances has been deprecated. The
139
+ instance itself should be used as the XRPC client.
140
+ - The `api` property of the `AtpAgent` and `BskyAgent` instances has been
141
+ deprecated. Use the instance itself instead.
142
+
143
+ #### Migration
144
+
145
+ ##### The `@atproto/api` package
146
+
147
+ If you were relying on the `AtpBaseClient` solely to perform validation, use
148
+ this:
149
+
150
+ <table>
151
+ <tr>
152
+ <td><center>Before</center></td> <td><center>After</center></td>
153
+ </tr>
154
+ <tr>
155
+ <td>
156
+
157
+ ```ts
158
+ import { AtpBaseClient, ComAtprotoSyncSubscribeRepos } from '@atproto/api'
159
+
160
+ const baseClient = new AtpBaseClient()
161
+
162
+ baseClient.xrpc.lex.assertValidXrpcMessage('io.example.doStuff', {
163
+ // ...
164
+ })
165
+ ```
166
+
167
+ </td>
168
+ <td>
169
+
170
+ ```ts
171
+ import { lexicons } from '@atproto/api'
172
+
173
+ lexicons.assertValidXrpcMessage('io.example.doStuff', {
174
+ // ...
175
+ })
176
+ ```
177
+
178
+ </td>
179
+ </tr>
180
+ </table>
181
+
182
+ If you are extending the `BskyAgent` to perform custom `session` manipulation, define your own `Agent` subclass instead:
183
+
184
+ <table>
185
+ <tr>
186
+ <td><center>Before</center></td> <td><center>After</center></td>
187
+ </tr>
188
+ <tr>
189
+ <td>
190
+
191
+ ```ts
192
+ import { BskyAgent } from '@atproto/api'
193
+
194
+ class MyAgent extends BskyAgent {
195
+ private accessToken?: string
196
+
197
+ async createOrRefreshSession(identifier: string, password: string) {
198
+ // custom logic here
199
+
200
+ this.accessToken = 'my-access-jwt'
201
+ }
202
+
203
+ async doStuff() {
204
+ return this.call('io.example.doStuff', {
205
+ headers: {
206
+ Authorization: this.accessToken && `Bearer ${this.accessToken}`,
207
+ },
208
+ })
209
+ }
210
+ }
211
+ ```
212
+
213
+ </td>
214
+ <td>
215
+
216
+ ```ts
217
+ import { Agent } from '@atproto/api'
218
+
219
+ class MyAgent extends Agent {
220
+ private accessToken?: string
221
+ public did?: string
222
+
223
+ constructor(private readonly service: string | URL) {
224
+ super({
225
+ service,
226
+ headers: {
227
+ Authorization: () =>
228
+ this.accessToken ? `Bearer ${this.accessToken}` : null,
229
+ },
230
+ })
231
+ }
232
+
233
+ clone(): MyAgent {
234
+ const agent = new MyAgent(this.service)
235
+ agent.accessToken = this.accessToken
236
+ agent.did = this.did
237
+ return this.copyInto(agent)
238
+ }
239
+
240
+ async createOrRefreshSession(identifier: string, password: string) {
241
+ // custom logic here
242
+
243
+ this.did = 'did:example:123'
244
+ this.accessToken = 'my-access-jwt'
245
+ }
246
+ }
247
+ ```
248
+
249
+ </td>
250
+ </tr>
251
+ </table>
252
+
253
+ If you are monkey patching the `xrpc` service client to perform client-side rate limiting, you can now do this in the `FetchHandler` function:
254
+
255
+ <table>
256
+ <tr>
257
+ <td><center>Before</center></td> <td><center>After</center></td>
258
+ </tr>
259
+ <tr>
260
+ <td>
261
+
262
+ ```ts
263
+ import { BskyAgent } from '@atproto/api'
264
+ import { RateLimitThreshold } from 'rate-limit-threshold'
265
+
266
+ const agent = new BskyAgent()
267
+ const limiter = new RateLimitThreshold(3000, 300_000)
268
+
269
+ const origCall = agent.api.xrpc.call
270
+ agent.api.xrpc.call = async function (...args) {
271
+ await limiter.wait()
272
+ return origCall.call(this, ...args)
273
+ }
274
+ ```
275
+
276
+ </td>
277
+ <td>
278
+
279
+ ```ts
280
+ import { AtpAgent } from '@atproto/api'
281
+ import { RateLimitThreshold } from 'rate-limit-threshold'
282
+
283
+ class LimitedAtpAgent extends AtpAgent {
284
+ constructor(options: AtpAgentOptions) {
285
+ const fetch: typeof globalThis.fetch = options.fetch ?? globalThis.fetch
286
+ const limiter = new RateLimitThreshold(3000, 300_000)
287
+
288
+ super({
289
+ ...options,
290
+ fetch: async (...args) => {
291
+ await limiter.wait()
292
+ return fetch(...args)
293
+ },
294
+ })
295
+ }
296
+ }
297
+ ```
298
+
299
+ </td>
300
+ </tr>
301
+ </table>
302
+
303
+ If you configure a static `fetch` handler on the `BskyAgent` class - for example
304
+ to modify the headers of every request - you can now do this by providing your
305
+ own `fetch` function:
306
+
307
+ <table>
308
+ <tr>
309
+ <td><center>Before</center></td> <td><center>After</center></td>
310
+ </tr>
311
+ <tr>
312
+ <td>
313
+
314
+ ```ts
315
+ import { BskyAgent, defaultFetchHandler } from '@atproto/api'
316
+
317
+ BskyAgent.configure({
318
+ fetch: async (httpUri, httpMethod, httpHeaders, httpReqBody) => {
319
+ const ua = httpHeaders['User-Agent']
320
+
321
+ httpHeaders['User-Agent'] = ua ? `${ua} ${userAgent}` : userAgent
322
+
323
+ return defaultFetchHandler(httpUri, httpMethod, httpHeaders, httpReqBody)
324
+ },
325
+ })
326
+ ```
327
+
328
+ </td>
329
+ <td>
330
+
331
+ ```ts
332
+ import { AtpAgent } from '@atproto/api'
333
+
334
+ class MyAtpAgent extends AtpAgent {
335
+ constructor(options: AtpAgentOptions) {
336
+ const fetch = options.fetch ?? globalThis.fetch
337
+
338
+ super({
339
+ ...options,
340
+ fetch: async (url, init) => {
341
+ const headers = new Headers(init.headers)
342
+
343
+ const ua = headersList.get('User-Agent')
344
+ headersList.set('User-Agent', ua ? `${ua} ${userAgent}` : userAgent)
345
+
346
+ return fetch(url, { ...init, headers })
347
+ },
348
+ })
349
+ }
350
+ }
351
+ ```
352
+
353
+ </td>
354
+ </tr>
355
+ </table>
356
+
357
+ <!-- <table>
358
+ <tr>
359
+ <td><center>Before</center></td> <td><center>After</center></td>
360
+ </tr>
361
+ <tr>
362
+ <td>
363
+
364
+ ```ts
365
+ // before
366
+ ```
367
+
368
+ </td>
369
+ <td>
370
+
371
+ ```ts
372
+ // after
373
+ ```
374
+
375
+ </td>
376
+ </tr>
377
+ </table> -->
378
+
379
+ ##### The `@atproto/xrpc` package
380
+
381
+ The `Client` and `ServiceClient` classes are now **deprecated**. If you need a
382
+ lexicon based client, you should update the code to use the `XrpcClient` class
383
+ instead.
384
+
385
+ The deprecated `ServiceClient` class now extends the new `XrpcClient` class.
386
+ Because of this, the `fetch` `FetchHandler` can no longer be configured on the
387
+ `Client` instances (including the default export of the package). If you are not
388
+ relying on the `fetch` `FetchHandler`, the new changes should have no impact on
389
+ your code. Beware that the deprecated classes will eventually be removed in a
390
+ future version.
391
+
392
+ Since its use has completely changed, the `FetchHandler` type has also
393
+ completely changed. The new `FetchHandler` type is now a function that receives
394
+ a `url` pathname and a `RequestInit` object and returns a `Promise<Response>`.
395
+ This function is responsible for making the actual request to the server.
396
+
397
+ ```ts
398
+ export type FetchHandler = (
399
+ this: void,
400
+ /**
401
+ * The URL (pathname + query parameters) to make the request to, without the
402
+ * origin. The origin (protocol, hostname, and port) must be added by this
403
+ * {@link FetchHandler}, typically based on authentication or other factors.
404
+ */
405
+ url: string,
406
+ init: RequestInit,
407
+ ) => Promise<Response>
408
+ ```
409
+
410
+ A noticeable change that has been introduced is that the `uri` field of the
411
+ `ServiceClient` class has _not_ been ported to the new `XrpcClient` class. It is
412
+ now the responsibility of the `FetchHandler` to determine the full URL to make
413
+ the request to. The same goes for the `headers`, which should now be set through
414
+ the `FetchHandler` function.
415
+
416
+ If you _do_ rely on the legacy `Client.fetch` property to perform custom logic
417
+ upon request, you will need to migrate your code to use the new `XrpcClient`
418
+ class. The `XrpcClient` class has a similar API to the old `ServiceClient`
419
+ class, but with a few differences:
420
+
421
+ - The `Client` + `ServiceClient` duality was removed in favor of a single
422
+ `XrpcClient` class. This means that:
423
+
424
+ - There no longer exists a centralized lexicon registry. If you need a global
425
+ lexicon registry, you can maintain one yourself using a `new Lexicons` (from
426
+ `@atproto/lexicon`).
427
+ - The `FetchHandler` is no longer a statically defined property of the
428
+ `Client` class. Instead, it is passed as an argument to the `XrpcClient`
429
+ constructor.
430
+
431
+ - The `XrpcClient` constructor now requires a `FetchHandler` function as the
432
+ first argument, and an optional `Lexicon` instance as the second argument.
433
+ - The `setHeader` and `unsetHeader` methods were not ported to the new
434
+ `XrpcClient` class. If you need to set or unset headers, you should do so in
435
+ the `FetchHandler` function provided in the constructor arg.
436
+
437
+ <table>
438
+ <tr>
439
+ <td><center>Before</center></td> <td><center>After</center></td>
440
+ </tr>
441
+ <tr>
442
+ <td>
443
+
444
+ ```ts
445
+ import client, { defaultFetchHandler } from '@atproto/xrpc'
446
+
447
+ client.fetch = function (
448
+ httpUri: string,
449
+ httpMethod: string,
450
+ httpHeaders: Headers,
451
+ httpReqBody: unknown,
452
+ ) {
453
+ // Custom logic here
454
+ return defaultFetchHandler(httpUri, httpMethod, httpHeaders, httpReqBody)
455
+ }
456
+
457
+ client.addLexicon({
458
+ lexicon: 1,
459
+ id: 'io.example.doStuff',
460
+ defs: {},
461
+ })
462
+
463
+ const instance = client.service('http://my-service.com')
464
+
465
+ instance.setHeader('my-header', 'my-value')
466
+
467
+ await instance.call('io.example.doStuff')
468
+ ```
469
+
470
+ </td>
471
+ <td>
472
+
473
+ ```ts
474
+ import { XrpcClient } from '@atproto/xrpc'
475
+
476
+ const instance = new XrpcClient(
477
+ async (url, init) => {
478
+ const headers = new Headers(init.headers)
479
+
480
+ headers.set('my-header', 'my-value')
481
+
482
+ // Custom logic here
483
+
484
+ const fullUrl = new URL(url, 'http://my-service.com')
485
+
486
+ return fetch(fullUrl, { ...init, headers })
487
+ },
488
+ [
489
+ {
490
+ lexicon: 1,
491
+ id: 'io.example.doStuff',
492
+ defs: {},
493
+ },
494
+ ],
495
+ )
496
+
497
+ await instance.call('io.example.doStuff')
498
+ ```
499
+
500
+ </td>
501
+ </tr>
502
+ </table>
503
+
504
+ If your fetch handler does not require any "custom logic", and all you need is
505
+ an `XrpcClient` that makes its HTTP requests towards a static service URL, the
506
+ previous example can be simplified to:
507
+
508
+ ```ts
509
+ import { XrpcClient } from '@atproto/xrpc'
510
+
511
+ const instance = new XrpcClient('http://my-service.com', [
512
+ {
513
+ lexicon: 1,
514
+ id: 'io.example.doStuff',
515
+ defs: {},
516
+ },
517
+ ])
518
+ ```
519
+
520
+ If you need to add static headers to all requests, you can instead instantiate
521
+ the `XrpcClient` as follows:
522
+
523
+ ```ts
524
+ import { XrpcClient } from '@atproto/xrpc'
525
+
526
+ const instance = new XrpcClient(
527
+ {
528
+ service: 'http://my-service.com',
529
+ headers: {
530
+ 'my-header': 'my-value',
531
+ },
532
+ },
533
+ [
534
+ {
535
+ lexicon: 1,
536
+ id: 'io.example.doStuff',
537
+ defs: {},
538
+ },
539
+ ],
540
+ )
541
+ ```
542
+
543
+ If you need the headers or service url to be dynamic, you can define them using
544
+ functions:
545
+
546
+ ```ts
547
+ import { XrpcClient } from '@atproto/xrpc'
548
+
549
+ const instance = new XrpcClient(
550
+ {
551
+ service: () => 'http://my-service.com',
552
+ headers: {
553
+ 'my-header': () => 'my-value',
554
+ 'my-ignored-header': () => null, // ignored
555
+ },
556
+ },
557
+ [
558
+ {
559
+ lexicon: 1,
560
+ id: 'io.example.doStuff',
561
+ defs: {},
562
+ },
563
+ ],
564
+ )
565
+ ```
566
+
567
+ - [#2483](https://github.com/bluesky-social/atproto/pull/2483) [`b934b396b`](https://github.com/bluesky-social/atproto/commit/b934b396b13ba32bf2bf7e75ecdf6871e5f310dd) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Add the ability to use `fetch()` compatible `BodyInit` body when making XRPC calls.
568
+
569
+ ### Patch Changes
570
+
571
+ - Updated dependencies [[`b934b396b`](https://github.com/bluesky-social/atproto/commit/b934b396b13ba32bf2bf7e75ecdf6871e5f310dd), [`2bdf75d7a`](https://github.com/bluesky-social/atproto/commit/2bdf75d7a63924c10e7a311f16cb447d595b933e), [`b934b396b`](https://github.com/bluesky-social/atproto/commit/b934b396b13ba32bf2bf7e75ecdf6871e5f310dd), [`b934b396b`](https://github.com/bluesky-social/atproto/commit/b934b396b13ba32bf2bf7e75ecdf6871e5f310dd)]:
572
+ - @atproto/lexicon@0.4.1
573
+ - @atproto/xrpc@0.6.0
574
+
3
575
  ## 0.12.29
4
576
 
5
577
  ### Patch Changes