@hypercerts-org/sdk-core 0.2.0-beta.0

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 (83) hide show
  1. package/.turbo/turbo-build.log +328 -0
  2. package/.turbo/turbo-test.log +118 -0
  3. package/CHANGELOG.md +16 -0
  4. package/LICENSE +21 -0
  5. package/README.md +100 -0
  6. package/dist/errors.cjs +260 -0
  7. package/dist/errors.cjs.map +1 -0
  8. package/dist/errors.d.ts +233 -0
  9. package/dist/errors.mjs +253 -0
  10. package/dist/errors.mjs.map +1 -0
  11. package/dist/index.cjs +4531 -0
  12. package/dist/index.cjs.map +1 -0
  13. package/dist/index.d.ts +3430 -0
  14. package/dist/index.mjs +4448 -0
  15. package/dist/index.mjs.map +1 -0
  16. package/dist/lexicons.cjs +420 -0
  17. package/dist/lexicons.cjs.map +1 -0
  18. package/dist/lexicons.d.ts +227 -0
  19. package/dist/lexicons.mjs +410 -0
  20. package/dist/lexicons.mjs.map +1 -0
  21. package/dist/storage.cjs +270 -0
  22. package/dist/storage.cjs.map +1 -0
  23. package/dist/storage.d.ts +474 -0
  24. package/dist/storage.mjs +267 -0
  25. package/dist/storage.mjs.map +1 -0
  26. package/dist/testing.cjs +415 -0
  27. package/dist/testing.cjs.map +1 -0
  28. package/dist/testing.d.ts +928 -0
  29. package/dist/testing.mjs +410 -0
  30. package/dist/testing.mjs.map +1 -0
  31. package/dist/types.cjs +220 -0
  32. package/dist/types.cjs.map +1 -0
  33. package/dist/types.d.ts +2118 -0
  34. package/dist/types.mjs +212 -0
  35. package/dist/types.mjs.map +1 -0
  36. package/eslint.config.mjs +22 -0
  37. package/package.json +90 -0
  38. package/rollup.config.js +75 -0
  39. package/src/auth/OAuthClient.ts +497 -0
  40. package/src/core/SDK.ts +410 -0
  41. package/src/core/config.ts +243 -0
  42. package/src/core/errors.ts +257 -0
  43. package/src/core/interfaces.ts +324 -0
  44. package/src/core/types.ts +281 -0
  45. package/src/errors.ts +57 -0
  46. package/src/index.ts +107 -0
  47. package/src/lexicons.ts +64 -0
  48. package/src/repository/BlobOperationsImpl.ts +199 -0
  49. package/src/repository/CollaboratorOperationsImpl.ts +288 -0
  50. package/src/repository/HypercertOperationsImpl.ts +1146 -0
  51. package/src/repository/LexiconRegistry.ts +332 -0
  52. package/src/repository/OrganizationOperationsImpl.ts +234 -0
  53. package/src/repository/ProfileOperationsImpl.ts +281 -0
  54. package/src/repository/RecordOperationsImpl.ts +340 -0
  55. package/src/repository/Repository.ts +482 -0
  56. package/src/repository/interfaces.ts +868 -0
  57. package/src/repository/types.ts +111 -0
  58. package/src/services/hypercerts/types.ts +87 -0
  59. package/src/storage/InMemorySessionStore.ts +127 -0
  60. package/src/storage/InMemoryStateStore.ts +146 -0
  61. package/src/storage.ts +63 -0
  62. package/src/testing/index.ts +67 -0
  63. package/src/testing/mocks.ts +142 -0
  64. package/src/testing/stores.ts +285 -0
  65. package/src/testing.ts +64 -0
  66. package/src/types.ts +86 -0
  67. package/tests/auth/OAuthClient.test.ts +164 -0
  68. package/tests/core/SDK.test.ts +176 -0
  69. package/tests/core/errors.test.ts +81 -0
  70. package/tests/repository/BlobOperationsImpl.test.ts +154 -0
  71. package/tests/repository/CollaboratorOperationsImpl.test.ts +323 -0
  72. package/tests/repository/HypercertOperationsImpl.test.ts +652 -0
  73. package/tests/repository/LexiconRegistry.test.ts +192 -0
  74. package/tests/repository/OrganizationOperationsImpl.test.ts +242 -0
  75. package/tests/repository/ProfileOperationsImpl.test.ts +254 -0
  76. package/tests/repository/RecordOperationsImpl.test.ts +375 -0
  77. package/tests/repository/Repository.test.ts +149 -0
  78. package/tests/utils/fixtures.ts +117 -0
  79. package/tests/utils/mocks.ts +109 -0
  80. package/tests/utils/repository-fixtures.ts +78 -0
  81. package/tsconfig.json +11 -0
  82. package/tsconfig.tsbuildinfo +1 -0
  83. package/vitest.config.ts +30 -0
