@atproto/lex-server 0.1.3 → 0.1.5

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/src/errors.ts DELETED
@@ -1,173 +0,0 @@
1
- import { XrpcError } from '@atproto/lex-client'
2
- import { LexError, LexErrorCode, LexErrorData } from '@atproto/lex-data'
3
- import { LexValidationError } from '@atproto/lex-schema'
4
- import {
5
- WWWAuthenticate,
6
- formatWWWAuthenticateHeader,
7
- } from './lib/www-authenticate.js'
8
-
9
- export { LexError }
10
- export type { LexErrorCode, LexErrorData, WWWAuthenticate }
11
-
12
- /**
13
- * Base error class for representing errors that should be converted to XRPC
14
- * error responses.
15
- */
16
- export class LexServerError<
17
- N extends LexErrorCode = LexErrorCode,
18
- > extends LexError<N> {
19
- name = 'LexServerError'
20
-
21
- readonly headers?: Headers
22
-
23
- constructor(
24
- readonly status: number,
25
- readonly body: LexErrorData<N>,
26
- headers?: HeadersInit,
27
- options?: ErrorOptions,
28
- ) {
29
- super(body.error, body.message, options)
30
- this.headers = headers ? new Headers(headers) : undefined
31
- }
32
-
33
- override toJSON(): LexErrorData<N> {
34
- return this.body
35
- }
36
-
37
- public toResponse(): Response {
38
- const { status, headers } = this
39
- // @NOTE using this.toJSON() instead of this.body to allow overrides in subclasses
40
- return Response.json(this.toJSON(), { status, headers })
41
- }
42
-
43
- static from(cause: unknown): LexServerError {
44
- if (cause instanceof LexServerError) {
45
- return cause
46
- }
47
-
48
- // Convert @atproto/lex-client errors to downstream LexServerError
49
- if (cause instanceof XrpcError) {
50
- const { status, body, headers } = cause.toDownstreamError()
51
- return new LexServerError(status, body, headers, { cause })
52
- }
53
-
54
- // Convert @atproto/lex-schema validation errors to 400 Bad Request
55
- if (cause instanceof LexValidationError) {
56
- return new LexServerError(400, cause.toJSON(), undefined, {
57
- cause,
58
- })
59
- }
60
-
61
- // Any other error is treated as a generic 500 Internal Server Error
62
- if (cause instanceof LexError) {
63
- return new LexServerError(500, cause.toJSON(), undefined, {
64
- cause,
65
- })
66
- }
67
-
68
- return new LexServerError(
69
- 500,
70
- { error: 'InternalServerError', message: 'An internal error occurred' },
71
- undefined,
72
- { cause },
73
- )
74
- }
75
- }
76
-
77
- /**
78
- * Error class for authentication failures in XRPC server handlers.
79
- *
80
- * Extends {@link LexError} to include WWW-Authenticate header support,
81
- * which is required by HTTP authentication standards (RFC 7235).
82
- * The error automatically generates the appropriate 401 response with
83
- * the WWW-Authenticate header when converted to a Response.
84
- *
85
- * @typeParam N - The Lexicon error code type
86
- *
87
- * @example Throwing an auth error
88
- * ```typescript
89
- * import { LexServerAuthError } from '@atproto/lex-server'
90
- *
91
- * throw new LexServerAuthError(
92
- * 'AuthenticationRequired',
93
- * 'Invalid or expired token',
94
- * { Bearer: { error: 'InvalidToken', realm: 'api.example.com' } }
95
- * )
96
- * ```
97
- */
98
- export class LexServerAuthError<
99
- N extends LexErrorCode = LexErrorCode,
100
- > extends LexServerError<N> {
101
- name = 'LexServerAuthError'
102
-
103
- /**
104
- * Creates a new authentication error.
105
- *
106
- * @param error - The Lexicon error code (e.g., 'AuthenticationRequired')
107
- * @param message - Human-readable error message
108
- * @param wwwAuthenticate - WWW-Authenticate header parameters
109
- * @param options - Standard Error options including `cause`
110
- */
111
- constructor(
112
- error: N,
113
- message: string,
114
- readonly wwwAuthenticate: WWWAuthenticate = {},
115
- options?: ErrorOptions,
116
- ) {
117
- const headers = Object.keys(wwwAuthenticate).length
118
- ? new Headers({
119
- 'WWW-Authenticate': formatWWWAuthenticateHeader(wwwAuthenticate),
120
- 'Access-Control-Expose-Headers': 'WWW-Authenticate', // CORS
121
- })
122
- : undefined
123
- super(401, { error, message }, headers, options)
124
- }
125
-
126
- /**
127
- * Creates a LexServerAuthError from an existing LexError.
128
- *
129
- * If the input is already a LexServerAuthError, returns it unchanged.
130
- * Otherwise, wraps the error with the provided WWW-Authenticate parameters.
131
- *
132
- * @param cause - The original LexError to wrap
133
- * @param wwwAuthenticate - WWW-Authenticate header parameters
134
- * @returns A LexServerAuthError instance
135
- *
136
- * @example
137
- * ```typescript
138
- * function authenticate(token: string): Promise<User> {
139
- * try {
140
- * return await validateToken(token)
141
- * } catch (cause) {
142
- * throw LexServerAuthError.from(cause, {
143
- * Bearer: { error: 'InvalidToken' }
144
- * })
145
- * }
146
- * }
147
- * ```
148
- */
149
- static from(
150
- cause: unknown,
151
- wwwAuthenticate?: WWWAuthenticate,
152
- ): LexServerAuthError {
153
- if (cause instanceof LexServerAuthError) {
154
- return cause
155
- }
156
-
157
- if (cause instanceof LexError) {
158
- return new LexServerAuthError(
159
- cause.error,
160
- cause.message,
161
- wwwAuthenticate,
162
- { cause },
163
- )
164
- }
165
-
166
- return new LexServerAuthError(
167
- 'AuthenticationRequired',
168
- 'Authentication failed',
169
- wwwAuthenticate,
170
- { cause },
171
- )
172
- }
173
- }
package/src/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from './errors.js'
2
- export * from './lex-router.js'
3
- export * from './service-auth.js'