@atproto/lex-client 0.0.5 → 0.0.7

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 (77) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/dist/agent.d.ts +1 -1
  3. package/dist/agent.d.ts.map +1 -1
  4. package/dist/agent.js +3 -0
  5. package/dist/agent.js.map +1 -1
  6. package/dist/client.d.ts +34 -32
  7. package/dist/client.d.ts.map +1 -1
  8. package/dist/client.js +17 -21
  9. package/dist/client.js.map +1 -1
  10. package/dist/{xrpc-error.d.ts → errors.d.ts} +22 -27
  11. package/dist/errors.d.ts.map +1 -0
  12. package/dist/{xrpc-error.js → errors.js} +38 -33
  13. package/dist/errors.js.map +1 -0
  14. package/dist/index.d.ts +2 -0
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +2 -0
  17. package/dist/index.js.map +1 -1
  18. package/dist/lexicons/com/atproto/repo/createRecord.defs.d.ts.map +1 -1
  19. package/dist/lexicons/com/atproto/repo/createRecord.defs.js +5 -7
  20. package/dist/lexicons/com/atproto/repo/createRecord.defs.js.map +1 -1
  21. package/dist/lexicons/com/atproto/repo/deleteRecord.defs.d.ts.map +1 -1
  22. package/dist/lexicons/com/atproto/repo/deleteRecord.defs.js +5 -7
  23. package/dist/lexicons/com/atproto/repo/deleteRecord.defs.js.map +1 -1
  24. package/dist/lexicons/com/atproto/repo/getRecord.defs.d.ts.map +1 -1
  25. package/dist/lexicons/com/atproto/repo/getRecord.defs.js +3 -5
  26. package/dist/lexicons/com/atproto/repo/getRecord.defs.js.map +1 -1
  27. package/dist/lexicons/com/atproto/repo/listRecords.defs.d.ts.map +1 -1
  28. package/dist/lexicons/com/atproto/repo/listRecords.defs.js +2 -3
  29. package/dist/lexicons/com/atproto/repo/listRecords.defs.js.map +1 -1
  30. package/dist/lexicons/com/atproto/repo/putRecord.defs.d.ts.map +1 -1
  31. package/dist/lexicons/com/atproto/repo/putRecord.defs.js +5 -7
  32. package/dist/lexicons/com/atproto/repo/putRecord.defs.js.map +1 -1
  33. package/dist/lexicons/com/atproto/repo/uploadBlob.defs.d.ts.map +1 -1
  34. package/dist/lexicons/com/atproto/repo/uploadBlob.defs.js +3 -4
  35. package/dist/lexicons/com/atproto/repo/uploadBlob.defs.js.map +1 -1
  36. package/dist/lexicons/index.d.ts +2 -0
  37. package/dist/lexicons/index.d.ts.map +1 -0
  38. package/dist/lexicons/index.js +9 -0
  39. package/dist/lexicons/index.js.map +1 -0
  40. package/dist/response.d.ts +38 -0
  41. package/dist/response.d.ts.map +1 -0
  42. package/dist/{xrpc-response.js → response.js} +24 -20
  43. package/dist/response.js.map +1 -0
  44. package/dist/types.d.ts +0 -4
  45. package/dist/types.d.ts.map +1 -1
  46. package/dist/types.js +0 -4
  47. package/dist/types.js.map +1 -1
  48. package/dist/xrpc.d.ts +23 -17
  49. package/dist/xrpc.d.ts.map +1 -1
  50. package/dist/xrpc.js +15 -17
  51. package/dist/xrpc.js.map +1 -1
  52. package/package.json +10 -10
  53. package/src/agent.ts +6 -2
  54. package/src/client.ts +72 -66
  55. package/src/{xrpc-error.ts → errors.ts} +59 -48
  56. package/src/index.ts +2 -0
  57. package/src/lexicons/com/atproto/repo/createRecord.defs.ts +22 -28
  58. package/src/lexicons/com/atproto/repo/deleteRecord.defs.ts +18 -24
  59. package/src/lexicons/com/atproto/repo/getRecord.defs.ts +5 -10
  60. package/src/lexicons/com/atproto/repo/listRecords.defs.ts +6 -9
  61. package/src/lexicons/com/atproto/repo/putRecord.defs.ts +23 -29
  62. package/src/lexicons/com/atproto/repo/uploadBlob.defs.ts +4 -7
  63. package/src/lexicons/index.ts +5 -0
  64. package/src/{xrpc-response.ts → response.ts} +45 -30
  65. package/src/types.ts +0 -6
  66. package/src/xrpc.ts +56 -57
  67. package/tsconfig.tests.json +4 -7
  68. package/dist/lexicons.d.ts +0 -2
  69. package/dist/lexicons.d.ts.map +0 -1
  70. package/dist/lexicons.js +0 -6
  71. package/dist/lexicons.js.map +0 -1
  72. package/dist/xrpc-error.d.ts.map +0 -1
  73. package/dist/xrpc-error.js.map +0 -1
  74. package/dist/xrpc-response.d.ts +0 -35
  75. package/dist/xrpc-response.d.ts.map +0 -1
  76. package/dist/xrpc-response.js.map +0 -1
  77. package/src/lexicons.ts +0 -1
