@calmlens/sdk 0.10.1 → 0.12.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.
@@ -0,0 +1,362 @@
1
+ //#region src/client/CalmLensErrorCodes.ts
2
+ /** Error codes the CalmLens API documents, by name. */
3
+ const API_ERROR_CODES = {
4
+ MISSING_TOKEN: "auth/missing-token",
5
+ INVALID_API_KEY: "auth/invalid-api-key",
6
+ API_KEY_PROJECT_MISMATCH: "auth/project-mismatch",
7
+ API_KEY_NOT_ACCEPTED: "auth/api-key-not-accepted",
8
+ FILE_TOO_LARGE: "upload/file-too-large",
9
+ IMAGE_REQUIRES_MULTIPART_UPLOAD: "upload/image-requires-multipart-upload",
10
+ MULTIPART_IMAGE_TOO_SMALL: "upload/multipart-image-too-small",
11
+ MULTIPART_IMAGE_TOO_LARGE: "upload/multipart-image-too-large",
12
+ IMAGE_EXCEEDED_MAX_DIMENSIONS: "upload/image-exceeded-max-dimensions",
13
+ UPLOAD_LIMIT_EXCEEDED: "upload/limit-exceeded",
14
+ INVALID_UPLOAD_MIME_TYPE: "upload/invalid-mime-type",
15
+ INVALID_EVENT_CURSOR: "events/invalid-cursor",
16
+ INVALID_UPLOAD_PAYLOAD: "upload/invalid-payload",
17
+ USE_UPLOAD_PART: "upload/use-upload-part",
18
+ UPLOAD_NOT_OPEN: "upload/not-open",
19
+ INVALID_UPLOAD_PART_NUMBER: "upload/invalid-part-number",
20
+ UPLOAD_MISSING: "upload/missing",
21
+ UPLOAD_SIZE_MISMATCH: "upload/size-mismatch",
22
+ INVALID_IDEMPOTENCY_KEY: "idempotency/invalid-key",
23
+ IDEMPOTENCY_KEY_REUSED: "idempotency/key-reused",
24
+ IDEMPOTENT_REQUEST_IN_PROGRESS: "idempotency/request-in-progress",
25
+ ASSET_NOT_FOUND: "asset/not-found",
26
+ PROJECT_NOT_FOUND: "project/not-found",
27
+ ASSET_UPDATE_FAILED: "asset/update-failed",
28
+ ASSET_SAFETY_HELD: "asset/safety-held",
29
+ ASSET_NOT_RETRYABLE: "asset/not-retryable",
30
+ ASSET_CONTENT_UNAVAILABLE: "asset/content-unavailable",
31
+ SUPPORT_TICKET_NOT_FOUND: "support/ticket-not-found",
32
+ DUPLICATE_EXTERNAL_ID: "asset/duplicate-external-id",
33
+ FOLDER_NOT_FOUND: "folder/not-found",
34
+ DUPLICATE_FOLDER_NAME: "folder/duplicate-name",
35
+ FOLDER_NOT_EMPTY: "folder/not-empty",
36
+ FOLDER_PARENT_CYCLE: "folder/parent-cycle",
37
+ LENS_VERSION_CONFLICT: "lens/version-conflict",
38
+ UPLOAD_PRESET_NOT_FOUND: "upload-preset/not-found",
39
+ UPLOAD_PRESET_DISABLED: "upload-preset/disabled",
40
+ UPLOAD_PRESET_TOKEN_REQUIRED: "upload-preset/token-required",
41
+ UPLOAD_PRESET_CONTENT_TYPE_NOT_ALLOWED: "upload-preset/content-type-not-allowed",
42
+ UPLOAD_PRESET_TOO_LARGE: "upload-preset/too-large",
43
+ UPLOAD_PRESET_DAILY_LIMIT_REACHED: "upload-preset/daily-limit-reached",
44
+ UPLOAD_PRESET_ORIGIN_NOT_ALLOWED: "upload-preset/origin-not-allowed",
45
+ UPLOAD_PRESET_FIELD_NOT_ALLOWED: "upload-preset/field-not-allowed",
46
+ UPLOAD_TOKEN_INVALID: "upload-token/invalid",
47
+ UPLOAD_TOKEN_EXPIRED: "upload-token/expired",
48
+ UPLOAD_TOKEN_USED: "upload-token/used"
49
+ };
50
+ /** The HTTP status and default message the API returns with each documented error code. */
51
+ const API_ERROR_DEFINITIONS = {
52
+ "auth/missing-token": {
53
+ status: 401,
54
+ message: "Authentication required. Send your project API key in the X-Api-Key header."
55
+ },
56
+ "auth/invalid-api-key": {
57
+ status: 401,
58
+ message: "The API key is invalid, revoked or expired. Create a key under Developer → API Keys."
59
+ },
60
+ "auth/project-mismatch": {
61
+ status: 403,
62
+ message: "This API key belongs to a different project. Use a key created for the project that owns this resource."
63
+ },
64
+ "auth/api-key-not-accepted": {
65
+ status: 403,
66
+ message: "API keys are only accepted by the public API at /v1."
67
+ },
68
+ "upload/file-too-large": {
69
+ status: 400,
70
+ message: "File size exceeds limit. Maximum direct image size is 10MB, audio/video size is 50MiB, and other files is 5GB."
71
+ },
72
+ "upload/image-requires-multipart-upload": {
73
+ status: 400,
74
+ message: "Images of 10MB or more must use a multipart upload."
75
+ },
76
+ "upload/multipart-image-too-small": {
77
+ status: 400,
78
+ message: "Multipart image uploads must total at least 10MB."
79
+ },
80
+ "upload/multipart-image-too-large": {
81
+ status: 400,
82
+ message: "Multipart image uploads cannot exceed 20MB."
83
+ },
84
+ "upload/image-exceeded-max-dimensions": {
85
+ status: 400,
86
+ message: "Image exceeds maximum dimensions. Maximum dimensions are 12,000px."
87
+ },
88
+ "upload/limit-exceeded": {
89
+ status: 403,
90
+ message: "Upload limit exceeded. You have used all your free uploads. Please upgrade to Plus or Pro for unlimited uploads."
91
+ },
92
+ "upload/invalid-mime-type": {
93
+ status: 400,
94
+ message: "Upload file type is not supported. Uploads must use a concrete file MIME type that CalmLens can store and process, such as image/png, image/jpeg, application/pdf, or text/plain."
95
+ },
96
+ "events/invalid-cursor": {
97
+ status: 400,
98
+ message: "The events cursor is invalid. Use the cursor from a previous response, or omit it to start from now."
99
+ },
100
+ "upload/invalid-payload": {
101
+ status: 400,
102
+ message: "Upload payload is invalid."
103
+ },
104
+ "upload/use-upload-part": {
105
+ status: 409,
106
+ message: "Use uploadPart for multipart upload parts after the first part"
107
+ },
108
+ "upload/not-open": {
109
+ status: 409,
110
+ message: "Asset is not accepting additional parts"
111
+ },
112
+ "upload/invalid-part-number": {
113
+ status: 409,
114
+ message: "Multipart upload part number is invalid."
115
+ },
116
+ "upload/missing": {
117
+ status: 409,
118
+ message: "Multipart upload state is missing"
119
+ },
120
+ "upload/size-mismatch": {
121
+ status: 400,
122
+ message: "The uploaded parts do not add up to the totalSizeInBytes declared when the multipart upload started."
123
+ },
124
+ "idempotency/invalid-key": {
125
+ status: 400,
126
+ message: "The Idempotency-Key header must be 1 to 255 printable ASCII characters, such as a UUID."
127
+ },
128
+ "idempotency/key-reused": {
129
+ status: 409,
130
+ message: "This Idempotency-Key was already used for a different request. Use a new key for each distinct request."
131
+ },
132
+ "idempotency/request-in-progress": {
133
+ status: 409,
134
+ message: "A request with this Idempotency-Key is still being processed. Retry after the Retry-After delay to receive its result."
135
+ },
136
+ "asset/not-found": {
137
+ status: 404,
138
+ message: "Asset not found"
139
+ },
140
+ "project/not-found": {
141
+ status: 404,
142
+ message: "Project not found"
143
+ },
144
+ "asset/update-failed": {
145
+ status: 500,
146
+ message: "Failed to update asset"
147
+ },
148
+ "asset/safety-held": {
149
+ status: 423,
150
+ message: "Asset access is restricted by a safety hold."
151
+ },
152
+ "asset/not-retryable": {
153
+ status: 409,
154
+ message: "Only assets that failed to process (status error) can be retried."
155
+ },
156
+ "asset/content-unavailable": {
157
+ status: 409,
158
+ message: "This asset's content is no longer stored, so it can't be processed again. Upload it again instead."
159
+ },
160
+ "support/ticket-not-found": {
161
+ status: 404,
162
+ message: "Support ticket not found."
163
+ },
164
+ "asset/duplicate-external-id": {
165
+ status: 409,
166
+ message: "An asset with this externalId already exists in the project."
167
+ },
168
+ "folder/not-found": {
169
+ status: 404,
170
+ message: "Folder not found"
171
+ },
172
+ "folder/duplicate-name": {
173
+ status: 409,
174
+ message: "A folder with this name already exists in this location."
175
+ },
176
+ "folder/not-empty": {
177
+ status: 409,
178
+ message: "Folder must be empty before it can be deleted."
179
+ },
180
+ "folder/parent-cycle": {
181
+ status: 409,
182
+ message: "A folder cannot be moved inside itself or one of its descendants."
183
+ },
184
+ "lens/version-conflict": {
185
+ status: 409,
186
+ message: "Lens updates require a version greater than the current version."
187
+ },
188
+ "upload-preset/not-found": {
189
+ status: 404,
190
+ message: "Upload preset not found. Check the preset ID in the portal."
191
+ },
192
+ "upload-preset/disabled": {
193
+ status: 403,
194
+ message: "This upload preset is paused. Turn it on in the portal to accept uploads."
195
+ },
196
+ "upload-preset/token-required": {
197
+ status: 401,
198
+ message: "This upload preset only accepts uploads with an upload token. Sign one on your server with uploadTokens.sign and pass it to the client."
199
+ },
200
+ "upload-preset/content-type-not-allowed": {
201
+ status: 415,
202
+ message: "This upload preset doesn't accept this file type."
203
+ },
204
+ "upload-preset/too-large": {
205
+ status: 413,
206
+ message: "This file is larger than the upload preset allows."
207
+ },
208
+ "upload-preset/daily-limit-reached": {
209
+ status: 429,
210
+ message: "This upload preset has reached its daily upload limit. Try again after midnight UTC, or raise the limit in the portal."
211
+ },
212
+ "upload-preset/origin-not-allowed": {
213
+ status: 403,
214
+ message: "This site isn't allowed to upload through this upload preset."
215
+ },
216
+ "upload-preset/field-not-allowed": {
217
+ status: 400,
218
+ message: "This field is set by the upload preset or the upload token and can't be changed by the upload."
219
+ },
220
+ "upload-token/invalid": {
221
+ status: 401,
222
+ message: "The upload token is invalid. Sign a new one on your server with an active API key for this project."
223
+ },
224
+ "upload-token/expired": {
225
+ status: 401,
226
+ message: "The upload token has expired. Sign a new one on your server."
227
+ },
228
+ "upload-token/used": {
229
+ status: 409,
230
+ message: "The upload token has already been used. Each token uploads one asset."
231
+ }
232
+ };
233
+ //#endregion
234
+ //#region src/client/CalmLensError.ts
235
+ /** Codes for failures the SDK detects itself, before or instead of an API error response. */
236
+ const SDK_ERROR_CODES = {
237
+ /** The request could not be completed: DNS, TLS, a refused or reset connection, a truncated body. */
238
+ TRANSPORT_ERROR: "sdk/transport-error",
239
+ /** The request did not finish within `timeoutMs`. */
240
+ TIMEOUT: "sdk/timeout",
241
+ /** The caller's `AbortSignal` cancelled the request. */
242
+ CANCELLED: "sdk/cancelled",
243
+ /** A successful response whose body is not the JSON the operation returns. */
244
+ INVALID_RESPONSE: "sdk/invalid-response",
245
+ /** An HTTP failure without a CalmLens error body, for example from a proxy. */
246
+ HTTP_ERROR: "sdk/http-error",
247
+ /** A response body larger than `maxResponseBytes`. */
248
+ RESPONSE_TOO_LARGE: "sdk/response-too-large",
249
+ /** A method argument is missing or invalid, for example an empty asset ID. */
250
+ INVALID_ARGUMENT: "sdk/invalid-argument",
251
+ /** The client options are invalid, for example a missing API key. */
252
+ INVALID_CONFIG: "sdk/invalid-config",
253
+ /** No `fetch` implementation is available in this runtime. */
254
+ NO_FETCH: "sdk/no-fetch",
255
+ /** A webhook delivery whose signature or timestamp did not verify. */
256
+ INVALID_WEBHOOK_SIGNATURE: "sdk/invalid-webhook-signature",
257
+ /** A verified webhook delivery whose body is not a webhook event. */
258
+ INVALID_WEBHOOK_PAYLOAD: "sdk/invalid-webhook-payload"
259
+ };
260
+ /** Every error code the SDK can throw: the documented API codes and the SDK's own `sdk/*` codes. */
261
+ const CALMLENS_ERROR_CODES = {
262
+ ...API_ERROR_CODES,
263
+ ...SDK_ERROR_CODES
264
+ };
265
+ const BRAND = Symbol.for("calmlens.sdk.CalmLensError");
266
+ const RETRYABLE_STATUSES = /* @__PURE__ */ new Set([
267
+ 429,
268
+ 502,
269
+ 503,
270
+ 504
271
+ ]);
272
+ const RETRYABLE_CODES = /* @__PURE__ */ new Set([SDK_ERROR_CODES.TRANSPORT_ERROR, API_ERROR_CODES.IDEMPOTENT_REQUEST_IN_PROGRESS]);
273
+ /**
274
+ * Every failure the SDK reports, whether the API returned an error or the request never
275
+ * completed. Branch on `code`; `status`, `requestId`, `headers` and `retryAfter` describe the
276
+ * HTTP response when there was one, and `cause` holds the underlying error.
277
+ *
278
+ * `instanceof CalmLensError` and `CalmLensError.isCalmLensError(value)` both recognise errors
279
+ * from any copy of the SDK, so they work when ESM and CommonJS builds are loaded side by side.
280
+ */
281
+ var CalmLensError = class CalmLensError extends Error {
282
+ constructor(code, options = {}) {
283
+ const id = typeof code === "string" ? code : code.id;
284
+ const defaultMessage = typeof code === "string" ? void 0 : code.defaultMessage;
285
+ super(`[calmlens] ${options.message ?? defaultMessage ?? id}`, { cause: options.cause });
286
+ this.name = "CalmLensError";
287
+ this.code = id;
288
+ this.status = options.status;
289
+ this.details = options.details;
290
+ this.requestId = options.requestId;
291
+ this.headers = options.headers;
292
+ this.retryAfter = options.retryAfter;
293
+ Object.defineProperty(this, BRAND, { value: true });
294
+ }
295
+ /** True for any `CalmLensError`, including one created by another copy or build of the SDK. */
296
+ static isCalmLensError(value) {
297
+ return typeof value === "object" && value !== null && value[BRAND] === true;
298
+ }
299
+ static [Symbol.hasInstance](value) {
300
+ return CalmLensError.isCalmLensError(value);
301
+ }
302
+ /** The resource does not exist or is not visible to this API key. */
303
+ isNotFound() {
304
+ return this.status === 404;
305
+ }
306
+ /**
307
+ * Whether repeating the same request may succeed: transport failures, throttling (429),
308
+ * gateway errors (502, 503, 504) and idempotent requests still in progress. Only repeat a
309
+ * create or upload with the same `idempotencyKey`.
310
+ */
311
+ isRetryable() {
312
+ return RETRYABLE_CODES.has(this.code) || this.status !== void 0 && RETRYABLE_STATUSES.has(this.status);
313
+ }
314
+ };
315
+ const invalidArgument = (message, details) => new CalmLensError(SDK_ERROR_CODES.INVALID_ARGUMENT, {
316
+ message,
317
+ details
318
+ });
319
+ const invalidConfig = (message) => new CalmLensError(SDK_ERROR_CODES.INVALID_CONFIG, { message });
320
+ //#endregion
321
+ Object.defineProperty(exports, "API_ERROR_CODES", {
322
+ enumerable: true,
323
+ get: function() {
324
+ return API_ERROR_CODES;
325
+ }
326
+ });
327
+ Object.defineProperty(exports, "API_ERROR_DEFINITIONS", {
328
+ enumerable: true,
329
+ get: function() {
330
+ return API_ERROR_DEFINITIONS;
331
+ }
332
+ });
333
+ Object.defineProperty(exports, "CALMLENS_ERROR_CODES", {
334
+ enumerable: true,
335
+ get: function() {
336
+ return CALMLENS_ERROR_CODES;
337
+ }
338
+ });
339
+ Object.defineProperty(exports, "CalmLensError", {
340
+ enumerable: true,
341
+ get: function() {
342
+ return CalmLensError;
343
+ }
344
+ });
345
+ Object.defineProperty(exports, "SDK_ERROR_CODES", {
346
+ enumerable: true,
347
+ get: function() {
348
+ return SDK_ERROR_CODES;
349
+ }
350
+ });
351
+ Object.defineProperty(exports, "invalidArgument", {
352
+ enumerable: true,
353
+ get: function() {
354
+ return invalidArgument;
355
+ }
356
+ });
357
+ Object.defineProperty(exports, "invalidConfig", {
358
+ enumerable: true,
359
+ get: function() {
360
+ return invalidConfig;
361
+ }
362
+ });