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