@atproto/lex-client 0.0.11 → 0.0.13

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.
Files changed (71) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/dist/agent.d.ts +67 -0
  3. package/dist/agent.d.ts.map +1 -1
  4. package/dist/agent.js +31 -0
  5. package/dist/agent.js.map +1 -1
  6. package/dist/client.d.ts +443 -45
  7. package/dist/client.d.ts.map +1 -1
  8. package/dist/client.js +145 -1
  9. package/dist/client.js.map +1 -1
  10. package/dist/errors.d.ts +162 -9
  11. package/dist/errors.d.ts.map +1 -1
  12. package/dist/errors.js +132 -8
  13. package/dist/errors.js.map +1 -1
  14. package/dist/lexicons/com/atproto/repo/createRecord.defs.d.ts +29 -25
  15. package/dist/lexicons/com/atproto/repo/createRecord.defs.d.ts.map +1 -1
  16. package/dist/lexicons/com/atproto/repo/createRecord.defs.js +3 -2
  17. package/dist/lexicons/com/atproto/repo/createRecord.defs.js.map +1 -1
  18. package/dist/lexicons/com/atproto/repo/deleteRecord.defs.d.ts +17 -17
  19. package/dist/lexicons/com/atproto/repo/deleteRecord.defs.d.ts.map +1 -1
  20. package/dist/lexicons/com/atproto/repo/deleteRecord.defs.js.map +1 -1
  21. package/dist/lexicons/com/atproto/repo/getRecord.defs.d.ts +9 -9
  22. package/dist/lexicons/com/atproto/repo/getRecord.defs.d.ts.map +1 -1
  23. package/dist/lexicons/com/atproto/repo/getRecord.defs.js +1 -1
  24. package/dist/lexicons/com/atproto/repo/getRecord.defs.js.map +1 -1
  25. package/dist/lexicons/com/atproto/repo/listRecords.defs.d.ts +8 -8
  26. package/dist/lexicons/com/atproto/repo/listRecords.defs.d.ts.map +1 -1
  27. package/dist/lexicons/com/atproto/repo/listRecords.defs.js +1 -1
  28. package/dist/lexicons/com/atproto/repo/listRecords.defs.js.map +1 -1
  29. package/dist/lexicons/com/atproto/repo/putRecord.defs.d.ts +31 -27
  30. package/dist/lexicons/com/atproto/repo/putRecord.defs.d.ts.map +1 -1
  31. package/dist/lexicons/com/atproto/repo/putRecord.defs.js +3 -2
  32. package/dist/lexicons/com/atproto/repo/putRecord.defs.js.map +1 -1
  33. package/dist/lexicons/com/atproto/repo/uploadBlob.defs.d.ts +7 -7
  34. package/dist/lexicons/com/atproto/repo/uploadBlob.defs.d.ts.map +1 -1
  35. package/dist/lexicons/com/atproto/repo/uploadBlob.defs.js.map +1 -1
  36. package/dist/lexicons/com/atproto/sync/getBlob.defs.d.ts +3 -3
  37. package/dist/lexicons/com/atproto/sync/getBlob.defs.d.ts.map +1 -1
  38. package/dist/lexicons/com/atproto/sync/getBlob.defs.js.map +1 -1
  39. package/dist/response.d.ts +14 -4
  40. package/dist/response.d.ts.map +1 -1
  41. package/dist/response.js +19 -9
  42. package/dist/response.js.map +1 -1
  43. package/dist/types.d.ts +51 -0
  44. package/dist/types.d.ts.map +1 -1
  45. package/dist/types.js.map +1 -1
  46. package/dist/util.d.ts +40 -5
  47. package/dist/util.d.ts.map +1 -1
  48. package/dist/util.js +22 -0
  49. package/dist/util.js.map +1 -1
  50. package/dist/www-authenticate.d.ts +23 -0
  51. package/dist/www-authenticate.d.ts.map +1 -1
  52. package/dist/www-authenticate.js.map +1 -1
  53. package/dist/xrpc.d.ts +81 -1
  54. package/dist/xrpc.d.ts.map +1 -1
  55. package/dist/xrpc.js.map +1 -1
  56. package/package.json +7 -7
  57. package/src/agent.ts +67 -0
  58. package/src/client.ts +424 -11
  59. package/src/errors.ts +165 -12
  60. package/src/lexicons/com/atproto/repo/createRecord.defs.ts +15 -7
  61. package/src/lexicons/com/atproto/repo/deleteRecord.defs.ts +11 -5
  62. package/src/lexicons/com/atproto/repo/getRecord.defs.ts +7 -4
  63. package/src/lexicons/com/atproto/repo/listRecords.defs.ts +8 -5
  64. package/src/lexicons/com/atproto/repo/putRecord.defs.ts +15 -7
  65. package/src/lexicons/com/atproto/repo/uploadBlob.defs.ts +11 -5
  66. package/src/lexicons/com/atproto/sync/getBlob.defs.ts +6 -3
  67. package/src/response.ts +22 -19
  68. package/src/types.ts +52 -0
  69. package/src/util.ts +50 -5
  70. package/src/www-authenticate.ts +24 -0
  71. package/src/xrpc.ts +83 -4
