@calmlens/sdk 0.8.1 → 0.9.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.
- package/cjs/{CalmLensTypes-B7hr0SBv.d.ts → CalmLensTypes-CZqyni4p.d.ts} +7 -1
- package/cjs/{CalmLensUtils-CJ6o7KRU.js → CalmLensUtils-CArkVfj0.js} +59 -0
- package/cjs/index.d.ts +12 -4
- package/cjs/index.js +93 -49
- package/cjs/testing.d.ts +7 -4
- package/cjs/testing.js +163 -133
- package/esm/{CalmLensTypes-B7hr0SBv.d.ts → CalmLensTypes-CZqyni4p.d.ts} +7 -1
- package/esm/{CalmLensUtils-BZsX8yeO.js → CalmLensUtils-Bnikzonc.js} +42 -1
- package/esm/index.d.ts +12 -4
- package/esm/index.js +92 -48
- package/esm/testing.d.ts +7 -4
- package/esm/testing.js +162 -132
- package/package.json +1 -1
package/esm/testing.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { C as _objectSpread2, S as _asyncToGenerator, _ as assetDataToUint8Array, a as validateClientOptions, g as stringifyJsonCanonical, h as PUBLIC_ERROR_CODES, i as toTextAssetOptions, l as calmLensError, m as isRejectedUploadMimeType, n as encodePathSegment, o as _objectWithoutProperties, p as getRejectedUploadMimeTypeError, t as appendCdnSelector, v as assetToData, x as validateAssetPayload, y as getAssetPayloadByteLength } from "./CalmLensUtils-Bnikzonc.js";
|
|
2
2
|
import * as zod from "zod";
|
|
3
3
|
//#region ../shared/src/Permissions.ts
|
|
4
4
|
const ACTION_SCHEMA = zod.enum([
|
|
@@ -211,12 +211,6 @@ var InMemoryMockDatabaseStorage = class {
|
|
|
211
211
|
};
|
|
212
212
|
//#endregion
|
|
213
213
|
//#region src/testing/MockUtils.ts
|
|
214
|
-
const appendCdnSelector = (url, options) => {
|
|
215
|
-
if (options.lens && options.transform) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Specify either a lens or a transform, not both" });
|
|
216
|
-
if (options.lens) return `${url}/@${encodeURIComponent(options.lens)}`;
|
|
217
|
-
if (!options.transform) return url;
|
|
218
|
-
return `${url}/${encodeURI(options.transform.replace(/^\/+/, ""))}`;
|
|
219
|
-
};
|
|
220
214
|
const inferAssetType = (options) => {
|
|
221
215
|
if ("url" in options) return "website";
|
|
222
216
|
const type = "content" in options ? options.content.type : options.part.type;
|
|
@@ -310,6 +304,15 @@ var MockCalmLensClient = class {
|
|
|
310
304
|
callsTo(operation) {
|
|
311
305
|
return this.calls.filter((call) => call.operation === operation);
|
|
312
306
|
}
|
|
307
|
+
findOwnAsset(assetId) {
|
|
308
|
+
var _this = this;
|
|
309
|
+
return _asyncToGenerator(function* () {
|
|
310
|
+
if (!assetId) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Asset ID is required" });
|
|
311
|
+
const asset = yield _this.databaseStorage.getAsset(assetId);
|
|
312
|
+
if (!asset || asset.projectId !== _this.options.projectId) throw calmLensError(PUBLIC_ERROR_CODES.ASSET_NOT_FOUND);
|
|
313
|
+
return asset;
|
|
314
|
+
})();
|
|
315
|
+
}
|
|
313
316
|
begin(operation, options) {
|
|
314
317
|
this.calls.push({
|
|
315
318
|
operation,
|
|
@@ -322,32 +325,32 @@ var MockCalmLensClient = class {
|
|
|
322
325
|
}
|
|
323
326
|
}
|
|
324
327
|
get cdn() {
|
|
325
|
-
var
|
|
328
|
+
var _this2 = this;
|
|
326
329
|
return {
|
|
327
330
|
getAssetUrl: (_ref) => {
|
|
328
331
|
var _options$resource;
|
|
329
332
|
let { assetIdentifier } = _ref, options = _objectWithoutProperties(_ref, _excluded);
|
|
330
333
|
if (!assetIdentifier) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Asset identifier is required" });
|
|
331
334
|
const resource = (_options$resource = options.resource) !== null && _options$resource !== void 0 ? _options$resource : "content";
|
|
332
|
-
const url = `${this.cdnBaseUrl.replace(/\/$/, "")}/assets/${encodeURIComponent(this.options.projectId)}/${
|
|
335
|
+
const url = `${this.cdnBaseUrl.replace(/\/$/, "")}/assets/${encodeURIComponent(this.options.projectId)}/${encodePathSegment(assetIdentifier, "Asset identifier")}/${resource}`;
|
|
333
336
|
return appendCdnSelector(url, options);
|
|
334
337
|
},
|
|
335
338
|
serve: function() {
|
|
336
339
|
var _ref2 = _asyncToGenerator(function* (serveOptions) {
|
|
337
340
|
var _asset$folderId, _await$this$databaseS, _this$databaseStorage, _this$databaseStorage2, _storedContent$type;
|
|
338
|
-
|
|
341
|
+
_this2.begin("cdn.serve", serveOptions);
|
|
339
342
|
const { assetIdentifier } = serveOptions, options = _objectWithoutProperties(serveOptions, _excluded2);
|
|
340
343
|
if (!assetIdentifier) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Asset identifier is required" });
|
|
341
|
-
const asset = (yield
|
|
344
|
+
const asset = (yield _this2.databaseStorage.listAssets(_this2.options.projectId)).find((asset) => asset.id === assetIdentifier || asset.externalId === assetIdentifier);
|
|
342
345
|
if (!asset) return new Response("Asset not found", { status: 404 });
|
|
343
|
-
if (asset.visibility !== "public" && !permits(
|
|
346
|
+
if (asset.visibility !== "public" && !permits(_this2.apiKeyGrants, "assets:download", (_asset$folderId = asset.folderId) !== null && _asset$folderId !== void 0 ? _asset$folderId : null, (_await$this$databaseS = yield (_this$databaseStorage = (_this$databaseStorage2 = _this2.databaseStorage).listFolders) === null || _this$databaseStorage === void 0 ? void 0 : _this$databaseStorage.call(_this$databaseStorage2, _this2.options.projectId)) !== null && _await$this$databaseS !== void 0 ? _await$this$databaseS : [])) return new Response(JSON.stringify({
|
|
344
347
|
code: "asset/api-key-missing-permission",
|
|
345
348
|
message: "This API key needs assets:download permission for this asset’s folder."
|
|
346
349
|
}), {
|
|
347
350
|
status: 403,
|
|
348
351
|
headers: { "Content-Type": "application/json" }
|
|
349
352
|
});
|
|
350
|
-
const storedContent = yield
|
|
353
|
+
const storedContent = yield _this2.imageStorage.getContent(asset.id);
|
|
351
354
|
if (!storedContent) return new Response("Asset content not found", { status: 404 });
|
|
352
355
|
const parts = [...storedContent.parts.entries()].sort(([left], [right]) => left - right).map(([, value]) => value);
|
|
353
356
|
const byteLength = parts.reduce((sum, value) => sum + value.byteLength, 0);
|
|
@@ -361,7 +364,7 @@ var MockCalmLensClient = class {
|
|
|
361
364
|
status: 200,
|
|
362
365
|
headers: {
|
|
363
366
|
"Content-Type": (_storedContent$type = storedContent.type) !== null && _storedContent$type !== void 0 ? _storedContent$type : "application/octet-stream",
|
|
364
|
-
"X-Mock-CalmLens-CDN-URL":
|
|
367
|
+
"X-Mock-CalmLens-CDN-URL": _this2.cdn.getAssetUrl(_objectSpread2({ assetIdentifier }, options))
|
|
365
368
|
}
|
|
366
369
|
});
|
|
367
370
|
});
|
|
@@ -372,16 +375,16 @@ var MockCalmLensClient = class {
|
|
|
372
375
|
};
|
|
373
376
|
}
|
|
374
377
|
get events() {
|
|
375
|
-
var
|
|
378
|
+
var _this3 = this;
|
|
376
379
|
return {
|
|
377
380
|
/** The mock does not record events; it always returns an empty page at "now". */
|
|
378
381
|
list: function() {
|
|
379
382
|
var _ref3 = _asyncToGenerator(function* (options = {}) {
|
|
380
383
|
var _options$after;
|
|
381
|
-
|
|
384
|
+
_this3.begin("events.list", options);
|
|
382
385
|
return {
|
|
383
386
|
events: [],
|
|
384
|
-
cursor: (_options$after = options.after) !== null && _options$after !== void 0 ? _options$after : `${
|
|
387
|
+
cursor: (_options$after = options.after) !== null && _options$after !== void 0 ? _options$after : `${_this3.now()}_`,
|
|
385
388
|
hasMore: false
|
|
386
389
|
};
|
|
387
390
|
});
|
|
@@ -391,11 +394,11 @@ list: function() {
|
|
|
391
394
|
}() };
|
|
392
395
|
}
|
|
393
396
|
get text() {
|
|
394
|
-
var
|
|
397
|
+
var _this4 = this;
|
|
395
398
|
return { moderate: function() {
|
|
396
399
|
var _ref4 = _asyncToGenerator(function* (options) {
|
|
397
|
-
|
|
398
|
-
return
|
|
400
|
+
_this4.begin("text.moderate", options);
|
|
401
|
+
return _this4.moderateAsset(toTextAssetOptions(options));
|
|
399
402
|
});
|
|
400
403
|
return function moderate(_x2) {
|
|
401
404
|
return _ref4.apply(this, arguments);
|
|
@@ -403,286 +406,313 @@ list: function() {
|
|
|
403
406
|
}() };
|
|
404
407
|
}
|
|
405
408
|
get assets() {
|
|
406
|
-
var
|
|
409
|
+
var _this5 = this;
|
|
407
410
|
return {
|
|
408
411
|
list: function() {
|
|
409
|
-
var _ref5 = _asyncToGenerator(function* (options) {
|
|
412
|
+
var _ref5 = _asyncToGenerator(function* (options = {}) {
|
|
410
413
|
var _options$pageIndex;
|
|
411
|
-
|
|
412
|
-
const assets = yield
|
|
414
|
+
_this5.begin("assets.list", options);
|
|
415
|
+
const assets = yield _this5.databaseStorage.listAssets(_this5.options.projectId);
|
|
413
416
|
return createPage(assets, _objectSpread2(_objectSpread2({}, options), {}, { pageIndex: (_options$pageIndex = options.pageIndex) !== null && _options$pageIndex !== void 0 ? _options$pageIndex : 0 }));
|
|
414
417
|
});
|
|
415
|
-
return function list(
|
|
418
|
+
return function list() {
|
|
416
419
|
return _ref5.apply(this, arguments);
|
|
417
420
|
};
|
|
418
421
|
}(),
|
|
419
422
|
retrieve: function() {
|
|
420
423
|
var _ref6 = _asyncToGenerator(function* (options) {
|
|
421
|
-
|
|
424
|
+
_this5.begin("assets.retrieve", options);
|
|
422
425
|
const { assetId } = options;
|
|
423
426
|
if (!assetId) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Asset ID is required" });
|
|
424
|
-
const asset = yield
|
|
427
|
+
const asset = yield _this5.databaseStorage.getAsset(assetId);
|
|
425
428
|
if (!asset) throw calmLensError(PUBLIC_ERROR_CODES.ASSET_NOT_FOUND);
|
|
426
429
|
return asset;
|
|
427
430
|
});
|
|
428
|
-
return function retrieve(
|
|
431
|
+
return function retrieve(_x3) {
|
|
429
432
|
return _ref6.apply(this, arguments);
|
|
430
433
|
};
|
|
431
434
|
}(),
|
|
432
435
|
listChildren: function() {
|
|
433
|
-
var _ref7 = _asyncToGenerator(function* (childrenOptions) {
|
|
436
|
+
var _ref7 = _asyncToGenerator(function* (childrenOptions = {}) {
|
|
434
437
|
var _options$pageIndex2;
|
|
435
|
-
|
|
438
|
+
_this5.begin("assets.listChildren", childrenOptions);
|
|
436
439
|
const { assetId } = childrenOptions, options = _objectWithoutProperties(childrenOptions, _excluded3);
|
|
437
440
|
if (!assetId) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Asset ID is required" });
|
|
438
|
-
const assets = yield
|
|
441
|
+
const assets = yield _this5.databaseStorage.listAssets(_this5.options.projectId);
|
|
439
442
|
return createPage(assets.filter((asset) => asset.parentId === assetId), _objectSpread2(_objectSpread2({}, options), {}, { pageIndex: (_options$pageIndex2 = options.pageIndex) !== null && _options$pageIndex2 !== void 0 ? _options$pageIndex2 : 0 }));
|
|
440
443
|
});
|
|
441
|
-
return function listChildren(
|
|
444
|
+
return function listChildren() {
|
|
442
445
|
return _ref7.apply(this, arguments);
|
|
443
446
|
};
|
|
444
447
|
}(),
|
|
445
448
|
update: function() {
|
|
446
449
|
var _ref8 = _asyncToGenerator(function* (options) {
|
|
447
|
-
|
|
450
|
+
_this5.begin("assets.update", options);
|
|
448
451
|
const { assetId } = options, body = _objectWithoutProperties(options, _excluded4);
|
|
449
452
|
if (!assetId) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Asset ID is required" });
|
|
450
|
-
const asset = yield
|
|
451
|
-
if (!asset || asset.projectId !==
|
|
453
|
+
const asset = yield _this5.databaseStorage.getAsset(assetId);
|
|
454
|
+
if (!asset || asset.projectId !== _this5.options.projectId) throw calmLensError(PUBLIC_ERROR_CODES.ASSET_NOT_FOUND);
|
|
452
455
|
const changes = Object.fromEntries(Object.entries(body).filter(([, value]) => value !== void 0));
|
|
453
|
-
return
|
|
456
|
+
return _this5.databaseStorage.updateAsset(_objectSpread2(_objectSpread2(_objectSpread2({}, asset), changes), {}, { updatedAt: _this5.now() }));
|
|
454
457
|
});
|
|
455
|
-
return function update(
|
|
458
|
+
return function update(_x4) {
|
|
456
459
|
return _ref8.apply(this, arguments);
|
|
457
460
|
};
|
|
458
461
|
}(),
|
|
459
462
|
create: function() {
|
|
460
463
|
var _ref9 = _asyncToGenerator(function* (options) {
|
|
461
|
-
|
|
462
|
-
return
|
|
464
|
+
_this5.begin("assets.create", options);
|
|
465
|
+
return _this5.createAsset(options, _this5.queuedStatus);
|
|
463
466
|
});
|
|
464
|
-
return function create(
|
|
467
|
+
return function create(_x5) {
|
|
465
468
|
return _ref9.apply(this, arguments);
|
|
466
469
|
};
|
|
467
470
|
}(),
|
|
468
471
|
moderate: function() {
|
|
469
472
|
var _ref10 = _asyncToGenerator(function* (options) {
|
|
470
|
-
|
|
471
|
-
return
|
|
473
|
+
_this5.begin("assets.moderate", options);
|
|
474
|
+
return _this5.moderateAsset(options);
|
|
472
475
|
});
|
|
473
|
-
return function moderate(
|
|
476
|
+
return function moderate(_x6) {
|
|
474
477
|
return _ref10.apply(this, arguments);
|
|
475
478
|
};
|
|
476
479
|
}(),
|
|
477
480
|
uploadPart: function() {
|
|
478
481
|
var _ref11 = _asyncToGenerator(function* (options) {
|
|
479
482
|
var _asset$uploadPartsRec;
|
|
480
|
-
|
|
483
|
+
_this5.begin("assets.uploadPart", options);
|
|
481
484
|
const { assetId, part } = options;
|
|
482
485
|
if (!assetId) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Asset ID is required" });
|
|
483
486
|
if (!part) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Part options are required" });
|
|
484
|
-
const asset = yield
|
|
487
|
+
const asset = yield _this5.databaseStorage.getAsset(assetId);
|
|
485
488
|
if (!asset) throw calmLensError(PUBLIC_ERROR_CODES.ASSET_NOT_FOUND);
|
|
486
489
|
const validation = validateAssetPayload(part, {
|
|
487
490
|
multipart: true,
|
|
488
491
|
isFinal: part.isFinal
|
|
489
492
|
});
|
|
490
493
|
if (!validation.ok) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: validation.error });
|
|
491
|
-
yield
|
|
494
|
+
yield _this5.imageStorage.putPart(assetId, part);
|
|
492
495
|
const updated = _objectSpread2(_objectSpread2({}, asset), {}, {
|
|
493
|
-
status: part.isFinal ?
|
|
496
|
+
status: part.isFinal ? _this5.queuedStatus : "uploading",
|
|
494
497
|
uploadPartsReceived: Math.max((_asset$uploadPartsRec = asset.uploadPartsReceived) !== null && _asset$uploadPartsRec !== void 0 ? _asset$uploadPartsRec : 0, part.partNumber),
|
|
495
|
-
updatedAt:
|
|
498
|
+
updatedAt: _this5.now()
|
|
496
499
|
});
|
|
497
|
-
return
|
|
500
|
+
return _this5.databaseStorage.updateAsset(updated);
|
|
498
501
|
});
|
|
499
|
-
return function uploadPart(
|
|
502
|
+
return function uploadPart(_x7) {
|
|
500
503
|
return _ref11.apply(this, arguments);
|
|
501
504
|
};
|
|
502
505
|
}(),
|
|
503
506
|
createSignedUrl: function() {
|
|
504
507
|
var _ref12 = _asyncToGenerator(function* (signedUrlOptions) {
|
|
505
508
|
var _options$expiresInSec, _options$resource2;
|
|
506
|
-
|
|
509
|
+
_this5.begin("assets.createSignedUrl", signedUrlOptions);
|
|
507
510
|
const { assetId } = signedUrlOptions, options = _objectWithoutProperties(signedUrlOptions, _excluded5);
|
|
508
511
|
if (!assetId) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Asset ID is required" });
|
|
509
|
-
if (!(yield
|
|
512
|
+
if (!(yield _this5.databaseStorage.getAsset(assetId))) throw calmLensError(PUBLIC_ERROR_CODES.ASSET_NOT_FOUND);
|
|
510
513
|
const expiresInSeconds = (_options$expiresInSec = options.expiresInSeconds) !== null && _options$expiresInSec !== void 0 ? _options$expiresInSec : 900;
|
|
511
514
|
const resource = (_options$resource2 = options.resource) !== null && _options$resource2 !== void 0 ? _options$resource2 : "content";
|
|
512
515
|
if (options.lens && options.transform) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Specify either a lens or a transform, not both" });
|
|
513
516
|
const selector = options.lens ? `&lens=${encodeURIComponent(options.lens)}` : options.transform ? `&transform=${encodeURIComponent(options.transform)}` : "";
|
|
514
517
|
return {
|
|
515
|
-
url: `${
|
|
516
|
-
expiresAt:
|
|
518
|
+
url: `${_this5.signedUrlBaseUrl}/assets/${assetId}/${resource}?expiresInSeconds=${expiresInSeconds}${selector}`,
|
|
519
|
+
expiresAt: _this5.now() + expiresInSeconds * 1e3
|
|
517
520
|
};
|
|
518
521
|
});
|
|
519
|
-
return function createSignedUrl(
|
|
522
|
+
return function createSignedUrl(_x8) {
|
|
520
523
|
return _ref12.apply(this, arguments);
|
|
521
524
|
};
|
|
522
525
|
}(),
|
|
523
526
|
move: function() {
|
|
524
527
|
var _ref13 = _asyncToGenerator(function* (options) {
|
|
525
|
-
|
|
528
|
+
_this5.begin("assets.move", options);
|
|
526
529
|
const { assetId, folderId } = options;
|
|
527
530
|
if (!assetId) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Asset ID is required" });
|
|
528
|
-
const asset = yield
|
|
529
|
-
if (!asset || asset.projectId !==
|
|
531
|
+
const asset = yield _this5.databaseStorage.getAsset(assetId);
|
|
532
|
+
if (!asset || asset.projectId !== _this5.options.projectId) throw calmLensError(PUBLIC_ERROR_CODES.ASSET_NOT_FOUND);
|
|
530
533
|
if (folderId !== null && folderId !== void 0) {
|
|
531
|
-
if (!(yield
|
|
534
|
+
if (!(yield _this5.resolveFolder(folderId))) throw calmLensError(PUBLIC_ERROR_CODES.FOLDER_NOT_FOUND);
|
|
532
535
|
}
|
|
533
536
|
const updated = _objectSpread2(_objectSpread2({}, asset), {}, {
|
|
534
537
|
folderId: folderId !== null && folderId !== void 0 ? folderId : null,
|
|
535
|
-
updatedAt:
|
|
538
|
+
updatedAt: _this5.now()
|
|
536
539
|
});
|
|
537
|
-
return
|
|
540
|
+
return _this5.databaseStorage.updateAsset(updated);
|
|
538
541
|
});
|
|
539
|
-
return function move(
|
|
542
|
+
return function move(_x9) {
|
|
540
543
|
return _ref13.apply(this, arguments);
|
|
541
544
|
};
|
|
542
545
|
}(),
|
|
543
546
|
retry: function() {
|
|
544
547
|
var _ref14 = _asyncToGenerator(function* (options) {
|
|
545
|
-
|
|
548
|
+
_this5.begin("assets.retry", options);
|
|
546
549
|
const { assetId } = options;
|
|
547
550
|
if (!assetId) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Asset ID is required" });
|
|
548
|
-
const asset = yield
|
|
549
|
-
if (!asset || asset.projectId !==
|
|
551
|
+
const asset = yield _this5.databaseStorage.getAsset(assetId);
|
|
552
|
+
if (!asset || asset.projectId !== _this5.options.projectId) throw calmLensError(PUBLIC_ERROR_CODES.ASSET_NOT_FOUND);
|
|
550
553
|
if (asset.status !== "error") throw calmLensError(PUBLIC_ERROR_CODES.ASSET_NOT_RETRYABLE);
|
|
551
|
-
return
|
|
554
|
+
return _this5.databaseStorage.updateAsset(_objectSpread2(_objectSpread2({}, asset), {}, {
|
|
552
555
|
status: "pending",
|
|
553
|
-
updatedAt:
|
|
556
|
+
updatedAt: _this5.now()
|
|
554
557
|
}));
|
|
555
558
|
});
|
|
556
|
-
return function retry(
|
|
559
|
+
return function retry(_x10) {
|
|
557
560
|
return _ref14.apply(this, arguments);
|
|
558
561
|
};
|
|
562
|
+
}(),
|
|
563
|
+
deleteContent: function() {
|
|
564
|
+
var _ref15 = _asyncToGenerator(function* (options) {
|
|
565
|
+
var _asset$storageDeleted;
|
|
566
|
+
_this5.begin("assets.deleteContent", options);
|
|
567
|
+
const asset = yield _this5.findOwnAsset(options.assetId);
|
|
568
|
+
yield _this5.imageStorage.deleteContent(asset.id);
|
|
569
|
+
return _this5.databaseStorage.updateAsset(_objectSpread2(_objectSpread2({}, asset), {}, {
|
|
570
|
+
extractedText: null,
|
|
571
|
+
storageDeletedAt: (_asset$storageDeleted = asset.storageDeletedAt) !== null && _asset$storageDeleted !== void 0 ? _asset$storageDeleted : _this5.now(),
|
|
572
|
+
updatedAt: _this5.now()
|
|
573
|
+
}));
|
|
574
|
+
});
|
|
575
|
+
return function deleteContent(_x11) {
|
|
576
|
+
return _ref15.apply(this, arguments);
|
|
577
|
+
};
|
|
578
|
+
}(),
|
|
579
|
+
delete: function() {
|
|
580
|
+
var _ref16 = _asyncToGenerator(function* (options) {
|
|
581
|
+
_this5.begin("assets.delete", options);
|
|
582
|
+
const asset = yield _this5.findOwnAsset(options.assetId);
|
|
583
|
+
yield _this5.imageStorage.deleteContent(asset.id);
|
|
584
|
+
yield _this5.databaseStorage.deleteAsset(asset.id);
|
|
585
|
+
});
|
|
586
|
+
return function _delete(_x12) {
|
|
587
|
+
return _ref16.apply(this, arguments);
|
|
588
|
+
};
|
|
559
589
|
}()
|
|
560
590
|
};
|
|
561
591
|
}
|
|
562
592
|
get folders() {
|
|
563
|
-
var
|
|
593
|
+
var _this6 = this;
|
|
564
594
|
return {
|
|
565
595
|
list: function() {
|
|
566
|
-
var
|
|
567
|
-
|
|
568
|
-
return [...
|
|
596
|
+
var _ref17 = _asyncToGenerator(function* () {
|
|
597
|
+
_this6.begin("folders.list", void 0);
|
|
598
|
+
return [..._this6.databaseStorage.listFolders ? yield _this6.databaseStorage.listFolders(_this6.options.projectId) : []].sort((a, b) => a.name.localeCompare(b.name));
|
|
569
599
|
});
|
|
570
600
|
return function list() {
|
|
571
|
-
return
|
|
601
|
+
return _ref17.apply(this, arguments);
|
|
572
602
|
};
|
|
573
603
|
}(),
|
|
574
604
|
retrieve: function() {
|
|
575
|
-
var
|
|
576
|
-
|
|
605
|
+
var _ref18 = _asyncToGenerator(function* (options) {
|
|
606
|
+
_this6.begin("folders.retrieve", options);
|
|
577
607
|
const { folderId } = options;
|
|
578
608
|
if (!folderId) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Folder ID is required" });
|
|
579
|
-
const folder = yield
|
|
609
|
+
const folder = yield _this6.resolveFolder(folderId);
|
|
580
610
|
if (!folder) throw calmLensError(PUBLIC_ERROR_CODES.FOLDER_NOT_FOUND);
|
|
581
611
|
return folder;
|
|
582
612
|
});
|
|
583
613
|
return function retrieve(_x13) {
|
|
584
|
-
return
|
|
614
|
+
return _ref18.apply(this, arguments);
|
|
585
615
|
};
|
|
586
616
|
}(),
|
|
587
617
|
create: function() {
|
|
588
|
-
var
|
|
618
|
+
var _ref19 = _asyncToGenerator(function* (options) {
|
|
589
619
|
var _options$parentId, _options$parentId2;
|
|
590
|
-
|
|
620
|
+
_this6.begin("folders.create", options);
|
|
591
621
|
if (!options.name) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Folder name is required" });
|
|
592
622
|
if (options.parentId) {
|
|
593
|
-
if (!(yield
|
|
623
|
+
if (!(yield _this6.resolveFolder(options.parentId))) throw calmLensError(PUBLIC_ERROR_CODES.FOLDER_NOT_FOUND);
|
|
594
624
|
}
|
|
595
|
-
yield
|
|
625
|
+
yield _this6.assertUniqueFolderName(options.name, (_options$parentId = options.parentId) !== null && _options$parentId !== void 0 ? _options$parentId : null);
|
|
596
626
|
const folder = {
|
|
597
|
-
id:
|
|
598
|
-
projectId:
|
|
627
|
+
id: _this6.createId(),
|
|
628
|
+
projectId: _this6.options.projectId,
|
|
599
629
|
parentId: (_options$parentId2 = options.parentId) !== null && _options$parentId2 !== void 0 ? _options$parentId2 : null,
|
|
600
630
|
name: options.name,
|
|
601
|
-
createdAt:
|
|
602
|
-
updatedAt:
|
|
631
|
+
createdAt: _this6.now(),
|
|
632
|
+
updatedAt: _this6.now()
|
|
603
633
|
};
|
|
604
|
-
if (
|
|
634
|
+
if (_this6.databaseStorage.createFolder) return yield _this6.databaseStorage.createFolder(folder);
|
|
605
635
|
return folder;
|
|
606
636
|
});
|
|
607
637
|
return function create(_x14) {
|
|
608
|
-
return
|
|
638
|
+
return _ref19.apply(this, arguments);
|
|
609
639
|
};
|
|
610
640
|
}(),
|
|
611
641
|
update: function() {
|
|
612
|
-
var
|
|
613
|
-
|
|
642
|
+
var _ref20 = _asyncToGenerator(function* (options) {
|
|
643
|
+
_this6.begin("folders.update", options);
|
|
614
644
|
const { folderId } = options, body = _objectWithoutProperties(options, _excluded6);
|
|
615
645
|
if (!folderId) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Folder ID is required" });
|
|
616
|
-
const folder = yield
|
|
646
|
+
const folder = yield _this6.resolveFolder(folderId);
|
|
617
647
|
if (!folder) throw calmLensError(PUBLIC_ERROR_CODES.FOLDER_NOT_FOUND);
|
|
618
648
|
if (body.name !== void 0 && body.name !== folder.name) {
|
|
619
649
|
var _folder$parentId;
|
|
620
|
-
yield
|
|
650
|
+
yield _this6.assertUniqueFolderName(body.name, (_folder$parentId = folder.parentId) !== null && _folder$parentId !== void 0 ? _folder$parentId : null, folderId);
|
|
621
651
|
}
|
|
622
|
-
const updated = _objectSpread2(_objectSpread2(_objectSpread2({}, folder), body.name !== void 0 ? { name: body.name } : {}), {}, { updatedAt:
|
|
623
|
-
if (
|
|
652
|
+
const updated = _objectSpread2(_objectSpread2(_objectSpread2({}, folder), body.name !== void 0 ? { name: body.name } : {}), {}, { updatedAt: _this6.now() });
|
|
653
|
+
if (_this6.databaseStorage.updateFolder) return yield _this6.databaseStorage.updateFolder(updated);
|
|
624
654
|
return updated;
|
|
625
655
|
});
|
|
626
656
|
return function update(_x15) {
|
|
627
|
-
return
|
|
657
|
+
return _ref20.apply(this, arguments);
|
|
628
658
|
};
|
|
629
659
|
}(),
|
|
630
660
|
delete: function() {
|
|
631
|
-
var
|
|
632
|
-
|
|
661
|
+
var _ref21 = _asyncToGenerator(function* (options) {
|
|
662
|
+
_this6.begin("folders.delete", options);
|
|
633
663
|
const { folderId } = options;
|
|
634
664
|
if (!folderId) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Folder ID is required" });
|
|
635
|
-
if (!(yield
|
|
636
|
-
const hasAssets = (yield
|
|
637
|
-
const hasChildren = (
|
|
665
|
+
if (!(yield _this6.resolveFolder(folderId))) throw calmLensError(PUBLIC_ERROR_CODES.FOLDER_NOT_FOUND);
|
|
666
|
+
const hasAssets = (yield _this6.databaseStorage.listAssets(_this6.options.projectId)).some((asset) => asset.folderId === folderId);
|
|
667
|
+
const hasChildren = (_this6.databaseStorage.listFolders ? yield _this6.databaseStorage.listFolders(_this6.options.projectId) : []).some((f) => f.parentId === folderId);
|
|
638
668
|
if (hasAssets || hasChildren) throw calmLensError(PUBLIC_ERROR_CODES.FOLDER_NOT_EMPTY, { details: {
|
|
639
669
|
hasAssets,
|
|
640
670
|
hasChildren
|
|
641
671
|
} });
|
|
642
|
-
if (
|
|
672
|
+
if (_this6.databaseStorage.deleteFolder) yield _this6.databaseStorage.deleteFolder(folderId);
|
|
643
673
|
});
|
|
644
674
|
return function _delete(_x16) {
|
|
645
|
-
return
|
|
675
|
+
return _ref21.apply(this, arguments);
|
|
646
676
|
};
|
|
647
677
|
}(),
|
|
648
678
|
move: function() {
|
|
649
|
-
var
|
|
650
|
-
|
|
679
|
+
var _ref22 = _asyncToGenerator(function* (options) {
|
|
680
|
+
_this6.begin("folders.move", options);
|
|
651
681
|
const { folderId, parentId } = options;
|
|
652
682
|
if (!folderId) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Folder ID is required" });
|
|
653
|
-
const folder = yield
|
|
683
|
+
const folder = yield _this6.resolveFolder(folderId);
|
|
654
684
|
if (!folder) throw calmLensError(PUBLIC_ERROR_CODES.FOLDER_NOT_FOUND);
|
|
655
685
|
const targetParentId = parentId !== null && parentId !== void 0 ? parentId : null;
|
|
656
686
|
if (targetParentId) {
|
|
657
|
-
const parent = yield
|
|
687
|
+
const parent = yield _this6.resolveFolder(targetParentId);
|
|
658
688
|
if (!parent) throw calmLensError(PUBLIC_ERROR_CODES.FOLDER_NOT_FOUND);
|
|
659
689
|
let candidate = parent;
|
|
660
690
|
const visited = /* @__PURE__ */ new Set();
|
|
661
691
|
while (candidate) {
|
|
662
692
|
if (candidate.id === folderId || visited.has(candidate.id)) throw calmLensError(PUBLIC_ERROR_CODES.FOLDER_PARENT_CYCLE);
|
|
663
693
|
visited.add(candidate.id);
|
|
664
|
-
candidate = candidate.parentId ? yield
|
|
694
|
+
candidate = candidate.parentId ? yield _this6.resolveFolder(candidate.parentId) : void 0;
|
|
665
695
|
}
|
|
666
696
|
}
|
|
667
|
-
if (targetParentId !== folder.parentId) yield
|
|
697
|
+
if (targetParentId !== folder.parentId) yield _this6.assertUniqueFolderName(folder.name, targetParentId, folderId);
|
|
668
698
|
const updated = _objectSpread2(_objectSpread2({}, folder), {}, {
|
|
669
699
|
parentId: targetParentId,
|
|
670
|
-
updatedAt:
|
|
700
|
+
updatedAt: _this6.now()
|
|
671
701
|
});
|
|
672
|
-
if (
|
|
702
|
+
if (_this6.databaseStorage.updateFolder) return yield _this6.databaseStorage.updateFolder(updated);
|
|
673
703
|
return updated;
|
|
674
704
|
});
|
|
675
705
|
return function move(_x17) {
|
|
676
|
-
return
|
|
706
|
+
return _ref22.apply(this, arguments);
|
|
677
707
|
};
|
|
678
708
|
}()
|
|
679
709
|
};
|
|
680
710
|
}
|
|
681
711
|
ensureLens(options) {
|
|
682
|
-
var
|
|
712
|
+
var _this7 = this;
|
|
683
713
|
return _asyncToGenerator(function* () {
|
|
684
|
-
|
|
685
|
-
return
|
|
714
|
+
_this7.begin("ensureLens", options);
|
|
715
|
+
return _this7.ensureLensDefinition(options);
|
|
686
716
|
})();
|
|
687
717
|
}
|
|
688
718
|
ensureLensDefinition(options) {
|
|
@@ -713,22 +743,22 @@ list: function() {
|
|
|
713
743
|
} });
|
|
714
744
|
}
|
|
715
745
|
ensureLenses(options) {
|
|
716
|
-
var
|
|
746
|
+
var _this8 = this;
|
|
717
747
|
return _asyncToGenerator(function* () {
|
|
718
|
-
|
|
748
|
+
_this8.begin("ensureLenses", options);
|
|
719
749
|
if (options.lenses.length === 0) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "At least one lens is required" });
|
|
720
|
-
return options.lenses.map((option) =>
|
|
750
|
+
return options.lenses.map((option) => _this8.ensureLensDefinition(option));
|
|
721
751
|
})();
|
|
722
752
|
}
|
|
723
753
|
moderateAsset(options) {
|
|
724
|
-
var
|
|
754
|
+
var _this9 = this;
|
|
725
755
|
return _asyncToGenerator(function* () {
|
|
726
756
|
if ("content" in options && "plainText" in options.content && (typeof options.content.plainText !== "string" || !options.content.plainText.trim())) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Text to moderate must be a non-empty string" });
|
|
727
|
-
return
|
|
757
|
+
return _this9.createAsset(options, _this9.analyzedStatus);
|
|
728
758
|
})();
|
|
729
759
|
}
|
|
730
760
|
createAsset(options, status) {
|
|
731
|
-
var
|
|
761
|
+
var _this10 = this;
|
|
732
762
|
return _asyncToGenerator(function* () {
|
|
733
763
|
var _options$folderId, _options$visibility, _options$part$partNum;
|
|
734
764
|
if (!("content" in options) && !("url" in options) && !("part" in options)) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Either file content, URL, or multipart part is required" });
|
|
@@ -750,13 +780,13 @@ list: function() {
|
|
|
750
780
|
});
|
|
751
781
|
if (!validation.ok) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: validation.error });
|
|
752
782
|
}
|
|
753
|
-
const id =
|
|
754
|
-
const now =
|
|
783
|
+
const id = _this10.createId();
|
|
784
|
+
const now = _this10.now();
|
|
755
785
|
const type = inferAssetType(options);
|
|
756
|
-
const sizeInBytes =
|
|
786
|
+
const sizeInBytes = _this10.getSizeInBytes(options);
|
|
757
787
|
const asset = {
|
|
758
788
|
id,
|
|
759
|
-
projectId:
|
|
789
|
+
projectId: _this10.options.projectId,
|
|
760
790
|
folderId: (_options$folderId = options.folderId) !== null && _options$folderId !== void 0 ? _options$folderId : null,
|
|
761
791
|
createdAt: now,
|
|
762
792
|
updatedAt: now,
|
|
@@ -773,24 +803,24 @@ list: function() {
|
|
|
773
803
|
uploadPartsReceived: "part" in options ? (_options$part$partNum = options.part.partNumber) !== null && _options$part$partNum !== void 0 ? _options$part$partNum : 1 : void 0
|
|
774
804
|
};
|
|
775
805
|
if ("content" in options) {
|
|
776
|
-
if (!("plainText" in options.content)) yield
|
|
777
|
-
} else if ("part" in options) yield
|
|
778
|
-
return
|
|
806
|
+
if (!("plainText" in options.content)) yield _this10.imageStorage.putContent(id, options.content);
|
|
807
|
+
} else if ("part" in options) yield _this10.imageStorage.putPart(id, options.part);
|
|
808
|
+
return _this10.databaseStorage.createAsset(asset);
|
|
779
809
|
})();
|
|
780
810
|
}
|
|
781
811
|
resolveFolder(folderId) {
|
|
782
|
-
var
|
|
812
|
+
var _this11 = this;
|
|
783
813
|
return _asyncToGenerator(function* () {
|
|
784
|
-
if (!
|
|
785
|
-
const folder = yield
|
|
786
|
-
if (!folder || folder.projectId !==
|
|
814
|
+
if (!_this11.databaseStorage.getFolder) return void 0;
|
|
815
|
+
const folder = yield _this11.databaseStorage.getFolder(folderId);
|
|
816
|
+
if (!folder || folder.projectId !== _this11.options.projectId) return;
|
|
787
817
|
return folder;
|
|
788
818
|
})();
|
|
789
819
|
}
|
|
790
820
|
assertUniqueFolderName(name, parentId, excludeFolderId) {
|
|
791
|
-
var
|
|
821
|
+
var _this12 = this;
|
|
792
822
|
return _asyncToGenerator(function* () {
|
|
793
|
-
if ((
|
|
823
|
+
if ((_this12.databaseStorage.listFolders ? yield _this12.databaseStorage.listFolders(_this12.options.projectId) : []).find((f) => f.id !== excludeFolderId && f.parentId === (parentId !== null && parentId !== void 0 ? parentId : null) && f.name === name)) throw calmLensError(PUBLIC_ERROR_CODES.DUPLICATE_FOLDER_NAME, { message: `A folder named "${name}" already exists in this location.` });
|
|
794
824
|
})();
|
|
795
825
|
}
|
|
796
826
|
getSizeInBytes(options) {
|