calmlens 0.24.0 → 0.26.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 +14 -14
- package/bin/calmlens.js +2202 -521
- package/cjs/{BrowserUpload-DB-G4tjH.js → BrowserUpload-DdkcoZLH.js} +1 -1
- package/{esm/BrowserUpload-nyagMGlJ.d.ts → cjs/BrowserUpload-zYWKtFSs.d.ts} +2 -2
- package/cjs/{CalmLensAppClient-CmikPp9x.js → CalmLensAppClient-Gu9mU_66.js} +22 -15
- package/{esm/CalmLensAppClient-8qOu4bIX.d.ts → cjs/CalmLensAppClient-mTTjqpRc.d.ts} +5 -11
- package/cjs/{CalmLensOptionKeys-BbMF4k44.js → CalmLensOptionKeys-BCITGsr9.js} +138 -90
- package/cjs/CalmLensServerClient-D5-njsNk.d.ts +357 -0
- package/cjs/{CalmLensServerClient-DJyULgDN.js → CalmLensServerClient-DHmAPT1u.js} +283 -303
- package/cjs/{CalmLensTypes-D3eKASff.d.ts → CalmLensTypes-Df8eBgWB.d.ts} +228 -59
- package/cjs/{Core-dsHfSHt3.d.ts → Core-CREGGR0L.d.ts} +10 -3
- package/cjs/{Core-DnXZJjWN.js → Core-DAf9lqe2.js} +30 -12
- package/{esm/ModerationContextLimits-BFILRYw_.js → cjs/ModerationContextLimits-D4iwqoN_.js} +858 -28
- package/cjs/{ReactUpload-DWnJElAd.js → ReactUpload-BNIbpxIH.js} +38 -2
- package/cjs/ReactUpload-D4htIz8X.d.ts +27 -0
- package/cjs/{UploadController-CEHCQar2.js → UploadController-B_lKxvpM.js} +16 -3
- package/cjs/{WebhookPayload-DiBh-W7_.d.ts → WebhookPayload-DMa6ihHr.d.ts} +152 -3
- package/cjs/app.d.ts +5 -5
- package/cjs/app.js +20 -2
- package/cjs/elements.d.ts +5 -5
- package/cjs/elements.js +102 -67
- package/{esm/index-L7S6-AX8.d.ts → cjs/index-CtpQldn_.d.ts} +2 -2
- package/cjs/index.d.ts +4 -4
- package/cjs/index.js +19 -1
- package/cjs/react-native.d.ts +3 -3
- package/cjs/react-native.js +6 -4
- package/cjs/react.d.ts +5 -4
- package/cjs/react.js +10 -14
- package/cjs/server.d.ts +5 -5
- package/cjs/server.js +20 -2
- package/cjs/testing.d.ts +11 -9
- package/cjs/testing.js +73 -41
- package/cjs/{ui-DJPSG8nD.d.ts → ui-BwFPFrno.d.ts} +2 -2
- package/cjs/{ui-DjX32iuB.js → ui-CvlJmRYM.js} +6 -4
- package/cjs/ui.d.ts +3 -3
- package/cjs/ui.js +4 -3
- package/cjs/vue.d.ts +15 -8
- package/cjs/vue.js +29 -16
- package/esm/{BrowserUpload-B9IJOAOn.js → BrowserUpload-BAWYEBd7.js} +1 -1
- package/{cjs/BrowserUpload-nyagMGlJ.d.ts → esm/BrowserUpload-zYWKtFSs.d.ts} +2 -2
- package/esm/{CalmLensAppClient-DKDX61Ip.js → CalmLensAppClient-DNkIZb6x.js} +22 -15
- package/{cjs/CalmLensAppClient-8qOu4bIX.d.ts → esm/CalmLensAppClient-mTTjqpRc.d.ts} +5 -11
- package/esm/{CalmLensOptionKeys-CEX6GvWi.js → CalmLensOptionKeys-CYHJo3nV.js} +133 -91
- package/esm/{CalmLensServerClient-DM147w2r.js → CalmLensServerClient-B1AiR4nv.js} +283 -303
- package/esm/CalmLensServerClient-D5-njsNk.d.ts +357 -0
- package/esm/{CalmLensTypes-D3eKASff.d.ts → CalmLensTypes-Df8eBgWB.d.ts} +228 -59
- package/esm/{Core-dsHfSHt3.d.ts → Core-CREGGR0L.d.ts} +10 -3
- package/esm/{Core-ix7mc07M.js → Core-xNBQxZWH.js} +30 -12
- package/{cjs/ModerationContextLimits-CPNFkp0P.js → esm/ModerationContextLimits-DxuPP4uM.js} +475 -285
- package/esm/{ReactUpload-CW1v2fmH.js → ReactUpload-BWifQdLv.js} +34 -4
- package/esm/ReactUpload-D4htIz8X.d.ts +27 -0
- package/esm/{UploadController-4wdF7tKa.js → UploadController-jlJdddyI.js} +11 -4
- package/esm/{WebhookPayload-DiBh-W7_.d.ts → WebhookPayload-DMa6ihHr.d.ts} +152 -3
- package/esm/app.d.ts +5 -5
- package/esm/app.js +3 -3
- package/esm/elements.d.ts +5 -5
- package/esm/elements.js +102 -68
- package/{cjs/index-L7S6-AX8.d.ts → esm/index-CtpQldn_.d.ts} +2 -2
- package/esm/index.d.ts +4 -4
- package/esm/index.js +1 -1
- package/esm/react-native.d.ts +3 -3
- package/esm/react-native.js +5 -5
- package/esm/react.d.ts +5 -4
- package/esm/react.js +9 -15
- package/esm/server.d.ts +5 -5
- package/esm/server.js +3 -3
- package/esm/styles.css +28 -10
- package/esm/svelte/CalmLensUploader.svelte +17 -17
- package/esm/svelte/context.js +21 -0
- package/esm/svelte/index.d.ts +12 -0
- package/esm/svelte/index.js +1 -0
- package/esm/testing.d.ts +11 -9
- package/esm/testing.js +73 -41
- package/esm/{ui-eVZ1gGuL.js → ui-BXzzqh7M.js} +6 -4
- package/esm/{ui-DJPSG8nD.d.ts → ui-BwFPFrno.d.ts} +2 -2
- package/esm/ui.d.ts +3 -3
- package/esm/ui.js +4 -4
- package/esm/vue.d.ts +15 -8
- package/esm/vue.js +29 -18
- package/package.json +16 -6
- package/cjs/CalmLensServerClient-CDhey01B.d.ts +0 -353
- package/cjs/ReactUpload-B_zmKB9X.d.ts +0 -17
- package/esm/CalmLensServerClient-CDhey01B.d.ts +0 -353
- package/esm/ReactUpload-B_zmKB9X.d.ts +0 -17
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const require_ModerationContextLimits = require("./ModerationContextLimits-
|
|
2
|
-
const require_CalmLensOptionKeys = require("./CalmLensOptionKeys-
|
|
1
|
+
const require_ModerationContextLimits = require("./ModerationContextLimits-D4iwqoN_.js");
|
|
2
|
+
const require_CalmLensOptionKeys = require("./CalmLensOptionKeys-BCITGsr9.js");
|
|
3
3
|
const require_CryptoUtils_B_x8lMIx = require("./CryptoUtils-B_x8lMIx-YBW1-bvL.js");
|
|
4
4
|
// src/contract/JsonUtils.js
|
|
5
5
|
const stringifyJsonCanonical = (value) => JSON.stringify(value, (_key, nestedValue) => {
|
|
@@ -184,6 +184,28 @@ const isTestWebhookEvent = (event) => Array.isArray(event?.facts) && event.facts
|
|
|
184
184
|
* new. Store the event's `assetSequence` per `assetId` once you have applied it.
|
|
185
185
|
*/
|
|
186
186
|
const isNewerAssetEvent = (event, lastSequence) => event.assetSequence === null || event.assetSequence === void 0 || lastSequence === null || lastSequence === void 0 || event.assetSequence > lastSequence;
|
|
187
|
+
// src/client/ServedAssetInfo.ts
|
|
188
|
+
function parseServedAssetInfo(header) {
|
|
189
|
+
if (!header) return void 0;
|
|
190
|
+
const dimensions = {};
|
|
191
|
+
let start = 0;
|
|
192
|
+
for (const delimiter of `${header},`.matchAll(/"(?:[^"\\]|\\.)*"|[",]/g)) {
|
|
193
|
+
if (delimiter[0] === "\"") return void 0;
|
|
194
|
+
if (delimiter[0] !== ",") continue;
|
|
195
|
+
const member = header.slice(start, delimiter.index).trim();
|
|
196
|
+
start = delimiter.index + 1;
|
|
197
|
+
const [key] = member.split(/[=; \t]/, 1);
|
|
198
|
+
if (key !== "width" && key !== "height") continue;
|
|
199
|
+
const match = /^(?:width|height)=([0-9]{1,15})$/.exec(member);
|
|
200
|
+
if (!match || dimensions[key] !== void 0 || Number(match[1]) === 0) return void 0;
|
|
201
|
+
dimensions[key] = Number(match[1]);
|
|
202
|
+
}
|
|
203
|
+
const { width, height } = dimensions;
|
|
204
|
+
return width === void 0 || height === void 0 ? void 0 : {
|
|
205
|
+
width,
|
|
206
|
+
height
|
|
207
|
+
};
|
|
208
|
+
}
|
|
187
209
|
const getEnsureLensCacheKey = (options) => stringifyJsonCanonical({
|
|
188
210
|
identifier: options.identifier,
|
|
189
211
|
name: options.name,
|
|
@@ -193,6 +215,35 @@ const getEnsureLensCacheKey = (options) => stringifyJsonCanonical({
|
|
|
193
215
|
transform: options.transform ?? null
|
|
194
216
|
});
|
|
195
217
|
const requireId = (value, name) => require_ModerationContextLimits.encodePathSegment(value, name);
|
|
218
|
+
/** `options`, after checking that text it classifies isn't empty. */
|
|
219
|
+
const checkedTextContent = (options) => {
|
|
220
|
+
if (options && "content" in options && "plainText" in options.content && (typeof options.content.plainText !== "string" || !options.content.plainText.trim())) throw require_CalmLensOptionKeys.invalidPayload("Text to classify must be a non-empty string");
|
|
221
|
+
return options;
|
|
222
|
+
};
|
|
223
|
+
/** A signal that aborts when any of `signals` does. */
|
|
224
|
+
const anySignal = (signals) => {
|
|
225
|
+
if (typeof AbortSignal.any === "function") return AbortSignal.any(signals);
|
|
226
|
+
const controller = new AbortController();
|
|
227
|
+
for (const signal of signals) if (signal.aborted) controller.abort(signal.reason);
|
|
228
|
+
else signal.addEventListener("abort", () => controller.abort(signal.reason), { once: true });
|
|
229
|
+
return controller.signal;
|
|
230
|
+
};
|
|
231
|
+
/**
|
|
232
|
+
* Fills in what the options leave out from the environment: `CALMLENS_API_KEY`,
|
|
233
|
+
* `CALMLENS_PROJECT_ID` and `CALMLENS_BASE_URL`, in runtimes that have one.
|
|
234
|
+
*/
|
|
235
|
+
const withEnvironmentDefaults = (options) => {
|
|
236
|
+
if (!options || typeof options !== "object" || require_CalmLensOptionKeys.getMockHooks(options)) return options;
|
|
237
|
+
const apiKey = options.apiKey ?? require_ModerationContextLimits.readEnv("CALMLENS_API_KEY");
|
|
238
|
+
const projectId = options.projectId ?? require_ModerationContextLimits.readEnv("CALMLENS_PROJECT_ID");
|
|
239
|
+
const baseUrl = options.baseUrl ?? require_ModerationContextLimits.readEnv("CALMLENS_BASE_URL");
|
|
240
|
+
return {
|
|
241
|
+
...options,
|
|
242
|
+
...apiKey !== void 0 && { apiKey },
|
|
243
|
+
...projectId !== void 0 && { projectId },
|
|
244
|
+
...baseUrl !== void 0 && { baseUrl }
|
|
245
|
+
};
|
|
246
|
+
};
|
|
196
247
|
/**
|
|
197
248
|
* `options`, once `checkOptionKeys` finds only keys `operation` takes, without the options given as
|
|
198
249
|
* `null` that the API refuses as `null`: those are left out, as if omitted.
|
|
@@ -203,26 +254,17 @@ const checked = (operation, options) => {
|
|
|
203
254
|
const nullable = require_CalmLensOptionKeys.NULLABLE_OPTION_KEYS[operation] ?? [];
|
|
204
255
|
return Object.fromEntries(Object.entries(options).filter(([key, value]) => value !== null || nullable.includes(key)));
|
|
205
256
|
};
|
|
206
|
-
/**
|
|
207
|
-
* Iterators stop rather than loop: a page that says more follow must not hand back a cursor this
|
|
208
|
-
* iteration has already sent, which would repeat the same pages forever.
|
|
209
|
-
*/
|
|
210
|
-
const assertNewCursor = (sent, cursor) => {
|
|
211
|
-
if (sent.has(cursor)) throw new require_ModerationContextLimits.CalmLensError(require_ModerationContextLimits.SDK_ERROR_CODES.INVALID_RESPONSE, {
|
|
212
|
-
message: "The API returned a cursor it had already returned, with more pages to follow",
|
|
213
|
-
details: { cursor }
|
|
214
|
-
});
|
|
215
|
-
sent.add(cursor);
|
|
216
|
-
};
|
|
217
257
|
const inspectSymbol = Symbol.for("nodejs.util.inspect.custom");
|
|
218
258
|
const writableCdnResponse = (response, metadata = response) => {
|
|
219
|
-
|
|
220
|
-
const writable = new Response(response.body, response);
|
|
259
|
+
const writable = response.status < 200 ? response : new Response(response.body, response);
|
|
221
260
|
const { url, redirected, type } = metadata;
|
|
222
261
|
return Object.defineProperties(writable, {
|
|
223
262
|
url: { value: url },
|
|
224
263
|
redirected: { value: redirected },
|
|
225
264
|
type: { value: type },
|
|
265
|
+
getAssetInfo: { value() {
|
|
266
|
+
return parseServedAssetInfo(this.headers.get("X-CalmLens-Asset-Info"));
|
|
267
|
+
} },
|
|
226
268
|
clone: { value() {
|
|
227
269
|
return writableCdnResponse(Response.prototype.clone.call(this), {
|
|
228
270
|
url,
|
|
@@ -234,13 +276,13 @@ const writableCdnResponse = (response, metadata = response) => {
|
|
|
234
276
|
};
|
|
235
277
|
/**
|
|
236
278
|
* The CalmLens API client for your server, authenticated with a project API key. Create one per
|
|
237
|
-
* project and reuse it; it holds no connections. Every method takes one options object
|
|
238
|
-
*
|
|
279
|
+
* project and reuse it; it holds no connections. Every method takes one options object, then
|
|
280
|
+
* optional `RequestOptions` for that call, and throws `CalmLensError` on failure. Options left
|
|
281
|
+
* out of the constructor come from `CALMLENS_API_KEY`, `CALMLENS_PROJECT_ID`, `CALMLENS_BASE_URL`
|
|
282
|
+
* and `CALMLENS_LOG`. Browsers and apps upload with `CalmLensAppClient` instead.
|
|
239
283
|
*/
|
|
240
|
-
var CalmLensServerClient = class
|
|
284
|
+
var CalmLensServerClient = class {
|
|
241
285
|
#apiKey;
|
|
242
|
-
/** The options this client was created with, without the API key, for `withRequestOptions`. */
|
|
243
|
-
#options;
|
|
244
286
|
#settings;
|
|
245
287
|
#lenses;
|
|
246
288
|
#connection;
|
|
@@ -263,13 +305,14 @@ var CalmLensServerClient = class CalmLensServerClient {
|
|
|
263
305
|
static async verifyWebhookSignature(options) {
|
|
264
306
|
return verifyWebhookSignature(options);
|
|
265
307
|
}
|
|
266
|
-
constructor(
|
|
308
|
+
constructor(clientOptions = {}, lenses) {
|
|
309
|
+
const options = withEnvironmentDefaults(clientOptions);
|
|
267
310
|
require_ModerationContextLimits.validateServerClientOptions(options);
|
|
268
311
|
if ((require_ModerationContextLimits.isBrowserRuntime() || require_ModerationContextLimits.isReactNativeRuntime()) && !options.dangerouslyAllowBrowser) throw require_ModerationContextLimits.invalidConfig("CalmLensServerClient is running in a browser or app, where anyone could read the API key. Call CalmLens from your server, upload from the browser or app with CalmLensAppClient, or set dangerouslyAllowBrowser: true for trusted internal tools.");
|
|
269
|
-
const { apiKey, ...rest } = options;
|
|
312
|
+
const { apiKey, projectId, ...rest } = options;
|
|
270
313
|
this.#apiKey = apiKey;
|
|
271
|
-
this.#options = rest;
|
|
272
314
|
this.#settings = {
|
|
315
|
+
projectId,
|
|
273
316
|
...require_CalmLensOptionKeys.resolveConnectionOptions(rest),
|
|
274
317
|
cdnBaseUrl: (rest.cdnBaseUrl ?? require_CalmLensOptionKeys.getMockHooks(rest)?.defaultCdnBaseUrl ?? require_ModerationContextLimits.DEFAULT_CDN_BASE_URL).replace(/\/+$/, "")
|
|
275
318
|
};
|
|
@@ -277,19 +320,7 @@ var CalmLensServerClient = class CalmLensServerClient {
|
|
|
277
320
|
ensuredLenses: /* @__PURE__ */ new Map(),
|
|
278
321
|
pendingEnsureLenses: /* @__PURE__ */ new Map()
|
|
279
322
|
};
|
|
280
|
-
this.#connection = new require_CalmLensOptionKeys.CalmLensConnection(this.#settings, apiKey);
|
|
281
|
-
}
|
|
282
|
-
/**
|
|
283
|
-
* A client with different cancellation, deadlines or retries for the calls made through it,
|
|
284
|
-
* sharing this client's credentials and lens cache. This client is unchanged.
|
|
285
|
-
*/
|
|
286
|
-
withRequestOptions(options) {
|
|
287
|
-
require_ModerationContextLimits.validateRequestOptions(options);
|
|
288
|
-
return new CalmLensServerClient({
|
|
289
|
-
...this.#options,
|
|
290
|
-
...options,
|
|
291
|
-
apiKey: this.#apiKey
|
|
292
|
-
}, this.#lenses);
|
|
323
|
+
this.#connection = new require_CalmLensOptionKeys.CalmLensConnection(this.#settings, apiKey, { readEnvironment: !require_CalmLensOptionKeys.getMockHooks(options) });
|
|
293
324
|
}
|
|
294
325
|
/** Keeps the API key out of `console.log`, `util.inspect` and `JSON.stringify`. */
|
|
295
326
|
[inspectSymbol]() {
|
|
@@ -304,28 +335,80 @@ var CalmLensServerClient = class CalmLensServerClient {
|
|
|
304
335
|
cdnBaseUrl
|
|
305
336
|
};
|
|
306
337
|
}
|
|
307
|
-
get projectPath() {
|
|
338
|
+
get #projectPath() {
|
|
308
339
|
return `/projects/${encodeURIComponent(this.#settings.projectId)}`;
|
|
309
340
|
}
|
|
310
|
-
|
|
311
|
-
|
|
341
|
+
/** Sends one of `operation`'s requests with its request options. */
|
|
342
|
+
async #send(operation, requestOptions, method, path, options = {}) {
|
|
343
|
+
return this.#connection.forCall(operation, requestOptions).json(method, path, options);
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Sends any API request with the client's API key, retries, deadlines and error handling, for
|
|
347
|
+
* endpoints or fields this SDK version doesn't cover yet. `path` is relative to the base URL,
|
|
348
|
+
* such as `/assets/asset_123`; `query` and `body` are sent as they are, without the option
|
|
349
|
+
* check the other methods make. GET, HEAD and requests with an `idempotencyKey` are retried.
|
|
350
|
+
* Resolves with the parsed JSON body and its `lastResponse` (`undefined` for an empty body).
|
|
351
|
+
*/
|
|
352
|
+
async request(method, path, { query, body, headers } = {}, requestOptions) {
|
|
353
|
+
const methods = [
|
|
354
|
+
"GET",
|
|
355
|
+
"HEAD",
|
|
356
|
+
"POST",
|
|
357
|
+
"PUT",
|
|
358
|
+
"PATCH",
|
|
359
|
+
"DELETE"
|
|
360
|
+
];
|
|
361
|
+
if (typeof method !== "string" || !methods.includes(method)) throw require_ModerationContextLimits.invalidArgument(`method must be one of ${methods.join(", ")}`);
|
|
362
|
+
if (typeof path !== "string" || !path.startsWith("/") || path.startsWith("//")) throw require_ModerationContextLimits.invalidArgument("path must start with \"/\", such as \"/assets/asset_123\"");
|
|
363
|
+
const { headers: callHeaders, ...rest } = requestOptions ?? {};
|
|
364
|
+
return this.#send(void 0, {
|
|
365
|
+
...rest,
|
|
366
|
+
...(callHeaders || headers) && { headers: {
|
|
367
|
+
...callHeaders,
|
|
368
|
+
...headers
|
|
369
|
+
} }
|
|
370
|
+
}, method, path, {
|
|
371
|
+
query,
|
|
372
|
+
body
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* A list call: each page sends `options`, checked as `operation` takes them, with the cursor of
|
|
377
|
+
* the page it fetches.
|
|
378
|
+
*/
|
|
379
|
+
#paginate(operation, options, requestOptions, send) {
|
|
380
|
+
const first = options?.cursor;
|
|
381
|
+
return require_ModerationContextLimits.listPages(async (cursor) => {
|
|
382
|
+
const valid = checked(operation, options);
|
|
383
|
+
return send(cursor === first ? valid : {
|
|
384
|
+
...valid,
|
|
385
|
+
cursor
|
|
386
|
+
});
|
|
387
|
+
}, {
|
|
388
|
+
cursor: typeof first === "string" ? first : void 0,
|
|
389
|
+
feed: operation === "events.list"
|
|
390
|
+
});
|
|
312
391
|
}
|
|
313
|
-
async submitAsset(options, wait) {
|
|
392
|
+
async #submitAsset(operation, options, wait, requestOptions) {
|
|
393
|
+
const connection = this.#connection.forCall(operation, requestOptions);
|
|
314
394
|
const body = require_CalmLensOptionKeys.prepareSubmitAssetOptions(options, {
|
|
315
|
-
deferUploadChecks:
|
|
316
|
-
partSizeBytes:
|
|
395
|
+
deferUploadChecks: connection.deferUploadChecks,
|
|
396
|
+
partSizeBytes: connection.partSizeBytes
|
|
317
397
|
});
|
|
318
|
-
const uploadsPath = `${this
|
|
319
|
-
if ("content" in body && require_CalmLensOptionKeys.splitsIntoParts(body.content,
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
398
|
+
const uploadsPath = `${this.#projectPath}/assets`;
|
|
399
|
+
if ("content" in body && require_CalmLensOptionKeys.splitsIntoParts(body.content, connection.partSizeBytes)) {
|
|
400
|
+
const { idempotencyKey: _key, extraBody: _body, ...readOptions } = requestOptions ?? {};
|
|
401
|
+
return require_CalmLensOptionKeys.expectAsset(await connection.submitInParts(uploadsPath, body, wait, {
|
|
402
|
+
partPath: (assetId) => `/assets/${encodeURIComponent(assetId)}/part`,
|
|
403
|
+
retrieve: (assetId) => this.#buildAssets().retrieve({ assetId }, readOptions)
|
|
404
|
+
}));
|
|
405
|
+
}
|
|
406
|
+
if ("content" in body || "part" in body) return require_CalmLensOptionKeys.expectAsset(await connection.submitUpload(uploadsPath, body, wait));
|
|
324
407
|
const { idempotencyKey, ...fields } = body;
|
|
325
|
-
const data = await
|
|
408
|
+
const data = await connection.json("POST", uploadsPath, {
|
|
326
409
|
query: { wait },
|
|
327
410
|
body: fields,
|
|
328
|
-
idempotencyKey: idempotencyKey ?? require_CalmLensOptionKeys.newIdempotencyKey()
|
|
411
|
+
idempotencyKey: idempotencyKey ?? requestOptions?.idempotencyKey ?? require_CalmLensOptionKeys.newIdempotencyKey()
|
|
329
412
|
});
|
|
330
413
|
return require_CalmLensOptionKeys.expectAsset(data);
|
|
331
414
|
}
|
|
@@ -348,21 +431,21 @@ var CalmLensServerClient = class CalmLensServerClient {
|
|
|
348
431
|
* URL beneath the grant; lenses, transforms and viewer claims are appended as `getAssetUrl`
|
|
349
432
|
* appends them and aren't part of the signature.
|
|
350
433
|
*/
|
|
351
|
-
signAssetUrl: async (signOptions) => {
|
|
434
|
+
signAssetUrl: async (signOptions, requestOptions) => {
|
|
352
435
|
const options = checked("cdn.signAssetUrl", signOptions);
|
|
353
436
|
require_ModerationContextLimits.validateSignAssetUrlOptions(options);
|
|
354
437
|
const { projectId, assetId, externalId, expiresIn, original, ...location } = options;
|
|
355
438
|
const project = requireId(projectId ?? this.#settings.projectId, "Project ID");
|
|
356
|
-
const signed = await this
|
|
439
|
+
const signed = await this.#send("cdn.signAssetUrl", requestOptions, "POST", `/projects/${project}/signed-asset-urls`, { body: {
|
|
357
440
|
...assetId !== void 0 ? { assetId } : { externalId },
|
|
358
441
|
expiresIn: expiresIn ?? 900,
|
|
359
442
|
original: original ?? false
|
|
360
443
|
} });
|
|
361
444
|
if (!signed || typeof signed.url !== "string" || !signed.url || typeof signed.expiresAt !== "number") throw new require_ModerationContextLimits.CalmLensError(require_ModerationContextLimits.SDK_ERROR_CODES.INVALID_RESPONSE, { message: "The API returned no signed asset URL" });
|
|
362
|
-
return {
|
|
445
|
+
return require_ModerationContextLimits.copyLastResponse({
|
|
363
446
|
url: require_ModerationContextLimits.appendAssetLocation(signed.url, location),
|
|
364
447
|
expiresAt: signed.expiresAt
|
|
365
|
-
};
|
|
448
|
+
}, signed);
|
|
366
449
|
},
|
|
367
450
|
/**
|
|
368
451
|
* The display info of an asset URL, public or signed (`{url}/info`): its type, size,
|
|
@@ -379,18 +462,23 @@ var CalmLensServerClient = class CalmLensServerClient {
|
|
|
379
462
|
* a `Response` with writable headers as soon as headers arrive, so the body streams.
|
|
380
463
|
* Non-2xx responses are returned, not thrown; `timeoutMs` covers only the wait for headers.
|
|
381
464
|
*/
|
|
382
|
-
serve: async (options) => {
|
|
465
|
+
serve: async (options, requestOptions) => {
|
|
383
466
|
const { init, resource = "rendered", ...urlOptions } = checked("cdn.serve", options);
|
|
467
|
+
const connection = this.#connection.forCall("cdn.serve", requestOptions);
|
|
468
|
+
if (requestOptions?.extraBody) throw require_ModerationContextLimits.invalidArgument("cdn.serve sends no body: extraBody can't apply");
|
|
384
469
|
const url = this.cdn.getAssetUrl({
|
|
385
470
|
...urlOptions,
|
|
386
471
|
resource
|
|
387
472
|
});
|
|
473
|
+
const extraQuery = requestOptions?.extraQuery && require_ModerationContextLimits.stringifyQuery(requestOptions.extraQuery);
|
|
388
474
|
const method = init?.method?.toUpperCase() ?? "GET";
|
|
389
|
-
const { response } = await
|
|
475
|
+
const { response } = await connection.transport.send({
|
|
476
|
+
...connection.callRequest(),
|
|
390
477
|
method,
|
|
391
|
-
url,
|
|
478
|
+
url: extraQuery ? `${url}${url.includes("?") ? "&" : "?"}${extraQuery}` : url,
|
|
479
|
+
headers: requestOptions?.headers,
|
|
392
480
|
init,
|
|
393
|
-
timeoutMs:
|
|
481
|
+
timeoutMs: connection.timeoutMs,
|
|
394
482
|
deadline: "headers",
|
|
395
483
|
fetch: this.#settings.cdnFetch
|
|
396
484
|
}, { allowHttpErrors: true });
|
|
@@ -404,11 +492,11 @@ var CalmLensServerClient = class CalmLensServerClient {
|
|
|
404
492
|
* Moderates text and waits for the decision. Sends `text` as `text/plain` content named
|
|
405
493
|
* `text` unless you pass `name`.
|
|
406
494
|
*/
|
|
407
|
-
classify: async (options) => this
|
|
495
|
+
classify: async (options, requestOptions) => this.#submitAsset("text.classify", checkedTextContent(require_ModerationContextLimits.toTextAssetOptions(checked("text.classify", options))), true, requestOptions) };
|
|
408
496
|
}
|
|
409
497
|
/** Assets: uploads, moderation and management. */
|
|
410
498
|
get assets() {
|
|
411
|
-
return this
|
|
499
|
+
return this.#buildAssets();
|
|
412
500
|
}
|
|
413
501
|
/**
|
|
414
502
|
* Signs upload tokens on your server, so a browser or app can upload through an upload preset
|
|
@@ -424,102 +512,73 @@ sign: async (options) => {
|
|
|
424
512
|
return signUploadTokenWithApiKey(this.#apiKey, checked("uploadTokens.sign", options), this.#connection.hooks?.uploadTokenTime());
|
|
425
513
|
} };
|
|
426
514
|
}
|
|
427
|
-
buildAssets() {
|
|
515
|
+
#buildAssets() {
|
|
428
516
|
return {
|
|
429
517
|
/** Searches accessible assets by keyword or a natural-language description. */
|
|
430
|
-
search: async (options) => this
|
|
431
|
-
/** Lists one page of the project's assets, with an optional filter and sort. */
|
|
432
|
-
list: async (options = {}) => this.request("GET", `${this.projectPath}/assets`, { query: checked("assets.list", options) }),
|
|
518
|
+
search: async (options, requestOptions) => this.#send("assets.search", requestOptions, "GET", `${this.#projectPath}/assets/search`, { query: checked("assets.search", options) }),
|
|
433
519
|
/**
|
|
434
|
-
*
|
|
435
|
-
*
|
|
520
|
+
* Lists the project's assets, with an optional filter and sort. Await it for one page, or
|
|
521
|
+
* `for await` over it for every matching asset, fetching pages as needed.
|
|
436
522
|
*/
|
|
437
|
-
|
|
438
|
-
checked("assets.iterate", options);
|
|
439
|
-
return this.iteratePages((cursor) => this.buildAssets().list({
|
|
440
|
-
...options,
|
|
441
|
-
cursor
|
|
442
|
-
}));
|
|
443
|
-
},
|
|
523
|
+
list: (options = {}, requestOptions) => this.#paginate("assets.list", options, requestOptions, (query) => this.#send("assets.list", requestOptions, "GET", `${this.#projectPath}/assets`, { query })),
|
|
444
524
|
/** Retrieves an asset with its status and classification results. */
|
|
445
|
-
retrieve: async (options) => {
|
|
525
|
+
retrieve: async (options, requestOptions) => {
|
|
446
526
|
const { assetId } = checked("assets.retrieve", options);
|
|
447
|
-
return this
|
|
527
|
+
return this.#send("assets.retrieve", requestOptions, "GET", `/assets/${requireId(assetId, "Asset ID")}`);
|
|
448
528
|
},
|
|
449
529
|
/** Updates an asset's editable fields. */
|
|
450
|
-
update: async (options) => {
|
|
530
|
+
update: async (options, requestOptions) => {
|
|
451
531
|
const { assetId, ...body } = checked("assets.update", options);
|
|
452
|
-
return this
|
|
453
|
-
},
|
|
454
|
-
/** Lists one page of the assets extracted from a website or multimedia document. */
|
|
455
|
-
listChildren: async (options = {}) => {
|
|
456
|
-
const { assetId, ...query } = checked("assets.listChildren", options);
|
|
457
|
-
return this.request("GET", `/assets/${requireId(assetId, "Asset ID")}/children`, { query });
|
|
532
|
+
return this.#send("assets.update", requestOptions, "PATCH", `/assets/${requireId(assetId, "Asset ID")}`, { body });
|
|
458
533
|
},
|
|
459
534
|
/**
|
|
460
|
-
* Lists
|
|
535
|
+
* Lists the assets extracted from a website or multimedia document. Await it for one page,
|
|
536
|
+
* or `for await` over it for every child.
|
|
537
|
+
*/
|
|
538
|
+
listChildren: (options, requestOptions) => this.#paginate("assets.listChildren", options ?? {}, requestOptions, ({ assetId, ...query }) => this.#send("assets.listChildren", requestOptions, "GET", `/assets/${requireId(assetId, "Asset ID")}/children`, { query })),
|
|
539
|
+
/**
|
|
540
|
+
* Lists an audio or video asset's findings timeline, in time order. Await it for one page,
|
|
541
|
+
* or `for await` over it for every segment.
|
|
461
542
|
* `classification.moderation.timeline.segmentCount` says how many segments there are.
|
|
462
543
|
*/
|
|
463
|
-
listTimeline:
|
|
464
|
-
const {
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
},
|
|
468
|
-
/** Yields every segment of an audio or video asset's findings timeline, in time order. */
|
|
469
|
-
iterateTimeline: (options) => {
|
|
470
|
-
checked("assets.iterateTimeline", options);
|
|
471
|
-
requireId(options?.assetId, "Asset ID");
|
|
472
|
-
return this.iteratePages((cursor) => this.buildAssets().listTimeline({
|
|
473
|
-
...options,
|
|
474
|
-
cursor
|
|
475
|
-
}));
|
|
476
|
-
},
|
|
477
|
-
/** Yields every child of an asset, fetching pages as needed. */
|
|
478
|
-
iterateChildren: (options) => {
|
|
479
|
-
checked("assets.iterateChildren", options);
|
|
480
|
-
requireId(options?.assetId, "Asset ID");
|
|
481
|
-
return this.iteratePages((cursor) => this.buildAssets().listChildren({
|
|
482
|
-
...options,
|
|
483
|
-
cursor
|
|
484
|
-
}));
|
|
485
|
-
},
|
|
544
|
+
listTimeline: (options, requestOptions) => this.#paginate("assets.listTimeline", options, requestOptions, ({ assetId, ...query }) => {
|
|
545
|
+
const assetPath = `${this.#projectPath}/assets/${requireId(assetId, "Asset ID")}`;
|
|
546
|
+
return this.#send("assets.listTimeline", requestOptions, "GET", `${assetPath}/timeline`, { query });
|
|
547
|
+
}),
|
|
486
548
|
/**
|
|
487
549
|
* Uploads an asset and returns without waiting for its decision (`wait=false`), with
|
|
488
550
|
* `classification.status` `queued`. Retried safely with an `Idempotency-Key`.
|
|
489
551
|
*/
|
|
490
|
-
create: async (options) => this
|
|
552
|
+
create: async (options, requestOptions) => this.#submitAsset("assets.create", checked("assets.create", options), false, requestOptions),
|
|
491
553
|
/**
|
|
492
554
|
* Uploads an asset and waits for its moderation decision. Documents, websites, audio, video
|
|
493
555
|
* and files over 20 MiB sent in parts still finish in the background and return `pending`.
|
|
494
556
|
* Retried safely with an `Idempotency-Key`.
|
|
495
557
|
*/
|
|
496
|
-
classify: async (
|
|
497
|
-
const options = checked("assets.classify", classifyOptions);
|
|
498
|
-
if (options && "content" in options && "plainText" in options.content && (typeof options.content.plainText !== "string" || !options.content.plainText.trim())) throw require_CalmLensOptionKeys.invalidPayload("Text to classify must be a non-empty string");
|
|
499
|
-
return this.submitAsset(options, true);
|
|
500
|
-
},
|
|
558
|
+
classify: async (options, requestOptions) => this.#submitAsset("assets.classify", checkedTextContent(checked("assets.classify", options)), true, requestOptions),
|
|
501
559
|
/**
|
|
502
560
|
* Uploads the next part of a multipart upload. Whether the final part waits for the
|
|
503
561
|
* decision was chosen when the upload started; pass `wait` to override it.
|
|
504
562
|
*/
|
|
505
|
-
uploadPart: async (partOptions) => {
|
|
563
|
+
uploadPart: async (partOptions, requestOptions) => {
|
|
506
564
|
const { assetId, ...options } = checked("assets.uploadPart", partOptions);
|
|
507
565
|
const encodedAssetId = requireId(assetId, "Asset ID");
|
|
508
|
-
|
|
566
|
+
const connection = this.#connection.forCall("assets.uploadPart", requestOptions);
|
|
567
|
+
return require_CalmLensOptionKeys.expectAsset(await connection.uploadPart(`/assets/${encodedAssetId}/part`, options));
|
|
509
568
|
},
|
|
510
569
|
/** Moves an asset into a folder, or to the project root with `folderId: null`. */
|
|
511
|
-
move: async (options) => {
|
|
570
|
+
move: async (options, requestOptions) => {
|
|
512
571
|
const { assetId, folderId } = checked("assets.move", options);
|
|
513
|
-
return this
|
|
572
|
+
return this.#send("assets.move", requestOptions, "POST", `/assets/${requireId(assetId, "Asset ID")}/move`, { body: { folderId } });
|
|
514
573
|
},
|
|
515
574
|
/**
|
|
516
575
|
* Processes an asset that failed (classification status `failed`) again. It returns to
|
|
517
576
|
* `processing` and is
|
|
518
577
|
* moderated like a new upload; its content must still be stored.
|
|
519
578
|
*/
|
|
520
|
-
retry: async (options) => {
|
|
579
|
+
retry: async (options, requestOptions) => {
|
|
521
580
|
const { assetId } = checked("assets.retry", options);
|
|
522
|
-
return this
|
|
581
|
+
return this.#send("assets.retry", requestOptions, "POST", `/assets/${requireId(assetId, "Asset ID")}/retry`);
|
|
523
582
|
},
|
|
524
583
|
/**
|
|
525
584
|
* Records a reviewer's decision, replacing the policy's: the asset's moderation status
|
|
@@ -527,9 +586,9 @@ sign: async (options) => {
|
|
|
527
586
|
* event is sent. An asset still pending moderation can only be blocked or purged. Purging
|
|
528
587
|
* needs `confirmPurge: true` and permanently deletes the stored content.
|
|
529
588
|
*/
|
|
530
|
-
decide: async (options) => {
|
|
589
|
+
decide: async (options, requestOptions) => {
|
|
531
590
|
const { assetId, ...body } = checked("assets.decide", options);
|
|
532
|
-
return this
|
|
591
|
+
return this.#send("assets.decide", requestOptions, "POST", `/assets/${requireId(assetId, "Asset ID")}/decision`, { body });
|
|
533
592
|
},
|
|
534
593
|
/**
|
|
535
594
|
* Completes the policy evidence behind a decided asset's moderation decision: its
|
|
@@ -541,39 +600,45 @@ sign: async (options) => {
|
|
|
541
600
|
* and a failed re-run keeps it. A reviewed asset fails with `asset/reviewed`, and one whose
|
|
542
601
|
* content is gone with `asset/content-unavailable`.
|
|
543
602
|
*/
|
|
544
|
-
explain: async (options) => {
|
|
603
|
+
explain: async (options, requestOptions) => {
|
|
545
604
|
const { assetId, rerun } = checked("assets.explain", options);
|
|
546
|
-
return this
|
|
605
|
+
return this.#send("assets.explain", requestOptions, "POST", `/assets/${requireId(assetId, "Asset ID")}/explain`, { body: rerun ? { rerun } : {} });
|
|
547
606
|
},
|
|
548
607
|
/**
|
|
549
608
|
* Waits until an asset is settled, its `classification.status` `completed` (with its
|
|
550
|
-
* decision in `classification.moderation.
|
|
609
|
+
* decision in `classification.moderation.decision`) or `failed`, and returns that asset,
|
|
551
610
|
* checking every few seconds. A `queued` or `processing` asset is still waiting, even when a
|
|
552
611
|
* reviewer has already decided it. Rejects with `sdk/timeout` if it is not settled within
|
|
553
612
|
* `timeoutMs` (default 60000). Webhooks report decisions without polling.
|
|
554
613
|
*/
|
|
555
|
-
waitForDecision: async (waitOptions) => {
|
|
614
|
+
waitForDecision: async (waitOptions, requestOptions) => {
|
|
556
615
|
const { assetId, ...options } = checked("assets.waitForDecision", waitOptions);
|
|
557
616
|
requireId(assetId, "Asset ID");
|
|
558
|
-
|
|
559
|
-
|
|
617
|
+
require_CalmLensOptionKeys.validateCallOptions(requestOptions);
|
|
618
|
+
const signals = [options.signal, requestOptions?.signal].filter((signal) => signal !== void 0);
|
|
619
|
+
const signal = signals.length > 1 ? anySignal(signals) : signals[0];
|
|
620
|
+
const checkOptions = {
|
|
621
|
+
...requestOptions,
|
|
622
|
+
...signal && { signal }
|
|
623
|
+
};
|
|
624
|
+
return require_ModerationContextLimits.pollForDecision(() => this.#send("assets.waitForDecision", checkOptions, "GET", `/assets/${requireId(assetId, "Asset ID")}`), options, this.#connection.sleep, this.#connection.hooks?.now);
|
|
560
625
|
},
|
|
561
626
|
/**
|
|
562
627
|
* Removes an asset's stored content (source, preview and retained extracted text) while
|
|
563
628
|
* keeping the asset record, metadata and classification results. The returned asset has
|
|
564
629
|
* `content.status` set to `removed` and `content.removalReason` to `requested`; the CDN no longer serves it.
|
|
565
630
|
*/
|
|
566
|
-
removeContent: async (options) => {
|
|
631
|
+
removeContent: async (options, requestOptions) => {
|
|
567
632
|
const { assetId } = checked("assets.removeContent", options);
|
|
568
|
-
return this
|
|
633
|
+
return this.#send("assets.removeContent", requestOptions, "DELETE", `/assets/${requireId(assetId, "Asset ID")}/content`);
|
|
569
634
|
},
|
|
570
635
|
/**
|
|
571
636
|
* Deletes an asset record together with its stored content. An asset extracted from another
|
|
572
637
|
* (one with a `parentId`) fails with `asset/child-not-deletable`: delete its parent instead.
|
|
573
638
|
*/
|
|
574
|
-
delete: async (options) => {
|
|
639
|
+
delete: async (options, requestOptions) => {
|
|
575
640
|
const { assetId } = checked("assets.delete", options);
|
|
576
|
-
await this
|
|
641
|
+
await this.#send("assets.delete", requestOptions, "DELETE", `/assets/${requireId(assetId, "Asset ID")}`);
|
|
577
642
|
},
|
|
578
643
|
/**
|
|
579
644
|
* Deletes up to 500 assets (`operation: "delete"`) or removes their stored content
|
|
@@ -585,13 +650,13 @@ sign: async (options) => {
|
|
|
585
650
|
* fact naming `batchId`. Assets under a safety hold are skipped. Not retried: it carries no
|
|
586
651
|
* `Idempotency-Key`.
|
|
587
652
|
*/
|
|
588
|
-
batch: async (options) => {
|
|
653
|
+
batch: async (options, requestOptions) => {
|
|
589
654
|
const { assetIds, operation } = checked("assets.batch", options);
|
|
590
655
|
require_ModerationContextLimits.validateBatchAssetsOptions({
|
|
591
656
|
assetIds,
|
|
592
657
|
operation
|
|
593
658
|
});
|
|
594
|
-
return this
|
|
659
|
+
return this.#send("assets.batch", requestOptions, "POST", `${this.#projectPath}/assets/batch`, { body: {
|
|
595
660
|
assetIds,
|
|
596
661
|
operation
|
|
597
662
|
} });
|
|
@@ -599,89 +664,47 @@ sign: async (options) => {
|
|
|
599
664
|
};
|
|
600
665
|
}
|
|
601
666
|
get events() {
|
|
602
|
-
return {
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
} });
|
|
616
|
-
},
|
|
617
|
-
/**
|
|
618
|
-
* Yields pages of events after `cursor` until no more are waiting. Save each page's
|
|
619
|
-
* `nextCursor` to resume later. Without `cursor`, yields one empty page whose `nextCursor`
|
|
620
|
-
* is "now".
|
|
621
|
-
*/
|
|
622
|
-
pages: (options = {}) => this.iterateEventPages(checked("events.pages", options))
|
|
623
|
-
};
|
|
624
|
-
}
|
|
625
|
-
async *iterateEventPages(options) {
|
|
626
|
-
let cursor = options.cursor;
|
|
627
|
-
const sent = new Set(cursor === void 0 ? [] : [cursor]);
|
|
628
|
-
for (;;) {
|
|
629
|
-
const page = await this.events.list({
|
|
630
|
-
...options,
|
|
631
|
-
cursor
|
|
632
|
-
});
|
|
633
|
-
if (page.hasMore) assertNewCursor(sent, page.nextCursor);
|
|
634
|
-
yield page;
|
|
635
|
-
if (!page.hasMore) return;
|
|
636
|
-
cursor = page.nextCursor;
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
async *iteratePages(listPage) {
|
|
640
|
-
let cursor;
|
|
641
|
-
const sent = /* @__PURE__ */ new Set();
|
|
642
|
-
for (;;) {
|
|
643
|
-
const page = await listPage(cursor);
|
|
644
|
-
const more = !!page.nextCursor && page.hasMore && page.items.length > 0;
|
|
645
|
-
if (more) assertNewCursor(sent, page.nextCursor);
|
|
646
|
-
yield* page.items;
|
|
647
|
-
if (!more) return;
|
|
648
|
-
cursor = page.nextCursor;
|
|
649
|
-
}
|
|
667
|
+
return {
|
|
668
|
+
/**
|
|
669
|
+
* Lists project events after a cursor, oldest first. Call without `cursor` to get a cursor
|
|
670
|
+
* for "now", then poll with each response's `nextCursor`. Webhook deliveries carry each
|
|
671
|
+
* event's same fields as a flat JSON body. `types` and `assetId` narrow the feed to some
|
|
672
|
+
* event types or one asset's events; a cursor belongs to the filters it was issued with, so
|
|
673
|
+
* pass the same ones with each `cursor`. `for await` over it yields every event waiting
|
|
674
|
+
* after `cursor`; save the last page's `nextCursor` to resume later.
|
|
675
|
+
*/
|
|
676
|
+
list: (options = {}, requestOptions) => this.#paginate("events.list", options, requestOptions, ({ types, ...query }) => this.#send("events.list", requestOptions, "GET", `${this.#projectPath}/events`, { query: {
|
|
677
|
+
...query,
|
|
678
|
+
types: types?.length ? types.join(",") : void 0
|
|
679
|
+
} })) };
|
|
650
680
|
}
|
|
651
681
|
get folders() {
|
|
652
682
|
return {
|
|
653
683
|
/**
|
|
654
|
-
* Lists
|
|
655
|
-
* `
|
|
684
|
+
* Lists the child folders of `parentId` (or the project root), sorted by name unless
|
|
685
|
+
* `sort` says otherwise. Await it for one page, or `for await` over it for every folder;
|
|
686
|
+
* fetch a branch when it is opened.
|
|
656
687
|
*/
|
|
657
|
-
list:
|
|
658
|
-
|
|
659
|
-
iterate: (options = {}) => {
|
|
660
|
-
checked("folders.iterate", options);
|
|
661
|
-
return this.iteratePages((cursor) => this.folders.list({
|
|
662
|
-
...options,
|
|
663
|
-
cursor
|
|
664
|
-
}));
|
|
665
|
-
},
|
|
666
|
-
retrieve: async (options) => {
|
|
688
|
+
list: (options = {}, requestOptions) => this.#paginate("folders.list", options, requestOptions, (query) => this.#send("folders.list", requestOptions, "GET", `${this.#projectPath}/folders`, { query })),
|
|
689
|
+
retrieve: async (options, requestOptions) => {
|
|
667
690
|
const { folderId } = checked("folders.retrieve", options);
|
|
668
|
-
return this
|
|
691
|
+
return this.#send("folders.retrieve", requestOptions, "GET", `/folders/${requireId(folderId, "Folder ID")}`);
|
|
669
692
|
},
|
|
670
693
|
/** Creates a folder at the project root or inside `parentId`. */
|
|
671
|
-
create: async (options) => this
|
|
672
|
-
update: async (options) => {
|
|
694
|
+
create: async (options, requestOptions) => this.#send("folders.create", requestOptions, "POST", `${this.#projectPath}/folders`, { body: checked("folders.create", options) }),
|
|
695
|
+
update: async (options, requestOptions) => {
|
|
673
696
|
const { folderId, ...body } = checked("folders.update", options);
|
|
674
|
-
return this
|
|
697
|
+
return this.#send("folders.update", requestOptions, "PATCH", `/folders/${requireId(folderId, "Folder ID")}`, { body });
|
|
675
698
|
},
|
|
676
699
|
/** Deletes an empty folder; throws `folder/not-empty` otherwise. */
|
|
677
|
-
delete: async (options) => {
|
|
700
|
+
delete: async (options, requestOptions) => {
|
|
678
701
|
const { folderId } = checked("folders.delete", options);
|
|
679
|
-
await this
|
|
702
|
+
await this.#send("folders.delete", requestOptions, "DELETE", `/folders/${requireId(folderId, "Folder ID")}`);
|
|
680
703
|
},
|
|
681
704
|
/** Moves a folder under `parentId`, or to the project root with `parentId: null`. */
|
|
682
|
-
move: async (options) => {
|
|
705
|
+
move: async (options, requestOptions) => {
|
|
683
706
|
const { folderId, parentId } = checked("folders.move", options);
|
|
684
|
-
return this
|
|
707
|
+
return this.#send("folders.move", requestOptions, "POST", `/folders/${requireId(folderId, "Folder ID")}/move`, { body: { parentId } });
|
|
685
708
|
}
|
|
686
709
|
};
|
|
687
710
|
}
|
|
@@ -691,53 +714,33 @@ sign: async (options) => {
|
|
|
691
714
|
*/
|
|
692
715
|
get hashLists() {
|
|
693
716
|
return {
|
|
694
|
-
/** Lists
|
|
695
|
-
list:
|
|
696
|
-
|
|
697
|
-
iterate: (options = {}) => {
|
|
698
|
-
checked("hashLists.iterate", options);
|
|
699
|
-
return this.iteratePages((cursor) => this.hashLists.list({
|
|
700
|
-
...options,
|
|
701
|
-
cursor
|
|
702
|
-
}));
|
|
703
|
-
},
|
|
704
|
-
retrieve: async (options) => {
|
|
717
|
+
/** Lists the project's hash lists, newest first unless `sort` says otherwise. */
|
|
718
|
+
list: (options = {}, requestOptions) => this.#paginate("hashLists.list", options, requestOptions, (query) => this.#send("hashLists.list", requestOptions, "GET", `${this.#projectPath}/hash-lists`, { query })),
|
|
719
|
+
retrieve: async (options, requestOptions) => {
|
|
705
720
|
const { hashListId } = checked("hashLists.retrieve", options);
|
|
706
|
-
return this
|
|
721
|
+
return this.#send("hashLists.retrieve", requestOptions, "GET", `/hash-lists/${requireId(hashListId, "Hash list ID")}`);
|
|
707
722
|
},
|
|
708
|
-
create: async (options) => this
|
|
723
|
+
create: async (options, requestOptions) => this.#send("hashLists.create", requestOptions, "POST", `${this.#projectPath}/hash-lists`, { body: checked("hashLists.create", options) }),
|
|
709
724
|
/** Renames a list or changes its action; the new action applies to later uploads. */
|
|
710
|
-
update: async (options) => {
|
|
725
|
+
update: async (options, requestOptions) => {
|
|
711
726
|
const { hashListId, ...body } = checked("hashLists.update", options);
|
|
712
|
-
return this
|
|
727
|
+
return this.#send("hashLists.update", requestOptions, "PATCH", `/hash-lists/${requireId(hashListId, "Hash list ID")}`, { body });
|
|
713
728
|
},
|
|
714
729
|
/** Deletes the list and every entry in it. */
|
|
715
|
-
delete: async (options) => {
|
|
730
|
+
delete: async (options, requestOptions) => {
|
|
716
731
|
const { hashListId } = checked("hashLists.delete", options);
|
|
717
|
-
await this
|
|
718
|
-
},
|
|
719
|
-
/** Yields every entry of a list, fetching pages as needed. */
|
|
720
|
-
iterateEntries: (options) => {
|
|
721
|
-
checked("hashLists.iterateEntries", options);
|
|
722
|
-
requireId(options?.hashListId, "Hash list ID");
|
|
723
|
-
return this.iteratePages((cursor) => this.hashLists.listEntries({
|
|
724
|
-
...options,
|
|
725
|
-
cursor
|
|
726
|
-
}));
|
|
727
|
-
},
|
|
728
|
-
/** Lists one page of a list's entries, newest first unless `sort` says otherwise. */
|
|
729
|
-
listEntries: async (options) => {
|
|
730
|
-
const { hashListId, ...query } = checked("hashLists.listEntries", options);
|
|
731
|
-
return this.request("GET", `/hash-lists/${requireId(hashListId, "Hash list ID")}/entries`, { query });
|
|
732
|
+
await this.#send("hashLists.delete", requestOptions, "DELETE", `/hash-lists/${requireId(hashListId, "Hash list ID")}`);
|
|
732
733
|
},
|
|
734
|
+
/** Lists a list's entries, newest first unless `sort` says otherwise. */
|
|
735
|
+
listEntries: (options, requestOptions) => this.#paginate("hashLists.listEntries", options, requestOptions, ({ hashListId, ...query }) => this.#send("hashLists.listEntries", requestOptions, "GET", `/hash-lists/${requireId(hashListId, "Hash list ID")}/entries`, { query })),
|
|
733
736
|
/** Adds a hash, or an image asset's hash; a hash already in the list returns its entry. */
|
|
734
|
-
addEntry: async (options) => {
|
|
737
|
+
addEntry: async (options, requestOptions) => {
|
|
735
738
|
const { hashListId, ...body } = checked("hashLists.addEntry", options);
|
|
736
|
-
return this
|
|
739
|
+
return this.#send("hashLists.addEntry", requestOptions, "POST", `/hash-lists/${requireId(hashListId, "Hash list ID")}/entries`, { body });
|
|
737
740
|
},
|
|
738
|
-
removeEntry: async (options) => {
|
|
741
|
+
removeEntry: async (options, requestOptions) => {
|
|
739
742
|
const { entryId } = checked("hashLists.removeEntry", options);
|
|
740
|
-
await this
|
|
743
|
+
await this.#send("hashLists.removeEntry", requestOptions, "DELETE", `/hash-list-entries/${requireId(entryId, "Hash list entry ID")}`);
|
|
741
744
|
}
|
|
742
745
|
};
|
|
743
746
|
}
|
|
@@ -749,47 +752,39 @@ sign: async (options) => {
|
|
|
749
752
|
get policies() {
|
|
750
753
|
const policyPath = (policyId) => `/policies/${requireId(policyId, "Policy ID")}`;
|
|
751
754
|
return {
|
|
752
|
-
/** Lists
|
|
753
|
-
list:
|
|
754
|
-
|
|
755
|
-
iterate: (options = {}) => {
|
|
756
|
-
checked("policies.iterate", options);
|
|
757
|
-
return this.iteratePages((cursor) => this.policies.list({
|
|
758
|
-
...options,
|
|
759
|
-
cursor
|
|
760
|
-
}));
|
|
761
|
-
},
|
|
762
|
-
retrieve: async (options) => this.request("GET", policyPath(checked("policies.retrieve", options).policyId)),
|
|
755
|
+
/** Lists the organisation's policies, oldest first unless `sort` says otherwise. */
|
|
756
|
+
list: (options = {}, requestOptions) => this.#paginate("policies.list", options, requestOptions, (query) => this.#send("policies.list", requestOptions, "GET", "/policies", { query })),
|
|
757
|
+
retrieve: async (options, requestOptions) => this.#send("policies.retrieve", requestOptions, "GET", policyPath(checked("policies.retrieve", options).policyId)),
|
|
763
758
|
/** Creates a policy from `settings`, or a copy of a built-in preset with `presetId`. */
|
|
764
|
-
create: async (options) => this
|
|
759
|
+
create: async (options, requestOptions) => this.#send("policies.create", requestOptions, "POST", "/policies", { body: checked("policies.create", options) }),
|
|
765
760
|
/** Changes the name, description or settings; later moderation uses them. */
|
|
766
|
-
update: async (options) => {
|
|
761
|
+
update: async (options, requestOptions) => {
|
|
767
762
|
const { policyId, ...body } = checked("policies.update", options);
|
|
768
|
-
return this
|
|
763
|
+
return this.#send("policies.update", requestOptions, "PATCH", policyPath(policyId), { body });
|
|
769
764
|
},
|
|
770
765
|
/** Restores the settings of the preset release a `pinned` or `copy` policy is pinned to. */
|
|
771
|
-
reset: async (options) => {
|
|
766
|
+
reset: async (options, requestOptions) => {
|
|
772
767
|
const { policyId, ...body } = checked("policies.reset", options);
|
|
773
|
-
return this
|
|
768
|
+
return this.#send("policies.reset", requestOptions, "POST", `${policyPath(policyId)}/reset`, { body });
|
|
774
769
|
},
|
|
775
770
|
/** Moves an unchanged `pinned` or `copy` policy to the latest release of its preset. */
|
|
776
|
-
upgrade: async (options) => {
|
|
771
|
+
upgrade: async (options, requestOptions) => {
|
|
777
772
|
const { policyId, ...body } = checked("policies.upgrade", options);
|
|
778
|
-
return this
|
|
773
|
+
return this.#send("policies.upgrade", requestOptions, "POST", `${policyPath(policyId)}/upgrade`, { body });
|
|
779
774
|
},
|
|
780
775
|
/** Switches automatic updates on (`live`) or off (`pinned`) for the organisation's own preset policy. */
|
|
781
|
-
setAutomaticUpdates: async (options) => {
|
|
776
|
+
setAutomaticUpdates: async (options, requestOptions) => {
|
|
782
777
|
const { policyId, ...body } = checked("policies.setAutomaticUpdates", options);
|
|
783
|
-
return this
|
|
778
|
+
return this.#send("policies.setAutomaticUpdates", requestOptions, "PUT", `${policyPath(policyId)}/automatic-updates`, { body });
|
|
784
779
|
},
|
|
785
780
|
/** Deletes a policy that no project or upload preset uses. */
|
|
786
|
-
delete: async (options) => {
|
|
787
|
-
await this
|
|
781
|
+
delete: async (options, requestOptions) => {
|
|
782
|
+
await this.#send("policies.delete", requestOptions, "DELETE", policyPath(checked("policies.delete", options).policyId));
|
|
788
783
|
},
|
|
789
784
|
/** The policy the project moderates with. */
|
|
790
|
-
retrieveAssigned: async () => this
|
|
785
|
+
retrieveAssigned: async (requestOptions) => this.#send("policies.retrieveAssigned", requestOptions, "GET", `${this.#projectPath}/policy`),
|
|
791
786
|
/** Chooses the policy the project moderates with. */
|
|
792
|
-
assign: async (options) => this
|
|
787
|
+
assign: async (options, requestOptions) => this.#send("policies.assign", requestOptions, "PUT", `${this.#projectPath}/policy`, { body: checked("policies.assign", options) })
|
|
793
788
|
};
|
|
794
789
|
}
|
|
795
790
|
/**
|
|
@@ -799,25 +794,17 @@ sign: async (options) => {
|
|
|
799
794
|
*/
|
|
800
795
|
get uploadPresets() {
|
|
801
796
|
return {
|
|
802
|
-
/** Lists
|
|
803
|
-
list:
|
|
804
|
-
/** Yields every upload preset of the project, fetching pages as needed. */
|
|
805
|
-
iterate: (options = {}) => {
|
|
806
|
-
checked("uploadPresets.iterate", options);
|
|
807
|
-
return this.iteratePages((cursor) => this.uploadPresets.list({
|
|
808
|
-
...options,
|
|
809
|
-
cursor
|
|
810
|
-
}));
|
|
811
|
-
},
|
|
797
|
+
/** Lists the project's upload presets, oldest first unless `sort` says otherwise. */
|
|
798
|
+
list: (options = {}, requestOptions) => this.#paginate("uploadPresets.list", options, requestOptions, (query) => this.#send("uploadPresets.list", requestOptions, "GET", `${this.#projectPath}/upload-presets`, { query })),
|
|
812
799
|
/**
|
|
813
800
|
* Creates each preset, or replaces it with the definition sent, by key. Every preset is
|
|
814
801
|
* checked before any is saved. Returns the stored presets in the order sent.
|
|
815
802
|
*/
|
|
816
|
-
ensure: async (options) => this
|
|
803
|
+
ensure: async (options, requestOptions) => this.#send("uploadPresets.ensure", requestOptions, "PUT", `${this.#projectPath}/upload-presets`, { body: checked("uploadPresets.ensure", options) }),
|
|
817
804
|
/** Deletes an upload preset; assets already uploaded through it stay. */
|
|
818
|
-
delete: async (options) => {
|
|
805
|
+
delete: async (options, requestOptions) => {
|
|
819
806
|
const { uploadPresetId } = checked("uploadPresets.delete", options);
|
|
820
|
-
await this
|
|
807
|
+
await this.#send("uploadPresets.delete", requestOptions, "DELETE", `/upload-presets/${requireId(uploadPresetId, "Upload preset ID")}`);
|
|
821
808
|
}
|
|
822
809
|
};
|
|
823
810
|
}
|
|
@@ -827,8 +814,8 @@ sign: async (options) => {
|
|
|
827
814
|
*/
|
|
828
815
|
get projectSettings() {
|
|
829
816
|
return {
|
|
830
|
-
get: async () => this
|
|
831
|
-
update: async (options) => this
|
|
817
|
+
get: async (requestOptions) => this.#send("projectSettings.get", requestOptions, "GET", `${this.#projectPath}/settings`),
|
|
818
|
+
update: async (options, requestOptions) => this.#send("projectSettings.update", requestOptions, "PUT", `${this.#projectPath}/settings`, { body: checked("projectSettings.update", options) })
|
|
832
819
|
};
|
|
833
820
|
}
|
|
834
821
|
/**
|
|
@@ -837,12 +824,12 @@ sign: async (options) => {
|
|
|
837
824
|
*/
|
|
838
825
|
get metadataFields() {
|
|
839
826
|
return {
|
|
840
|
-
list: async () => this
|
|
841
|
-
ensure: async (options) => this
|
|
827
|
+
list: async (requestOptions) => this.#send("metadataFields.list", requestOptions, "GET", `${this.#projectPath}/metadata-fields`),
|
|
828
|
+
ensure: async (options, requestOptions) => this.#send("metadataFields.ensure", requestOptions, "PUT", `${this.#projectPath}/metadata-fields`, { body: checked("metadataFields.ensure", options) }),
|
|
842
829
|
/** Deletes a field definition; assets keep their values. */
|
|
843
|
-
delete: async (options) => {
|
|
830
|
+
delete: async (options, requestOptions) => {
|
|
844
831
|
const { fieldId, ...query } = checked("metadataFields.delete", options);
|
|
845
|
-
await this
|
|
832
|
+
await this.#send("metadataFields.delete", requestOptions, "DELETE", `${this.#projectPath}/metadata-fields/${requireId(fieldId, "Metadata field ID")}`, { query });
|
|
846
833
|
}
|
|
847
834
|
};
|
|
848
835
|
}
|
|
@@ -857,34 +844,26 @@ sign: async (options) => {
|
|
|
857
844
|
for (const [cacheKey, lens] of this.#lenses.ensuredLenses) if (matches(lens)) this.#lenses.ensuredLenses.delete(cacheKey);
|
|
858
845
|
};
|
|
859
846
|
return {
|
|
860
|
-
/** Lists
|
|
861
|
-
list:
|
|
862
|
-
/** Yields every lens of the project, fetching pages as needed. */
|
|
863
|
-
iterate: (options = {}) => {
|
|
864
|
-
checked("lenses.iterate", options);
|
|
865
|
-
return this.iteratePages((cursor) => this.lenses.list({
|
|
866
|
-
...options,
|
|
867
|
-
cursor
|
|
868
|
-
}));
|
|
869
|
-
},
|
|
847
|
+
/** Lists the project's lenses, newest first unless `sort` says otherwise. */
|
|
848
|
+
list: (options = {}, requestOptions) => this.#paginate("lenses.list", options, requestOptions, (query) => this.#send("lenses.list", requestOptions, "GET", `${this.#projectPath}/lenses`, { query })),
|
|
870
849
|
/** Creates a lens; its identifier must be new to the project. */
|
|
871
|
-
create: async (options) => {
|
|
872
|
-
const lens = await this
|
|
850
|
+
create: async (options, requestOptions) => {
|
|
851
|
+
const lens = await this.#send("lenses.create", requestOptions, "POST", `${this.#projectPath}/lenses`, { body: checked("lenses.create", options) });
|
|
873
852
|
forget((cached) => cached.identifier === lens.identifier);
|
|
874
853
|
return lens;
|
|
875
854
|
},
|
|
876
|
-
retrieve: async (options) => this
|
|
855
|
+
retrieve: async (options, requestOptions) => this.#send("lenses.retrieve", requestOptions, "GET", lensPath(checked("lenses.retrieve", options).lensId)),
|
|
877
856
|
/** Changes a lens's name, description, type, transform or whether the CDN serves it. */
|
|
878
|
-
update: async (options) => {
|
|
857
|
+
update: async (options, requestOptions) => {
|
|
879
858
|
const { lensId, ...body } = checked("lenses.update", options);
|
|
880
|
-
const lens = await this
|
|
859
|
+
const lens = await this.#send("lenses.update", requestOptions, "PATCH", lensPath(lensId), { body });
|
|
881
860
|
forget((cached) => cached.id === lens.id);
|
|
882
861
|
return lens;
|
|
883
862
|
},
|
|
884
863
|
/** Deletes a lens; URLs that select it stop being served. */
|
|
885
|
-
delete: async (options) => {
|
|
864
|
+
delete: async (options, requestOptions) => {
|
|
886
865
|
const { lensId } = checked("lenses.delete", options);
|
|
887
|
-
await this
|
|
866
|
+
await this.#send("lenses.delete", requestOptions, "DELETE", lensPath(lensId));
|
|
888
867
|
forget((cached) => cached.id === lensId);
|
|
889
868
|
}
|
|
890
869
|
};
|
|
@@ -893,10 +872,10 @@ sign: async (options) => {
|
|
|
893
872
|
* Creates a named lens, or changes it to this definition. Sending the same definition again
|
|
894
873
|
* returns the lens unchanged, from the client's lens cache once it has seen it.
|
|
895
874
|
*/
|
|
896
|
-
async ensureLens(options) {
|
|
875
|
+
async ensureLens(options, requestOptions) {
|
|
897
876
|
checked("ensureLens", options);
|
|
898
877
|
if (!options?.identifier) throw require_ModerationContextLimits.invalidArgument("Lens identifier is required");
|
|
899
|
-
const [lens] = await this.ensureLenses({ lenses: [options] });
|
|
878
|
+
const [lens] = await this.ensureLenses({ lenses: [options] }, requestOptions);
|
|
900
879
|
return lens;
|
|
901
880
|
}
|
|
902
881
|
/**
|
|
@@ -904,7 +883,8 @@ sign: async (options) => {
|
|
|
904
883
|
* consecutive requests of at most {@link ENSURE_LENSES_MAX_PER_REQUEST} definitions, in order.
|
|
905
884
|
* Each lens is created or changed as ensureLens() does.
|
|
906
885
|
*/
|
|
907
|
-
async ensureLenses(options) {
|
|
886
|
+
async ensureLenses(options, requestOptions) {
|
|
887
|
+
require_CalmLensOptionKeys.validateCallOptions(requestOptions);
|
|
908
888
|
const { lenses } = checked("ensureLenses", options);
|
|
909
889
|
if (!Array.isArray(lenses) || lenses.length === 0) throw require_ModerationContextLimits.invalidArgument("At least one lens is required");
|
|
910
890
|
if (lenses.some((option) => !option?.identifier)) throw require_ModerationContextLimits.invalidArgument("Lens identifier is required");
|
|
@@ -923,7 +903,7 @@ sign: async (options) => {
|
|
|
923
903
|
const batchCacheKey = JSON.stringify([...missingKeys].sort());
|
|
924
904
|
let pending = pendingEnsureLenses.get(batchCacheKey);
|
|
925
905
|
if (!pending) {
|
|
926
|
-
pending = this
|
|
906
|
+
pending = this.#submitEnsureLenses(missingOptions, requestOptions);
|
|
927
907
|
pendingEnsureLenses.set(batchCacheKey, pending);
|
|
928
908
|
pending.then(() => pendingEnsureLenses.delete(batchCacheKey), () => pendingEnsureLenses.delete(batchCacheKey));
|
|
929
909
|
}
|
|
@@ -935,11 +915,11 @@ sign: async (options) => {
|
|
|
935
915
|
return lens;
|
|
936
916
|
});
|
|
937
917
|
}
|
|
938
|
-
async submitEnsureLenses(options) {
|
|
939
|
-
for (let start = 0; start < options.length; start += 100) await this
|
|
918
|
+
async #submitEnsureLenses(options, requestOptions) {
|
|
919
|
+
for (let start = 0; start < options.length; start += 100) await this.#submitEnsureLensesChunk(options.slice(start, start + 100), requestOptions);
|
|
940
920
|
}
|
|
941
|
-
async submitEnsureLensesChunk(options) {
|
|
942
|
-
const result = await this
|
|
921
|
+
async #submitEnsureLensesChunk(options, requestOptions) {
|
|
922
|
+
const result = await this.#send("ensureLenses", requestOptions, "PUT", `${this.#projectPath}/lenses`, { body: { lenses: options } });
|
|
943
923
|
const returned = Array.isArray(result?.lenses) ? result.lenses : [];
|
|
944
924
|
if (returned.length !== options.length) throw new require_ModerationContextLimits.CalmLensError(require_ModerationContextLimits.SDK_ERROR_CODES.INVALID_RESPONSE, { message: `Expected ${options.length} lenses but CalmLens returned ${returned.length}` });
|
|
945
925
|
const { ensuredLenses } = this.#lenses;
|