package/src/util.ts CHANGED
@@ -1,10 +1,42 @@
1
- import { DidString } from '@atproto/lex-schema'
1
+ import {
2
+ DidString,
3
+ InferMethodOutput,
4
+ InferMethodOutputBody,
5
+ Procedure,
6
+ Query,
7
+ } from '@atproto/lex-schema'
2
8
 
3
- export type XrpcPayload<B = unknown, E extends string = string> = {
4
- body: B
5
- encoding: E
6
- }
9
+ /**
10
+ * The body type of an XRPC response, inferred from the method's output schema.
11
+ *
12
+ * For JSON responses, this is the parsed LexValue. For binary responses,
13
+ * this is a Uint8Array.
14
+ *
15
+ * @typeParam M - The XRPC method type (Procedure or Query)
16
+ */
17
+ export type XrpcResponseBody<M extends Procedure | Query = Procedure | Query> =
18
+ InferMethodOutputBody<M, Uint8Array>
19
+
20
+ /**
21
+ * The full payload type of an XRPC response, including body and encoding.
22
+ *
23
+ * Returns `null` for methods that have no output.
24
+ *
25
+ * @typeParam M - The XRPC method type (Procedure or Query)
26
+ */
27
+ export type XrpcResponsePayload<
28
+ M extends Procedure | Query = Procedure | Query,
29
+ > = InferMethodOutput<M, Uint8Array>
7
30
 
31
+ /**
32
+ * Type guard to check if a value is {@link Blob}-like.
33
+ *
34
+ * Handles both native Blobs and polyfilled Blob implementations
35
+ * (e.g., fetch-blob from node-fetch).
36
+ *
37
+ * @param value - The value to check
38
+ * @returns `true` if the value is a Blob or Blob-like object
39
+ */
8
40
  export function isBlobLike(value: unknown): value is Blob {
9
41
  if (value == null) return false
10
42
  if (typeof value !== 'object') return false
@@ -32,6 +64,19 @@ export function isAsyncIterable<T>(
32
64
  )
33
65
  }
34
66
 