@@ -0,0 +1,410 @@
1
+ import { Lexicons } from '@atproto/lexicon';
2
+ export { HYPERCERT_COLLECTIONS, HYPERCERT_LEXICONS } from '@hypercerts-org/lexicon';
3
+
4
+ /**
5
+ * Base error class for all SDK errors.
6
+ *
7
+ * All errors thrown by the Hypercerts SDK extend this class, making it easy
8
+ * to catch and handle SDK-specific errors.
9
+ *
10
+ * @example Catching all SDK errors
11
+ * ```typescript
12
+ * try {
13
+ * await sdk.authorize("user.bsky.social");
14
+ * } catch (error) {
15
+ * if (error instanceof ATProtoSDKError) {
16
+ * console.error(`SDK Error [${error.code}]: ${error.message}`);
17
+ * console.error(`HTTP Status: ${error.status}`);
18
+ * }
19
+ * }
20
+ * ```
21
+ *
22
+ * @example Checking error codes
23
+ * ```typescript
24
+ * try {
25
+ * await repo.records.get(collection, rkey);
26
+ * } catch (error) {
27
+ * if (error instanceof ATProtoSDKError) {
28
+ * switch (error.code) {
29
+ * case "AUTHENTICATION_ERROR":
30
+ * // Redirect to login
31
+ * break;
32
+ * case "VALIDATION_ERROR":
33
+ * // Show form errors
34
+ * break;
35
+ * case "NETWORK_ERROR":
36
+ * // Retry or show offline message
37
+ * break;
38
+ * }
39
+ * }
40
+ * }
41
+ * ```
42
+ */
43
+ class ATProtoSDKError extends Error {
44
+ /**
45
+ * Creates a new SDK error.
46
+ *
47
+ * @param message - Human-readable error description
48
+ * @param code - Machine-readable error code for programmatic handling
49
+ * @param status - HTTP status code associated with this error type
50
+ * @param cause - The underlying error that caused this error, if any
51
+ */
52
+ constructor(message, code, status, cause) {
53
+ super(message);
54
+ this.code = code;
55
+ this.status = status;
56
+ this.cause = cause;
57
+ this.name = "ATProtoSDKError";
58
+ Error.captureStackTrace?.(this, this.constructor);
59
+ }
60
+ }
61
+ /**
62
+ * Error thrown when input validation fails.
63
+ *
64
+ * This error indicates that provided data doesn't meet the required format
65
+ * or constraints. Common causes:
66
+ * - Missing required fields
67
+ * - Invalid URL formats
68
+ * - Invalid DID format
69
+ * - Schema validation failures for records
70
+ * - Invalid configuration values
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * try {
75
+ * await sdk.authorize(""); // Empty identifier
76
+ * } catch (error) {
77
+ * if (error instanceof ValidationError) {
78
+ * console.error("Invalid input:", error.message);
79
+ * // Show validation error to user
80
+ * }
81
+ * }
82
+ * ```
83
+ *
84
+ * @example With Zod validation cause
85
+ * ```typescript
86
+ * try {
87
+ * await repo.records.create(collection, record);
88
+ * } catch (error) {
89
+ * if (error instanceof ValidationError && error.cause) {
90
+ * // error.cause may be a ZodError with detailed field errors
91
+ * const zodError = error.cause as ZodError;
92
+ * zodError.errors.forEach(e => {
93
+ * console.error(`Field ${e.path.join(".")}: ${e.message}`);
94
+ * });
95
+ * }
96
+ * }
97
+ * ```
98
+ */
99
+ class ValidationError extends ATProtoSDKError {
100
+ /**
101
+ * Creates a validation error.
102
+ *
103
+ * @param message - Description of what validation failed
104
+ * @param cause - The underlying validation error (e.g., ZodError)
105
+ */
106
+ constructor(message, cause) {
107
+ super(message, "VALIDATION_ERROR", 400, cause);
108
+ this.name = "ValidationError";
109
+ }
110
+ }
111
+
112
+ /**
113
+ * Registry for managing and validating AT Protocol lexicon schemas.
114
+ *
115
+ * Lexicons are schema definitions that describe the structure of records
116
+ * in the AT Protocol. This registry allows you to:
117
+ *
118
+ * - Register custom lexicons for your application's record types
119
+ * - Validate records against their lexicon schemas
120
+ * - Extend the AT Protocol Agent with custom lexicon support
121
+ *
122
+ * @remarks
123
+ * The SDK automatically registers hypercert lexicons when creating a Repository.
124
+ * You only need to use this class directly if you're working with custom
125
+ * record types.
126
+ *
127
+ * **Lexicon IDs** follow the NSID (Namespaced Identifier) format:
128
+ * `{authority}.{name}` (e.g., `org.hypercerts.hypercert`)
129
+ *
130
+ * @example Registering custom lexicons
131
+ * ```typescript
132
+ * const registry = sdk.getLexiconRegistry();
133
+ *
134
+ * // Register a single lexicon
135
+ * registry.register({
136
+ * lexicon: 1,
137
+ * id: "org.example.myRecord",
138
+ * defs: {
139
+ * main: {
140
+ * type: "record",
141
+ * key: "tid",
142
+ * record: {
143
+ * type: "object",
144
+ * required: ["title", "createdAt"],
145
+ * properties: {
146
+ * title: { type: "string" },
147
+ * description: { type: "string" },
148
+ * createdAt: { type: "string", format: "datetime" },
149
+ * },
150
+ * },
151
+ * },
152
+ * },
153
+ * });
154
+ *
155
+ * // Register multiple lexicons at once
156
+ * registry.registerMany([lexicon1, lexicon2, lexicon3]);
157
+ * ```
158
+ *
159
+ * @example Validating records
160
+ * ```typescript
161
+ * const result = registry.validate("org.example.myRecord", {
162
+ * title: "Test",
163
+ * createdAt: new Date().toISOString(),
164
+ * });
165
+ *
166
+ * if (!result.valid) {
167
+ * console.error(`Validation failed: ${result.error}`);
168
+ * }
169
+ * ```
170
+ *
171
+ * @see https://atproto.com/specs/lexicon for the Lexicon specification
172
+ */
173
+ class LexiconRegistry {
174
+ /**
175
+ * Creates a new LexiconRegistry.
176
+ *
177
+ * The registry starts empty. Use {@link register} or {@link registerMany}
178
+ * to add lexicons.
179
+ */
180
+ constructor() {
181
+ /** Map of lexicon ID to lexicon document */
182
+ this.lexicons = new Map();
183
+ this.lexiconsCollection = new Lexicons();
184
+ }
185
+ /**
186
+ * Registers a single lexicon schema.
187
+ *
188
+ * @param lexicon - The lexicon document to register
189
+ * @throws {@link ValidationError} if the lexicon doesn't have an `id` field
190
+ *
191
+ * @remarks
192
+ * If a lexicon with the same ID is already registered, it will be
193
+ * replaced with the new definition. This is useful for testing but
194
+ * should generally be avoided in production.
195
+ *
196
+ * @example
197
+ * ```typescript
198
+ * registry.register({
199
+ * lexicon: 1,
200
+ * id: "org.example.post",
201
+ * defs: {
202
+ * main: {
203
+ * type: "record",
204
+ * key: "tid",
205
+ * record: {
206
+ * type: "object",
207
+ * required: ["text", "createdAt"],
208
+ * properties: {
209
+ * text: { type: "string", maxLength: 300 },
210
+ * createdAt: { type: "string", format: "datetime" },
211
+ * },
212
+ * },
213
+ * },
214
+ * },
215
+ * });
216
+ * ```
217
+ */
218
+ register(lexicon) {
219
+ if (!lexicon.id) {
220
+ throw new ValidationError("Lexicon must have an 'id' field");
221
+ }
222
+ // Remove existing lexicon if present (to allow overwriting)
223
+ if (this.lexicons.has(lexicon.id)) {
224
+ // Lexicons collection doesn't support removal, so we create a new one
225
+ // This is a limitation - in practice, lexicons shouldn't be overwritten
226
+ // But we allow it for testing and flexibility
227
+ const existingLexicon = this.lexicons.get(lexicon.id);
228
+ if (existingLexicon) {
229
+ // Try to remove from collection (may fail if not supported)
230
+ try {
231
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
232
+ this.lexiconsCollection.remove?.(lexicon.id);
233
+ }
234
+ catch {
235
+ // If removal fails, create a new collection
236
+ this.lexiconsCollection = new Lexicons();
237
+ // Re-register all other lexicons
238
+ for (const [id, lex] of this.lexicons.entries()) {
239
+ if (id !== lexicon.id) {
240
+ this.lexiconsCollection.add(lex);
241
+ }
242
+ }
243
+ }
244
+ }
245
+ }
246
+ this.lexicons.set(lexicon.id, lexicon);
247
+ this.lexiconsCollection.add(lexicon);
248
+ }
249
+ /**
250
+ * Registers multiple lexicons at once.
251
+ *
252
+ * @param lexicons - Array of lexicon documents to register
253
+ *
254
+ * @example
255
+ * ```typescript
256
+ * import { HYPERCERT_LEXICONS } from "@hypercerts-org/sdk/lexicons";
257
+ *
258
+ * registry.registerMany(HYPERCERT_LEXICONS);
259
+ * ```
260
+ */
261
+ registerMany(lexicons) {
262
+ for (const lexicon of lexicons) {
263
+ this.register(lexicon);
264
+ }
265
+ }
266
+ /**
267
+ * Gets a lexicon document by ID.
268
+ *
269
+ * @param id - The lexicon NSID (e.g., "org.hypercerts.hypercert")
270
+ * @returns The lexicon document, or `undefined` if not registered
271
+ *
272
+ * @example
273
+ * ```typescript
274
+ * const lexicon = registry.get("org.hypercerts.hypercert");
275
+ * if (lexicon) {
276
+ * console.log(`Found lexicon: ${lexicon.id}`);
277
+ * }
278
+ * ```
279
+ */
280
+ get(id) {
281
+ return this.lexicons.get(id);
282
+ }
283
+ /**
284
+ * Validates a record against a collection's lexicon schema.
285
+ *
286
+ * @param collection - The collection NSID (same as lexicon ID)
287
+ * @param record - The record data to validate
288
+ * @returns Validation result with `valid` boolean and optional `error` message
289
+ *
290
+ * @remarks
291
+ * - If no lexicon is registered for the collection, validation passes
292
+ * (we can't validate against unknown schemas)
293
+ * - Validation checks required fields and type constraints defined
294
+ * in the lexicon schema
295
+ *
296
+ * @example
297
+ * ```typescript
298
+ * const result = registry.validate("org.hypercerts.hypercert", {
299
+ * title: "My Hypercert",
300
+ * description: "Description...",
301
+ * // ... other fields
302
+ * });
303
+ *
304
+ * if (!result.valid) {
305
+ * throw new Error(`Invalid record: ${result.error}`);
306
+ * }
307
+ * ```
308
+ */
309
+ validate(collection, record) {
310
+ // Check if we have a lexicon registered for this collection
311
+ // Collection format is typically "namespace.collection" (e.g., "app.bsky.feed.post")
312
+ // Lexicon ID format is the same
313
+ const lexiconId = collection;
314
+ const lexicon = this.lexicons.get(lexiconId);
315
+ if (!lexicon) {
316
+ // No lexicon registered - validation passes (can't validate unknown schemas)
317
+ return { valid: true };
318
+ }
319
+ // Check required fields if the lexicon defines them
320
+ const recordDef = lexicon.defs?.record;
321
+ if (recordDef && typeof recordDef === "object" && "record" in recordDef) {
322
+ const recordSchema = recordDef.record;
323
+ if (typeof recordSchema === "object" && "required" in recordSchema && Array.isArray(recordSchema.required)) {
324
+ const recordObj = record;
325
+ for (const requiredField of recordSchema.required) {
326
+ if (typeof requiredField === "string" && !(requiredField in recordObj)) {
327
+ return {
328
+ valid: false,
329
+ error: `Missing required field: ${requiredField}`,
330
+ };
331
+ }
332
+ }
333
+ }
334
+ }
335
+ try {
336
+ this.lexiconsCollection.assertValidRecord(collection, record);
337
+ return { valid: true };
338
+ }
339
+ catch (error) {
340
+ // If error indicates lexicon not found, treat as validation pass
341
+ // (the lexicon might exist in Agent's collection but not ours)
342
+ const errorMessage = error instanceof Error ? error.message : String(error);
343
+ if (errorMessage.includes("not found") || errorMessage.includes("Lexicon not found")) {
344
+ return { valid: true };
345
+ }
346
+ return {
347
+ valid: false,
348
+ error: errorMessage,
349
+ };
350
+ }
351
+ }
352
+ /**
353
+ * Adds all registered lexicons to an AT Protocol Agent instance.
354
+ *
355
+ * This allows the Agent to understand custom lexicon types when making
356
+ * API requests.
357
+ *
358
+ * @param agent - The Agent instance to extend
359
+ *
360
+ * @remarks
361
+ * This is called automatically when creating a Repository. You typically
362
+ * don't need to call this directly unless you're using the Agent
363
+ * independently.
364
+ *
365
+ * @internal
366
+ */
367
+ addToAgent(agent) {
368
+ // Access the internal lexicons collection and merge our lexicons
369
+ // The Agent's lex property is a Lexicons instance
370
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
371
+ const agentLex = agent.lex;
372
+ // Add each registered lexicon to the agent
373
+ for (const lexicon of this.lexicons.values()) {
374
+ agentLex.add(lexicon);
375
+ }
376
+ }
377
+ /**
378
+ * Gets all registered lexicon IDs.
379
+ *
380
+ * @returns Array of lexicon NSIDs
381
+ *
382
+ * @example
383
+ * ```typescript
384
+ * const ids = registry.getRegisteredIds();
385
+ * console.log(`Registered lexicons: ${ids.join(", ")}`);
386
+ * ```
387
+ */
388
+ getRegisteredIds() {
389
+ return Array.from(this.lexicons.keys());
390
+ }
391
+ /**
392
+ * Checks if a lexicon is registered.
393
+ *
394
+ * @param id - The lexicon NSID to check
395
+ * @returns `true` if the lexicon is registered
396
+ *
397
+ * @example
398
+ * ```typescript
399
+ * if (registry.has("org.hypercerts.hypercert")) {
400
+ * // Hypercert lexicon is available
401
+ * }
402
+ * ```
403
+ */
404
+ has(id) {
405
+ return this.lexicons.has(id);
406
+ }
407
+ }
408
+
409
+ export { LexiconRegistry };
410
+ //# sourceMappingURL=lexicons.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lexicons.mjs","sources":["../src/core/errors.ts","../src/repository/LexiconRegistry.ts"],"sourcesContent":["/**\n * Base error class for all SDK errors.\n *\n * All errors thrown by the Hypercerts SDK extend this class, making it easy\n * to catch and handle SDK-specific errors.\n *\n * @example Catching all SDK errors\n * ```typescript\n * try {\n * await sdk.authorize(\"user.bsky.social\");\n * } catch (error) {\n * if (error instanceof ATProtoSDKError) {\n * console.error(`SDK Error [${error.code}]: ${error.message}`);\n * console.error(`HTTP Status: ${error.status}`);\n * }\n * }\n * ```\n *\n * @example Checking error codes\n * ```typescript\n * try {\n * await repo.records.get(collection, rkey);\n * } catch (error) {\n * if (error instanceof ATProtoSDKError) {\n * switch (error.code) {\n * case \"AUTHENTICATION_ERROR\":\n * // Redirect to login\n * break;\n * case \"VALIDATION_ERROR\":\n * // Show form errors\n * break;\n * case \"NETWORK_ERROR\":\n * // Retry or show offline message\n * break;\n * }\n * }\n * }\n * ```\n */\nexport class ATProtoSDKError extends Error {\n /**\n * Creates a new SDK error.\n *\n * @param message - Human-readable error description\n * @param code - Machine-readable error code for programmatic handling\n * @param status - HTTP status code associated with this error type\n * @param cause - The underlying error that caused this error, if any\n */\n constructor(\n message: string,\n public code: string,\n public status?: number,\n public cause?: unknown,\n ) {\n super(message);\n this.name = \"ATProtoSDKError\";\n Error.captureStackTrace?.(this, this.constructor);\n }\n}\n\n/**\n * Error thrown when authentication fails.\n *\n * This error indicates problems with the OAuth flow, invalid credentials,\n * or failed token exchanges. Common causes:\n * - Invalid authorization code\n * - Expired or invalid state parameter\n * - Revoked or invalid tokens\n * - User denied authorization\n *\n * @example\n * ```typescript\n * try {\n * const session = await sdk.callback(params);\n * } catch (error) {\n * if (error instanceof AuthenticationError) {\n * // Clear any stored state and redirect to login\n * console.error(\"Authentication failed:\", error.message);\n * }\n * }\n * ```\n */\nexport class AuthenticationError extends ATProtoSDKError {\n /**\n * Creates an authentication error.\n *\n * @param message - Description of what went wrong during authentication\n * @param cause - The underlying error (e.g., from the OAuth client)\n */\n constructor(message: string, cause?: unknown) {\n super(message, \"AUTHENTICATION_ERROR\", 401, cause);\n this.name = \"AuthenticationError\";\n }\n}\n\n/**\n * Error thrown when a session has expired and cannot be refreshed.\n *\n * This typically occurs when:\n * - The refresh token has expired (usually after extended inactivity)\n * - The user has revoked access to your application\n * - The PDS has invalidated all sessions for the user\n *\n * When this error occurs, the user must re-authenticate.\n *\n * @example\n * ```typescript\n * try {\n * const session = await sdk.restoreSession(did);\n * } catch (error) {\n * if (error instanceof SessionExpiredError) {\n * // Clear stored session and prompt user to log in again\n * localStorage.removeItem(\"userDid\");\n * window.location.href = \"/login\";\n * }\n * }\n * ```\n */\nexport class SessionExpiredError extends ATProtoSDKError {\n /**\n * Creates a session expired error.\n *\n * @param message - Description of why the session expired\n * @param cause - The underlying error from the token refresh attempt\n */\n constructor(message: string = \"Session expired\", cause?: unknown) {\n super(message, \"SESSION_EXPIRED\", 401, cause);\n this.name = \"SessionExpiredError\";\n }\n}\n\n/**\n * Error thrown when input validation fails.\n *\n * This error indicates that provided data doesn't meet the required format\n * or constraints. Common causes:\n * - Missing required fields\n * - Invalid URL formats\n * - Invalid DID format\n * - Schema validation failures for records\n * - Invalid configuration values\n *\n * @example\n * ```typescript\n * try {\n * await sdk.authorize(\"\"); // Empty identifier\n * } catch (error) {\n * if (error instanceof ValidationError) {\n * console.error(\"Invalid input:\", error.message);\n * // Show validation error to user\n * }\n * }\n * ```\n *\n * @example With Zod validation cause\n * ```typescript\n * try {\n * await repo.records.create(collection, record);\n * } catch (error) {\n * if (error instanceof ValidationError && error.cause) {\n * // error.cause may be a ZodError with detailed field errors\n * const zodError = error.cause as ZodError;\n * zodError.errors.forEach(e => {\n * console.error(`Field ${e.path.join(\".\")}: ${e.message}`);\n * });\n * }\n * }\n * ```\n */\nexport class ValidationError extends ATProtoSDKError {\n /**\n * Creates a validation error.\n *\n * @param message - Description of what validation failed\n * @param cause - The underlying validation error (e.g., ZodError)\n */\n constructor(message: string, cause?: unknown) {\n super(message, \"VALIDATION_ERROR\", 400, cause);\n this.name = \"ValidationError\";\n }\n}\n\n/**\n * Error thrown when a network request fails.\n *\n * This error indicates connectivity issues or server unavailability.\n * Common causes:\n * - No internet connection\n * - DNS resolution failure\n * - Server timeout\n * - Server returned 5xx error\n * - TLS/SSL errors\n *\n * These errors are typically transient and may succeed on retry.\n *\n * @example\n * ```typescript\n * try {\n * await repo.records.list(collection);\n * } catch (error) {\n * if (error instanceof NetworkError) {\n * // Implement retry logic or show offline indicator\n * console.error(\"Network error:\", error.message);\n * await retryWithBackoff(() => repo.records.list(collection));\n * }\n * }\n * ```\n */\nexport class NetworkError extends ATProtoSDKError {\n /**\n * Creates a network error.\n *\n * @param message - Description of the network failure\n * @param cause - The underlying error (e.g., fetch error, timeout)\n */\n constructor(message: string, cause?: unknown) {\n super(message, \"NETWORK_ERROR\", 503, cause);\n this.name = \"NetworkError\";\n }\n}\n\n/**\n * Error thrown when an SDS-only operation is attempted on a PDS.\n *\n * Certain operations are only available on Shared Data Servers (SDS),\n * such as collaborator management and organization operations.\n * This error is thrown when these operations are attempted on a\n * Personal Data Server (PDS).\n *\n * @example\n * ```typescript\n * const pdsRepo = sdk.repository(session); // Default is PDS\n *\n * try {\n * // This will throw SDSRequiredError\n * await pdsRepo.collaborators.list();\n * } catch (error) {\n * if (error instanceof SDSRequiredError) {\n * // Switch to SDS for this operation\n * const sdsRepo = sdk.repository(session, { server: \"sds\" });\n * const collaborators = await sdsRepo.collaborators.list();\n * }\n * }\n * ```\n */\nexport class SDSRequiredError extends ATProtoSDKError {\n /**\n * Creates an SDS required error.\n *\n * @param message - Description of which operation requires SDS\n * @param cause - Any underlying error\n */\n constructor(message: string = \"This operation requires a Shared Data Server (SDS)\", cause?: unknown) {\n super(message, \"SDS_REQUIRED\", 400, cause);\n this.name = \"SDSRequiredError\";\n }\n}\n","import type { Agent } from \"@atproto/api\";\nimport type { LexiconDoc } from \"@atproto/lexicon\";\nimport { Lexicons } from \"@atproto/lexicon\";\nimport { ValidationError } from \"../core/errors.js\";\n\n/**\n * Result of validating a record against a lexicon schema.\n */\nexport interface ValidationResult {\n /**\n * Whether the record is valid according to the lexicon schema.\n */\n valid: boolean;\n\n /**\n * Error message if validation failed.\n *\n * Only present when `valid` is `false`.\n */\n error?: string;\n}\n\n/**\n * Registry for managing and validating AT Protocol lexicon schemas.\n *\n * Lexicons are schema definitions that describe the structure of records\n * in the AT Protocol. This registry allows you to:\n *\n * - Register custom lexicons for your application's record types\n * - Validate records against their lexicon schemas\n * - Extend the AT Protocol Agent with custom lexicon support\n *\n * @remarks\n * The SDK automatically registers hypercert lexicons when creating a Repository.\n * You only need to use this class directly if you're working with custom\n * record types.\n *\n * **Lexicon IDs** follow the NSID (Namespaced Identifier) format:\n * `{authority}.{name}` (e.g., `org.hypercerts.hypercert`)\n *\n * @example Registering custom lexicons\n * ```typescript\n * const registry = sdk.getLexiconRegistry();\n *\n * // Register a single lexicon\n * registry.register({\n * lexicon: 1,\n * id: \"org.example.myRecord\",\n * defs: {\n * main: {\n * type: \"record\",\n * key: \"tid\",\n * record: {\n * type: \"object\",\n * required: [\"title\", \"createdAt\"],\n * properties: {\n * title: { type: \"string\" },\n * description: { type: \"string\" },\n * createdAt: { type: \"string\", format: \"datetime\" },\n * },\n * },\n * },\n * },\n * });\n *\n * // Register multiple lexicons at once\n * registry.registerMany([lexicon1, lexicon2, lexicon3]);\n * ```\n *\n * @example Validating records\n * ```typescript\n * const result = registry.validate(\"org.example.myRecord\", {\n * title: \"Test\",\n * createdAt: new Date().toISOString(),\n * });\n *\n * if (!result.valid) {\n * console.error(`Validation failed: ${result.error}`);\n * }\n * ```\n *\n * @see https://atproto.com/specs/lexicon for the Lexicon specification\n */\nexport class LexiconRegistry {\n /** Map of lexicon ID to lexicon document */\n private lexicons = new Map<string, LexiconDoc>();\n\n /** Lexicons collection for validation */\n private lexiconsCollection: Lexicons;\n\n /**\n * Creates a new LexiconRegistry.\n *\n * The registry starts empty. Use {@link register} or {@link registerMany}\n * to add lexicons.\n */\n constructor() {\n this.lexiconsCollection = new Lexicons();\n }\n\n /**\n * Registers a single lexicon schema.\n *\n * @param lexicon - The lexicon document to register\n * @throws {@link ValidationError} if the lexicon doesn't have an `id` field\n *\n * @remarks\n * If a lexicon with the same ID is already registered, it will be\n * replaced with the new definition. This is useful for testing but\n * should generally be avoided in production.\n *\n * @example\n * ```typescript\n * registry.register({\n * lexicon: 1,\n * id: \"org.example.post\",\n * defs: {\n * main: {\n * type: \"record\",\n * key: \"tid\",\n * record: {\n * type: \"object\",\n * required: [\"text\", \"createdAt\"],\n * properties: {\n * text: { type: \"string\", maxLength: 300 },\n * createdAt: { type: \"string\", format: \"datetime\" },\n * },\n * },\n * },\n * },\n * });\n * ```\n */\n register(lexicon: LexiconDoc): void {\n if (!lexicon.id) {\n throw new ValidationError(\"Lexicon must have an 'id' field\");\n }\n\n // Remove existing lexicon if present (to allow overwriting)\n if (this.lexicons.has(lexicon.id)) {\n // Lexicons collection doesn't support removal, so we create a new one\n // This is a limitation - in practice, lexicons shouldn't be overwritten\n // But we allow it for testing and flexibility\n const existingLexicon = this.lexicons.get(lexicon.id);\n if (existingLexicon) {\n // Try to remove from collection (may fail if not supported)\n try {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (this.lexiconsCollection as any).remove?.(lexicon.id);\n } catch {\n // If removal fails, create a new collection\n this.lexiconsCollection = new Lexicons();\n // Re-register all other lexicons\n for (const [id, lex] of this.lexicons.entries()) {\n if (id !== lexicon.id) {\n this.lexiconsCollection.add(lex);\n }\n }\n }\n }\n }\n\n this.lexicons.set(lexicon.id, lexicon);\n this.lexiconsCollection.add(lexicon);\n }\n\n /**\n * Registers multiple lexicons at once.\n *\n * @param lexicons - Array of lexicon documents to register\n *\n * @example\n * ```typescript\n * import { HYPERCERT_LEXICONS } from \"@hypercerts-org/sdk/lexicons\";\n *\n * registry.registerMany(HYPERCERT_LEXICONS);\n * ```\n */\n registerMany(lexicons: LexiconDoc[]): void {\n for (const lexicon of lexicons) {\n this.register(lexicon);\n }\n }\n\n /**\n * Gets a lexicon document by ID.\n *\n * @param id - The lexicon NSID (e.g., \"org.hypercerts.hypercert\")\n * @returns The lexicon document, or `undefined` if not registered\n *\n * @example\n * ```typescript\n * const lexicon = registry.get(\"org.hypercerts.hypercert\");\n * if (lexicon) {\n * console.log(`Found lexicon: ${lexicon.id}`);\n * }\n * ```\n */\n get(id: string): LexiconDoc | undefined {\n return this.lexicons.get(id);\n }\n\n /**\n * Validates a record against a collection's lexicon schema.\n *\n * @param collection - The collection NSID (same as lexicon ID)\n * @param record - The record data to validate\n * @returns Validation result with `valid` boolean and optional `error` message\n *\n * @remarks\n * - If no lexicon is registered for the collection, validation passes\n * (we can't validate against unknown schemas)\n * - Validation checks required fields and type constraints defined\n * in the lexicon schema\n *\n * @example\n * ```typescript\n * const result = registry.validate(\"org.hypercerts.hypercert\", {\n * title: \"My Hypercert\",\n * description: \"Description...\",\n * // ... other fields\n * });\n *\n * if (!result.valid) {\n * throw new Error(`Invalid record: ${result.error}`);\n * }\n * ```\n */\n validate(collection: string, record: unknown): ValidationResult {\n // Check if we have a lexicon registered for this collection\n // Collection format is typically \"namespace.collection\" (e.g., \"app.bsky.feed.post\")\n // Lexicon ID format is the same\n const lexiconId = collection;\n const lexicon = this.lexicons.get(lexiconId);\n if (!lexicon) {\n // No lexicon registered - validation passes (can't validate unknown schemas)\n return { valid: true };\n }\n\n // Check required fields if the lexicon defines them\n const recordDef = lexicon.defs?.record;\n if (recordDef && typeof recordDef === \"object\" && \"record\" in recordDef) {\n const recordSchema = recordDef.record;\n if (typeof recordSchema === \"object\" && \"required\" in recordSchema && Array.isArray(recordSchema.required)) {\n const recordObj = record as Record<string, unknown>;\n for (const requiredField of recordSchema.required) {\n if (typeof requiredField === \"string\" && !(requiredField in recordObj)) {\n return {\n valid: false,\n error: `Missing required field: ${requiredField}`,\n };\n }\n }\n }\n }\n\n try {\n this.lexiconsCollection.assertValidRecord(collection, record);\n return { valid: true };\n } catch (error) {\n // If error indicates lexicon not found, treat as validation pass\n // (the lexicon might exist in Agent's collection but not ours)\n const errorMessage = error instanceof Error ? error.message : String(error);\n if (errorMessage.includes(\"not found\") || errorMessage.includes(\"Lexicon not found\")) {\n return { valid: true };\n }\n return {\n valid: false,\n error: errorMessage,\n };\n }\n }\n\n /**\n * Adds all registered lexicons to an AT Protocol Agent instance.\n *\n * This allows the Agent to understand custom lexicon types when making\n * API requests.\n *\n * @param agent - The Agent instance to extend\n *\n * @remarks\n * This is called automatically when creating a Repository. You typically\n * don't need to call this directly unless you're using the Agent\n * independently.\n *\n * @internal\n */\n addToAgent(agent: Agent): void {\n // Access the internal lexicons collection and merge our lexicons\n // The Agent's lex property is a Lexicons instance\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const agentLex = (agent as any).lex as Lexicons;\n\n // Add each registered lexicon to the agent\n for (const lexicon of this.lexicons.values()) {\n agentLex.add(lexicon);\n }\n }\n\n /**\n * Gets all registered lexicon IDs.\n *\n * @returns Array of lexicon NSIDs\n *\n * @example\n * ```typescript\n * const ids = registry.getRegisteredIds();\n * console.log(`Registered lexicons: ${ids.join(\", \")}`);\n * ```\n */\n getRegisteredIds(): string[] {\n return Array.from(this.lexicons.keys());\n }\n\n /**\n * Checks if a lexicon is registered.\n *\n * @param id - The lexicon NSID to check\n * @returns `true` if the lexicon is registered\n *\n * @example\n * ```typescript\n * if (registry.has(\"org.hypercerts.hypercert\")) {\n * // Hypercert lexicon is available\n * }\n * ```\n */\n has(id: string): boolean {\n return this.lexicons.has(id);\n }\n}\n"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCG;AACG,MAAO,eAAgB,SAAQ,KAAK,CAAA;AACxC;;;;;;;AAOG;AACH,IAAA,WAAA,CACE,OAAe,EACR,IAAY,EACZ,MAAe,EACf,KAAe,EAAA;QAEtB,KAAK,CAAC,OAAO,CAAC;QAJP,IAAA,CAAA,IAAI,GAAJ,IAAI;QACJ,IAAA,CAAA,MAAM,GAAN,MAAM;QACN,IAAA,CAAA,KAAK,GAAL,KAAK;AAGZ,QAAA,IAAI,CAAC,IAAI,GAAG,iBAAiB;QAC7B,KAAK,CAAC,iBAAiB,GAA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