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,4 +1,4 @@
|
|
|
1
|
-
const require_UploadController = require("./UploadController-
|
|
1
|
+
const require_UploadController = require("./UploadController-B_lKxvpM.js");
|
|
2
2
|
// src/ui/BrowserUpload.ts
|
|
3
3
|
/** Creates an upload queue for the browser components, such as one a form starts on submit. */
|
|
4
4
|
const createUploadController = (options) => require_UploadController.createUploadController(options);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { F as UploadItem$1,
|
|
2
|
-
import {
|
|
1
|
+
import { F as UploadFile, I as UploadItem$1, L as UploadSnapshot$1, N as UploadController, P as UploadControllerOptions$1, h as UploaderProps$1, k as CalmLensUpload$1 } from "./Core-CREGGR0L.js";
|
|
2
|
+
import { r as UseCalmLensUploadResult$1 } from "./ReactUpload-D4htIz8X.js";
|
|
3
3
|
type UploadItem<F extends UploadFile = File> = UploadItem$1<F>;
|
|
4
4
|
type CalmLensUpload<F extends UploadFile = File> = CalmLensUpload$1<F>;
|
|
5
5
|
type UploadSnapshot<F extends UploadFile = File> = UploadSnapshot$1<F>;
|
|
@@ -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
|
// src/client/CalmLensAppClient.ts
|
|
4
4
|
const inspectSymbol = Symbol.for("nodejs.util.inspect.custom");
|
|
5
5
|
/** `options`, once `checkOptionKeys` finds only keys `operation` takes. */
|
|
@@ -13,6 +13,12 @@ const checked = (operation, options) => {
|
|
|
13
13
|
* key. It can upload (`assets.create`, `assets.classify`, `assets.uploadPart`) and check its own
|
|
14
14
|
* uploads (`assets.retrieve`). Every method throws `CalmLensError` on failure.
|
|
15
15
|
*/
|
|
16
|
+
/** An upload's request options for reading it back: no key or body of the upload's own. */
|
|
17
|
+
const readOptions = (requestOptions) => {
|
|
18
|
+
if (!requestOptions) return void 0;
|
|
19
|
+
const { idempotencyKey: _key, extraBody: _body, ...read } = requestOptions;
|
|
20
|
+
return read;
|
|
21
|
+
};
|
|
16
22
|
var CalmLensAppClient = class {
|
|
17
23
|
#uploadPresetId;
|
|
18
24
|
#token;
|
|
@@ -55,26 +61,27 @@ var CalmLensAppClient = class {
|
|
|
55
61
|
return token ? { Authorization: `Bearer ${token}` } : {};
|
|
56
62
|
}
|
|
57
63
|
/** Uploads with a new token, kept for the upload's parts and status checks. */
|
|
58
|
-
async submit(options, wait) {
|
|
64
|
+
async submit(operation, options, wait, requestOptions) {
|
|
65
|
+
const connection = this.#connection.forCall(operation, requestOptions);
|
|
59
66
|
if (!options || typeof options !== "object") throw require_ModerationContextLimits.invalidArgument("Upload options are required");
|
|
60
67
|
if (!("content" in options) && !("part" in options)) throw require_ModerationContextLimits.invalidArgument("Uploads through an upload preset need content or a part");
|
|
61
68
|
require_CalmLensOptionKeys.prepareSubmitAssetOptions(options, {
|
|
62
|
-
deferUploadChecks:
|
|
63
|
-
partSizeBytes:
|
|
69
|
+
deferUploadChecks: connection.deferUploadChecks,
|
|
70
|
+
partSizeBytes: connection.partSizeBytes
|
|
64
71
|
});
|
|
65
72
|
const token = await this.newUploadToken();
|
|
66
|
-
if ("content" in options && require_CalmLensOptionKeys.splitsIntoParts(options.content,
|
|
67
|
-
const data = await
|
|
73
|
+
if ("content" in options && require_CalmLensOptionKeys.splitsIntoParts(options.content, connection.partSizeBytes)) {
|
|
74
|
+
const data = await connection.submitInParts(this.uploadsPath, options, wait, {
|
|
68
75
|
authHeaders: token ? { Authorization: `Bearer ${token}` } : {},
|
|
69
76
|
partPath: (assetId) => `${this.uploadsPath}/${encodeURIComponent(assetId)}/part`,
|
|
70
77
|
onStarted: (assetId) => {
|
|
71
78
|
if (token) this.#uploadTokens.set(assetId, token);
|
|
72
79
|
},
|
|
73
|
-
retrieve: (assetId) => this.assets.retrieve({ assetId })
|
|
80
|
+
retrieve: (assetId) => this.assets.retrieve({ assetId }, readOptions(requestOptions))
|
|
74
81
|
});
|
|
75
82
|
return require_CalmLensOptionKeys.expectUploadedAsset(data);
|
|
76
83
|
}
|
|
77
|
-
const data = await
|
|
84
|
+
const data = await connection.submitUpload(this.uploadsPath, options, wait, token ? { Authorization: `Bearer ${token}` } : {});
|
|
78
85
|
const uploaded = require_CalmLensOptionKeys.expectUploadedAsset(data);
|
|
79
86
|
if (token) this.#uploadTokens.set(uploaded.id, token);
|
|
80
87
|
return uploaded;
|
|
@@ -99,24 +106,24 @@ getAssetUrl: (urlOptions) => {
|
|
|
99
106
|
get assets() {
|
|
100
107
|
return {
|
|
101
108
|
/** Uploads a file and returns without waiting for its decision. */
|
|
102
|
-
create: async (options) => this.submit(checked("assets.create", options), false),
|
|
109
|
+
create: async (options, requestOptions) => this.submit("assets.create", checked("assets.create", options), false, requestOptions),
|
|
103
110
|
/** Uploads a file and waits for its moderation decision. */
|
|
104
|
-
classify: async (options) => this.submit(checked("assets.classify", options), true),
|
|
111
|
+
classify: async (options, requestOptions) => this.submit("assets.classify", checked("assets.classify", options), true, requestOptions),
|
|
105
112
|
/**
|
|
106
113
|
* Uploads the next part of a multipart upload this client started. The upload's wait
|
|
107
114
|
* mode, chosen when it started, applies when the final part arrives.
|
|
108
115
|
*/
|
|
109
|
-
uploadPart: async (partOptions) => {
|
|
116
|
+
uploadPart: async (partOptions, requestOptions) => {
|
|
110
117
|
const { assetId, ...options } = checked("assets.uploadPart", partOptions);
|
|
111
118
|
const encodedAssetId = require_ModerationContextLimits.encodePathSegment(assetId, "Asset ID");
|
|
112
|
-
const data = await this.#connection.uploadPart(`${this.uploadsPath}/${encodedAssetId}/part`, options, this.authHeaders(assetId));
|
|
119
|
+
const data = await this.#connection.forCall("assets.uploadPart", requestOptions).uploadPart(`${this.uploadsPath}/${encodedAssetId}/part`, options, this.authHeaders(assetId));
|
|
113
120
|
return require_CalmLensOptionKeys.expectUploadedAsset(data);
|
|
114
121
|
},
|
|
115
122
|
/** Checks an upload this client made. `status` is present when the preset shows decisions. */
|
|
116
|
-
retrieve: async (options) => {
|
|
123
|
+
retrieve: async (options, requestOptions) => {
|
|
117
124
|
const { assetId } = checked("assets.retrieve", options);
|
|
118
125
|
const encodedAssetId = require_ModerationContextLimits.encodePathSegment(assetId, "Asset ID");
|
|
119
|
-
const data = await this.#connection.json("GET", `${this.uploadsPath}/${encodedAssetId}`, { headers: this.authHeaders(assetId) });
|
|
126
|
+
const data = await this.#connection.forCall("assets.retrieve", requestOptions).json("GET", `${this.uploadsPath}/${encodedAssetId}`, { headers: this.authHeaders(assetId) });
|
|
120
127
|
return require_CalmLensOptionKeys.expectUploadedAsset(data);
|
|
121
128
|
}
|
|
122
129
|
};
|
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { $t as UploadedAsset, Jt as UploadAssetPartOptions, M as CdnAssetUrlOptions, O as CalmLensAppClientOptions, Qt as UploadPresetSubmitAssetOptions, jt as RetrieveAssetOptions, kt as RequestOptions } from "./CalmLensTypes-Df8eBgWB.js";
|
|
2
2
|
declare const inspectSymbol: unique symbol;
|
|
3
|
-
/**
|
|
4
|
-
* The CalmLens client for browsers and React Native apps. It uploads through an upload preset,
|
|
5
|
-
* with an upload token from your server when the preset requires one, and never holds an API
|
|
6
|
-
* key. It can upload (`assets.create`, `assets.classify`, `assets.uploadPart`) and check its own
|
|
7
|
-
* uploads (`assets.retrieve`). Every method throws `CalmLensError` on failure.
|
|
8
|
-
*/
|
|
9
3
|
declare class CalmLensAppClient {
|
|
10
4
|
#private;
|
|
11
5
|
constructor(options: CalmLensAppClientOptions);
|
|
@@ -35,16 +29,16 @@ declare class CalmLensAppClient {
|
|
|
35
29
|
/** Uploads through the client's upload preset and checks those uploads. */
|
|
36
30
|
get assets(): {
|
|
37
31
|
/** Uploads a file and returns without waiting for its decision. */
|
|
38
|
-
create: (options: UploadPresetSubmitAssetOptions) => Promise<UploadedAsset>;
|
|
32
|
+
create: (options: UploadPresetSubmitAssetOptions, requestOptions?: RequestOptions) => Promise<UploadedAsset>;
|
|
39
33
|
/** Uploads a file and waits for its moderation decision. */
|
|
40
|
-
classify: (options: UploadPresetSubmitAssetOptions) => Promise<UploadedAsset>;
|
|
34
|
+
classify: (options: UploadPresetSubmitAssetOptions, requestOptions?: RequestOptions) => Promise<UploadedAsset>;
|
|
41
35
|
/**
|
|
42
36
|
* Uploads the next part of a multipart upload this client started. The upload's wait
|
|
43
37
|
* mode, chosen when it started, applies when the final part arrives.
|
|
44
38
|
*/
|
|
45
|
-
uploadPart: (partOptions: UploadAssetPartOptions) => Promise<UploadedAsset>;
|
|
39
|
+
uploadPart: (partOptions: UploadAssetPartOptions, requestOptions?: RequestOptions) => Promise<UploadedAsset>;
|
|
46
40
|
/** Checks an upload this client made. `status` is present when the preset shows decisions. */
|
|
47
|
-
retrieve: (options: RetrieveAssetOptions) => Promise<UploadedAsset>;
|
|
41
|
+
retrieve: (options: RetrieveAssetOptions, requestOptions?: RequestOptions) => Promise<UploadedAsset>;
|
|
48
42
|
};
|
|
49
43
|
}
|
|
50
44
|
export { CalmLensAppClient as t };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const require_ModerationContextLimits = require("./ModerationContextLimits-
|
|
1
|
+
const require_ModerationContextLimits = require("./ModerationContextLimits-D4iwqoN_.js");
|
|
2
2
|
// src/contract/AssetPayload.js
|
|
3
3
|
const bufferInputToBytes = (value) => {
|
|
4
4
|
if (value instanceof ArrayBuffer) return new Uint8Array(value);
|
|
@@ -153,23 +153,17 @@ const textEncoder = new TextEncoder();
|
|
|
153
153
|
/**
|
|
154
154
|
* An upload form: `multipart/form-data` with the asset's fields as the JSON `options` part, then
|
|
155
155
|
* the bytes as the `file` part. Asset fields never go in the URL, which proxies and servers log.
|
|
156
|
-
* Bytes stay bytes (React Native can't build a Blob from them)
|
|
157
|
-
*
|
|
156
|
+
* Bytes stay bytes (React Native can't build a Blob from them). Read Blob bytes before composing
|
|
157
|
+
* the form: Bun's Blob constructor drops disk-backed BunFile parts. Automatic multipart uploads
|
|
158
|
+
* read only the current part. Streams remain streamed, and buffered bodies remain retryable.
|
|
158
159
|
*/
|
|
159
|
-
const toUploadForm = (options, payload) => {
|
|
160
|
+
const toUploadForm = async (options, payload) => {
|
|
160
161
|
const boundary = `calmlens-${require_ModerationContextLimits.randomUuid()}`;
|
|
161
162
|
const head = `--${boundary}\r\nContent-Disposition: form-data; name="options"\r\nContent-Type: application/json\r\n\r\n${JSON.stringify(options)}\r\n--${boundary}\r\nContent-Disposition: form-data; name="file"\r\nContent-Type: ${payload.type.replace(/[\r\n]/g, "")}\r\n\r\n`;
|
|
162
163
|
const tail = `\r\n--${boundary}--\r\n`;
|
|
163
164
|
const contentType = `multipart/form-data; boundary=${boundary}`;
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
body: new Blob([
|
|
167
|
-
head,
|
|
168
|
-
data,
|
|
169
|
-
tail
|
|
170
|
-
]),
|
|
171
|
-
contentType
|
|
172
|
-
};
|
|
165
|
+
const input = toUploadData(payload);
|
|
166
|
+
const data = input instanceof Blob ? new Uint8Array(await input.arrayBuffer()) : input;
|
|
173
167
|
const headBytes = textEncoder.encode(head);
|
|
174
168
|
const tailBytes = textEncoder.encode(tail);
|
|
175
169
|
if (data instanceof Uint8Array) {
|
|
@@ -322,7 +316,11 @@ const newIdempotencyKey = () => require_ModerationContextLimits.randomUuid();
|
|
|
322
316
|
/** Text content is small, so it keeps the API deadline; binary files may be arbitrarily large. */
|
|
323
317
|
const isTextPayload = (payload) => "plainText" in payload;
|
|
324
318
|
const hasStringStatus = (value) => !!value && typeof value === "object" && typeof value.status === "string";
|
|
325
|
-
const
|
|
319
|
+
const hasDecision = (data) => {
|
|
320
|
+
const decision = data.classification?.moderation?.decision;
|
|
321
|
+
return decision === null || typeof decision === "string";
|
|
322
|
+
};
|
|
323
|
+
const isAsset = (data) => !!data && typeof data === "object" && typeof data.id === "string" && !!data.id && hasStringStatus(data.classification) && hasStringStatus(data.upload) && hasDecision(data);
|
|
326
324
|
const isUploadedAsset = (data) => !!data && typeof data === "object" && typeof data.id === "string" && !!data.id && hasStringStatus(data.classification) && hasStringStatus(data.upload);
|
|
327
325
|
const expectUploadedAsset = (data) => {
|
|
328
326
|
if (!isUploadedAsset(data)) throw new require_ModerationContextLimits.CalmLensError(require_ModerationContextLimits.SDK_ERROR_CODES.INVALID_RESPONSE, {
|
|
@@ -338,30 +336,114 @@ const expectAsset = (data) => {
|
|
|
338
336
|
});
|
|
339
337
|
return data;
|
|
340
338
|
};
|
|
339
|
+
/** Checks one call's request options. */
|
|
340
|
+
const validateCallOptions = (options) => {
|
|
341
|
+
if (options === void 0) return;
|
|
342
|
+
if (!options || typeof options !== "object" || Array.isArray(options)) throw require_ModerationContextLimits.invalidArgument("Request options must be an object");
|
|
343
|
+
const known = [
|
|
344
|
+
"signal",
|
|
345
|
+
"timeoutMs",
|
|
346
|
+
"maxRetries",
|
|
347
|
+
"idempotencyKey",
|
|
348
|
+
"headers",
|
|
349
|
+
"extraQuery",
|
|
350
|
+
"extraBody"
|
|
351
|
+
];
|
|
352
|
+
const unknown = Object.keys(options).filter((key) => !known.includes(key));
|
|
353
|
+
if (unknown.length) throw require_ModerationContextLimits.invalidArgument(`Unknown request options: ${unknown.join(", ")}`, { options: unknown });
|
|
354
|
+
const { timeoutMs, maxRetries, idempotencyKey, headers, extraQuery, extraBody } = options;
|
|
355
|
+
if (timeoutMs !== void 0 && (!Number.isSafeInteger(timeoutMs) || timeoutMs < 1)) throw require_ModerationContextLimits.invalidArgument("timeoutMs must be a positive integer");
|
|
356
|
+
if (maxRetries !== void 0 && (!Number.isSafeInteger(maxRetries) || maxRetries < 0)) throw require_ModerationContextLimits.invalidArgument("maxRetries must be a non-negative integer");
|
|
357
|
+
if (idempotencyKey !== void 0 && (typeof idempotencyKey !== "string" || !idempotencyKey)) throw require_ModerationContextLimits.invalidArgument("idempotencyKey must be a non-empty string");
|
|
358
|
+
for (const [name, value] of Object.entries({
|
|
359
|
+
headers,
|
|
360
|
+
extraQuery,
|
|
361
|
+
extraBody
|
|
362
|
+
})) if (value !== void 0 && (!value || typeof value !== "object" || Array.isArray(value))) throw require_ModerationContextLimits.invalidArgument(`${name} must be an object`);
|
|
363
|
+
if (options.signal !== void 0 && !(options.signal instanceof AbortSignal)) throw require_ModerationContextLimits.invalidArgument("signal must be an AbortSignal");
|
|
364
|
+
};
|
|
365
|
+
const isPlainObject = (value) => !!value && typeof value === "object" && !Array.isArray(value);
|
|
341
366
|
/** Fills in the default API base URL and drops trailing slashes. */
|
|
342
367
|
const resolveConnectionOptions = (options) => ({
|
|
343
368
|
...options,
|
|
344
369
|
baseUrl: (options.baseUrl ?? `https://api.calmlens.com/v1`).replace(/\/+$/, "")
|
|
345
370
|
});
|
|
346
371
|
/** One client's transport, base URL and deadlines, and the requests both clients make. */
|
|
347
|
-
var CalmLensConnection = class {
|
|
348
|
-
constructor(options, apiKey) {
|
|
372
|
+
var CalmLensConnection = class CalmLensConnection {
|
|
373
|
+
constructor(options, apiKey, { readEnvironment = false, shared } = {}) {
|
|
349
374
|
this.options = options;
|
|
375
|
+
if (shared) {
|
|
376
|
+
this.transport = shared.transport;
|
|
377
|
+
this.hooks = shared.hooks;
|
|
378
|
+
this.call = shared.call;
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
this.call = {};
|
|
350
382
|
const resolveFetch = () => {
|
|
351
383
|
const fetchImpl = this.options.fetch ?? globalThis.fetch;
|
|
352
384
|
if (typeof fetchImpl !== "function") throw new require_ModerationContextLimits.CalmLensError(require_ModerationContextLimits.SDK_ERROR_CODES.NO_FETCH, { message: "No fetch implementation is available; pass one as the fetch option" });
|
|
353
385
|
return fetchImpl;
|
|
354
386
|
};
|
|
355
387
|
this.hooks = getMockHooks(options);
|
|
388
|
+
const logLevel = require_ModerationContextLimits.resolveLogLevel(options.logLevel, readEnvironment);
|
|
356
389
|
this.transport = new require_ModerationContextLimits.CalmLensTransport({
|
|
357
390
|
apiKey,
|
|
358
391
|
sleep: this.hooks?.sleep,
|
|
359
392
|
fetch: ((input, init) => resolveFetch()(input, init)),
|
|
360
393
|
signal: options.signal,
|
|
361
394
|
maxRetries: options.maxRetries ?? 2,
|
|
362
|
-
maxResponseBytes: options.maxResponseBytes ?? 33554432
|
|
395
|
+
maxResponseBytes: options.maxResponseBytes ?? 33554432,
|
|
396
|
+
appInfo: options.appInfo,
|
|
397
|
+
logLevel,
|
|
398
|
+
logger: logLevel === "off" ? void 0 : options.logger ?? console,
|
|
399
|
+
onAttempt: options.onAttempt,
|
|
400
|
+
now: this.hooks?.now
|
|
363
401
|
});
|
|
364
402
|
}
|
|
403
|
+
/**
|
|
404
|
+
* This connection for one call: its requests carry `operation` to the attempt hook and logs, and
|
|
405
|
+
* follow `requestOptions` (checked here) over the client's.
|
|
406
|
+
*/
|
|
407
|
+
forCall(operation, requestOptions) {
|
|
408
|
+
validateCallOptions(requestOptions);
|
|
409
|
+
return new CalmLensConnection(this.options, void 0, { shared: {
|
|
410
|
+
transport: this.transport,
|
|
411
|
+
hooks: this.hooks,
|
|
412
|
+
call: {
|
|
413
|
+
...this.call,
|
|
414
|
+
...requestOptions,
|
|
415
|
+
operation
|
|
416
|
+
}
|
|
417
|
+
} });
|
|
418
|
+
}
|
|
419
|
+
/** The call's transport settings, for a request built here or by the client. */
|
|
420
|
+
callRequest() {
|
|
421
|
+
const { operation, signal, maxRetries } = this.call;
|
|
422
|
+
return {
|
|
423
|
+
operation,
|
|
424
|
+
signal,
|
|
425
|
+
maxRetries
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
/** The call's extra headers under the SDK's own, which win. */
|
|
429
|
+
callHeaders(headers) {
|
|
430
|
+
return this.call.headers ? {
|
|
431
|
+
...this.call.headers,
|
|
432
|
+
...headers
|
|
433
|
+
} : headers;
|
|
434
|
+
}
|
|
435
|
+
/** The call's `extraBody` merged into a JSON body. */
|
|
436
|
+
callBody(method, body) {
|
|
437
|
+
const { extraBody } = this.call;
|
|
438
|
+
if (!extraBody) return body;
|
|
439
|
+
if (method === "GET" || method === "HEAD") throw require_ModerationContextLimits.invalidArgument("extraBody needs a request with a body");
|
|
440
|
+
if (body === void 0) return extraBody;
|
|
441
|
+
if (!isPlainObject(body)) throw require_ModerationContextLimits.invalidArgument("extraBody needs a JSON object body");
|
|
442
|
+
return {
|
|
443
|
+
...body,
|
|
444
|
+
...extraBody
|
|
445
|
+
};
|
|
446
|
+
}
|
|
365
447
|
/** How the client waits between polls and retries. */
|
|
366
448
|
get sleep() {
|
|
367
449
|
return this.hooks?.sleep ?? require_ModerationContextLimits.sleep;
|
|
@@ -371,35 +453,40 @@ var CalmLensConnection = class {
|
|
|
371
453
|
return this.hooks?.deferUploadChecks ?? false;
|
|
372
454
|
}
|
|
373
455
|
get timeoutMs() {
|
|
374
|
-
return this.options.timeoutMs ?? 12e4;
|
|
456
|
+
return this.call.timeoutMs ?? this.options.timeoutMs ?? 12e4;
|
|
375
457
|
}
|
|
458
|
+
/** The URL of `path` (which may carry a query already) with `query` and the call's `extraQuery`. */
|
|
376
459
|
url(path, query) {
|
|
377
|
-
const search = query ? require_ModerationContextLimits.stringifyQuery(
|
|
378
|
-
|
|
460
|
+
const search = [query, this.call.extraQuery].map((values) => values ? require_ModerationContextLimits.stringifyQuery(values) : "").filter(Boolean).join("&");
|
|
461
|
+
const separator = path.includes("?") ? "&" : "?";
|
|
462
|
+
return `${this.options.baseUrl}${path}${search ? `${separator}${search}` : ""}`;
|
|
379
463
|
}
|
|
380
464
|
/** A JSON request whose deadline covers reading the response. */
|
|
381
465
|
json(method, path, { query, body, headers, idempotencyKey } = {}) {
|
|
466
|
+
const sent = this.callBody(method, body);
|
|
382
467
|
return this.transport.json({
|
|
468
|
+
...this.callRequest(),
|
|
383
469
|
method,
|
|
384
470
|
url: this.url(path, query),
|
|
385
|
-
headers:
|
|
471
|
+
headers: this.callHeaders(sent === void 0 ? headers : {
|
|
386
472
|
...headers,
|
|
387
473
|
"Content-Type": "application/json"
|
|
388
|
-
},
|
|
389
|
-
body:
|
|
390
|
-
idempotencyKey,
|
|
474
|
+
}),
|
|
475
|
+
body: sent === void 0 ? void 0 : JSON.stringify(sent),
|
|
476
|
+
idempotencyKey: idempotencyKey ?? this.call.idempotencyKey,
|
|
391
477
|
timeoutMs: this.timeoutMs,
|
|
392
478
|
deadline: "body"
|
|
393
479
|
});
|
|
394
480
|
}
|
|
395
481
|
upload(url, body, headers, idempotencyKey, text) {
|
|
396
482
|
return this.transport.json({
|
|
483
|
+
...this.callRequest(),
|
|
397
484
|
method: "POST",
|
|
398
485
|
url,
|
|
399
|
-
headers,
|
|
486
|
+
headers: this.callHeaders(headers),
|
|
400
487
|
body,
|
|
401
488
|
idempotencyKey,
|
|
402
|
-
timeoutMs: text ? this.timeoutMs : this.options.uploadTimeoutMs,
|
|
489
|
+
timeoutMs: text ? this.timeoutMs : this.call.timeoutMs ?? this.options.uploadTimeoutMs,
|
|
403
490
|
deadline: "body",
|
|
404
491
|
fetch: this.options.uploadFetch
|
|
405
492
|
});
|
|
@@ -409,12 +496,15 @@ var CalmLensConnection = class {
|
|
|
409
496
|
* goes as a JSON body; other content as an upload form, the asset's fields in its options part
|
|
410
497
|
* and the bytes in its file part. Either way the query holds only transport values.
|
|
411
498
|
*/
|
|
412
|
-
submitUpload(uploadsPath, options, wait, authHeaders = {}) {
|
|
499
|
+
async submitUpload(uploadsPath, options, wait, authHeaders = {}) {
|
|
413
500
|
const isMultipart = "part" in options;
|
|
414
501
|
const payload = isMultipart ? options.part : options.content;
|
|
415
502
|
const multipartOptions = options;
|
|
416
|
-
const idempotencyKey = options.idempotencyKey ?? newIdempotencyKey();
|
|
417
|
-
const fields =
|
|
503
|
+
const idempotencyKey = options.idempotencyKey ?? this.call.idempotencyKey ?? newIdempotencyKey();
|
|
504
|
+
const fields = {
|
|
505
|
+
...uploadFields(options),
|
|
506
|
+
...this.call.extraBody
|
|
507
|
+
};
|
|
418
508
|
if (isTextPayload(payload)) {
|
|
419
509
|
const { sizeInBytes: _size, ...text } = payload;
|
|
420
510
|
const body = isMultipart ? {
|
|
@@ -442,7 +532,7 @@ var CalmLensConnection = class {
|
|
|
442
532
|
setOptionalQueryValue(query, "sizeInBytes", part.sizeInBytes ?? multipartOptions.sizeInBytes);
|
|
443
533
|
setOptionalQueryValue(query, "totalSizeInBytes", multipartOptions.totalSizeInBytes);
|
|
444
534
|
}
|
|
445
|
-
const form = toUploadForm(fields, payload);
|
|
535
|
+
const form = await toUploadForm(fields, payload);
|
|
446
536
|
return this.upload(this.url(`${uploadsPath}?${query}`), form.body, {
|
|
447
537
|
...authHeaders,
|
|
448
538
|
"Content-Type": form.contentType,
|
|
@@ -465,7 +555,7 @@ var CalmLensConnection = class {
|
|
|
465
555
|
const source = getSplittableContent(content);
|
|
466
556
|
const partSize = this.partSizeBytes;
|
|
467
557
|
const count = Math.ceil(source.size / partSize);
|
|
468
|
-
const key = idempotencyKey ?? newIdempotencyKey();
|
|
558
|
+
const key = idempotencyKey ?? this.call.idempotencyKey ?? newIdempotencyKey();
|
|
469
559
|
const part = (partNumber) => source.slice((partNumber - 1) * partSize, Math.min(partNumber * partSize, source.size));
|
|
470
560
|
const first = await this.submitUpload(uploadsPath, {
|
|
471
561
|
...fields,
|
|
@@ -480,11 +570,19 @@ var CalmLensConnection = class {
|
|
|
480
570
|
}, wait, authHeaders);
|
|
481
571
|
const assetId = expectUploadedAsset(first).id;
|
|
482
572
|
onStarted?.(assetId);
|
|
573
|
+
const laterParts = new CalmLensConnection(this.options, void 0, { shared: {
|
|
574
|
+
transport: this.transport,
|
|
575
|
+
hooks: this.hooks,
|
|
576
|
+
call: {
|
|
577
|
+
...this.call,
|
|
578
|
+
extraBody: void 0
|
|
579
|
+
}
|
|
580
|
+
} });
|
|
483
581
|
let result = first;
|
|
484
582
|
for (let partNumber = 2; partNumber <= count;) {
|
|
485
583
|
const isFinal = partNumber === count;
|
|
486
584
|
try {
|
|
487
|
-
result = await
|
|
585
|
+
result = await laterParts.uploadPart(partPath(assetId), {
|
|
488
586
|
part: {
|
|
489
587
|
...part(partNumber),
|
|
490
588
|
partNumber,
|
|
@@ -509,6 +607,7 @@ var CalmLensConnection = class {
|
|
|
509
607
|
/** Uploads the next part of a multipart upload to `partPath` (`.../assets/{id}/part`). */
|
|
510
608
|
uploadPart(partPath, { part, wait, idempotencyKey }, authHeaders = {}) {
|
|
511
609
|
if (!part) throw require_ModerationContextLimits.invalidArgument("Part options are required");
|
|
610
|
+
if (this.call.extraBody) throw require_ModerationContextLimits.invalidArgument("A part's body is its bytes: extraBody can't apply");
|
|
512
611
|
assertDeclaredPayloadSize(part);
|
|
513
612
|
const validation = validateAssetPayload(part, {
|
|
514
613
|
multipart: true,
|
|
@@ -524,7 +623,7 @@ var CalmLensConnection = class {
|
|
|
524
623
|
return this.upload(this.url(`${partPath}?${query}`), toRawUploadBody(part), {
|
|
525
624
|
...authHeaders,
|
|
526
625
|
"X-CalmLens-Upload": "part"
|
|
527
|
-
}, idempotencyKey ?? newIdempotencyKey(), isTextPayload(part));
|
|
626
|
+
}, idempotencyKey ?? this.call.idempotencyKey ?? newIdempotencyKey(), isTextPayload(part));
|
|
528
627
|
}
|
|
529
628
|
};
|
|
530
629
|
// src/client/CalmLensOptionKeys.ts
|
|
@@ -579,23 +678,6 @@ const SERVER_OPTION_KEYS = {
|
|
|
579
678
|
],
|
|
580
679
|
"assets.delete": ["assetId"],
|
|
581
680
|
"assets.explain": ["assetId", "rerun"],
|
|
582
|
-
"assets.iterate": [
|
|
583
|
-
"cursor",
|
|
584
|
-
"filter",
|
|
585
|
-
"pageSize",
|
|
586
|
-
"sort"
|
|
587
|
-
],
|
|
588
|
-
"assets.iterateChildren": [
|
|
589
|
-
"assetId",
|
|
590
|
-
"cursor",
|
|
591
|
-
"pageSize",
|
|
592
|
-
"sort"
|
|
593
|
-
],
|
|
594
|
-
"assets.iterateTimeline": [
|
|
595
|
-
"assetId",
|
|
596
|
-
"cursor",
|
|
597
|
-
"pageSize"
|
|
598
|
-
],
|
|
599
681
|
"assets.list": [
|
|
600
682
|
"cursor",
|
|
601
683
|
"filter",
|
|
@@ -663,21 +745,8 @@ const SERVER_OPTION_KEYS = {
|
|
|
663
745
|
"pageSize",
|
|
664
746
|
"types"
|
|
665
747
|
],
|
|
666
|
-
"events.pages": [
|
|
667
|
-
"assetId",
|
|
668
|
-
"cursor",
|
|
669
|
-
"pageSize",
|
|
670
|
-
"types"
|
|
671
|
-
],
|
|
672
748
|
"folders.create": ["name", "parentId"],
|
|
673
749
|
"folders.delete": ["folderId"],
|
|
674
|
-
"folders.iterate": [
|
|
675
|
-
"cursor",
|
|
676
|
-
"pageSize",
|
|
677
|
-
"parentId",
|
|
678
|
-
"search",
|
|
679
|
-
"sort"
|
|
680
|
-
],
|
|
681
750
|
"folders.list": [
|
|
682
751
|
"cursor",
|
|
683
752
|
"pageSize",
|
|
@@ -701,17 +770,6 @@ const SERVER_OPTION_KEYS = {
|
|
|
701
770
|
"name"
|
|
702
771
|
],
|
|
703
772
|
"hashLists.delete": ["hashListId"],
|
|
704
|
-
"hashLists.iterate": [
|
|
705
|
-
"cursor",
|
|
706
|
-
"pageSize",
|
|
707
|
-
"sort"
|
|
708
|
-
],
|
|
709
|
-
"hashLists.iterateEntries": [
|
|
710
|
-
"cursor",
|
|
711
|
-
"hashListId",
|
|
712
|
-
"pageSize",
|
|
713
|
-
"sort"
|
|
714
|
-
],
|
|
715
773
|
"hashLists.list": [
|
|
716
774
|
"cursor",
|
|
717
775
|
"pageSize",
|
|
@@ -740,11 +798,6 @@ const SERVER_OPTION_KEYS = {
|
|
|
740
798
|
"type"
|
|
741
799
|
],
|
|
742
800
|
"lenses.delete": ["lensId"],
|
|
743
|
-
"lenses.iterate": [
|
|
744
|
-
"cursor",
|
|
745
|
-
"pageSize",
|
|
746
|
-
"sort"
|
|
747
|
-
],
|
|
748
801
|
"lenses.list": [
|
|
749
802
|
"cursor",
|
|
750
803
|
"pageSize",
|
|
@@ -770,11 +823,6 @@ const SERVER_OPTION_KEYS = {
|
|
|
770
823
|
"settings"
|
|
771
824
|
],
|
|
772
825
|
"policies.delete": ["policyId"],
|
|
773
|
-
"policies.iterate": [
|
|
774
|
-
"cursor",
|
|
775
|
-
"pageSize",
|
|
776
|
-
"sort"
|
|
777
|
-
],
|
|
778
826
|
"policies.list": [
|
|
779
827
|
"cursor",
|
|
780
828
|
"pageSize",
|
|
@@ -828,11 +876,6 @@ const SERVER_OPTION_KEYS = {
|
|
|
828
876
|
],
|
|
829
877
|
"uploadPresets.delete": ["uploadPresetId"],
|
|
830
878
|
"uploadPresets.ensure": ["uploadPresets"],
|
|
831
|
-
"uploadPresets.iterate": [
|
|
832
|
-
"cursor",
|
|
833
|
-
"pageSize",
|
|
834
|
-
"sort"
|
|
835
|
-
],
|
|
836
879
|
"uploadPresets.list": [
|
|
837
880
|
"cursor",
|
|
838
881
|
"pageSize",
|
|
@@ -895,7 +938,6 @@ const NULLABLE_OPTION_KEYS = {
|
|
|
895
938
|
"uploadPresetId",
|
|
896
939
|
"visibility"
|
|
897
940
|
],
|
|
898
|
-
"assets.iterate": ["filter"],
|
|
899
941
|
"assets.list": ["filter"],
|
|
900
942
|
"assets.move": ["folderId"],
|
|
901
943
|
"assets.update": [
|
|
@@ -1022,3 +1064,9 @@ Object.defineProperty(exports, "validateAssetPayload", {
|
|
|
1022
1064
|
return validateAssetPayload;
|
|
1023
1065
|
}
|
|
1024
1066
|
});
|
|
1067
|
+
Object.defineProperty(exports, "validateCallOptions", {
|
|
1068
|
+
enumerable: true,
|
|
1069
|
+
get: function() {
|
|
1070
|
+
return validateCallOptions;
|
|
1071
|
+
}
|
|
1072
|
+
});
|