calmlens 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +523 -2
- package/bin/calmlens.js +1157 -0
- package/cjs/CalmLensTypes-Bnq4u8HJ.d.ts +1758 -0
- package/cjs/CalmLensUtils-DioFUAmI.js +534 -0
- package/cjs/index.d.ts +158 -0
- package/cjs/index.js +778 -0
- package/cjs/package.json +3 -0
- package/cjs/testing.d.ts +138 -0
- package/cjs/testing.js +674 -0
- package/esm/CalmLensTypes-Bnq4u8HJ.d.ts +1758 -0
- package/esm/CalmLensUtils-Dixed1Ox.js +415 -0
- package/esm/index.d.ts +158 -0
- package/esm/index.js +775 -0
- package/esm/package.json +3 -0
- package/esm/testing.d.ts +138 -0
- package/esm/testing.js +646 -0
- package/package.json +89 -4
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
import { ErrorCode } from "sendable-error";
|
|
2
|
+
//#region \0@oxc-project+runtime@0.148.0/helpers/esm/asyncToGenerator.js
|
|
3
|
+
function asyncGeneratorStep(n, t, e, r, o, a, c) {
|
|
4
|
+
try {
|
|
5
|
+
var i = n[a](c), u = i.value;
|
|
6
|
+
} catch (n) {
|
|
7
|
+
e(n);
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
i.done ? t(u) : Promise.resolve(u).then(r, o);
|
|
11
|
+
}
|
|
12
|
+
function _asyncToGenerator(n) {
|
|
13
|
+
return function() {
|
|
14
|
+
var t = this, e = arguments;
|
|
15
|
+
return new Promise(function(r, o) {
|
|
16
|
+
var a = n.apply(t, e);
|
|
17
|
+
function _next(n) {
|
|
18
|
+
asyncGeneratorStep(a, r, o, _next, _throw, "next", n);
|
|
19
|
+
}
|
|
20
|
+
function _throw(n) {
|
|
21
|
+
asyncGeneratorStep(a, r, o, _next, _throw, "throw", n);
|
|
22
|
+
}
|
|
23
|
+
_next(void 0);
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
//#endregion
|
|
28
|
+
//#region ../shared/src/AssetPayload.ts
|
|
29
|
+
const uint8ArrayInputToNumbers = (value) => {
|
|
30
|
+
if (Array.isArray(value)) return value;
|
|
31
|
+
return Array.from(value);
|
|
32
|
+
};
|
|
33
|
+
const bufferInputToBytes = (value) => {
|
|
34
|
+
if (value instanceof ArrayBuffer) return new Uint8Array(value);
|
|
35
|
+
return Uint8Array.from(value);
|
|
36
|
+
};
|
|
37
|
+
const BASE64_PART_BOUNDARY_ERROR = "Invalid base64 part. For multipart uploads, split the original bytes first, then base64-encode each part. Do not split one full base64 string into substrings.";
|
|
38
|
+
const MULTIPART_PART_TOO_SMALL_ERROR = `Multipart upload parts must be at least 5MiB except the final part.`;
|
|
39
|
+
const getRawBase64 = (value) => {
|
|
40
|
+
const markerIndex = value.indexOf(";base64,");
|
|
41
|
+
if (markerIndex === -1) return value;
|
|
42
|
+
return value.slice(markerIndex + 8);
|
|
43
|
+
};
|
|
44
|
+
const validateBase64PayloadString = (payload, options) => {
|
|
45
|
+
const raw = getRawBase64(payload.base64).replace(/\s/g, "");
|
|
46
|
+
if ((options === null || options === void 0 ? void 0 : options.multipart) && raw.length % 4 !== 0) return {
|
|
47
|
+
ok: false,
|
|
48
|
+
error: BASE64_PART_BOUNDARY_ERROR
|
|
49
|
+
};
|
|
50
|
+
return { ok: true };
|
|
51
|
+
};
|
|
52
|
+
const isReadableStream = (value) => typeof value === "object" && value !== null && "getReader" in value && typeof value.getReader === "function";
|
|
53
|
+
const assetToData = (payload) => {
|
|
54
|
+
if ("plainText" in payload && payload.plainText !== void 0) return new TextEncoder().encode(payload.plainText);
|
|
55
|
+
if ("data" in payload && payload.data) return payload.data;
|
|
56
|
+
if ("buffer" in payload) {
|
|
57
|
+
if (payload.buffer instanceof ArrayBuffer) return payload.buffer;
|
|
58
|
+
return bufferInputToBytes(payload.buffer);
|
|
59
|
+
}
|
|
60
|
+
if (payload.uint8Array !== void 0) return Uint8Array.from(uint8ArrayInputToNumbers(payload.uint8Array));
|
|
61
|
+
if (payload.base64 === void 0) throw new Error("Invalid asset payload");
|
|
62
|
+
const raw = getRawBase64(payload.base64);
|
|
63
|
+
if (typeof Buffer !== "undefined") return Uint8Array.from(Buffer.from(raw, "base64"));
|
|
64
|
+
const binary = atob(raw);
|
|
65
|
+
return Uint8Array.from(binary, (char) => char.charCodeAt(0));
|
|
66
|
+
};
|
|
67
|
+
const assetDataToUint8Array = function() {
|
|
68
|
+
var _ref = _asyncToGenerator(function* (data) {
|
|
69
|
+
if (data instanceof Response) return new Uint8Array(yield data.arrayBuffer());
|
|
70
|
+
if (isReadableStream(data)) return new Uint8Array(yield new Response(data).arrayBuffer());
|
|
71
|
+
if (data instanceof Blob) return new Uint8Array(yield data.arrayBuffer());
|
|
72
|
+
if (data instanceof ArrayBuffer) return new Uint8Array(data);
|
|
73
|
+
return data;
|
|
74
|
+
});
|
|
75
|
+
return function assetDataToUint8Array(_x) {
|
|
76
|
+
return _ref.apply(this, arguments);
|
|
77
|
+
};
|
|
78
|
+
}();
|
|
79
|
+
const getAssetPayloadByteLength = (payload) => {
|
|
80
|
+
if ("plainText" in payload && payload.plainText !== void 0) return new TextEncoder().encode(payload.plainText).byteLength;
|
|
81
|
+
if ("data" in payload && payload.data) {
|
|
82
|
+
if (payload.data instanceof Blob) return payload.data.size;
|
|
83
|
+
if (payload.data instanceof Response) {
|
|
84
|
+
const contentLength = payload.data.headers.get("content-length");
|
|
85
|
+
return contentLength ? Number(contentLength) : void 0;
|
|
86
|
+
}
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if ("buffer" in payload && payload.buffer) return payload.buffer.byteLength;
|
|
90
|
+
if (payload.uint8Array !== void 0) {
|
|
91
|
+
if (Array.isArray(payload.uint8Array)) return payload.uint8Array.length;
|
|
92
|
+
return payload.uint8Array.byteLength;
|
|
93
|
+
}
|
|
94
|
+
if (payload.base64 === void 0) return;
|
|
95
|
+
const raw = getRawBase64(payload.base64).replace(/\s/g, "");
|
|
96
|
+
const padding = raw.endsWith("==") ? 2 : raw.endsWith("=") ? 1 : 0;
|
|
97
|
+
return Math.max(0, Math.floor(raw.length * 3 / 4) - padding);
|
|
98
|
+
};
|
|
99
|
+
const validateAssetPayload = (payload, options) => {
|
|
100
|
+
if ("base64" in payload && payload.base64 !== void 0) {
|
|
101
|
+
const validation = validateBase64PayloadString(payload, options);
|
|
102
|
+
if (!validation.ok) return {
|
|
103
|
+
ok: false,
|
|
104
|
+
error: validation.error
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
const byteLength = getAssetPayloadByteLength(payload);
|
|
108
|
+
if ((options === null || options === void 0 ? void 0 : options.multipart) && !options.isFinal && byteLength !== void 0 && byteLength < 5242880) return {
|
|
109
|
+
ok: false,
|
|
110
|
+
error: MULTIPART_PART_TOO_SMALL_ERROR
|
|
111
|
+
};
|
|
112
|
+
return { ok: true };
|
|
113
|
+
};
|
|
114
|
+
//#endregion
|
|
115
|
+
//#region ../shared/src/JsonUtils.ts
|
|
116
|
+
const stringifyJsonCanonical = (value) => JSON.stringify(value, (_key, nestedValue) => {
|
|
117
|
+
if (!nestedValue || typeof nestedValue !== "object" || Array.isArray(nestedValue)) return nestedValue;
|
|
118
|
+
return Object.fromEntries(Object.keys(nestedValue).sort().map((key) => [key, nestedValue[key]]));
|
|
119
|
+
});
|
|
120
|
+
//#endregion
|
|
121
|
+
//#region ../shared/src/PublicErrorCodes.ts
|
|
122
|
+
const PUBLIC_ERROR_CODES = {
|
|
123
|
+
MISSING_TOKEN: new ErrorCode({
|
|
124
|
+
id: "auth/missing-token",
|
|
125
|
+
defaultMessage: "Authentication required. Send your project API key in the X-Api-Key header.",
|
|
126
|
+
status: 401
|
|
127
|
+
}),
|
|
128
|
+
INVALID_API_KEY: new ErrorCode({
|
|
129
|
+
id: "auth/invalid-api-key",
|
|
130
|
+
defaultMessage: "The API key is invalid, revoked or expired. Create a key under Developer → API Keys.",
|
|
131
|
+
status: 401
|
|
132
|
+
}),
|
|
133
|
+
API_KEY_PROJECT_MISMATCH: new ErrorCode({
|
|
134
|
+
id: "auth/project-mismatch",
|
|
135
|
+
defaultMessage: "This API key belongs to a different project. Use a key created for the project that owns this resource.",
|
|
136
|
+
status: 403
|
|
137
|
+
}),
|
|
138
|
+
FILE_TOO_LARGE: new ErrorCode({
|
|
139
|
+
id: "upload/file-too-large",
|
|
140
|
+
defaultMessage: "File size exceeds limit. Maximum size for images is 10MB, for other files is 5GB.",
|
|
141
|
+
status: 400
|
|
142
|
+
}),
|
|
143
|
+
IMAGE_REQUIRES_MULTIPART_UPLOAD: new ErrorCode({
|
|
144
|
+
id: "upload/image-requires-multipart-upload",
|
|
145
|
+
defaultMessage: "Images of 10MB or more must use a multipart upload.",
|
|
146
|
+
status: 400
|
|
147
|
+
}),
|
|
148
|
+
MULTIPART_IMAGE_TOO_SMALL: new ErrorCode({
|
|
149
|
+
id: "upload/multipart-image-too-small",
|
|
150
|
+
defaultMessage: "Multipart image uploads must total at least 10MB.",
|
|
151
|
+
status: 400
|
|
152
|
+
}),
|
|
153
|
+
MULTIPART_IMAGE_TOO_LARGE: new ErrorCode({
|
|
154
|
+
id: "upload/multipart-image-too-large",
|
|
155
|
+
defaultMessage: "Multipart image uploads cannot exceed 20MB.",
|
|
156
|
+
status: 400
|
|
157
|
+
}),
|
|
158
|
+
IMAGE_EXCEEDED_MAX_DIMENSIONS: new ErrorCode({
|
|
159
|
+
id: "upload/image-exceeded-max-dimensions",
|
|
160
|
+
defaultMessage: "Image exceeds maximum dimensions. Maximum dimensions are 12,000px.",
|
|
161
|
+
status: 400
|
|
162
|
+
}),
|
|
163
|
+
UPLOAD_LIMIT_EXCEEDED: new ErrorCode({
|
|
164
|
+
id: "upload/limit-exceeded",
|
|
165
|
+
defaultMessage: "Upload limit exceeded. You have used all your free uploads. Please upgrade to Plus or Pro for unlimited uploads.",
|
|
166
|
+
status: 403
|
|
167
|
+
}),
|
|
168
|
+
INVALID_UPLOAD_MIME_TYPE: new ErrorCode({
|
|
169
|
+
id: "upload/invalid-mime-type",
|
|
170
|
+
defaultMessage: "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.",
|
|
171
|
+
status: 400
|
|
172
|
+
}),
|
|
173
|
+
INVALID_EVENT_CURSOR: new ErrorCode({
|
|
174
|
+
id: "events/invalid-cursor",
|
|
175
|
+
defaultMessage: "The events cursor is invalid. Use the cursor from a previous response, or omit it to start from now.",
|
|
176
|
+
status: 400
|
|
177
|
+
}),
|
|
178
|
+
INVALID_UPLOAD_PAYLOAD: new ErrorCode({
|
|
179
|
+
id: "upload/invalid-payload",
|
|
180
|
+
defaultMessage: "Upload payload is invalid.",
|
|
181
|
+
status: 400
|
|
182
|
+
}),
|
|
183
|
+
USE_UPLOAD_PART: new ErrorCode({
|
|
184
|
+
id: "upload/use-upload-part",
|
|
185
|
+
defaultMessage: "Use uploadPart for multipart upload parts after the first part",
|
|
186
|
+
status: 409
|
|
187
|
+
}),
|
|
188
|
+
UPLOAD_NOT_OPEN: new ErrorCode({
|
|
189
|
+
id: "upload/not-open",
|
|
190
|
+
defaultMessage: "Asset is not accepting additional parts",
|
|
191
|
+
status: 409
|
|
192
|
+
}),
|
|
193
|
+
INVALID_UPLOAD_PART_NUMBER: new ErrorCode({
|
|
194
|
+
id: "upload/invalid-part-number",
|
|
195
|
+
defaultMessage: "Multipart upload part number is invalid.",
|
|
196
|
+
status: 409
|
|
197
|
+
}),
|
|
198
|
+
UPLOAD_MISSING: new ErrorCode({
|
|
199
|
+
id: "upload/missing",
|
|
200
|
+
defaultMessage: "Multipart upload state is missing",
|
|
201
|
+
status: 409
|
|
202
|
+
}),
|
|
203
|
+
ASSET_NOT_FOUND: new ErrorCode({
|
|
204
|
+
id: "asset/not-found",
|
|
205
|
+
defaultMessage: "Asset not found",
|
|
206
|
+
status: 404
|
|
207
|
+
}),
|
|
208
|
+
PROJECT_NOT_FOUND: new ErrorCode({
|
|
209
|
+
id: "project/not-found",
|
|
210
|
+
defaultMessage: "Project not found",
|
|
211
|
+
status: 404
|
|
212
|
+
}),
|
|
213
|
+
ASSET_UPDATE_FAILED: new ErrorCode({
|
|
214
|
+
id: "asset/update-failed",
|
|
215
|
+
defaultMessage: "Failed to update asset",
|
|
216
|
+
status: 500
|
|
217
|
+
}),
|
|
218
|
+
ASSET_SAFETY_HELD: new ErrorCode({
|
|
219
|
+
id: "asset/safety-held",
|
|
220
|
+
defaultMessage: "Asset access is restricted by a safety hold.",
|
|
221
|
+
status: 423
|
|
222
|
+
}),
|
|
223
|
+
SUPPORT_TICKET_NOT_FOUND: new ErrorCode({
|
|
224
|
+
id: "support/ticket-not-found",
|
|
225
|
+
defaultMessage: "Support ticket not found.",
|
|
226
|
+
status: 404
|
|
227
|
+
}),
|
|
228
|
+
DUPLICATE_EXTERNAL_ID: new ErrorCode({
|
|
229
|
+
id: "asset/duplicate-external-id",
|
|
230
|
+
defaultMessage: "An asset with this externalId already exists in the project.",
|
|
231
|
+
status: 409
|
|
232
|
+
}),
|
|
233
|
+
FOLDER_NOT_FOUND: new ErrorCode({
|
|
234
|
+
id: "folder/not-found",
|
|
235
|
+
defaultMessage: "Folder not found",
|
|
236
|
+
status: 404
|
|
237
|
+
}),
|
|
238
|
+
DUPLICATE_FOLDER_NAME: new ErrorCode({
|
|
239
|
+
id: "folder/duplicate-name",
|
|
240
|
+
defaultMessage: "A folder with this name already exists in this location.",
|
|
241
|
+
status: 409
|
|
242
|
+
}),
|
|
243
|
+
FOLDER_NOT_EMPTY: new ErrorCode({
|
|
244
|
+
id: "folder/not-empty",
|
|
245
|
+
defaultMessage: "Folder must be empty before it can be deleted.",
|
|
246
|
+
status: 409
|
|
247
|
+
}),
|
|
248
|
+
FOLDER_PARENT_CYCLE: new ErrorCode({
|
|
249
|
+
id: "folder/parent-cycle",
|
|
250
|
+
defaultMessage: "A folder cannot be moved inside itself or one of its descendants.",
|
|
251
|
+
status: 409
|
|
252
|
+
}),
|
|
253
|
+
LENS_VERSION_CONFLICT: new ErrorCode({
|
|
254
|
+
id: "lens/version-conflict",
|
|
255
|
+
defaultMessage: "Lens updates require a version greater than the current version.",
|
|
256
|
+
status: 409
|
|
257
|
+
})
|
|
258
|
+
};
|
|
259
|
+
PUBLIC_ERROR_CODES.MISSING_TOKEN, PUBLIC_ERROR_CODES.INVALID_API_KEY, PUBLIC_ERROR_CODES.API_KEY_PROJECT_MISMATCH;
|
|
260
|
+
const MAX_IMAGE_UPLOAD_BYTES = 20971520;
|
|
261
|
+
const MIN_MULTIPART_IMAGE_UPLOAD_BYTES = 10485760;
|
|
262
|
+
const REJECTED_UPLOAD_MIME_TYPE_SET = /* @__PURE__ */ new Set([
|
|
263
|
+
"application/octet-stream",
|
|
264
|
+
"multipart/form-data",
|
|
265
|
+
"multipart/mixed",
|
|
266
|
+
"multipart/alternative",
|
|
267
|
+
"multipart/related",
|
|
268
|
+
"multipart/signed",
|
|
269
|
+
"multipart/encrypted"
|
|
270
|
+
]);
|
|
271
|
+
const normalizeUploadMimeType = (mimeType) => {
|
|
272
|
+
var _mimeType$toLowerCase;
|
|
273
|
+
return ((_mimeType$toLowerCase = mimeType.toLowerCase().split(";")[0]) === null || _mimeType$toLowerCase === void 0 ? void 0 : _mimeType$toLowerCase.trim()) || mimeType.toLowerCase().trim();
|
|
274
|
+
};
|
|
275
|
+
const isRejectedUploadMimeType = (mimeType) => REJECTED_UPLOAD_MIME_TYPE_SET.has(normalizeUploadMimeType(mimeType));
|
|
276
|
+
const getRejectedUploadMimeTypeError = (mimeType) => `${normalizeUploadMimeType(mimeType)} is not a valid upload file type. 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.`;
|
|
277
|
+
const DOMAIN = "calmlens.com";
|
|
278
|
+
//#endregion
|
|
279
|
+
//#region src/client/CalmLensError.ts
|
|
280
|
+
const SDK_ERRORS = { NO_CDN_FETCH: {
|
|
281
|
+
id: "sdk/no-cdn-fetch",
|
|
282
|
+
defaultMessage: "No fetch implementation is available for CDN requests"
|
|
283
|
+
} };
|
|
284
|
+
var CalmLensError = class extends Error {
|
|
285
|
+
constructor(errorCode, options) {
|
|
286
|
+
var _ref, _options$message;
|
|
287
|
+
const code = "getId" in errorCode ? errorCode.getId() : errorCode.id;
|
|
288
|
+
const defaultMessage = "getDefaultMessage" in errorCode ? errorCode.getDefaultMessage() : errorCode.defaultMessage;
|
|
289
|
+
super(`[calmlens] ${(_ref = (_options$message = options === null || options === void 0 ? void 0 : options.message) !== null && _options$message !== void 0 ? _options$message : defaultMessage) !== null && _ref !== void 0 ? _ref : code}`);
|
|
290
|
+
this.name = "CalmLensError";
|
|
291
|
+
this.code = code;
|
|
292
|
+
this.details = options === null || options === void 0 ? void 0 : options.details;
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
const calmLensError = (errorCode, options) => new CalmLensError(errorCode, options);
|
|
296
|
+
//#endregion
|
|
297
|
+
//#region \0@oxc-project+runtime@0.148.0/helpers/esm/objectWithoutPropertiesLoose.js
|
|
298
|
+
function _objectWithoutPropertiesLoose(r, e) {
|
|
299
|
+
if (null == r) return {};
|
|
300
|
+
var t = {};
|
|
301
|
+
for (var n in r) if ({}.hasOwnProperty.call(r, n)) {
|
|
302
|
+
if (e.includes(n)) continue;
|
|
303
|
+
t[n] = r[n];
|
|
304
|
+
}
|
|
305
|
+
return t;
|
|
306
|
+
}
|
|
307
|
+
//#endregion
|
|
308
|
+
//#region \0@oxc-project+runtime@0.148.0/helpers/esm/objectWithoutProperties.js
|
|
309
|
+
function _objectWithoutProperties(e, t) {
|
|
310
|
+
if (null == e) return {};
|
|
311
|
+
var o, r, i = _objectWithoutPropertiesLoose(e, t);
|
|
312
|
+
if (Object.getOwnPropertySymbols) {
|
|
313
|
+
var s = Object.getOwnPropertySymbols(e);
|
|
314
|
+
for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]);
|
|
315
|
+
}
|
|
316
|
+
return i;
|
|
317
|
+
}
|
|
318
|
+
//#endregion
|
|
319
|
+
//#region \0@oxc-project+runtime@0.148.0/helpers/esm/typeof.js
|
|
320
|
+
function _typeof(o) {
|
|
321
|
+
"@babel/helpers - typeof";
|
|
322
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
323
|
+
return typeof o;
|
|
324
|
+
} : function(o) {
|
|
325
|
+
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
326
|
+
}, _typeof(o);
|
|
327
|
+
}
|
|
328
|
+
//#endregion
|
|
329
|
+
//#region \0@oxc-project+runtime@0.148.0/helpers/esm/toPrimitive.js
|
|
330
|
+
function toPrimitive(t, r) {
|
|
331
|
+
if ("object" != _typeof(t) || !t) return t;
|
|
332
|
+
var e = t[Symbol.toPrimitive];
|
|
333
|
+
if (void 0 !== e) {
|
|
334
|
+
var i = e.call(t, r || "default");
|
|
335
|
+
if ("object" != _typeof(i)) return i;
|
|
336
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
337
|
+
}
|
|
338
|
+
return ("string" === r ? String : Number)(t);
|
|
339
|
+
}
|
|
340
|
+
//#endregion
|
|
341
|
+
//#region \0@oxc-project+runtime@0.148.0/helpers/esm/toPropertyKey.js
|
|
342
|
+
function toPropertyKey(t) {
|
|
343
|
+
var i = toPrimitive(t, "string");
|
|
344
|
+
return "symbol" == _typeof(i) ? i : i + "";
|
|
345
|
+
}
|
|
346
|
+
//#endregion
|
|
347
|
+
//#region \0@oxc-project+runtime@0.148.0/helpers/esm/defineProperty.js
|
|
348
|
+
function _defineProperty(e, r, t) {
|
|
349
|
+
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
350
|
+
value: t,
|
|
351
|
+
enumerable: !0,
|
|
352
|
+
configurable: !0,
|
|
353
|
+
writable: !0
|
|
354
|
+
}) : e[r] = t, e;
|
|
355
|
+
}
|
|
356
|
+
//#endregion
|
|
357
|
+
//#region \0@oxc-project+runtime@0.148.0/helpers/esm/objectSpread2.js
|
|
358
|
+
function ownKeys(e, r) {
|
|
359
|
+
var t = Object.keys(e);
|
|
360
|
+
if (Object.getOwnPropertySymbols) {
|
|
361
|
+
var o = Object.getOwnPropertySymbols(e);
|
|
362
|
+
r && (o = o.filter(function(r) {
|
|
363
|
+
return Object.getOwnPropertyDescriptor(e, r).enumerable;
|
|
364
|
+
})), t.push.apply(t, o);
|
|
365
|
+
}
|
|
366
|
+
return t;
|
|
367
|
+
}
|
|
368
|
+
function _objectSpread2(e) {
|
|
369
|
+
for (var r = 1; r < arguments.length; r++) {
|
|
370
|
+
var t = null != arguments[r] ? arguments[r] : {};
|
|
371
|
+
r % 2 ? ownKeys(Object(t), !0).forEach(function(r) {
|
|
372
|
+
_defineProperty(e, r, t[r]);
|
|
373
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function(r) {
|
|
374
|
+
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
return e;
|
|
378
|
+
}
|
|
379
|
+
//#endregion
|
|
380
|
+
//#region src/client/CalmLensUtils.ts
|
|
381
|
+
const _excluded = ["text", "name"];
|
|
382
|
+
/** Converts `text.moderate()` options into a plain-text asset submission. */
|
|
383
|
+
const toTextAssetOptions = (_ref) => {
|
|
384
|
+
let { text, name } = _ref, options = _objectWithoutProperties(_ref, _excluded);
|
|
385
|
+
if (typeof text !== "string" || text.trim().length === 0) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Text to moderate must be a non-empty string" });
|
|
386
|
+
return _objectSpread2(_objectSpread2({}, options), {}, {
|
|
387
|
+
name: name !== null && name !== void 0 ? name : "text",
|
|
388
|
+
content: {
|
|
389
|
+
plainText: text,
|
|
390
|
+
type: "text/plain"
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
};
|
|
394
|
+
function verifyWebhookSignature(_x) {
|
|
395
|
+
return _verifyWebhookSignature.apply(this, arguments);
|
|
396
|
+
}
|
|
397
|
+
function _verifyWebhookSignature() {
|
|
398
|
+
_verifyWebhookSignature = _asyncToGenerator(function* ({ payload, signatureHex, secret }) {
|
|
399
|
+
const encoder = new TextEncoder();
|
|
400
|
+
const normalizePayload = typeof payload === "string" ? payload : JSON.stringify(payload);
|
|
401
|
+
const key = yield crypto.subtle.importKey("raw", encoder.encode(secret), {
|
|
402
|
+
name: "HMAC",
|
|
403
|
+
hash: "SHA-256"
|
|
404
|
+
}, false, ["sign"]);
|
|
405
|
+
const sig = yield crypto.subtle.sign("HMAC", key, encoder.encode(normalizePayload));
|
|
406
|
+
const hex = Array.from(new Uint8Array(sig)).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
407
|
+
if (hex.length !== signatureHex.length) return false;
|
|
408
|
+
let diff = 0;
|
|
409
|
+
for (let i = 0; i < hex.length; i++) diff |= hex.charCodeAt(i) ^ signatureHex.charCodeAt(i);
|
|
410
|
+
return diff === 0;
|
|
411
|
+
});
|
|
412
|
+
return _verifyWebhookSignature.apply(this, arguments);
|
|
413
|
+
}
|
|
414
|
+
//#endregion
|
|
415
|
+
export { getAssetPayloadByteLength as _, CalmLensError as a, _asyncToGenerator as b, DOMAIN as c, getRejectedUploadMimeTypeError as d, isRejectedUploadMimeType as f, assetToData as g, assetDataToUint8Array as h, _objectWithoutProperties as i, MAX_IMAGE_UPLOAD_BYTES as l, stringifyJsonCanonical as m, verifyWebhookSignature as n, SDK_ERRORS as o, PUBLIC_ERROR_CODES as p, _objectSpread2 as r, calmLensError as s, toTextAssetOptions as t, MIN_MULTIPART_IMAGE_UPLOAD_BYTES as u, getRawBase64 as v, validateAssetPayload as y };
|
package/esm/index.d.ts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { C as VerifyWebhookSignatureOptions, D as GetEventsResponse, E as GetAssetsPageResponse, M as Folder, N as Asset, O as PostAssetSignedUrlResponse, S as UploadAssetPartOptions, T as GetAssetResponse, _ as MultipartUploadPartOptions, a as CreateFolderOptions, b as UpdateAssetOptions, c as EnsureLensesOptions, d as ListAssetsOptions, f as ListEventsOptions, g as MultipartUploadOptions, h as MoveFolderOptions, i as CreateAssetSignedUrlOptions, j as Lens, k as RequestErrorCode, l as FolderIdOptions, m as MoveAssetOptions, n as CdnAssetUrlOptions, o as CreateMultipartUploadPartOptions, p as ModerateTextOptions, r as CdnServeOptions, s as EnsureLensOptions, t as CalmLensClientOptions, u as ListAssetChildrenOptions, v as RetrieveAssetOptions, w as GetAssetChildrenResponse, x as UpdateFolderOptions, y as SubmitAssetOptions } from "./CalmLensTypes-Bnq4u8HJ.js";
|
|
2
|
+
//#region src/client/CalmLensClient.d.ts
|
|
3
|
+
declare class CalmLensClient {
|
|
4
|
+
private options;
|
|
5
|
+
private readonly api;
|
|
6
|
+
private readonly endpoints;
|
|
7
|
+
private readonly ensuredLenses;
|
|
8
|
+
private readonly pendingEnsureLenses;
|
|
9
|
+
private readonly multipartImageUploadSizes;
|
|
10
|
+
private readonly multipartUploadQueues;
|
|
11
|
+
/**
|
|
12
|
+
* Validates a webhook signature to ensure the webhook payload is authentic.
|
|
13
|
+
*
|
|
14
|
+
* @param payload - The webhook payload (string or object)
|
|
15
|
+
* @param signatureHex - The signature from the 'x-calmlens-signature' header
|
|
16
|
+
* @param secret - The webhook secret used to sign the payload
|
|
17
|
+
* @returns Promise<boolean> - True if the signature is valid, false otherwise
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```javascript
|
|
21
|
+
* // In your webhook handler
|
|
22
|
+
* const signature = req.headers['x-calmlens-signature'];
|
|
23
|
+
* const payload = req.body;
|
|
24
|
+
* const isValid = await CalmLensClient.verifyWebhookSignature({
|
|
25
|
+
* payload,
|
|
26
|
+
* signatureHex: signature,
|
|
27
|
+
* secret: WEBHOOK_SECRET,
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* if (!isValid) {
|
|
31
|
+
* return res.status(401).send('Unauthorized');
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
static verifyWebhookSignature(options: VerifyWebhookSignatureOptions): Promise<boolean>;
|
|
36
|
+
constructor(options: CalmLensClientOptions);
|
|
37
|
+
private getUploadFetch;
|
|
38
|
+
private submitRawAssetUpload;
|
|
39
|
+
private submitRawAssetPart;
|
|
40
|
+
private parseRawUploadResponse;
|
|
41
|
+
get cdn(): {
|
|
42
|
+
/**
|
|
43
|
+
* Create a CDN URL for asset content or previews.
|
|
44
|
+
* Private assets require an authenticated request or signed URL.
|
|
45
|
+
*/
|
|
46
|
+
getAssetUrl: ({ assetIdentifier, ...options }: CdnAssetUrlOptions) => string;
|
|
47
|
+
/**
|
|
48
|
+
* Fetch asset content or previews from the CDN.
|
|
49
|
+
* Private assets are authenticated with the client's API key.
|
|
50
|
+
*/
|
|
51
|
+
serve: ({ assetIdentifier, init, ...urlOptions }: CdnServeOptions) => Promise<Response>;
|
|
52
|
+
};
|
|
53
|
+
get text(): {
|
|
54
|
+
/**
|
|
55
|
+
* Moderate a short piece of text, such as a comment, caption or bio, and wait for the
|
|
56
|
+
* decision. Check `status` (`"approved"` or `"rejected"`) and `report.flagged`.
|
|
57
|
+
* The text is not retained after classification unless `processingOptions` says so.
|
|
58
|
+
*/
|
|
59
|
+
moderate: (options: ModerateTextOptions) => Promise<Asset>;
|
|
60
|
+
};
|
|
61
|
+
get assets(): {
|
|
62
|
+
/**
|
|
63
|
+
* List all assets for a project
|
|
64
|
+
* @param options - Pagination and filtering options
|
|
65
|
+
* @returns Promise with paginated assets
|
|
66
|
+
*/
|
|
67
|
+
list: (options: ListAssetsOptions) => Promise<GetAssetsPageResponse>;
|
|
68
|
+
/**
|
|
69
|
+
* Retrieve a specific asset by ID
|
|
70
|
+
* @param assetId - The asset ID
|
|
71
|
+
* @returns Promise with asset data
|
|
72
|
+
*/
|
|
73
|
+
retrieve: ({ assetId }: RetrieveAssetOptions) => Promise<GetAssetResponse>;
|
|
74
|
+
update: ({ assetId, ...body }: UpdateAssetOptions) => Promise<Asset>;
|
|
75
|
+
/**
|
|
76
|
+
* Get child assets of a parent asset
|
|
77
|
+
* @param assetId - The parent asset ID
|
|
78
|
+
* @param options - Pagination options
|
|
79
|
+
* @returns Promise with paginated child assets
|
|
80
|
+
*/
|
|
81
|
+
listChildren: ({ assetId, ...options }: ListAssetChildrenOptions) => Promise<GetAssetChildrenResponse>;
|
|
82
|
+
/**
|
|
83
|
+
* Submit a new asset for processing (queued asynchronously)
|
|
84
|
+
* The asset will be queued for classification and returned immediately.
|
|
85
|
+
* @param options - Asset submission options
|
|
86
|
+
* @returns Promise with created asset
|
|
87
|
+
*/
|
|
88
|
+
create: (options: SubmitAssetOptions) => Promise<Asset>;
|
|
89
|
+
/**
|
|
90
|
+
* Submit a new asset and analyze it synchronously
|
|
91
|
+
* The asset will be classified immediately before the response is returned.
|
|
92
|
+
* This may take longer but ensures the asset is fully processed.
|
|
93
|
+
* @param options - Asset submission options
|
|
94
|
+
* @returns Promise with created and analyzed asset
|
|
95
|
+
*/
|
|
96
|
+
analyze: (options: SubmitAssetOptions) => Promise<Asset>;
|
|
97
|
+
/**
|
|
98
|
+
* Upload a part for a multipart upload
|
|
99
|
+
* @param assetId - The asset ID
|
|
100
|
+
* @param part - The part to upload
|
|
101
|
+
* @returns Promise with asset data
|
|
102
|
+
*/
|
|
103
|
+
uploadPart: ({ assetId, part }: UploadAssetPartOptions) => Promise<Asset>;
|
|
104
|
+
/**
|
|
105
|
+
* Create a short-lived signed CDN URL for an asset.
|
|
106
|
+
* This is useful for private assets where clients should not send app
|
|
107
|
+
* auth cookies or API keys to the CDN on every media request.
|
|
108
|
+
*/
|
|
109
|
+
createSignedUrl: ({ assetId, ...options }: CreateAssetSignedUrlOptions) => Promise<PostAssetSignedUrlResponse>;
|
|
110
|
+
move: ({ assetId, folderId }: MoveAssetOptions) => Promise<Asset>;
|
|
111
|
+
};
|
|
112
|
+
get events(): {
|
|
113
|
+
/**
|
|
114
|
+
* List project events after a cursor, oldest first. Call without `after` to get a cursor
|
|
115
|
+
* for "now", then poll with the returned `cursor`. Event bodies match webhook deliveries.
|
|
116
|
+
*/
|
|
117
|
+
list: (options?: ListEventsOptions) => Promise<GetEventsResponse>;
|
|
118
|
+
};
|
|
119
|
+
get folders(): {
|
|
120
|
+
list: () => Promise<Folder[]>;
|
|
121
|
+
retrieve: ({ folderId }: FolderIdOptions) => Promise<Folder>;
|
|
122
|
+
create: (options: CreateFolderOptions) => Promise<Folder>;
|
|
123
|
+
update: ({ folderId, ...body }: UpdateFolderOptions) => Promise<Folder>;
|
|
124
|
+
delete: ({ folderId }: FolderIdOptions) => Promise<void>;
|
|
125
|
+
move: ({ folderId, parentId }: MoveFolderOptions) => Promise<Folder>;
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* Create a named lens at any positive version, or update it with a greater version.
|
|
129
|
+
* Supplying the same definition and version is idempotent.
|
|
130
|
+
*/
|
|
131
|
+
ensureLens(options: EnsureLensOptions): Promise<Lens>;
|
|
132
|
+
/**
|
|
133
|
+
* Create or update several named lenses in one API request.
|
|
134
|
+
* Each lens follows the same versioning and idempotency rules as ensureLens().
|
|
135
|
+
*/
|
|
136
|
+
ensureLenses({ lenses }: EnsureLensesOptions): Promise<Lens[]>;
|
|
137
|
+
private submitEnsureLenses;
|
|
138
|
+
}
|
|
139
|
+
//#endregion
|
|
140
|
+
//#region src/client/CalmLensError.d.ts
|
|
141
|
+
type CalmLensErrorDetails = Record<string, unknown>;
|
|
142
|
+
type CalmLensErrorCodeInput = RequestErrorCode | {
|
|
143
|
+
id: string;
|
|
144
|
+
defaultMessage?: string;
|
|
145
|
+
};
|
|
146
|
+
export declare class CalmLensError extends Error {
|
|
147
|
+
code: string;
|
|
148
|
+
details?: CalmLensErrorDetails;
|
|
149
|
+
constructor(errorCode: CalmLensErrorCodeInput, options?: {
|
|
150
|
+
message?: string;
|
|
151
|
+
details?: CalmLensErrorDetails;
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
//#endregion
|
|
155
|
+
//#region src/client/CalmLensUtils.d.ts
|
|
156
|
+
export declare function verifyWebhookSignature({ payload, signatureHex, secret }: VerifyWebhookSignatureOptions): Promise<boolean>;
|
|
157
|
+
//#endregion
|
|
158
|
+
export { CalmLensClient, type CalmLensClientOptions, type CalmLensErrorDetails, type CdnAssetUrlOptions, type CdnServeOptions, type CreateAssetSignedUrlOptions, type CreateFolderOptions, type CreateMultipartUploadPartOptions, type EnsureLensOptions, type EnsureLensesOptions, type FolderIdOptions, type ListAssetChildrenOptions, type ListAssetsOptions, type ListEventsOptions, type ModerateTextOptions, type MoveAssetOptions, type MoveFolderOptions, type MultipartUploadOptions, type MultipartUploadPartOptions, type RetrieveAssetOptions, type UpdateAssetOptions, type UpdateFolderOptions, type UploadAssetPartOptions, type VerifyWebhookSignatureOptions };
|