@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,260 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Base error class for all SDK errors.
5
+ *
6
+ * All errors thrown by the Hypercerts SDK extend this class, making it easy
7
+ * to catch and handle SDK-specific errors.
8
+ *
9
+ * @example Catching all SDK errors
10
+ * ```typescript
11
+ * try {
12
+ * await sdk.authorize("user.bsky.social");
13
+ * } catch (error) {
14
+ * if (error instanceof ATProtoSDKError) {
15
+ * console.error(`SDK Error [${error.code}]: ${error.message}`);
16
+ * console.error(`HTTP Status: ${error.status}`);
17
+ * }
18
+ * }
19
+ * ```
20
+ *
21
+ * @example Checking error codes
22
+ * ```typescript
23
+ * try {
24
+ * await repo.records.get(collection, rkey);
25
+ * } catch (error) {
26
+ * if (error instanceof ATProtoSDKError) {
27
+ * switch (error.code) {
28
+ * case "AUTHENTICATION_ERROR":
29
+ * // Redirect to login
30
+ * break;
31
+ * case "VALIDATION_ERROR":
32
+ * // Show form errors
33
+ * break;
34
+ * case "NETWORK_ERROR":
35
+ * // Retry or show offline message
36
+ * break;
37
+ * }
38
+ * }
39
+ * }
40
+ * ```
41
+ */
42
+ class ATProtoSDKError extends Error {
43
+ /**
44
+ * Creates a new SDK error.
45
+ *
46
+ * @param message - Human-readable error description
47
+ * @param code - Machine-readable error code for programmatic handling
48
+ * @param status - HTTP status code associated with this error type
49
+ * @param cause - The underlying error that caused this error, if any
50
+ */
51
+ constructor(message, code, status, cause) {
52
+ super(message);
53
+ this.code = code;
54
+ this.status = status;
55
+ this.cause = cause;
56
+ this.name = "ATProtoSDKError";
57
+ Error.captureStackTrace?.(this, this.constructor);
58
+ }
59
+ }
60
+ /**
61
+ * Error thrown when authentication fails.
62
+ *
63
+ * This error indicates problems with the OAuth flow, invalid credentials,
64
+ * or failed token exchanges. Common causes:
65
+ * - Invalid authorization code
66
+ * - Expired or invalid state parameter
67
+ * - Revoked or invalid tokens
68
+ * - User denied authorization
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * try {
73
+ * const session = await sdk.callback(params);
74
+ * } catch (error) {
75
+ * if (error instanceof AuthenticationError) {
76
+ * // Clear any stored state and redirect to login
77
+ * console.error("Authentication failed:", error.message);
78
+ * }
79
+ * }
80
+ * ```
81
+ */
82
+ class AuthenticationError extends ATProtoSDKError {
83
+ /**
84
+ * Creates an authentication error.
85
+ *
86
+ * @param message - Description of what went wrong during authentication
87
+ * @param cause - The underlying error (e.g., from the OAuth client)
88
+ */
89
+ constructor(message, cause) {
90
+ super(message, "AUTHENTICATION_ERROR", 401, cause);
91
+ this.name = "AuthenticationError";
92
+ }
93
+ }
94
+ /**
95
+ * Error thrown when a session has expired and cannot be refreshed.
96
+ *
97
+ * This typically occurs when:
98
+ * - The refresh token has expired (usually after extended inactivity)
99
+ * - The user has revoked access to your application
100
+ * - The PDS has invalidated all sessions for the user
101
+ *
102
+ * When this error occurs, the user must re-authenticate.
103
+ *
104
+ * @example
105
+ * ```typescript
106
+ * try {
107
+ * const session = await sdk.restoreSession(did);
108
+ * } catch (error) {
109
+ * if (error instanceof SessionExpiredError) {
110
+ * // Clear stored session and prompt user to log in again
111
+ * localStorage.removeItem("userDid");
112
+ * window.location.href = "/login";
113
+ * }
114
+ * }
115
+ * ```
116
+ */
117
+ class SessionExpiredError extends ATProtoSDKError {
118
+ /**
119
+ * Creates a session expired error.
120
+ *
121
+ * @param message - Description of why the session expired
122
+ * @param cause - The underlying error from the token refresh attempt
123
+ */
124
+ constructor(message = "Session expired", cause) {
125
+ super(message, "SESSION_EXPIRED", 401, cause);
126
+ this.name = "SessionExpiredError";
127
+ }
128
+ }
129
+ /**
130
+ * Error thrown when input validation fails.
131
+ *
132
+ * This error indicates that provided data doesn't meet the required format
133
+ * or constraints. Common causes:
134
+ * - Missing required fields
135
+ * - Invalid URL formats
136
+ * - Invalid DID format
137
+ * - Schema validation failures for records
138
+ * - Invalid configuration values
139
+ *
140
+ * @example
141
+ * ```typescript
142
+ * try {
143
+ * await sdk.authorize(""); // Empty identifier
144
+ * } catch (error) {
145
+ * if (error instanceof ValidationError) {
146
+ * console.error("Invalid input:", error.message);
147
+ * // Show validation error to user
148
+ * }
149
+ * }
150
+ * ```
151
+ *
152
+ * @example With Zod validation cause
153
+ * ```typescript
154
+ * try {
155
+ * await repo.records.create(collection, record);
156
+ * } catch (error) {
157
+ * if (error instanceof ValidationError && error.cause) {
158
+ * // error.cause may be a ZodError with detailed field errors
159
+ * const zodError = error.cause as ZodError;
160
+ * zodError.errors.forEach(e => {
161
+ * console.error(`Field ${e.path.join(".")}: ${e.message}`);
162
+ * });
163
+ * }
164
+ * }
165
+ * ```
166
+ */
167
+ class ValidationError extends ATProtoSDKError {
168
+ /**
169
+ * Creates a validation error.
170
+ *
171
+ * @param message - Description of what validation failed
172
+ * @param cause - The underlying validation error (e.g., ZodError)
173
+ */
174
+ constructor(message, cause) {
175
+ super(message, "VALIDATION_ERROR", 400, cause);
176
+ this.name = "ValidationError";
177
+ }
178
+ }
179
+ /**
180
+ * Error thrown when a network request fails.
181
+ *
182
+ * This error indicates connectivity issues or server unavailability.
183
+ * Common causes:
184
+ * - No internet connection
185
+ * - DNS resolution failure
186
+ * - Server timeout
187
+ * - Server returned 5xx error
188
+ * - TLS/SSL errors
189
+ *
190
+ * These errors are typically transient and may succeed on retry.
191
+ *
192
+ * @example
193
+ * ```typescript
194
+ * try {
195
+ * await repo.records.list(collection);
196
+ * } catch (error) {
197
+ * if (error instanceof NetworkError) {
198
+ * // Implement retry logic or show offline indicator
199
+ * console.error("Network error:", error.message);
200
+ * await retryWithBackoff(() => repo.records.list(collection));
201
+ * }
202
+ * }
203
+ * ```
204
+ */
205
+ class NetworkError extends ATProtoSDKError {
206
+ /**
207
+ * Creates a network error.
208
+ *
209
+ * @param message - Description of the network failure
210
+ * @param cause - The underlying error (e.g., fetch error, timeout)
211
+ */
212
+ constructor(message, cause) {
213
+ super(message, "NETWORK_ERROR", 503, cause);
214
+ this.name = "NetworkError";
215
+ }
216
+ }
217
+ /**
218
+ * Error thrown when an SDS-only operation is attempted on a PDS.
219
+ *
220
+ * Certain operations are only available on Shared Data Servers (SDS),
221
+ * such as collaborator management and organization operations.
222
+ * This error is thrown when these operations are attempted on a
223
+ * Personal Data Server (PDS).
224
+ *
225
+ * @example
226
+ * ```typescript
227
+ * const pdsRepo = sdk.repository(session); // Default is PDS
228
+ *
229
+ * try {
230
+ * // This will throw SDSRequiredError
231
+ * await pdsRepo.collaborators.list();
232
+ * } catch (error) {
233
+ * if (error instanceof SDSRequiredError) {
234
+ * // Switch to SDS for this operation
235
+ * const sdsRepo = sdk.repository(session, { server: "sds" });
236
+ * const collaborators = await sdsRepo.collaborators.list();
237
+ * }
238
+ * }
239
+ * ```
240
+ */
241
+ class SDSRequiredError extends ATProtoSDKError {
242
+ /**
243
+ * Creates an SDS required error.
244
+ *
245
+ * @param message - Description of which operation requires SDS
246
+ * @param cause - Any underlying error
247
+ */
248
+ constructor(message = "This operation requires a Shared Data Server (SDS)", cause) {
249
+ super(message, "SDS_REQUIRED", 400, cause);
250
+ this.name = "SDSRequiredError";
251
+ }
252
+ }
253
+
254
+ exports.ATProtoSDKError = ATProtoSDKError;
255
+ exports.AuthenticationError = AuthenticationError;
256
+ exports.NetworkError = NetworkError;
257
+ exports.SDSRequiredError = SDSRequiredError;
258
+ exports.SessionExpiredError = SessionExpiredError;
259
+ exports.ValidationError = ValidationError;
260
+ //# sourceMappingURL=errors.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.cjs","sources":["../src/core/errors.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"],"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,GAAG,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC;IACnD;AACD;AAED;;;;;;;;;;;;;;;;;;;;;AAqBG;AACG,MAAO,mBAAoB,SAAQ,eAAe,CAAA;AACtD;;;;;AAKG;IACH,WAAA,CAAY,OAAe,EAAE,KAAe,EAAA;QAC1C,KAAK,CAAC,OAAO,EAAE,sBAAsB,EAAE,GAAG,EAAE,KAAK,CAAC;AAClD,QAAA,IAAI,CAAC,IAAI,GAAG,qBAAqB;IACnC;AACD;AAED;;;;;;;;;;;;;;;;;;;;;;AAsBG;AACG,MAAO,mBAAoB,SAAQ,eAAe,CAAA;AACtD;;;;;AAKG;IACH,WAAA,CAAY,OAAA,GAAkB,iBAAiB,EAAE,KAAe,EAAA;QAC9D,KAAK,CAAC,OAAO,EAAE,iBAAiB,EAAE,GAAG,EAAE,KAAK,CAAC;AAC7C,QAAA,IAAI,CAAC,IAAI,GAAG,qBAAqB;IACnC;AACD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCG;AACG,MAAO,eAAgB,SAAQ,eAAe,CAAA;AAClD;;;;;AAKG;IACH,WAAA,CAAY,OAAe,EAAE,KAAe,EAAA;QAC1C,KAAK,CAAC,OAAO,EAAE,kBAAkB,EAAE,GAAG,EAAE,KAAK,CAAC;AAC9C,QAAA,IAAI,CAAC,IAAI,GAAG,iBAAiB;IAC/B;AACD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;AAyBG;AACG,MAAO,YAAa,SAAQ,eAAe,CAAA;AAC/C;;;;;AAKG;IACH,WAAA,CAAY,OAAe,EAAE,KAAe,EAAA;QAC1C,KAAK,CAAC,OAAO,EAAE,eAAe,EAAE,GAAG,EAAE,KAAK,CAAC;AAC3C,QAAA,IAAI,CAAC,IAAI,GAAG,cAAc;IAC5B;AACD;AAED;;;;;;;;;;;;;;;;;;;;;;;AAuBG;AACG,MAAO,gBAAiB,SAAQ,eAAe,CAAA;AACnD;;;;;AAKG;IACH,WAAA,CAAY,OAAA,GAAkB,oDAAoD,EAAE,KAAe,EAAA;QACjG,KAAK,CAAC,OAAO,EAAE,cAAc,EAAE,GAAG,EAAE,KAAK,CAAC;AAC1C,QAAA,IAAI,CAAC,IAAI,GAAG,kBAAkB;IAChC;AACD;;;;;;;;;"}
@@ -0,0 +1,233 @@
1
+ /**
2
+ * Base error class for all SDK errors.
3
+ *
4
+ * All errors thrown by the Hypercerts SDK extend this class, making it easy
5
+ * to catch and handle SDK-specific errors.
6
+ *
7
+ * @example Catching all SDK errors
8
+ * ```typescript
9
+ * try {
10
+ * await sdk.authorize("user.bsky.social");
11
+ * } catch (error) {
12
+ * if (error instanceof ATProtoSDKError) {
13
+ * console.error(`SDK Error [${error.code}]: ${error.message}`);
14
+ * console.error(`HTTP Status: ${error.status}`);
15
+ * }
16
+ * }
17
+ * ```
18
+ *
19
+ * @example Checking error codes
20
+ * ```typescript
21
+ * try {
22
+ * await repo.records.get(collection, rkey);
23
+ * } catch (error) {
24
+ * if (error instanceof ATProtoSDKError) {
25
+ * switch (error.code) {
26
+ * case "AUTHENTICATION_ERROR":
27
+ * // Redirect to login
28
+ * break;
29
+ * case "VALIDATION_ERROR":
30
+ * // Show form errors
31
+ * break;
32
+ * case "NETWORK_ERROR":
33
+ * // Retry or show offline message
34
+ * break;
35
+ * }
36
+ * }
37
+ * }
38
+ * ```
39
+ */
40
+ declare class ATProtoSDKError extends Error {
41
+ code: string;
42
+ status?: number | undefined;
43
+ cause?: unknown | undefined;
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: string, code: string, status?: number | undefined, cause?: unknown | undefined);
53
+ }
54
+ /**
55
+ * Error thrown when authentication fails.
56
+ *
57
+ * This error indicates problems with the OAuth flow, invalid credentials,
58
+ * or failed token exchanges. Common causes:
59
+ * - Invalid authorization code
60
+ * - Expired or invalid state parameter
61
+ * - Revoked or invalid tokens
62
+ * - User denied authorization
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * try {
67
+ * const session = await sdk.callback(params);
68
+ * } catch (error) {
69
+ * if (error instanceof AuthenticationError) {
70
+ * // Clear any stored state and redirect to login
71
+ * console.error("Authentication failed:", error.message);
72
+ * }
73
+ * }
74
+ * ```
75
+ */
76
+ declare class AuthenticationError extends ATProtoSDKError {
77
+ /**
78
+ * Creates an authentication error.
79
+ *
80
+ * @param message - Description of what went wrong during authentication
81
+ * @param cause - The underlying error (e.g., from the OAuth client)
82
+ */
83
+ constructor(message: string, cause?: unknown);
84
+ }
85
+ /**
86
+ * Error thrown when a session has expired and cannot be refreshed.
87
+ *
88
+ * This typically occurs when:
89
+ * - The refresh token has expired (usually after extended inactivity)
90
+ * - The user has revoked access to your application
91
+ * - The PDS has invalidated all sessions for the user
92
+ *
93
+ * When this error occurs, the user must re-authenticate.
94
+ *
95
+ * @example
96
+ * ```typescript
97
+ * try {
98
+ * const session = await sdk.restoreSession(did);
99
+ * } catch (error) {
100
+ * if (error instanceof SessionExpiredError) {
101
+ * // Clear stored session and prompt user to log in again
102
+ * localStorage.removeItem("userDid");
103
+ * window.location.href = "/login";
104
+ * }
105
+ * }
106
+ * ```
107
+ */
108
+ declare class SessionExpiredError extends ATProtoSDKError {
109
+ /**
110
+ * Creates a session expired error.
111
+ *
112
+ * @param message - Description of why the session expired
113
+ * @param cause - The underlying error from the token refresh attempt
114
+ */
115
+ constructor(message?: string, cause?: unknown);
116
+ }
117
+ /**
118
+ * Error thrown when input validation fails.
119
+ *
120
+ * This error indicates that provided data doesn't meet the required format
121
+ * or constraints. Common causes:
122
+ * - Missing required fields
123
+ * - Invalid URL formats
124
+ * - Invalid DID format
125
+ * - Schema validation failures for records
126
+ * - Invalid configuration values
127
+ *
128
+ * @example
129
+ * ```typescript
130
+ * try {
131
+ * await sdk.authorize(""); // Empty identifier
132
+ * } catch (error) {
133
+ * if (error instanceof ValidationError) {
134
+ * console.error("Invalid input:", error.message);
135
+ * // Show validation error to user
136
+ * }
137
+ * }
138
+ * ```
139
+ *
140
+ * @example With Zod validation cause
141
+ * ```typescript
142
+ * try {
143
+ * await repo.records.create(collection, record);
144
+ * } catch (error) {
145
+ * if (error instanceof ValidationError && error.cause) {
146
+ * // error.cause may be a ZodError with detailed field errors
147
+ * const zodError = error.cause as ZodError;
148
+ * zodError.errors.forEach(e => {
149
+ * console.error(`Field ${e.path.join(".")}: ${e.message}`);
150
+ * });
151
+ * }
152
+ * }
153
+ * ```
154
+ */
155
+ declare class ValidationError extends ATProtoSDKError {
156
+ /**
157
+ * Creates a validation error.
158
+ *
159
+ * @param message - Description of what validation failed
160
+ * @param cause - The underlying validation error (e.g., ZodError)
161
+ */
162
+ constructor(message: string, cause?: unknown);
163
+ }
164
+ /**
165
+ * Error thrown when a network request fails.
166
+ *
167
+ * This error indicates connectivity issues or server unavailability.
168
+ * Common causes:
169
+ * - No internet connection
170
+ * - DNS resolution failure
171
+ * - Server timeout
172
+ * - Server returned 5xx error
173
+ * - TLS/SSL errors
174
+ *
175
+ * These errors are typically transient and may succeed on retry.
176
+ *
177
+ * @example
178
+ * ```typescript
179
+ * try {
180
+ * await repo.records.list(collection);
181
+ * } catch (error) {
182
+ * if (error instanceof NetworkError) {
183
+ * // Implement retry logic or show offline indicator
184
+ * console.error("Network error:", error.message);
185
+ * await retryWithBackoff(() => repo.records.list(collection));
186
+ * }
187
+ * }
188
+ * ```
189
+ */
190
+ declare class NetworkError extends ATProtoSDKError {
191
+ /**
192
+ * Creates a network error.
193
+ *
194
+ * @param message - Description of the network failure
195
+ * @param cause - The underlying error (e.g., fetch error, timeout)
196
+ */
197
+ constructor(message: string, cause?: unknown);
198
+ }
199
+ /**
200
+ * Error thrown when an SDS-only operation is attempted on a PDS.
201
+ *
202
+ * Certain operations are only available on Shared Data Servers (SDS),
203
+ * such as collaborator management and organization operations.
204
+ * This error is thrown when these operations are attempted on a
205
+ * Personal Data Server (PDS).
206
+ *
207
+ * @example
208
+ * ```typescript
209
+ * const pdsRepo = sdk.repository(session); // Default is PDS
210
+ *
211
+ * try {
212
+ * // This will throw SDSRequiredError
213
+ * await pdsRepo.collaborators.list();
214
+ * } catch (error) {
215
+ * if (error instanceof SDSRequiredError) {
216
+ * // Switch to SDS for this operation
217
+ * const sdsRepo = sdk.repository(session, { server: "sds" });
218
+ * const collaborators = await sdsRepo.collaborators.list();
219
+ * }
220
+ * }
221
+ * ```
222
+ */
223
+ declare class SDSRequiredError extends ATProtoSDKError {
224
+ /**
225
+ * Creates an SDS required error.
226
+ *
227
+ * @param message - Description of which operation requires SDS
228
+ * @param cause - Any underlying error
229
+ */
230
+ constructor(message?: string, cause?: unknown);
231
+ }
232
+
233
+ export { ATProtoSDKError, AuthenticationError, NetworkError, SDSRequiredError, SessionExpiredError, ValidationError };