package/src/client.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { LexMap, LexValue } from '@atproto/lex-data'
1
+ import { LexError, LexMap, LexValue } from '@atproto/lex-data'
2
2
  import {
3
3
  AtIdentifierString,
4
4
  CidString,
@@ -9,6 +9,7 @@ import {
9
9
  InferMethodParams,
10
10
  InferRecordKey,
11
11
  LexiconRecordKey,
12
+ Main,
12
13
  NsidString,
13
14
  Params,
14
15
  Procedure,
@@ -16,26 +17,34 @@ import {
16
17
  RecordSchema,
17
18
  Restricted,
18
19
  Schema,
20
+ getMain,
19
21
  } from '@atproto/lex-schema'
20
22
  import { Agent, AgentOptions, buildAgent } from './agent.js'
21
- import { com } from './lexicons.js'
22
- import {
23
- BinaryBodyInit,
24
- CallOptions,
25
- Namespace,
26
- Service,
27
- getMain,
28
- } from './types.js'
23
+ import { com } from './lexicons/index.js'
24
+ import { LexRpcResponse, LexRpcResponseBody } from './response.js'
25
+ import { BinaryBodyInit, CallOptions, Service } from './types.js'
29
26
  import { buildAtprotoHeaders } from './util.js'
30
- import { XrpcError } from './xrpc-error.js'
31
- import {
32
- XrpcFailure,
33
- XrpcOptions,
34
- XrpcResponse,
35
- XrpcResponseBody,
36
- xrpc,
37
- xrpcSafe,
38
- } from './xrpc.js'
27
+ import { LexRpcFailure, LexRpcOptions, xrpc, xrpcSafe } from './xrpc.js'
28
+
29
+ export type {
30
+ AtIdentifierString,
31
+ CidString,
32
+ DidString,
33
+ InferMethodInputBody,
34
+ InferMethodOutputBody,
35
+ InferMethodParams,
36
+ InferRecordKey,
37
+ LexMap,
38
+ LexValue,
39
+ LexiconRecordKey,
40
+ NsidString,
41
+ Params,
42
+ Procedure,
43
+ Query,
44
+ RecordSchema,
45
+ Restricted,
46
+ Schema,
47
+ }
39
48
 
40
49
  export type ClientOptions = {
41
50
  labelers?: Iterable<DidString>
@@ -122,14 +131,14 @@ export type ListOutput<T extends RecordSchema> = InferMethodOutputBody<
122
131
  typeof com.atproto.repo.listRecords.main,
123
132
  Uint8Array
124
133
  > & {
125
- records: ListRecord<T>[]
134
+ records: ListRecord<Infer<T>>[]
126
135
  // @NOTE Because the schema uses "type": "unknown" instead of an open union,
127
136
  // we have to use LexMap instead of TypedObject here.
128
137
  invalid: LexMap[]
129
138
  }
130
- export type ListRecord<T extends RecordSchema> =
139
+ export type ListRecord<Value extends LexMap> =
131
140
  com.atproto.repo.listRecords.DefRecord & {
132
- value: Infer<T>
141
+ value: Value
133
142
  }
134
143
 
135
144
  export class Client implements Agent {
@@ -148,10 +157,7 @@ export class Client implements Agent {
148
157
  public readonly labelers: Set<DidString>
149
158
 
150
159
  constructor(agent: Agent | AgentOptions, options: ClientOptions = {}) {
151
- this.agent =
152
- typeof agent === 'object' && 'fetchHandler' in agent
153
- ? agent
154
- : buildAgent(agent)
160
+ this.agent = buildAgent(agent)
155
161
  this.service = options.service
156
162
  this.labelers = new Set(options.labelers)
157
163
  this.headers = new Headers(options.headers)
@@ -167,7 +173,7 @@ export class Client implements Agent {
167
173
  }
168
174
 
169
175
  public assertAuthenticated(): asserts this is { did: DidString } {
170
- if (!this.did) throw new XrpcError('AuthenticationRequired')
176
+ if (!this.did) throw new LexError('AuthenticationRequired')
171
177
  }
172
178
 
173
179
  public setLabelers(labelers: Iterable<DidString> = []) {
@@ -205,37 +211,37 @@ export class Client implements Agent {
205
211
  }
206
212
 
207
213
  /**
208
- * @throws {XrpcFailure<M>} when the request fails or the response is an error
214
+ * @throws {LexRpcFailure<M>} when the request fails or the response is an error
209
215
  */
210
216
  async xrpc<const M extends Query | Procedure>(
211
- ns: NonNullable<unknown> extends XrpcOptions<M>
212
- ? Namespace<M>
217
+ ns: NonNullable<unknown> extends LexRpcOptions<M>
218
+ ? Main<M>
213
219
  : Restricted<'This XRPC method requires an "options" argument'>,
214
- ): Promise<XrpcResponse<M>>
220
+ ): Promise<LexRpcResponse<M>>
215
221
  async xrpc<const M extends Query | Procedure>(
216
- ns: Namespace<M>,
217
- options: XrpcOptions<M>,
218
- ): Promise<XrpcResponse<M>>
222
+ ns: Main<M>,
223
+ options: LexRpcOptions<M>,
224
+ ): Promise<LexRpcResponse<M>>
219
225
  async xrpc<const M extends Query | Procedure>(
220
- ns: Namespace<M>,
221
- options: XrpcOptions<M> = {} as XrpcOptions<M>,
222
- ): Promise<XrpcResponse<M>> {
226
+ ns: Main<M>,
227
+ options: LexRpcOptions<M> = {} as LexRpcOptions<M>,
228
+ ): Promise<LexRpcResponse<M>> {
223
229
  return xrpc(this, ns, options)
224
230
  }
225
231
 
226
232
  async xrpcSafe<const M extends Query | Procedure>(
227
- ns: NonNullable<unknown> extends XrpcOptions<M>
228
- ? Namespace<M>
233
+ ns: NonNullable<unknown> extends LexRpcOptions<M>
234
+ ? Main<M>
229
235
  : Restricted<'This XRPC method requires an "options" argument'>,
230
- ): Promise<XrpcResponse<M> | XrpcFailure<M>>
236
+ ): Promise<LexRpcResponse<M> | LexRpcFailure<M>>
231
237
  async xrpcSafe<const M extends Query | Procedure>(
232
- ns: Namespace<M>,
233
- options: XrpcOptions<M>,
234
- ): Promise<XrpcResponse<M> | XrpcFailure<M>>
238
+ ns: Main<M>,
239
+ options: LexRpcOptions<M>,
240
+ ): Promise<LexRpcResponse<M> | LexRpcFailure<M>>
235
241
  async xrpcSafe<const M extends Query | Procedure>(
236
- ns: Namespace<M>,
237
- options: XrpcOptions<M> = {} as XrpcOptions<M>,
238
- ): Promise<XrpcResponse<M> | XrpcFailure<M>> {
242
+ ns: Main<M>,
243
+ options: LexRpcOptions<M> = {} as LexRpcOptions<M>,
244
+ ): Promise<LexRpcResponse<M> | LexRpcFailure<M>> {
239
245
  return xrpcSafe(this, ns, options)
240
246
  }
241
247
 
@@ -343,16 +349,16 @@ export class Client implements Agent {
343
349
 
344
350
  public async call<const T extends Query>(
345
351
  ns: NonNullable<unknown> extends InferMethodParams<T>
346
- ? Namespace<T>
352
+ ? Main<T>
347
353
  : Restricted<'This query type requires a "params" argument'>,
348
- ): Promise<XrpcResponseBody<T>>
354
+ ): Promise<LexRpcResponseBody<T>>
349
355
  public async call<const T extends Action>(
350
356
  ns: void extends InferActionInput<T>
351
- ? Namespace<T>
357
+ ? Main<T>
352
358
  : Restricted<'This action type requires an "input" argument'>,
353
359
  ): Promise<InferActionOutput<T>>
354
360
  public async call<const T extends Action | Procedure | Query>(
355
- ns: Namespace<T>,
361
+ ns: Main<T>,
356
362
  arg: T extends Action
357
363
  ? InferActionInput<T>
358
364
  : T extends Procedure
@@ -365,13 +371,13 @@ export class Client implements Agent {
365
371
  T extends Action
366
372
  ? InferActionOutput<T>
367
373
  : T extends Procedure
368
- ? XrpcResponseBody<T>
374
+ ? LexRpcResponseBody<T>
369
375
  : T extends Query
370
- ? XrpcResponseBody<T>
376
+ ? LexRpcResponseBody<T>
371
377
  : never
372
378
  >
373
379
  public async call(
374
- ns: Namespace<Action> | Namespace<Procedure> | Namespace<Query>,
380
+ ns: Main<Action> | Main<Procedure> | Main<Query>,
375
381
  arg?: LexValue | Params,
376
382
  options: CallOptions = {},
377
383
  ): Promise<unknown> {
@@ -394,17 +400,17 @@ export class Client implements Agent {
394
400
 
395
401
  public async create<const T extends RecordSchema>(
396
402
  ns: NonNullable<unknown> extends CreateOptions<T>
397
- ? Namespace<T>
403
+ ? Main<T>
398
404
  : Restricted<'This record type requires an "options" argument'>,
399
405
  input: Omit<Infer<T>, '$type'>,
400
406
  ): Promise<CreateOutput>
401
407
  public async create<const T extends RecordSchema>(
402
- ns: Namespace<T>,
408
+ ns: Main<T>,
403
409
  input: Omit<Infer<T>, '$type'>,
404
410
  options: CreateOptions<T>,
405
411
  ): Promise<CreateOutput>
406
412
  public async create<const T extends RecordSchema>(
407
- ns: Namespace<T>,
413
+ ns: Main<T>,
408
414
  input: Omit<Infer<T>, '$type'>,
409
415
  options: CreateOptions<T> = {} as CreateOptions<T>,
410
416
  ): Promise<CreateOutput> {
@@ -419,15 +425,15 @@ export class Client implements Agent {
419
425
 
420
426
  public async delete<const T extends RecordSchema>(
421
427
  ns: NonNullable<unknown> extends DeleteOptions<T>
422
- ? Namespace<T>
428
+ ? Main<T>
423
429
  : Restricted<'This record type requires an "options" argument'>,
424
430
  ): Promise<DeleteOutput>
425
431
  public async delete<const T extends RecordSchema>(
426
- ns: Namespace<T>,
432
+ ns: Main<T>,
427
433
  options?: DeleteOptions<T>,
428
434
  ): Promise<DeleteOutput>
429
435
  public async delete<const T extends RecordSchema>(
430
- ns: Namespace<T>,
436
+ ns: Main<T>,
431
437
  options: DeleteOptions<T> = {} as DeleteOptions<T>,
432
438
  ): Promise<DeleteOutput> {
433
439
  const schema = getMain(ns)
@@ -440,15 +446,15 @@ export class Client implements Agent {
440
446
 
441
447
  public async get<const T extends RecordSchema>(
442
448
  ns: T['key'] extends `literal:${string}`
443
- ? Namespace<T>
449
+ ? Main<T>
444
450
  : Restricted<'This record type requires an "options" argument'>,
445
451
  ): Promise<GetOutput<T>>
446
452
  public async get<const T extends RecordSchema>(
447
- ns: Namespace<T>,
453
+ ns: Main<T>,
448
454
  options?: GetOptions<T>,
449
455
  ): Promise<GetOutput<T>>
450
456
  public async get<const T extends RecordSchema>(
451
- ns: Namespace<T>,
457
+ ns: Main<T>,
452
458
  options: GetOptions<T> = {} as GetOptions<T>,
453
459
  ): Promise<GetOutput<T>> {
454
460
  const schema = getMain(ns)
@@ -462,17 +468,17 @@ export class Client implements Agent {
462
468
 
463
469
  public async put<const T extends RecordSchema>(
464
470
  ns: NonNullable<unknown> extends PutOptions<T>
465
- ? Namespace<T>
471
+ ? Main<T>
466
472
  : Restricted<'This record type requires an "options" argument'>,
467
473
  input: Omit<Infer<T>, '$type'>,
468
474
  ): Promise<PutOutput>
469
475
  public async put<const T extends RecordSchema>(
470
- ns: Namespace<T>,
476
+ ns: Main<T>,
471
477
  input: Omit<Infer<T>, '$type'>,
472
478
  options: PutOptions<T>,
473
479
  ): Promise<PutOutput>
474
480
  public async put<const T extends RecordSchema>(
475
- ns: Namespace<T>,
481
+ ns: Main<T>,
476
482
  input: Omit<Infer<T>, '$type'>,
477
483
  options: PutOptions<T> = {} as PutOptions<T>,
478
484
  ): Promise<PutOutput> {
@@ -485,13 +491,13 @@ export class Client implements Agent {
485
491
  }
486
492
 
487
493
  async list<const T extends RecordSchema>(
488
- ns: Namespace<T>,
494
+ ns: Main<T>,
489
495
  options?: ListOptions,
490
496
  ): Promise<ListOutput<T>> {
491
497
  const schema = getMain(ns)
492
498
  const { body } = await this.listRecords(schema.$type, options)
493
499
 
494
- const records: ListRecord<T>[] = []
500
+ const records: ListRecord<Infer<T>>[] = []
495
501
  const invalid: LexMap[] = []
496
502
 
497
503
  for (const record of body.records) {
@@ -1,37 +1,26 @@
1
+ import { LexError, LexErrorCode, LexErrorData } from '@atproto/lex-data'
1
2
  import { l } from '@atproto/lex-schema'
2
3
  import { Payload } from './util.js'
3
4
 
4
- export type XrpcErrorCode = string
5
- export const xrpcErrorCodeSchema: l.Schema<XrpcErrorCode> = l.string({
6
- minLength: 1,
7
- })
5
+ export type LexRpcErrorPayload<N extends LexErrorCode = LexErrorCode> = Payload<
6
+ LexErrorData<N>,
7
+ 'application/json'
8
+ >
8
9
 
9
- export class XrpcError<N extends XrpcErrorCode = XrpcErrorCode> extends Error {
10
- name = 'XrpcError'
10
+ export class LexRpcError<
11
+ N extends LexErrorCode = LexErrorCode,
12
+ > extends LexError<N> {
13
+ name = 'LexRpcError'
11
14
 
12
15
  constructor(
13
- readonly error: N,
14
- message: string = `An ${error} XRPC error occurred.`,
16
+ error: N,
17
+ message: string = `${error} Lexicon RPC error`,
15
18
  options?: ErrorOptions,
16
19
  ) {
17
- super(message, options)
20
+ super(error, message, options)
18
21
  }
19
22
  }
20
23
 
21
- export type XrpcErrorBody<N extends XrpcErrorCode = XrpcErrorCode> = {
22
- error: N
23
- message?: string
24
- }
25
- export type XrpcErrorPayload<N extends XrpcErrorCode = XrpcErrorCode> = {
26
- encoding: 'application/json'
27
- body: XrpcErrorBody<N>
28
- }
29
-
30
- const xrpcErrorBodySchema: l.Schema<XrpcErrorBody> = l.object({
31
- error: xrpcErrorCodeSchema,
32
- message: l.optional(l.string()),
33
- })
34
-
35
24
  /**
36
25
  * All unsuccessful responses should follow a standard error response
37
26
  * schema. The Content-Type should be application/json, and the payload
@@ -44,20 +33,20 @@ const xrpcErrorBodySchema: l.Schema<XrpcErrorBody> = l.object({
44
33
  *
45
34
  * This function checks whether a given payload matches this schema.
46
35
  */
47
- export function isXrpcErrorPayload(
36
+ export function isLexRpcErrorPayload(
48
37
  payload: Payload | null,
49
- ): payload is XrpcErrorPayload {
38
+ ): payload is LexRpcErrorPayload {
50
39
  return (
51
40
  payload !== null &&
52
41
  payload.encoding === 'application/json' &&
53
- xrpcErrorBodySchema.matches(payload.body)
42
+ l.lexErrorData.matches(payload.body)
54
43
  )
55
44
  }
56
45
 
57
46
  /**
58
47
  * Interface representing a failed XRPC request result.
59
48
  */
60
- type XrpcFailureResult<N extends XrpcErrorCode, E> = l.ResultFailure<E> & {
49
+ type LexRpcFailureResult<N extends LexErrorCode, E> = l.ResultFailure<E> & {
61
50
  readonly error: N
62
51
  shouldRetry(): boolean
63
52
  matchesSchema(): boolean
@@ -67,20 +56,20 @@ type XrpcFailureResult<N extends XrpcErrorCode, E> = l.ResultFailure<E> & {
67
56
  * Class used to represent an HTTP request that resulted in an XRPC method error
68
57
  * That is, a non-2xx response with a valid XRPC error payload.
69
58
  */
70
- export class XrpcResponseError<
59
+ export class LexRpcResponseError<
71
60
  M extends l.Procedure | l.Query = l.Procedure | l.Query,
72
- N extends XrpcErrorCode = XrpcErrorCode,
61
+ N extends LexErrorCode = LexErrorCode,
73
62
  >
74
- extends XrpcError<N>
75
- implements XrpcFailureResult<N, XrpcResponseError<M, N>>
63
+ extends LexRpcError<N>
64
+ implements LexRpcFailureResult<N, LexRpcResponseError<M, N>>
76
65
  {
77
- name = 'XrpcResponseError' as const
66
+ name = 'LexRpcResponseError'
78
67
 
79
68
  constructor(
80
69
  readonly method: M,
81
70
  readonly status: number,
82
71
  readonly headers: Headers,
83
- readonly payload: XrpcErrorPayload<N>,
72
+ readonly payload: LexRpcErrorPayload<N>,
84
73
  options?: ErrorOptions,
85
74
  ) {
86
75
  const { error, message } = payload.body
@@ -93,14 +82,14 @@ export class XrpcResponseError<
93
82
  return this as this
94
83
  }
95
84
 
96
- get body(): XrpcErrorBody {
85
+ get body(): LexErrorData {
97
86
  return this.payload.body
98
87
  }
99
88
 
100
89
  matchesSchema(): this is M extends {
101
90
  errors: readonly (infer E extends string)[]
102
91
  }
103
- ? XrpcResponseError<M, E>
92
+ ? LexRpcResponseError<M, E>
104
93
  : never {
105
94
  return this.method.errors?.includes(this.error) ?? false
106
95
  }
@@ -111,16 +100,29 @@ export class XrpcResponseError<
111
100
 
112
101
  return true
113
102
  }
103
+
104
+ toJSON() {
105
+ return this.payload.body
106
+ }
107
+
108
+ toResponse(): Response {
109
+ const { status, headers } = this
110
+ return Response.json(this.toJSON(), { status, headers })
111
+ }
114
112
  }
115
113
 
116
114
  /**
117
115
  * This class represents an invalid XRPC response from the server.
118
116
  */
119
- export class XrpcInvalidResponseError
120
- extends XrpcError<'UpstreamFailure'>
121
- implements XrpcFailureResult<'UpstreamFailure', XrpcInvalidResponseError>
117
+ export class LexRpcUpstreamError<
118
+ N extends 'InvalidResponse' | 'UpstreamFailure' =
119
+ | 'InvalidResponse'
120
+ | 'UpstreamFailure',
121
+ >
122
+ extends LexRpcError<N>
123
+ implements LexRpcFailureResult<N, LexRpcUpstreamError<N>>
122
124
  {
123
- name = 'XrpcInvalidResponseError' as const
125
+ name = 'LexRpcUpstreamError' as const
124
126
 
125
127
  // For debugging purposes, we keep the response details here
126
128
  readonly response: {
@@ -130,12 +132,13 @@ export class XrpcInvalidResponseError
130
132
  }
131
133
 
132
134
  constructor(
135
+ error: N,
133
136
  message: string,
134
137
  response: { status: number; headers: Headers },
135
138
  payload: Payload | null,
136
139
  options?: ErrorOptions,
137
140
  ) {
138
- super('UpstreamFailure', message, { cause: options?.cause })
141
+ super(error, message, { cause: options?.cause })
139
142
  this.response = {
140
143
  status: response.status,
141
144
  headers: response.headers,
@@ -157,13 +160,17 @@ export class XrpcInvalidResponseError
157
160
  // Do not retry client errors
158
161
  return this.response.status >= 500
159
162
  }
163
+
164
+ toResponse(): Response {
165
+ return Response.json(this.toJSON(), { status: 502 })
166
+ }
160
167
  }
161
168
 
162
- export class XrpcUnexpectedError
163
- extends XrpcError<'InternalServerError'>
164
- implements XrpcFailureResult<'InternalServerError', unknown>
169
+ export class LexRpcUnexpectedError
170
+ extends LexRpcError<'InternalServerError'>
171
+ implements LexRpcFailureResult<'InternalServerError', unknown>
165
172
  {
166
- name = 'XrpcUnexpectedError' as const
173
+ name = 'LexRpcUnexpectedError' as const
167
174
 
168
175
  protected constructor(message: string, options: Required<ErrorOptions>) {
169
176
  super('InternalServerError', message, options)
@@ -183,13 +190,17 @@ export class XrpcUnexpectedError
183
190
  return true
184
191
  }
185
192
 
193
+ toResponse(): Response {
194
+ return Response.json(this.toJSON(), { status: 500 })
195
+ }
196
+
186
197
  static from(
187
198
  cause: unknown,
188
- message: string = cause instanceof XrpcError
199
+ message: string = cause instanceof LexError
189
200
  ? cause.message
190
201
  : 'XRPC request failed',
191
- ): XrpcUnexpectedError {
192
- if (cause instanceof XrpcUnexpectedError) return cause
193
- return new XrpcUnexpectedError(message, { cause })
202
+ ): LexRpcUnexpectedError {
203
+ if (cause instanceof LexRpcUnexpectedError) return cause
204
+ return new LexRpcUnexpectedError(message, { cause })
194
205
  }
195
206
  }
package/src/index.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export * from './agent.js'
2
2
  export * from './client.js'
3
+ export * from './errors.js'
4
+ export * from './response.js'
3
5
  export * from './types.js'
4
6
  export * from './xrpc.js'
@@ -14,35 +14,29 @@ const main =
14
14
  /*#__PURE__*/
15
15
  l.procedure(
16
16
  $nsid,
17
- /*#__PURE__*/ l.params({}),
18
- /*#__PURE__*/ l.payload(
19
- 'application/json',
20
- /*#__PURE__*/ l.object({
21
- repo: /*#__PURE__*/ l.string({ format: 'at-identifier' }),
22
- collection: /*#__PURE__*/ l.string({ format: 'nsid' }),
23
- rkey: /*#__PURE__*/ l.optional(
24
- /*#__PURE__*/ l.string({ format: 'record-key', maxLength: 512 }),
17
+ /*#__PURE__*/ l.params(),
18
+ /*#__PURE__*/ l.jsonPayload({
19
+ repo: /*#__PURE__*/ l.string({ format: 'at-identifier' }),
20
+ collection: /*#__PURE__*/ l.string({ format: 'nsid' }),
21
+ rkey: /*#__PURE__*/ l.optional(
22
+ /*#__PURE__*/ l.string({ format: 'record-key', maxLength: 512 }),
23
+ ),
24
+ validate: /*#__PURE__*/ l.optional(/*#__PURE__*/ l.boolean()),
25
+ record: /*#__PURE__*/ l.unknownObject(),
26
+ swapCommit: /*#__PURE__*/ l.optional(
27
+ /*#__PURE__*/ l.string({ format: 'cid' }),
28
+ ),
29
+ }),
30
+ /*#__PURE__*/ l.jsonPayload({
31
+ uri: /*#__PURE__*/ l.string({ format: 'at-uri' }),
32
+ cid: /*#__PURE__*/ l.string({ format: 'cid' }),
33
+ commit: /*#__PURE__*/ l.optional(
34
+ /*#__PURE__*/ l.ref<RepoDefs.CommitMeta>(
35
+ (() => RepoDefs.commitMeta) as any,
25
36
  ),
26
- validate: /*#__PURE__*/ l.optional(/*#__PURE__*/ l.boolean()),
27
- record: /*#__PURE__*/ l.unknownObject(),
28
- swapCommit: /*#__PURE__*/ l.optional(
29
- /*#__PURE__*/ l.string({ format: 'cid' }),
30
- ),
31
- }),
32
- ),
33
- /*#__PURE__*/ l.payload(
34
- 'application/json',
35
- /*#__PURE__*/ l.object({
36
- uri: /*#__PURE__*/ l.string({ format: 'at-uri' }),
37
- cid: /*#__PURE__*/ l.string({ format: 'cid' }),
38
- commit: /*#__PURE__*/ l.optional(
39
- /*#__PURE__*/ l.ref<RepoDefs.CommitMeta>(
40
- (() => RepoDefs.commitMeta) as any,
41
- ),
42
- ),
43
- validationStatus: /*#__PURE__*/ l.optional(/*#__PURE__*/ l.string()),
44
- }),
45
- ),
37
+ ),
38
+ validationStatus: /*#__PURE__*/ l.optional(/*#__PURE__*/ l.string()),
39
+ }),
46
40
  ['InvalidSwap'],
47
41
  )
48
42
  export { main }
@@ -14,31 +14,25 @@ const main =
14
14
  /*#__PURE__*/
15
15
  l.procedure(
16
16
  $nsid,
17
- /*#__PURE__*/ l.params({}),
18
- /*#__PURE__*/ l.payload(
19
- 'application/json',
20
- /*#__PURE__*/ l.object({
21
- repo: /*#__PURE__*/ l.string({ format: 'at-identifier' }),
22
- collection: /*#__PURE__*/ l.string({ format: 'nsid' }),
23
- rkey: /*#__PURE__*/ l.string({ format: 'record-key' }),
24
- swapRecord: /*#__PURE__*/ l.optional(
25
- /*#__PURE__*/ l.string({ format: 'cid' }),
17
+ /*#__PURE__*/ l.params(),
18
+ /*#__PURE__*/ l.jsonPayload({
19
+ repo: /*#__PURE__*/ l.string({ format: 'at-identifier' }),
20
+ collection: /*#__PURE__*/ l.string({ format: 'nsid' }),
21
+ rkey: /*#__PURE__*/ l.string({ format: 'record-key' }),
22
+ swapRecord: /*#__PURE__*/ l.optional(
23
+ /*#__PURE__*/ l.string({ format: 'cid' }),
24
+ ),
25
+ swapCommit: /*#__PURE__*/ l.optional(
26
+ /*#__PURE__*/ l.string({ format: 'cid' }),
27
+ ),
28
+ }),
29
+ /*#__PURE__*/ l.jsonPayload({
30
+ commit: /*#__PURE__*/ l.optional(
31
+ /*#__PURE__*/ l.ref<RepoDefs.CommitMeta>(
32
+ (() => RepoDefs.commitMeta) as any,
26
33
  ),
27
- swapCommit: /*#__PURE__*/ l.optional(
28
- /*#__PURE__*/ l.string({ format: 'cid' }),
29
- ),
30
- }),
31
- ),
32
- /*#__PURE__*/ l.payload(
33
- 'application/json',
34
- /*#__PURE__*/ l.object({
35
- commit: /*#__PURE__*/ l.optional(
36
- /*#__PURE__*/ l.ref<RepoDefs.CommitMeta>(
37
- (() => RepoDefs.commitMeta) as any,
38
- ),
39
- ),
40
- }),
41
- ),
34
+ ),
35
+ }),
42
36
  ['InvalidSwap'],
43
37
  )
44
38
  export { main }
@@ -19,16 +19,11 @@ const main =
19
19
  rkey: /*#__PURE__*/ l.string({ format: 'record-key' }),
20
20
  cid: /*#__PURE__*/ l.optional(/*#__PURE__*/ l.string({ format: 'cid' })),
21
21
  }),
22
- /*#__PURE__*/ l.payload(
23
- 'application/json',
24
- /*#__PURE__*/ l.object({
25
- uri: /*#__PURE__*/ l.string({ format: 'at-uri' }),
26
- cid: /*#__PURE__*/ l.optional(
27
- /*#__PURE__*/ l.string({ format: 'cid' }),
28
- ),
29
- value: /*#__PURE__*/ l.unknownObject(),
30
- }),
31
- ),
22
+ /*#__PURE__*/ l.jsonPayload({
23
+ uri: /*#__PURE__*/ l.string({ format: 'at-uri' }),
24
+ cid: /*#__PURE__*/ l.optional(/*#__PURE__*/ l.string({ format: 'cid' })),
25
+ value: /*#__PURE__*/ l.unknownObject(),
26
+ }),
32
27
  ['RecordNotFound'],
33
28
  )
34
29
  export { main }
@@ -22,15 +22,12 @@ const main =
22
22
  cursor: /*#__PURE__*/ l.optional(/*#__PURE__*/ l.string()),
23
23
  reverse: /*#__PURE__*/ l.optional(/*#__PURE__*/ l.boolean()),
24
24
  }),
25
- /*#__PURE__*/ l.payload(
26
- 'application/json',
27
- /*#__PURE__*/ l.object({
28
- cursor: /*#__PURE__*/ l.optional(/*#__PURE__*/ l.string()),
29
- records: /*#__PURE__*/ l.array(
30
- /*#__PURE__*/ l.ref<Def$0>((() => def$0) as any),
31
- ),
32
- }),
33
- ),
25
+ /*#__PURE__*/ l.jsonPayload({
26
+ cursor: /*#__PURE__*/ l.optional(/*#__PURE__*/ l.string()),
27
+ records: /*#__PURE__*/ l.array(
28
+ /*#__PURE__*/ l.ref<Def$0>((() => def$0) as any),
29
+ ),
30
+ }),
34
31
  )
35
32
  export { main }
36
33