calmlens 0.0.0-stage → 0.2.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/README.md +523 -2
- package/bin/calmlens.js +1158 -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
package/esm/index.js
ADDED
|
@@ -0,0 +1,775 @@
|
|
|
1
|
+
import { _ as getAssetPayloadByteLength, a as CalmLensError, b as _asyncToGenerator, c as DOMAIN, d as getRejectedUploadMimeTypeError, f as isRejectedUploadMimeType, g as assetToData, i as _objectWithoutProperties, m as stringifyJsonCanonical, n as verifyWebhookSignature, o as SDK_ERRORS, p as PUBLIC_ERROR_CODES, r as _objectSpread2, s as calmLensError, t as toTextAssetOptions, v as getRawBase64, y as validateAssetPayload } from "./CalmLensUtils-Dixed1Ox.js";
|
|
2
|
+
import { Api, RequestCoalescingMiddleware, RequestEvent, getErrorResponse, isRequestError } from "api-def";
|
|
3
|
+
//#region src/client/CalmLensClient.ts
|
|
4
|
+
const _excluded = ["uint8Array"];
|
|
5
|
+
const _excluded2 = ["buffer"];
|
|
6
|
+
const _excluded3 = ["assetIdentifier"];
|
|
7
|
+
const _excluded4 = ["assetIdentifier", "init"];
|
|
8
|
+
const _excluded5 = ["assetId"];
|
|
9
|
+
const _excluded6 = ["assetId"];
|
|
10
|
+
const _excluded7 = ["assetId"];
|
|
11
|
+
const _excluded8 = ["folderId"];
|
|
12
|
+
const toBlobPartBytes = (value) => {
|
|
13
|
+
if (value instanceof ArrayBuffer) return new Uint8Array(value);
|
|
14
|
+
return Uint8Array.from(value);
|
|
15
|
+
};
|
|
16
|
+
const normalizePayloadForRequest = (payload) => {
|
|
17
|
+
if ("base64" in payload && payload.base64) return _objectSpread2(_objectSpread2({}, payload), {}, { base64: getRawBase64(payload.base64) });
|
|
18
|
+
if ("uint8Array" in payload && payload.uint8Array) {
|
|
19
|
+
const { uint8Array } = payload, rest = _objectWithoutProperties(payload, _excluded);
|
|
20
|
+
return _objectSpread2(_objectSpread2({}, rest), {}, { data: new Blob([toBlobPartBytes(uint8Array)]) });
|
|
21
|
+
}
|
|
22
|
+
if ("buffer" in payload && payload.buffer) {
|
|
23
|
+
const { buffer } = payload, rest = _objectWithoutProperties(payload, _excluded2);
|
|
24
|
+
return _objectSpread2(_objectSpread2({}, rest), {}, { data: new Blob([toBlobPartBytes(buffer)]) });
|
|
25
|
+
}
|
|
26
|
+
return payload;
|
|
27
|
+
};
|
|
28
|
+
const normalizeMetadataForMultipart = (metadata) => {
|
|
29
|
+
if (!Array.isArray(metadata)) return metadata;
|
|
30
|
+
return Object.fromEntries(metadata.map((item, index) => [index, item]));
|
|
31
|
+
};
|
|
32
|
+
const normalizeMetadataOptionsForRequest = (options) => {
|
|
33
|
+
if (!("metadata" in options) || !Array.isArray(options.metadata)) return options;
|
|
34
|
+
return _objectSpread2(_objectSpread2({}, options), {}, { metadata: normalizeMetadataForMultipart(options.metadata) });
|
|
35
|
+
};
|
|
36
|
+
const normalizeAssetOptionsForRequest = (options) => {
|
|
37
|
+
const normalizedOptions = normalizeMetadataOptionsForRequest(options);
|
|
38
|
+
if ("part" in normalizedOptions) return _objectSpread2(_objectSpread2({}, normalizedOptions), {}, { part: normalizePayloadForRequest(normalizedOptions.part) });
|
|
39
|
+
if ("content" in normalizedOptions && !("plainText" in normalizedOptions.content)) return _objectSpread2(_objectSpread2({}, normalizedOptions), {}, { content: normalizePayloadForRequest(normalizedOptions.content) });
|
|
40
|
+
return normalizedOptions;
|
|
41
|
+
};
|
|
42
|
+
const toRawUploadBody = (payload) => {
|
|
43
|
+
const data = assetToData(normalizePayloadForRequest(payload));
|
|
44
|
+
if (data instanceof Response) {
|
|
45
|
+
if (!data.body) throw new Error("Response upload data must have a body");
|
|
46
|
+
return data.body;
|
|
47
|
+
}
|
|
48
|
+
if (data instanceof ArrayBuffer || data instanceof Blob) return data;
|
|
49
|
+
if ("getReader" in data) return data;
|
|
50
|
+
return new Blob([toBlobPartBytes(data)]);
|
|
51
|
+
};
|
|
52
|
+
const setOptionalQueryValue = (params, key, value) => {
|
|
53
|
+
if (value !== void 0 && value !== null) params.set(key, typeof value === "string" ? value : JSON.stringify(value));
|
|
54
|
+
};
|
|
55
|
+
const assertValidUploadMimeType = (mimeType) => {
|
|
56
|
+
if (isRejectedUploadMimeType(mimeType)) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_MIME_TYPE, {
|
|
57
|
+
message: getRejectedUploadMimeTypeError(mimeType),
|
|
58
|
+
details: { mimeType }
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
const isImageMimeType = (mimeType) => mimeType.toLowerCase().startsWith("image/");
|
|
62
|
+
const prepareSubmitAssetOptions = (options) => {
|
|
63
|
+
if (!("content" in options) && !("url" in options) && !("part" in options)) throw new Error("Either file content, URL, or multipart part is required");
|
|
64
|
+
if (!options.name) throw new Error("Asset name is required");
|
|
65
|
+
if (!("part" in options) && "isFinal" in options) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "isFinal is only valid for multipart uploads" });
|
|
66
|
+
if ("part" in options && options.part.partNumber && options.part.partNumber > 1) throw calmLensError(PUBLIC_ERROR_CODES.USE_UPLOAD_PART);
|
|
67
|
+
if ("content" in options) {
|
|
68
|
+
assertValidUploadMimeType(options.content.type);
|
|
69
|
+
if (!("plainText" in options.content)) {
|
|
70
|
+
const validation = validateAssetPayload(options.content);
|
|
71
|
+
if (!validation.ok) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: validation.error });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if ("part" in options) {
|
|
75
|
+
var _options$part$sizeInB;
|
|
76
|
+
assertValidUploadMimeType(options.part.type);
|
|
77
|
+
const partSizeInBytes = (_options$part$sizeInB = options.part.sizeInBytes) !== null && _options$part$sizeInB !== void 0 ? _options$part$sizeInB : getAssetPayloadByteLength(options.part);
|
|
78
|
+
if (isImageMimeType(options.part.type) && partSizeInBytes !== void 0) {
|
|
79
|
+
if (partSizeInBytes > 20971520) throw calmLensError(PUBLIC_ERROR_CODES.MULTIPART_IMAGE_TOO_LARGE);
|
|
80
|
+
if (options.part.isFinal && partSizeInBytes < 10485760) throw calmLensError(PUBLIC_ERROR_CODES.MULTIPART_IMAGE_TOO_SMALL);
|
|
81
|
+
}
|
|
82
|
+
const validation = validateAssetPayload(options.part, {
|
|
83
|
+
multipart: true,
|
|
84
|
+
isFinal: options.part.isFinal
|
|
85
|
+
});
|
|
86
|
+
if (!validation.ok) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: validation.error });
|
|
87
|
+
}
|
|
88
|
+
return options;
|
|
89
|
+
};
|
|
90
|
+
const appendCdnSelector = (url, options) => {
|
|
91
|
+
if (options.lens && options.transform) throw new Error("Specify either a lens or a transform, not both");
|
|
92
|
+
if (options.lens) return `${url}/@${encodeURIComponent(options.lens)}`;
|
|
93
|
+
if (!options.transform) return url;
|
|
94
|
+
return `${url}/${encodeURI(options.transform.replace(/^\/+/, ""))}`;
|
|
95
|
+
};
|
|
96
|
+
const getEnsureLensCacheKey = (options) => {
|
|
97
|
+
var _options$description, _options$transforms;
|
|
98
|
+
return stringifyJsonCanonical({
|
|
99
|
+
identifier: options.identifier,
|
|
100
|
+
name: options.name,
|
|
101
|
+
description: (_options$description = options.description) !== null && _options$description !== void 0 ? _options$description : null,
|
|
102
|
+
type: options.type,
|
|
103
|
+
active: options.active,
|
|
104
|
+
transforms: (_options$transforms = options.transforms) !== null && _options$transforms !== void 0 ? _options$transforms : null,
|
|
105
|
+
version: options.version
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
const withApiKeyHeader = (init, apiKey) => {
|
|
109
|
+
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
110
|
+
headers.set("X-Api-Key", apiKey);
|
|
111
|
+
return _objectSpread2(_objectSpread2({}, init), {}, { headers: Object.fromEntries(headers.entries()) });
|
|
112
|
+
};
|
|
113
|
+
const createApi = (options) => {
|
|
114
|
+
return new Api({
|
|
115
|
+
name: "Calm Lens",
|
|
116
|
+
baseUrl: options.baseUrl,
|
|
117
|
+
middleware: [
|
|
118
|
+
RequestCoalescingMiddleware(),
|
|
119
|
+
{ beforeSend: (context) => {
|
|
120
|
+
context.updateHeaders({ "X-Api-Key": options.apiKey });
|
|
121
|
+
} },
|
|
122
|
+
{ [RequestEvent.ERROR]: (context) => {
|
|
123
|
+
if (context.error) throw normalizeApiError(context.error);
|
|
124
|
+
} }
|
|
125
|
+
],
|
|
126
|
+
requestBackend: options.requestBackend
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
const isApiErrorPayload = (value) => {
|
|
130
|
+
return !!value && typeof value === "object" && typeof value.code === "string";
|
|
131
|
+
};
|
|
132
|
+
const normalizeApiError = (error) => {
|
|
133
|
+
const response = error instanceof Error ? getErrorResponse(error) : void 0;
|
|
134
|
+
if (error instanceof Error && isRequestError(error) && isApiErrorPayload(response === null || response === void 0 ? void 0 : response.data)) {
|
|
135
|
+
const { code, message, details, traceId } = response.data;
|
|
136
|
+
return calmLensError({
|
|
137
|
+
id: code,
|
|
138
|
+
defaultMessage: message
|
|
139
|
+
}, {
|
|
140
|
+
message,
|
|
141
|
+
details: _objectSpread2(_objectSpread2({}, details), {}, {
|
|
142
|
+
traceId,
|
|
143
|
+
network: true
|
|
144
|
+
})
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
return error;
|
|
148
|
+
};
|
|
149
|
+
const createEndpoints = (api) => {
|
|
150
|
+
const getFolders = api.endpoint().paramsOf().responseOf().build({
|
|
151
|
+
id: "getFolders",
|
|
152
|
+
method: "get",
|
|
153
|
+
path: "/projects/:projectId/folders"
|
|
154
|
+
});
|
|
155
|
+
const getFolder = api.endpoint().paramsOf().responseOf().build({
|
|
156
|
+
id: "getFolder",
|
|
157
|
+
method: "get",
|
|
158
|
+
path: "/folders/:folderId"
|
|
159
|
+
});
|
|
160
|
+
const postFolder = api.endpoint().paramsOf().bodyOf().responseOf().build({
|
|
161
|
+
id: "postFolder",
|
|
162
|
+
method: "post",
|
|
163
|
+
path: "/projects/:projectId/folders"
|
|
164
|
+
});
|
|
165
|
+
const patchFolder = api.endpoint().paramsOf().bodyOf().responseOf().build({
|
|
166
|
+
id: "patchFolder",
|
|
167
|
+
method: "patch",
|
|
168
|
+
path: "/folders/:folderId"
|
|
169
|
+
});
|
|
170
|
+
const deleteFolder = api.endpoint().paramsOf().responseOf().build({
|
|
171
|
+
id: "deleteFolder",
|
|
172
|
+
method: "delete",
|
|
173
|
+
path: "/folders/:folderId"
|
|
174
|
+
});
|
|
175
|
+
const moveFolder = api.endpoint().paramsOf().bodyOf().responseOf().build({
|
|
176
|
+
id: "moveFolder",
|
|
177
|
+
method: "post",
|
|
178
|
+
path: "/folders/:folderId/move"
|
|
179
|
+
});
|
|
180
|
+
const getAssetsPage = api.endpoint().queryOf().paramsOf().responseOf().build({
|
|
181
|
+
id: "getAssetsPage",
|
|
182
|
+
method: "get",
|
|
183
|
+
path: "/projects/:projectId/assets/page"
|
|
184
|
+
});
|
|
185
|
+
const getAsset = api.endpoint().paramsOf().responseOf().build({
|
|
186
|
+
id: "getAsset",
|
|
187
|
+
method: "get",
|
|
188
|
+
path: "/assets/:assetId"
|
|
189
|
+
});
|
|
190
|
+
const getAssetChildren = api.endpoint().paramsOf().queryOf().responseOf().build({
|
|
191
|
+
id: "getAssetChildren",
|
|
192
|
+
method: "get",
|
|
193
|
+
path: "/assets/:assetId/children"
|
|
194
|
+
});
|
|
195
|
+
const patchAsset = api.endpoint().paramsOf().bodyOf().responseOf().build({
|
|
196
|
+
id: "patchAsset",
|
|
197
|
+
method: "patch",
|
|
198
|
+
path: "/assets/:assetId"
|
|
199
|
+
});
|
|
200
|
+
const postUploadAsset = api.endpoint().bodyOf({ encoding: "multipart/form-data" }).paramsOf().queryOf().responseOf().build({
|
|
201
|
+
id: "postUploadAsset",
|
|
202
|
+
method: "post",
|
|
203
|
+
path: "/projects/:projectId/assets"
|
|
204
|
+
});
|
|
205
|
+
const postAssetSignedUrl = api.endpoint().bodyOf().paramsOf().responseOf().build({
|
|
206
|
+
id: "postAssetSignedUrl",
|
|
207
|
+
method: "post",
|
|
208
|
+
path: "/assets/:assetId/signed-url"
|
|
209
|
+
});
|
|
210
|
+
const postAssetPart = api.endpoint().bodyOf({ encoding: "multipart/form-data" }).paramsOf().queryOf().responseOf().build({
|
|
211
|
+
id: "postAssetPart",
|
|
212
|
+
method: "post",
|
|
213
|
+
path: "/assets/:assetId/part"
|
|
214
|
+
});
|
|
215
|
+
const moveAsset = api.endpoint().paramsOf().bodyOf().responseOf().build({
|
|
216
|
+
id: "moveAsset",
|
|
217
|
+
method: "post",
|
|
218
|
+
path: "/assets/:assetId/move"
|
|
219
|
+
});
|
|
220
|
+
const getEvents = api.endpoint().paramsOf().queryOf().responseOf().build({
|
|
221
|
+
id: "getEvents",
|
|
222
|
+
method: "get",
|
|
223
|
+
path: "/projects/:projectId/events"
|
|
224
|
+
});
|
|
225
|
+
return {
|
|
226
|
+
getFolders,
|
|
227
|
+
getFolder,
|
|
228
|
+
postFolder,
|
|
229
|
+
patchFolder,
|
|
230
|
+
deleteFolder,
|
|
231
|
+
moveFolder,
|
|
232
|
+
getAssetsPage,
|
|
233
|
+
getAsset,
|
|
234
|
+
getAssetChildren,
|
|
235
|
+
patchAsset,
|
|
236
|
+
postUploadAsset,
|
|
237
|
+
postAssetSignedUrl,
|
|
238
|
+
postAssetPart,
|
|
239
|
+
moveAsset,
|
|
240
|
+
ensureLens: api.endpoint().bodyOf().paramsOf().responseOf().build({
|
|
241
|
+
id: "ensureLens",
|
|
242
|
+
method: "put",
|
|
243
|
+
path: "/lenses/:lensIdentifier"
|
|
244
|
+
}),
|
|
245
|
+
ensureLenses: api.endpoint().bodyOf().paramsOf().responseOf().build({
|
|
246
|
+
id: "ensureLenses",
|
|
247
|
+
method: "put",
|
|
248
|
+
path: "/projects/:projectId/lenses"
|
|
249
|
+
}),
|
|
250
|
+
getEvents
|
|
251
|
+
};
|
|
252
|
+
};
|
|
253
|
+
var CalmLensClient = class {
|
|
254
|
+
/**
|
|
255
|
+
* Validates a webhook signature to ensure the webhook payload is authentic.
|
|
256
|
+
*
|
|
257
|
+
* @param payload - The webhook payload (string or object)
|
|
258
|
+
* @param signatureHex - The signature from the 'x-calmlens-signature' header
|
|
259
|
+
* @param secret - The webhook secret used to sign the payload
|
|
260
|
+
* @returns Promise<boolean> - True if the signature is valid, false otherwise
|
|
261
|
+
*
|
|
262
|
+
* @example
|
|
263
|
+
* ```javascript
|
|
264
|
+
* // In your webhook handler
|
|
265
|
+
* const signature = req.headers['x-calmlens-signature'];
|
|
266
|
+
* const payload = req.body;
|
|
267
|
+
* const isValid = await CalmLensClient.verifyWebhookSignature({
|
|
268
|
+
* payload,
|
|
269
|
+
* signatureHex: signature,
|
|
270
|
+
* secret: WEBHOOK_SECRET,
|
|
271
|
+
* });
|
|
272
|
+
*
|
|
273
|
+
* if (!isValid) {
|
|
274
|
+
* return res.status(401).send('Unauthorized');
|
|
275
|
+
* }
|
|
276
|
+
* ```
|
|
277
|
+
*/
|
|
278
|
+
static verifyWebhookSignature(options) {
|
|
279
|
+
return _asyncToGenerator(function* () {
|
|
280
|
+
return verifyWebhookSignature(options);
|
|
281
|
+
})();
|
|
282
|
+
}
|
|
283
|
+
constructor(options) {
|
|
284
|
+
var _this$options$baseUrl, _this$options$cdnBase;
|
|
285
|
+
this.options = options;
|
|
286
|
+
this.ensuredLenses = /* @__PURE__ */ new Map();
|
|
287
|
+
this.pendingEnsureLenses = /* @__PURE__ */ new Map();
|
|
288
|
+
this.multipartImageUploadSizes = /* @__PURE__ */ new Map();
|
|
289
|
+
this.multipartUploadQueues = /* @__PURE__ */ new Map();
|
|
290
|
+
if (!options.apiKey) throw new Error("API key is required");
|
|
291
|
+
if (!options.projectId) throw new Error("Project ID is required");
|
|
292
|
+
this.options.baseUrl = (_this$options$baseUrl = this.options.baseUrl) !== null && _this$options$baseUrl !== void 0 ? _this$options$baseUrl : `https://api.${DOMAIN}/v1`;
|
|
293
|
+
this.options.cdnBaseUrl = (_this$options$cdnBase = this.options.cdnBaseUrl) !== null && _this$options$cdnBase !== void 0 ? _this$options$cdnBase : `https://cdn.${DOMAIN}/v1`;
|
|
294
|
+
this.api = createApi(this.options);
|
|
295
|
+
this.endpoints = createEndpoints(this.api);
|
|
296
|
+
}
|
|
297
|
+
getUploadFetch() {
|
|
298
|
+
var _this$options$request, _ref, _this$options$uploadF;
|
|
299
|
+
const requestBackendFetch = (_this$options$request = this.options.requestBackend) === null || _this$options$request === void 0 ? void 0 : _this$options$request.fetch;
|
|
300
|
+
const fetchImpl = (_ref = (_this$options$uploadF = this.options.uploadFetch) !== null && _this$options$uploadF !== void 0 ? _this$options$uploadF : requestBackendFetch) !== null && _ref !== void 0 ? _ref : globalThis.fetch;
|
|
301
|
+
if (!fetchImpl) throw calmLensError(SDK_ERRORS.NO_CDN_FETCH);
|
|
302
|
+
return fetchImpl;
|
|
303
|
+
}
|
|
304
|
+
submitRawAssetUpload(options, queue) {
|
|
305
|
+
var _this = this;
|
|
306
|
+
return _asyncToGenerator(function* () {
|
|
307
|
+
var _payload$sizeInBytes;
|
|
308
|
+
const query = new URLSearchParams();
|
|
309
|
+
query.set("name", options.name);
|
|
310
|
+
query.set("queue", String(queue));
|
|
311
|
+
setOptionalQueryValue(query, "description", options.description);
|
|
312
|
+
setOptionalQueryValue(query, "processingOptions", options.processingOptions);
|
|
313
|
+
setOptionalQueryValue(query, "visibility", options.visibility);
|
|
314
|
+
setOptionalQueryValue(query, "metadata", options.metadata);
|
|
315
|
+
setOptionalQueryValue(query, "externalId", options.externalId);
|
|
316
|
+
setOptionalQueryValue(query, "externalUrl", options.externalUrl);
|
|
317
|
+
setOptionalQueryValue(query, "folderId", options.folderId);
|
|
318
|
+
const isMultipart = "part" in options;
|
|
319
|
+
const payload = isMultipart ? options.part : options.content;
|
|
320
|
+
query.set("contentType", payload.type);
|
|
321
|
+
setOptionalQueryValue(query, "sizeInBytes", (_payload$sizeInBytes = payload.sizeInBytes) !== null && _payload$sizeInBytes !== void 0 ? _payload$sizeInBytes : getAssetPayloadByteLength(payload));
|
|
322
|
+
if (isMultipart) {
|
|
323
|
+
var _part$partNumber, _part$isFinal, _part$sizeInBytes;
|
|
324
|
+
const part = payload;
|
|
325
|
+
const multipartOptions = options;
|
|
326
|
+
query.set("partNumber", String((_part$partNumber = part.partNumber) !== null && _part$partNumber !== void 0 ? _part$partNumber : 1));
|
|
327
|
+
query.set("isFinal", String((_part$isFinal = part.isFinal) !== null && _part$isFinal !== void 0 ? _part$isFinal : false));
|
|
328
|
+
setOptionalQueryValue(query, "sizeInBytes", (_part$sizeInBytes = part.sizeInBytes) !== null && _part$sizeInBytes !== void 0 ? _part$sizeInBytes : multipartOptions.sizeInBytes);
|
|
329
|
+
}
|
|
330
|
+
const baseUrl = _this.options.baseUrl.replace(/\/$/, "");
|
|
331
|
+
const path = isMultipart ? "assets/multipart-upload" : "assets/upload";
|
|
332
|
+
const url = `${baseUrl}/projects/${encodeURIComponent(_this.options.projectId)}/${path}?${query}`;
|
|
333
|
+
const response = yield _this.getUploadFetch()(url, {
|
|
334
|
+
method: "POST",
|
|
335
|
+
headers: {
|
|
336
|
+
"Content-Type": payload.type,
|
|
337
|
+
"X-Api-Key": _this.options.apiKey,
|
|
338
|
+
"X-CalmLens-Upload": isMultipart ? "part" : "content"
|
|
339
|
+
},
|
|
340
|
+
body: toRawUploadBody(payload),
|
|
341
|
+
duplex: "half"
|
|
342
|
+
});
|
|
343
|
+
const asset = yield _this.parseRawUploadResponse(response);
|
|
344
|
+
if (isMultipart && asset.status === "uploading") {
|
|
345
|
+
_this.multipartUploadQueues.set(asset.id, queue);
|
|
346
|
+
if (isImageMimeType(payload.type)) {
|
|
347
|
+
var _asset$sizeInBytes;
|
|
348
|
+
_this.multipartImageUploadSizes.set(asset.id, (_asset$sizeInBytes = asset.sizeInBytes) !== null && _asset$sizeInBytes !== void 0 ? _asset$sizeInBytes : 0);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return asset;
|
|
352
|
+
})();
|
|
353
|
+
}
|
|
354
|
+
submitRawAssetPart(assetId, part, queue) {
|
|
355
|
+
var _this2 = this;
|
|
356
|
+
return _asyncToGenerator(function* () {
|
|
357
|
+
var _part$isFinal2, _part$sizeInBytes2;
|
|
358
|
+
const query = new URLSearchParams({
|
|
359
|
+
queue: String(queue),
|
|
360
|
+
partNumber: String(part.partNumber),
|
|
361
|
+
isFinal: String((_part$isFinal2 = part.isFinal) !== null && _part$isFinal2 !== void 0 ? _part$isFinal2 : false)
|
|
362
|
+
});
|
|
363
|
+
setOptionalQueryValue(query, "sizeInBytes", (_part$sizeInBytes2 = part.sizeInBytes) !== null && _part$sizeInBytes2 !== void 0 ? _part$sizeInBytes2 : getAssetPayloadByteLength(part));
|
|
364
|
+
const url = `${_this2.options.baseUrl.replace(/\/$/, "")}/assets/${encodeURIComponent(assetId)}/part?${query}`;
|
|
365
|
+
const response = yield _this2.getUploadFetch()(url, {
|
|
366
|
+
method: "POST",
|
|
367
|
+
headers: {
|
|
368
|
+
"X-Api-Key": _this2.options.apiKey,
|
|
369
|
+
"X-CalmLens-Upload": "part"
|
|
370
|
+
},
|
|
371
|
+
body: toRawUploadBody(part),
|
|
372
|
+
duplex: "half"
|
|
373
|
+
});
|
|
374
|
+
const asset = yield _this2.parseRawUploadResponse(response);
|
|
375
|
+
if (asset.status === "uploading") {
|
|
376
|
+
var _asset$sizeInBytes2;
|
|
377
|
+
_this2.multipartImageUploadSizes.set(asset.id, (_asset$sizeInBytes2 = asset.sizeInBytes) !== null && _asset$sizeInBytes2 !== void 0 ? _asset$sizeInBytes2 : 0);
|
|
378
|
+
} else {
|
|
379
|
+
_this2.multipartImageUploadSizes.delete(assetId);
|
|
380
|
+
_this2.multipartUploadQueues.delete(assetId);
|
|
381
|
+
}
|
|
382
|
+
return asset;
|
|
383
|
+
})();
|
|
384
|
+
}
|
|
385
|
+
parseRawUploadResponse(response) {
|
|
386
|
+
return _asyncToGenerator(function* () {
|
|
387
|
+
const data = yield response.json().catch(() => void 0);
|
|
388
|
+
if (!response.ok) {
|
|
389
|
+
if (isApiErrorPayload(data)) throw calmLensError({
|
|
390
|
+
id: data.code,
|
|
391
|
+
defaultMessage: data.message
|
|
392
|
+
}, {
|
|
393
|
+
message: data.message,
|
|
394
|
+
details: _objectSpread2(_objectSpread2({}, data.details), {}, {
|
|
395
|
+
traceId: data.traceId,
|
|
396
|
+
network: true
|
|
397
|
+
})
|
|
398
|
+
});
|
|
399
|
+
throw new Error(`CalmLens upload failed with status ${response.status}`);
|
|
400
|
+
}
|
|
401
|
+
return data;
|
|
402
|
+
})();
|
|
403
|
+
}
|
|
404
|
+
get cdn() {
|
|
405
|
+
var _this3 = this;
|
|
406
|
+
return {
|
|
407
|
+
/**
|
|
408
|
+
* Create a CDN URL for asset content or previews.
|
|
409
|
+
* Private assets require an authenticated request or signed URL.
|
|
410
|
+
*/
|
|
411
|
+
getAssetUrl: (_ref2) => {
|
|
412
|
+
var _options$resource;
|
|
413
|
+
let { assetIdentifier } = _ref2, options = _objectWithoutProperties(_ref2, _excluded3);
|
|
414
|
+
if (!assetIdentifier) throw new Error("Asset identifier is required");
|
|
415
|
+
const resource = (_options$resource = options.resource) !== null && _options$resource !== void 0 ? _options$resource : "content";
|
|
416
|
+
const url = `${this.options.cdnBaseUrl.replace(/\/$/, "")}/assets/${encodeURIComponent(this.options.projectId)}/${encodeURIComponent(assetIdentifier)}/${resource}`;
|
|
417
|
+
return appendCdnSelector(url, options);
|
|
418
|
+
},
|
|
419
|
+
/**
|
|
420
|
+
* Fetch asset content or previews from the CDN.
|
|
421
|
+
* Private assets are authenticated with the client's API key.
|
|
422
|
+
*/
|
|
423
|
+
serve: function() {
|
|
424
|
+
var _ref4 = _asyncToGenerator(function* (_ref3) {
|
|
425
|
+
var _this$options$cdnFetc;
|
|
426
|
+
let { assetIdentifier, init } = _ref3, urlOptions = _objectWithoutProperties(_ref3, _excluded4);
|
|
427
|
+
const fetchImpl = (_this$options$cdnFetc = _this3.options.cdnFetch) !== null && _this$options$cdnFetc !== void 0 ? _this$options$cdnFetc : globalThis.fetch;
|
|
428
|
+
if (!fetchImpl) throw calmLensError(SDK_ERRORS.NO_CDN_FETCH);
|
|
429
|
+
return fetchImpl(_this3.cdn.getAssetUrl(_objectSpread2({ assetIdentifier }, urlOptions)), withApiKeyHeader(init, _this3.options.apiKey));
|
|
430
|
+
});
|
|
431
|
+
return function serve(_x) {
|
|
432
|
+
return _ref4.apply(this, arguments);
|
|
433
|
+
};
|
|
434
|
+
}()
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
get text() {
|
|
438
|
+
var _this4 = this;
|
|
439
|
+
return {
|
|
440
|
+
/**
|
|
441
|
+
* Moderate a short piece of text, such as a comment, caption or bio, and wait for the
|
|
442
|
+
* decision. Check `status` (`"approved"` or `"rejected"`) and `report.flagged`.
|
|
443
|
+
* The text is not retained after classification unless `processingOptions` says so.
|
|
444
|
+
*/
|
|
445
|
+
moderate: function() {
|
|
446
|
+
var _ref5 = _asyncToGenerator(function* (options) {
|
|
447
|
+
return _this4.assets.analyze(toTextAssetOptions(options));
|
|
448
|
+
});
|
|
449
|
+
return function moderate(_x2) {
|
|
450
|
+
return _ref5.apply(this, arguments);
|
|
451
|
+
};
|
|
452
|
+
}() };
|
|
453
|
+
}
|
|
454
|
+
get assets() {
|
|
455
|
+
var _this5 = this;
|
|
456
|
+
return {
|
|
457
|
+
/**
|
|
458
|
+
* List all assets for a project
|
|
459
|
+
* @param options - Pagination and filtering options
|
|
460
|
+
* @returns Promise with paginated assets
|
|
461
|
+
*/
|
|
462
|
+
list: function() {
|
|
463
|
+
var _ref6 = _asyncToGenerator(function* (options) {
|
|
464
|
+
var _options$pageIndex;
|
|
465
|
+
return (yield _this5.endpoints.getAssetsPage.submit({
|
|
466
|
+
params: { projectId: _this5.options.projectId },
|
|
467
|
+
query: _objectSpread2(_objectSpread2({}, options), {}, { pageIndex: (_options$pageIndex = options.pageIndex) !== null && _options$pageIndex !== void 0 ? _options$pageIndex : 0 })
|
|
468
|
+
})).data;
|
|
469
|
+
});
|
|
470
|
+
return function list(_x3) {
|
|
471
|
+
return _ref6.apply(this, arguments);
|
|
472
|
+
};
|
|
473
|
+
}(),
|
|
474
|
+
/**
|
|
475
|
+
* Retrieve a specific asset by ID
|
|
476
|
+
* @param assetId - The asset ID
|
|
477
|
+
* @returns Promise with asset data
|
|
478
|
+
*/
|
|
479
|
+
retrieve: function() {
|
|
480
|
+
var _ref7 = _asyncToGenerator(function* ({ assetId }) {
|
|
481
|
+
if (!assetId) throw new Error("Asset ID is required");
|
|
482
|
+
return (yield _this5.endpoints.getAsset.submit({ params: { assetId } })).data;
|
|
483
|
+
});
|
|
484
|
+
return function retrieve(_x4) {
|
|
485
|
+
return _ref7.apply(this, arguments);
|
|
486
|
+
};
|
|
487
|
+
}(),
|
|
488
|
+
update: function() {
|
|
489
|
+
var _ref9 = _asyncToGenerator(function* (_ref8) {
|
|
490
|
+
let { assetId } = _ref8, body = _objectWithoutProperties(_ref8, _excluded5);
|
|
491
|
+
if (!assetId) throw new Error("Asset ID is required");
|
|
492
|
+
return (yield _this5.endpoints.patchAsset.submit({
|
|
493
|
+
params: { assetId },
|
|
494
|
+
body
|
|
495
|
+
})).data;
|
|
496
|
+
});
|
|
497
|
+
return function update(_x5) {
|
|
498
|
+
return _ref9.apply(this, arguments);
|
|
499
|
+
};
|
|
500
|
+
}(),
|
|
501
|
+
/**
|
|
502
|
+
* Get child assets of a parent asset
|
|
503
|
+
* @param assetId - The parent asset ID
|
|
504
|
+
* @param options - Pagination options
|
|
505
|
+
* @returns Promise with paginated child assets
|
|
506
|
+
*/
|
|
507
|
+
listChildren: function() {
|
|
508
|
+
var _ref11 = _asyncToGenerator(function* (_ref10) {
|
|
509
|
+
var _options$pageIndex2;
|
|
510
|
+
let { assetId } = _ref10, options = _objectWithoutProperties(_ref10, _excluded6);
|
|
511
|
+
if (!assetId) throw new Error("Asset ID is required");
|
|
512
|
+
return (yield _this5.endpoints.getAssetChildren.submit({
|
|
513
|
+
params: { assetId },
|
|
514
|
+
query: _objectSpread2(_objectSpread2({}, options), {}, { pageIndex: (_options$pageIndex2 = options.pageIndex) !== null && _options$pageIndex2 !== void 0 ? _options$pageIndex2 : 0 })
|
|
515
|
+
})).data;
|
|
516
|
+
});
|
|
517
|
+
return function listChildren(_x6) {
|
|
518
|
+
return _ref11.apply(this, arguments);
|
|
519
|
+
};
|
|
520
|
+
}(),
|
|
521
|
+
/**
|
|
522
|
+
* Submit a new asset for processing (queued asynchronously)
|
|
523
|
+
* The asset will be queued for classification and returned immediately.
|
|
524
|
+
* @param options - Asset submission options
|
|
525
|
+
* @returns Promise with created asset
|
|
526
|
+
*/
|
|
527
|
+
create: function() {
|
|
528
|
+
var _ref12 = _asyncToGenerator(function* (options) {
|
|
529
|
+
const body = prepareSubmitAssetOptions(options);
|
|
530
|
+
if ("content" in body || "part" in body) return yield _this5.submitRawAssetUpload(body, true);
|
|
531
|
+
return (yield _this5.endpoints.postUploadAsset.submit({
|
|
532
|
+
body: normalizeAssetOptionsForRequest(body),
|
|
533
|
+
params: { projectId: _this5.options.projectId },
|
|
534
|
+
query: { queue: true }
|
|
535
|
+
})).data;
|
|
536
|
+
});
|
|
537
|
+
return function create(_x7) {
|
|
538
|
+
return _ref12.apply(this, arguments);
|
|
539
|
+
};
|
|
540
|
+
}(),
|
|
541
|
+
/**
|
|
542
|
+
* Submit a new asset and analyze it synchronously
|
|
543
|
+
* The asset will be classified immediately before the response is returned.
|
|
544
|
+
* This may take longer but ensures the asset is fully processed.
|
|
545
|
+
* @param options - Asset submission options
|
|
546
|
+
* @returns Promise with created and analyzed asset
|
|
547
|
+
*/
|
|
548
|
+
analyze: function() {
|
|
549
|
+
var _ref13 = _asyncToGenerator(function* (options) {
|
|
550
|
+
const body = prepareSubmitAssetOptions(options);
|
|
551
|
+
if ("content" in body || "part" in body) return yield _this5.submitRawAssetUpload(body, false);
|
|
552
|
+
return (yield _this5.endpoints.postUploadAsset.submit({
|
|
553
|
+
body: normalizeAssetOptionsForRequest(body),
|
|
554
|
+
params: { projectId: _this5.options.projectId },
|
|
555
|
+
query: { queue: false }
|
|
556
|
+
})).data;
|
|
557
|
+
});
|
|
558
|
+
return function analyze(_x8) {
|
|
559
|
+
return _ref13.apply(this, arguments);
|
|
560
|
+
};
|
|
561
|
+
}(),
|
|
562
|
+
/**
|
|
563
|
+
* Upload a part for a multipart upload
|
|
564
|
+
* @param assetId - The asset ID
|
|
565
|
+
* @param part - The part to upload
|
|
566
|
+
* @returns Promise with asset data
|
|
567
|
+
*/
|
|
568
|
+
uploadPart: function() {
|
|
569
|
+
var _ref14 = _asyncToGenerator(function* ({ assetId, part }) {
|
|
570
|
+
var _part$sizeInBytes3, _this$multipartUpload;
|
|
571
|
+
if (!assetId) throw new Error("Asset ID is required");
|
|
572
|
+
if (!part) throw new Error("Part options are required");
|
|
573
|
+
const validation = validateAssetPayload(part, {
|
|
574
|
+
multipart: true,
|
|
575
|
+
isFinal: part.isFinal
|
|
576
|
+
});
|
|
577
|
+
if (!validation.ok) throw calmLensError(PUBLIC_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: validation.error });
|
|
578
|
+
const existingImageSizeInBytes = _this5.multipartImageUploadSizes.get(assetId);
|
|
579
|
+
const partSizeInBytes = (_part$sizeInBytes3 = part.sizeInBytes) !== null && _part$sizeInBytes3 !== void 0 ? _part$sizeInBytes3 : getAssetPayloadByteLength(part);
|
|
580
|
+
if (existingImageSizeInBytes !== void 0 && partSizeInBytes !== void 0) {
|
|
581
|
+
const totalSizeInBytes = existingImageSizeInBytes + partSizeInBytes;
|
|
582
|
+
if (totalSizeInBytes > 20971520) throw calmLensError(PUBLIC_ERROR_CODES.MULTIPART_IMAGE_TOO_LARGE);
|
|
583
|
+
if (part.isFinal && totalSizeInBytes < 10485760) throw calmLensError(PUBLIC_ERROR_CODES.MULTIPART_IMAGE_TOO_SMALL);
|
|
584
|
+
}
|
|
585
|
+
return yield _this5.submitRawAssetPart(assetId, part, (_this$multipartUpload = _this5.multipartUploadQueues.get(assetId)) !== null && _this$multipartUpload !== void 0 ? _this$multipartUpload : false);
|
|
586
|
+
});
|
|
587
|
+
return function uploadPart(_x9) {
|
|
588
|
+
return _ref14.apply(this, arguments);
|
|
589
|
+
};
|
|
590
|
+
}(),
|
|
591
|
+
/**
|
|
592
|
+
* Create a short-lived signed CDN URL for an asset.
|
|
593
|
+
* This is useful for private assets where clients should not send app
|
|
594
|
+
* auth cookies or API keys to the CDN on every media request.
|
|
595
|
+
*/
|
|
596
|
+
createSignedUrl: function() {
|
|
597
|
+
var _ref16 = _asyncToGenerator(function* (_ref15) {
|
|
598
|
+
var _options$resource2, _options$expiresInSec;
|
|
599
|
+
let { assetId } = _ref15, options = _objectWithoutProperties(_ref15, _excluded7);
|
|
600
|
+
if (!assetId) throw new Error("Asset ID is required");
|
|
601
|
+
if (options.lens && options.transform) throw new Error("Specify either a lens or a transform, not both");
|
|
602
|
+
return (yield _this5.endpoints.postAssetSignedUrl.submit({
|
|
603
|
+
body: {
|
|
604
|
+
resource: (_options$resource2 = options.resource) !== null && _options$resource2 !== void 0 ? _options$resource2 : "content",
|
|
605
|
+
expiresInSeconds: (_options$expiresInSec = options.expiresInSeconds) !== null && _options$expiresInSec !== void 0 ? _options$expiresInSec : 900,
|
|
606
|
+
lens: options.lens,
|
|
607
|
+
transform: options.transform
|
|
608
|
+
},
|
|
609
|
+
params: { assetId }
|
|
610
|
+
})).data;
|
|
611
|
+
});
|
|
612
|
+
return function createSignedUrl(_x10) {
|
|
613
|
+
return _ref16.apply(this, arguments);
|
|
614
|
+
};
|
|
615
|
+
}(),
|
|
616
|
+
move: function() {
|
|
617
|
+
var _ref17 = _asyncToGenerator(function* ({ assetId, folderId }) {
|
|
618
|
+
if (!assetId) throw new Error("Asset ID is required");
|
|
619
|
+
return (yield _this5.endpoints.moveAsset.submit({
|
|
620
|
+
params: { assetId },
|
|
621
|
+
body: { folderId }
|
|
622
|
+
})).data;
|
|
623
|
+
});
|
|
624
|
+
return function move(_x11) {
|
|
625
|
+
return _ref17.apply(this, arguments);
|
|
626
|
+
};
|
|
627
|
+
}()
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
get events() {
|
|
631
|
+
var _this6 = this;
|
|
632
|
+
return {
|
|
633
|
+
/**
|
|
634
|
+
* List project events after a cursor, oldest first. Call without `after` to get a cursor
|
|
635
|
+
* for "now", then poll with the returned `cursor`. Event bodies match webhook deliveries.
|
|
636
|
+
*/
|
|
637
|
+
list: function() {
|
|
638
|
+
var _ref18 = _asyncToGenerator(function* (options = {}) {
|
|
639
|
+
return (yield _this6.endpoints.getEvents.submit({
|
|
640
|
+
params: { projectId: _this6.options.projectId },
|
|
641
|
+
query: options
|
|
642
|
+
})).data;
|
|
643
|
+
});
|
|
644
|
+
return function list() {
|
|
645
|
+
return _ref18.apply(this, arguments);
|
|
646
|
+
};
|
|
647
|
+
}() };
|
|
648
|
+
}
|
|
649
|
+
get folders() {
|
|
650
|
+
var _this7 = this;
|
|
651
|
+
return {
|
|
652
|
+
list: function() {
|
|
653
|
+
var _ref19 = _asyncToGenerator(function* () {
|
|
654
|
+
return (yield _this7.endpoints.getFolders.submit({ params: { projectId: _this7.options.projectId } })).data.folders;
|
|
655
|
+
});
|
|
656
|
+
return function list() {
|
|
657
|
+
return _ref19.apply(this, arguments);
|
|
658
|
+
};
|
|
659
|
+
}(),
|
|
660
|
+
retrieve: function() {
|
|
661
|
+
var _ref20 = _asyncToGenerator(function* ({ folderId }) {
|
|
662
|
+
if (!folderId) throw new Error("Folder ID is required");
|
|
663
|
+
return (yield _this7.endpoints.getFolder.submit({ params: { folderId } })).data;
|
|
664
|
+
});
|
|
665
|
+
return function retrieve(_x12) {
|
|
666
|
+
return _ref20.apply(this, arguments);
|
|
667
|
+
};
|
|
668
|
+
}(),
|
|
669
|
+
create: function() {
|
|
670
|
+
var _ref21 = _asyncToGenerator(function* (options) {
|
|
671
|
+
return (yield _this7.endpoints.postFolder.submit({
|
|
672
|
+
params: { projectId: _this7.options.projectId },
|
|
673
|
+
body: options
|
|
674
|
+
})).data;
|
|
675
|
+
});
|
|
676
|
+
return function create(_x13) {
|
|
677
|
+
return _ref21.apply(this, arguments);
|
|
678
|
+
};
|
|
679
|
+
}(),
|
|
680
|
+
update: function() {
|
|
681
|
+
var _ref23 = _asyncToGenerator(function* (_ref22) {
|
|
682
|
+
let { folderId } = _ref22, body = _objectWithoutProperties(_ref22, _excluded8);
|
|
683
|
+
if (!folderId) throw new Error("Folder ID is required");
|
|
684
|
+
return (yield _this7.endpoints.patchFolder.submit({
|
|
685
|
+
params: { folderId },
|
|
686
|
+
body
|
|
687
|
+
})).data;
|
|
688
|
+
});
|
|
689
|
+
return function update(_x14) {
|
|
690
|
+
return _ref23.apply(this, arguments);
|
|
691
|
+
};
|
|
692
|
+
}(),
|
|
693
|
+
delete: function() {
|
|
694
|
+
var _ref24 = _asyncToGenerator(function* ({ folderId }) {
|
|
695
|
+
if (!folderId) throw new Error("Folder ID is required");
|
|
696
|
+
yield _this7.endpoints.deleteFolder.submit({ params: { folderId } });
|
|
697
|
+
});
|
|
698
|
+
return function _delete(_x15) {
|
|
699
|
+
return _ref24.apply(this, arguments);
|
|
700
|
+
};
|
|
701
|
+
}(),
|
|
702
|
+
move: function() {
|
|
703
|
+
var _ref25 = _asyncToGenerator(function* ({ folderId, parentId }) {
|
|
704
|
+
if (!folderId) throw new Error("Folder ID is required");
|
|
705
|
+
return (yield _this7.endpoints.moveFolder.submit({
|
|
706
|
+
params: { folderId },
|
|
707
|
+
body: { parentId }
|
|
708
|
+
})).data;
|
|
709
|
+
});
|
|
710
|
+
return function move(_x16) {
|
|
711
|
+
return _ref25.apply(this, arguments);
|
|
712
|
+
};
|
|
713
|
+
}()
|
|
714
|
+
};
|
|
715
|
+
}
|
|
716
|
+
/**
|
|
717
|
+
* Create a named lens at any positive version, or update it with a greater version.
|
|
718
|
+
* Supplying the same definition and version is idempotent.
|
|
719
|
+
*/
|
|
720
|
+
ensureLens(options) {
|
|
721
|
+
var _this8 = this;
|
|
722
|
+
return _asyncToGenerator(function* () {
|
|
723
|
+
if (!options.identifier) throw new Error("Lens identifier is required");
|
|
724
|
+
const [lens] = yield _this8.ensureLenses({ lenses: [options] });
|
|
725
|
+
return lens;
|
|
726
|
+
})();
|
|
727
|
+
}
|
|
728
|
+
/**
|
|
729
|
+
* Create or update several named lenses in one API request.
|
|
730
|
+
* Each lens follows the same versioning and idempotency rules as ensureLens().
|
|
731
|
+
*/
|
|
732
|
+
ensureLenses({ lenses }) {
|
|
733
|
+
var _this9 = this;
|
|
734
|
+
return _asyncToGenerator(function* () {
|
|
735
|
+
if (lenses.length === 0) throw new Error("At least one lens is required");
|
|
736
|
+
if (lenses.some((option) => !option.identifier)) throw new Error("Lens identifier is required");
|
|
737
|
+
const missingOptions = [];
|
|
738
|
+
const missingKeys = /* @__PURE__ */ new Set();
|
|
739
|
+
for (const option of lenses) {
|
|
740
|
+
const cacheKey = getEnsureLensCacheKey(option);
|
|
741
|
+
if (!_this9.ensuredLenses.has(cacheKey) && !missingKeys.has(cacheKey)) {
|
|
742
|
+
missingKeys.add(cacheKey);
|
|
743
|
+
missingOptions.push(option);
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
if (missingOptions.length === 0) return lenses.map((option) => _this9.ensuredLenses.get(getEnsureLensCacheKey(option)));
|
|
747
|
+
const batchCacheKey = JSON.stringify([...missingKeys].sort());
|
|
748
|
+
let pending = _this9.pendingEnsureLenses.get(batchCacheKey);
|
|
749
|
+
if (!pending) {
|
|
750
|
+
pending = _this9.submitEnsureLenses(missingOptions);
|
|
751
|
+
_this9.pendingEnsureLenses.set(batchCacheKey, pending);
|
|
752
|
+
pending.then(() => _this9.pendingEnsureLenses.delete(batchCacheKey), () => _this9.pendingEnsureLenses.delete(batchCacheKey));
|
|
753
|
+
}
|
|
754
|
+
yield pending;
|
|
755
|
+
return lenses.map((option) => _this9.ensuredLenses.get(getEnsureLensCacheKey(option)));
|
|
756
|
+
})();
|
|
757
|
+
}
|
|
758
|
+
submitEnsureLenses(options) {
|
|
759
|
+
var _this10 = this;
|
|
760
|
+
return _asyncToGenerator(function* () {
|
|
761
|
+
const result = yield _this10.endpoints.ensureLenses.submit({
|
|
762
|
+
body: { lenses: options },
|
|
763
|
+
params: { projectId: _this10.options.projectId }
|
|
764
|
+
});
|
|
765
|
+
for (const [index, lens] of result.data.lenses.entries()) {
|
|
766
|
+
const optionsForLens = options[index];
|
|
767
|
+
if (!optionsForLens) throw new Error("CalmLens returned an unexpected lens response");
|
|
768
|
+
for (const [cacheKey, cachedLens] of _this10.ensuredLenses) if (cachedLens.identifier === optionsForLens.identifier) _this10.ensuredLenses.delete(cacheKey);
|
|
769
|
+
_this10.ensuredLenses.set(getEnsureLensCacheKey(optionsForLens), lens);
|
|
770
|
+
}
|
|
771
|
+
})();
|
|
772
|
+
}
|
|
773
|
+
};
|
|
774
|
+
//#endregion
|
|
775
|
+
export { CalmLensClient, CalmLensError, verifyWebhookSignature };
|