@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,253 @@
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
+ class ATProtoSDKError extends Error {
41
+ /**
42
+ * Creates a new SDK error.
43
+ *
44
+ * @param message - Human-readable error description
45
+ * @param code - Machine-readable error code for programmatic handling
46
+ * @param status - HTTP status code associated with this error type
47
+ * @param cause - The underlying error that caused this error, if any
48
+ */
49
+ constructor(message, code, status, cause) {
50
+ super(message);
51
+ this.code = code;
52
+ this.status = status;
53
+ this.cause = cause;
54
+ this.name = "ATProtoSDKError";
55
+ Error.captureStackTrace?.(this, this.constructor);
56
+ }
57
+ }
58
+ /**
59
+ * Error thrown when authentication fails.
60
+ *
61
+ * This error indicates problems with the OAuth flow, invalid credentials,
62
+ * or failed token exchanges. Common causes:
63
+ * - Invalid authorization code
64
+ * - Expired or invalid state parameter
65
+ * - Revoked or invalid tokens
66
+ * - User denied authorization
67
+ *
68
+ * @example
69
+ * ```typescript
70
+ * try {
71
+ * const session = await sdk.callback(params);
72
+ * } catch (error) {
73
+ * if (error instanceof AuthenticationError) {
74
+ * // Clear any stored state and redirect to login
75
+ * console.error("Authentication failed:", error.message);
76
+ * }
77
+ * }
78
+ * ```
79
+ */
80
+ class AuthenticationError extends ATProtoSDKError {
81
+ /**
82
+ * Creates an authentication error.
83
+ *
84
+ * @param message - Description of what went wrong during authentication
85
+ * @param cause - The underlying error (e.g., from the OAuth client)
86
+ */
87
+ constructor(message, cause) {
88
+ super(message, "AUTHENTICATION_ERROR", 401, cause);
89
+ this.name = "AuthenticationError";
90
+ }
91
+ }
92
+ /**
93
+ * Error thrown when a session has expired and cannot be refreshed.
94
+ *
95
+ * This typically occurs when:
96
+ * - The refresh token has expired (usually after extended inactivity)
97
+ * - The user has revoked access to your application
98
+ * - The PDS has invalidated all sessions for the user
99
+ *
100
+ * When this error occurs, the user must re-authenticate.
101
+ *
102
+ * @example
103
+ * ```typescript
104
+ * try {
105
+ * const session = await sdk.restoreSession(did);
106
+ * } catch (error) {
107
+ * if (error instanceof SessionExpiredError) {
108
+ * // Clear stored session and prompt user to log in again
109
+ * localStorage.removeItem("userDid");
110
+ * window.location.href = "/login";
111
+ * }
112
+ * }
113
+ * ```
114
+ */
115
+ class SessionExpiredError extends ATProtoSDKError {
116
+ /**
117
+ * Creates a session expired error.
118
+ *
119
+ * @param message - Description of why the session expired
120
+ * @param cause - The underlying error from the token refresh attempt
121
+ */
122
+ constructor(message = "Session expired", cause) {
123
+ super(message, "SESSION_EXPIRED", 401, cause);
124
+ this.name = "SessionExpiredError";
125
+ }
126
+ }
127
+ /**
128
+ * Error thrown when input validation fails.
129
+ *
130
+ * This error indicates that provided data doesn't meet the required format
131
+ * or constraints. Common causes:
132
+ * - Missing required fields
133
+ * - Invalid URL formats
134
+ * - Invalid DID format
135
+ * - Schema validation failures for records
136
+ * - Invalid configuration values
137
+ *
138
+ * @example
139
+ * ```typescript
140
+ * try {
141
+ * await sdk.authorize(""); // Empty identifier
142
+ * } catch (error) {
143
+ * if (error instanceof ValidationError) {
144
+ * console.error("Invalid input:", error.message);
145
+ * // Show validation error to user
146
+ * }
147
+ * }
148
+ * ```
149
+ *
150
+ * @example With Zod validation cause
151
+ * ```typescript
152
+ * try {
153
+ * await repo.records.create(collection, record);
154
+ * } catch (error) {
155
+ * if (error instanceof ValidationError && error.cause) {
156
+ * // error.cause may be a ZodError with detailed field errors
157
+ * const zodError = error.cause as ZodError;
158
+ * zodError.errors.forEach(e => {
159
+ * console.error(`Field ${e.path.join(".")}: ${e.message}`);
160
+ * });
161
+ * }
162
+ * }
163
+ * ```
164
+ */
165
+ class ValidationError extends ATProtoSDKError {
166
+ /**
167
+ * Creates a validation error.
168
+ *
169
+ * @param message - Description of what validation failed
170
+ * @param cause - The underlying validation error (e.g., ZodError)
171
+ */
172
+ constructor(message, cause) {
173
+ super(message, "VALIDATION_ERROR", 400, cause);
174
+ this.name = "ValidationError";
175
+ }
176
+ }
177
+ /**
178
+ * Error thrown when a network request fails.
179
+ *
180
+ * This error indicates connectivity issues or server unavailability.
181
+ * Common causes:
182
+ * - No internet connection
183
+ * - DNS resolution failure
184
+ * - Server timeout
185
+ * - Server returned 5xx error
186
+ * - TLS/SSL errors
187
+ *
188
+ * These errors are typically transient and may succeed on retry.
189
+ *
190
+ * @example
191
+ * ```typescript
192
+ * try {
193
+ * await repo.records.list(collection);
194
+ * } catch (error) {
195
+ * if (error instanceof NetworkError) {
196
+ * // Implement retry logic or show offline indicator
197
+ * console.error("Network error:", error.message);
198
+ * await retryWithBackoff(() => repo.records.list(collection));
199
+ * }
200
+ * }
201
+ * ```
202
+ */
203
+ class NetworkError extends ATProtoSDKError {
204
+ /**
205
+ * Creates a network error.
206
+ *
207
+ * @param message - Description of the network failure
208
+ * @param cause - The underlying error (e.g., fetch error, timeout)
209
+ */
210
+ constructor(message, cause) {
211
+ super(message, "NETWORK_ERROR", 503, cause);
212
+ this.name = "NetworkError";
213
+ }
214
+ }
215
+ /**
216
+ * Error thrown when an SDS-only operation is attempted on a PDS.
217
+ *
218
+ * Certain operations are only available on Shared Data Servers (SDS),
219
+ * such as collaborator management and organization operations.
220
+ * This error is thrown when these operations are attempted on a
221
+ * Personal Data Server (PDS).
222
+ *
223
+ * @example
224
+ * ```typescript
225
+ * const pdsRepo = sdk.repository(session); // Default is PDS
226
+ *
227
+ * try {
228
+ * // This will throw SDSRequiredError
229
+ * await pdsRepo.collaborators.list();
230
+ * } catch (error) {
231
+ * if (error instanceof SDSRequiredError) {
232
+ * // Switch to SDS for this operation
233
+ * const sdsRepo = sdk.repository(session, { server: "sds" });
234
+ * const collaborators = await sdsRepo.collaborators.list();
235
+ * }
236
+ * }
237
+ * ```
238
+ */
239
+ class SDSRequiredError extends ATProtoSDKError {
240
+ /**
241
+ * Creates an SDS required error.
242
+ *
243
+ * @param message - Description of which operation requires SDS
244
+ * @param cause - Any underlying error
245
+ */
246
+ constructor(message = "This operation requires a Shared Data Server (SDS)", cause) {
247
+ super(message, "SDS_REQUIRED", 400, cause);
248
+ this.name = "SDSRequiredError";
249
+ }
250
+ }
251
+
252
+ export { ATProtoSDKError, AuthenticationError, NetworkError, SDSRequiredError, SessionExpiredError, ValidationError };
253
+ //# sourceMappingURL=errors.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.mjs","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;;;;"}