@atproto/lex-client 0.0.16 → 0.0.17
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 +19 -0
- package/dist/client.d.ts +24 -21
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +10 -7
- package/dist/client.js.map +1 -1
- package/dist/errors.d.ts +6 -5
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/response.d.ts +61 -6
- package/dist/response.d.ts.map +1 -1
- package/dist/response.js +59 -40
- package/dist/response.js.map +1 -1
- package/dist/types.d.ts +8 -37
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +4 -0
- package/dist/types.js.map +1 -1
- package/dist/util.d.ts +14 -27
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +15 -6
- package/dist/util.js.map +1 -1
- package/dist/xrpc.d.ts +40 -15
- package/dist/xrpc.d.ts.map +1 -1
- package/dist/xrpc.js +3 -1
- package/dist/xrpc.js.map +1 -1
- package/package.json +6 -6
- package/src/client.ts +81 -31
- package/src/errors.ts +6 -4
- package/src/response.ts +186 -63
- package/src/types.ts +17 -40
- package/src/util.test.ts +11 -11
- package/src/util.ts +33 -36
- package/src/xrpc.test.ts +641 -92
- package/src/xrpc.ts +72 -26
package/src/xrpc.ts
CHANGED
|
@@ -15,19 +15,16 @@ import {
|
|
|
15
15
|
} from '@atproto/lex-schema'
|
|
16
16
|
import { Agent, AgentOptions, buildAgent } from './agent.js'
|
|
17
17
|
import { XrpcFailure, XrpcFetchError, asXrpcFailure } from './errors.js'
|
|
18
|
-
import { XrpcResponse } from './response.js'
|
|
19
|
-
import { BinaryBodyInit
|
|
18
|
+
import { XrpcResponse, XrpcResponseOptions } from './response.js'
|
|
19
|
+
import { BinaryBodyInit } from './types.js'
|
|
20
20
|
import {
|
|
21
|
-
|
|
21
|
+
XrpcRequestHeadersOptions,
|
|
22
|
+
buildXrpcRequestHeaders,
|
|
22
23
|
isAsyncIterable,
|
|
23
24
|
isBlobLike,
|
|
24
25
|
toReadableStream,
|
|
25
26
|
} from './util.js'
|
|
26
27
|
|
|
27
|
-
// If all params are optional, allow omitting the params object
|
|
28
|
-
type XrpcParamsOptions<P extends Params> =
|
|
29
|
-
NonNullable<unknown> extends P ? { params?: P } : { params: P }
|
|
30
|
-
|
|
31
28
|
/**
|
|
32
29
|
* The query/path parameters type for an XRPC method, inferred from its schema.
|
|
33
30
|
*
|
|
@@ -36,25 +33,43 @@ type XrpcParamsOptions<P extends Params> =
|
|
|
36
33
|
export type XrpcRequestParams<M extends Procedure | Query | Subscription> =
|
|
37
34
|
InferInput<M['parameters']>
|
|
38
35
|
|
|
36
|
+
// If all params are optional, allow omitting the params object
|
|
37
|
+
type XrpcRequestParamsOptions<P extends Params> =
|
|
38
|
+
NonNullable<unknown> extends P ? { params?: P } : { params: P }
|
|
39
|
+
|
|
39
40
|
type XrpcRequestPayload<M extends Procedure | Query> = M extends Procedure
|
|
40
41
|
? InferPayload<M['input'], BinaryBodyInit>
|
|
41
42
|
: undefined
|
|
42
43
|
|
|
43
|
-
type
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
type XrpcRequestPayloadOptions<TPayload> = TPayload extends {
|
|
45
|
+
body: infer B
|
|
46
|
+
encoding: infer E
|
|
47
|
+
}
|
|
48
|
+
? {
|
|
49
|
+
body: B
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* mime type hint for binary bodies
|
|
53
|
+
*
|
|
54
|
+
* Only needed for endpoints that accept binary input (e.g. file uploads)
|
|
55
|
+
* when the body is a Blob-like object without a type (e.g. fetch-blob's
|
|
56
|
+
* Blob). If the body is a Blob-like object with a type, that type will be
|
|
57
|
+
* used as the content-type header instead of this option.
|
|
58
|
+
*
|
|
59
|
+
* @default "application/octet-stream"
|
|
60
|
+
*/
|
|
61
|
+
encoding?: E
|
|
62
|
+
}
|
|
46
63
|
: { body?: undefined; encoding?: undefined }
|
|
47
64
|
|
|
48
65
|
/**
|
|
49
|
-
* Options for making an XRPC request.
|
|
66
|
+
* Options for making an XRPC request, based on the method schema.
|
|
50
67
|
*
|
|
51
|
-
* Combines {@link
|
|
52
|
-
*
|
|
68
|
+
* Combines {@link XrpcRequestOptions} and {@link XrpcResponseOptions} with
|
|
69
|
+
* method-specific params and body requirements. The type system ensures
|
|
70
|
+
* required params/body are provided based on the method schema.
|
|
53
71
|
*
|
|
54
72
|
* @typeParam M - The XRPC method type (Procedure or Query)
|
|
55
|
-
* @see {@link CallOptions} for general request options like signal and validateRequest
|
|
56
|
-
* @see {@link XrpcParamsOptions} for method-specific query parameters
|
|
57
|
-
* @see {@link XrpcInputOptions} for method-specific body and encoding requirements
|
|
58
73
|
*
|
|
59
74
|
* @example Query with params
|
|
60
75
|
* ```typescript
|
|
@@ -71,9 +86,31 @@ type XrpcInputOptions<In> = In extends { body: infer B; encoding: infer E }
|
|
|
71
86
|
* ```
|
|
72
87
|
*/
|
|
73
88
|
export type XrpcOptions<M extends Procedure | Query = Procedure | Query> =
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
89
|
+
XrpcRequestOptions<M> & XrpcResponseOptions
|
|
90
|
+
|
|
91
|
+
export type XrpcRequestOptions<
|
|
92
|
+
M extends Procedure | Query = Procedure | Query,
|
|
93
|
+
> = XrpcRequestProcessingOptions &
|
|
94
|
+
XrpcRequestHeadersOptions &
|
|
95
|
+
XrpcRequestPayloadOptions<XrpcRequestPayload<M>> &
|
|
96
|
+
XrpcRequestParamsOptions<XrpcRequestParams<M>>
|
|
97
|
+
|
|
98
|
+
export type XrpcRequestProcessingOptions = {
|
|
99
|
+
/**
|
|
100
|
+
* AbortSignal to cancel the request.
|
|
101
|
+
*/
|
|
102
|
+
signal?: AbortSignal
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Whether to validate the request against the method's input schema. Enabling
|
|
106
|
+
* this can help catch errors early but may have a performance cost. This
|
|
107
|
+
* would typically only be set to `true` in development or debugging
|
|
108
|
+
* scenarios.
|
|
109
|
+
*
|
|
110
|
+
* @default false
|
|
111
|
+
*/
|
|
112
|
+
validateRequest?: boolean
|
|
113
|
+
}
|
|
77
114
|
|
|
78
115
|
/**
|
|
79
116
|
* Makes an XRPC request and throws on failure.
|
|
@@ -192,10 +229,13 @@ export async function xrpcSafe<const M extends Query | Procedure>(
|
|
|
192
229
|
|
|
193
230
|
function xrpcRequestUrl<M extends Procedure | Query | Subscription>(
|
|
194
231
|
method: M,
|
|
195
|
-
options:
|
|
232
|
+
options: { params?: Params },
|
|
196
233
|
): `/xrpc/${NsidString}${'' | `?${string}`}` {
|
|
197
234
|
const path = `/xrpc/${method.nsid}` as const
|
|
198
235
|
|
|
236
|
+
// @NOTE param.toURLSearchParams() will always validate the params in order to
|
|
237
|
+
// apply default values, so we can't disable it with options.validateRequest
|
|
238
|
+
|
|
199
239
|
const queryString = method.parameters
|
|
200
240
|
?.toURLSearchParams(options.params ?? {})
|
|
201
241
|
.toString()
|
|
@@ -205,12 +245,13 @@ function xrpcRequestUrl<M extends Procedure | Query | Subscription>(
|
|
|
205
245
|
|
|
206
246
|
function xrpcRequestInit<T extends Procedure | Query>(
|
|
207
247
|
schema: T,
|
|
208
|
-
options:
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
248
|
+
options: XrpcRequestProcessingOptions &
|
|
249
|
+
XrpcRequestHeadersOptions &
|
|
250
|
+
XrpcProcedureInputOptions & {
|
|
251
|
+
encoding?: string
|
|
252
|
+
},
|
|
212
253
|
): RequestInit & { duplex?: 'half' } {
|
|
213
|
-
const headers =
|
|
254
|
+
const headers = buildXrpcRequestHeaders(options)
|
|
214
255
|
|
|
215
256
|
// Tell the server what type of response we're expecting
|
|
216
257
|
if (schema.output.encoding) {
|
|
@@ -258,9 +299,14 @@ function xrpcRequestInit<T extends Procedure | Query>(
|
|
|
258
299
|
}
|
|
259
300
|
}
|
|
260
301
|
|
|
302
|
+
type XrpcProcedureInputOptions = {
|
|
303
|
+
body?: LexValue | BinaryBodyInit
|
|
304
|
+
validateRequest?: boolean
|
|
305
|
+
}
|
|
306
|
+
|
|
261
307
|
function xrpcProcedureInput(
|
|
262
308
|
method: Procedure,
|
|
263
|
-
options:
|
|
309
|
+
options: XrpcProcedureInputOptions,
|
|
264
310
|
encodingHint?: string,
|
|
265
311
|
): null | { body: BodyInit; encoding: string } {
|
|
266
312
|
const { input } = method
|