67
+ /**
68
+ * Builds HTTP headers for AT Protocol requests.
69
+ *
70
+ * Adds the following headers when applicable:
71
+ * - `atproto-proxy`: Service routing header (if service is specified)
72
+ * - `atproto-accept-labelers`: Comma-separated list of labeler DIDs
73
+ *
74
+ * @param options - Header building options
75
+ * @param options.headers - Base headers to include
76
+ * @param options.service - Service proxy identifier
77
+ * @param options.labelers - Labeler DIDs to request labels from
78
+ * @returns A new Headers object with AT Protocol headers added
79
+ */
35
80
  export function buildAtprotoHeaders(options: {
36
81
  headers?: HeadersInit
37
82
  service?: `${DidString}#${string}`
@@ -1,4 +1,28 @@
1
1
  type WWWAuthenticateParams = { [authParam in string]: string }
2
+
3
+ /**
4
+ * Parsed representation of a WWW-Authenticate HTTP header.
5
+ *
6
+ * Maps authentication scheme names to either:
7
+ * - A token68 string (compact authentication data)
8
+ * - A params object with key-value pairs
9
+ *
10
+ * @example Bearer with realm
11
+ * ```typescript
12
+ * // WWW-Authenticate: Bearer realm="example"
13
+ * const parsed: WWWAuthenticate = {
14
+ * Bearer: { realm: 'example' }
15
+ * }
16
+ * ```
17
+ *
18
+ * @example DPoP with error
19
+ * ```typescript
20
+ * // WWW-Authenticate: DPoP error="use_dpop_nonce", error_description="..."
21
+ * const parsed: WWWAuthenticate = {
22
+ * DPoP: { error: 'use_dpop_nonce', error_description: '...' }
23
+ * }
24
+ * ```
25
+ */
2
26
  export type WWWAuthenticate = {
3
27
  [authScheme in string]:
4
28
  | string // token68
package/src/xrpc.ts CHANGED
@@ -17,7 +17,6 @@ import { XrpcFailure, asXrpcFailure } from './errors.js'
17
17
  import { XrpcResponse } from './response.js'
18
18
  import { BinaryBodyInit, CallOptions } from './types.js'
19
19
  import {
20
- XrpcPayload,
21
20
  buildAtprotoHeaders,
22
21
  isAsyncIterable,
23
22
  isBlobLike,
@@ -28,6 +27,11 @@ import {
28
27
  type XrpcParamsOptions<P extends Params> =
29
28
  NonNullable<unknown> extends P ? { params?: P } : { params: P }
30
29
 
30
+ /**
31
+ * The query/path parameters type for an XRPC method, inferred from its schema.
32
+ *
33
+ * @typeParam M - The XRPC method type (Procedure, Query, or Subscription)
34
+ */
31
35
  export type XrpcRequestParams<M extends Procedure | Query | Subscription> =
32
36
  InferInput<M['parameters']>
33
37
 
@@ -40,13 +44,54 @@ type XrpcInputOptions<In> = In extends { body: infer B; encoding: infer E }
40
44
  { body: B; encoding?: E }
41
45
  : { body?: undefined; encoding?: undefined }
42
46
 
47
+ /**
48
+ * Options for making an XRPC request.
49
+ *
50
+ * Combines {@link CallOptions} with method-specific params and body requirements.
51
+ * The type system ensures required params/body are provided based on the method schema.
52
+ *
53
+ * @typeParam M - The XRPC method type (Procedure or Query)
54
+ * @see {@link CallOptions} for general request options like signal and validateRequest
55
+ * @see {@link XrpcParamsOptions} for method-specific query parameters
56
+ * @see {@link XrpcInputOptions} for method-specific body and encoding requirements
57
+ *
58
+ * @example Query with params
59
+ * ```typescript
60
+ * const options: XrpcOptions<typeof app.bsky.feed.getTimeline.main> = {
61
+ * params: { limit: 50 }
62
+ * }
63
+ * ```
64
+ *
65
+ * @example Procedure with body
66
+ * ```typescript
67
+ * const options: XrpcOptions<typeof com.atproto.repo.createRecord.main> = {
68
+ * body: { repo: did, collection: 'app.bsky.feed.post', record: { ... } }
69
+ * }
70
+ * ```
71
+ */
43
72
  export type XrpcOptions<M extends Procedure | Query = Procedure | Query> =
44
73
  CallOptions &
45
74
  XrpcInputOptions<XrpcRequestPayload<M>> &
46
75
  XrpcParamsOptions<XrpcRequestParams<M>>
47
76
 
48
77
  /**
49
- * @throws XrpcFailure<M>
78
+ * Makes an XRPC request and throws on failure.
79
+ *
80
+ * This is the low-level function for making XRPC calls. For most use cases,
81
+ * prefer using {@link Client.xrpc} which provides a more ergonomic API.
82
+ *
83
+ * @param agent - The {@link Agent} to use for making the request
84
+ * @param ns - The lexicon method definition
85
+ * @param options - Request {@link XrpcOptions options} (params, body, headers, etc.)
86
+ * @returns The successful {@link XrpcResponse}
87
+ * @throws {XrpcFailure} When the request fails
88
+ *
89
+ * @example
90
+ * ```typescript
91
+ * const response = await xrpc(agent, app.bsky.feed.getTimeline.main, {
92
+ * params: { limit: 50 }
93
+ * })
94
+ * ```
50
95
  */
51
96
  export async function xrpc<const M extends Query | Procedure>(
52
97
  agent: Agent,
@@ -69,10 +114,44 @@ export async function xrpc<const M extends Query | Procedure>(
69
114
  else throw response
70
115
  }
71
116
 
117
+ /**
118
+ * Union type representing either a successful response or a failure.
119
+ *
120
+ * Both {@link XrpcResponse} and {@link XrpcFailure} have a `success` property
121
+ * that can be used to discriminate between them.
122
+ *
123
+ * @typeParam M - The XRPC method type
124
+ */
72
125
  export type XrpcResult<M extends Procedure | Query> =
73
126
  | XrpcResponse<M>
74
127
  | XrpcFailure<M>
75
128
 
129
+ /**
130
+ * Makes an XRPC request without throwing on failure.
131
+ *
132
+ * Returns a discriminated union that can be checked via the `success` property.
133
+ * This is useful for handling errors without try/catch blocks. This also allow
134
+ * failure results to be typed with the method schema, which can provide better
135
+ * type safety when handling errors (e.g. checking for specific error codes).
136
+ *
137
+ * @param agent - The {@link Agent} to use for making the request
138
+ * @param ns - The lexicon method definition
139
+ * @param options - Request {@link XrpcOptions options} (params, body, headers, etc.)
140
+ * @returns Either a successful {@link XrpcResponse} or an {@link XrpcFailure}
141
+ *
142
+ * @example
143
+ * ```typescript
144
+ * const result = await xrpcSafe(agent, app.bsky.actor.getProfile.main, {
145
+ * params: { actor: 'alice.bsky.social' }
146
+ * })
147
+ *
148
+ * if (result.success) {
149
+ * console.log(result.body.displayName)
150
+ * } else {
151
+ * console.error('Request failed:', result.error)
152
+ * }
153
+ * ```
154
+ */
76
155
  export async function xrpcSafe<const M extends Query | Procedure>(
77
156
  agent: Agent,
78
157
  ns: NonNullable<unknown> extends XrpcOptions<M>
@@ -173,7 +252,7 @@ function xrpcProcedureInput(
173
252
  method: Procedure,
174
253
  options: CallOptions & { body?: LexValue | BinaryBodyInit },
175
254
  encodingHint?: string,
176
- ): null | XrpcPayload<BodyInit> {
255
+ ): null | { body: BodyInit; encoding: string } {
177
256
  const { input } = method
178
257
  const { body } = options
179
258
 
@@ -222,7 +301,7 @@ function buildPayload(
222
301
  schema: Payload,
223
302
  body: undefined | BodyInit,
224
303
  encodingHint?: string,
225
- ): null | XrpcPayload<BodyInit> {
304
+ ): null | { body: BodyInit; encoding: string } {
226
305
  if (schema.encoding === undefined) {
227
306
  if (body !== undefined) {
228
307
  throw new TypeError(