@calmlens/sdk 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -1
- package/cjs/CalmLensConnection-DzRDCMOP.js +233 -0
- package/cjs/CalmLensUtils-vX5H_vMn.js +699 -0
- package/cjs/{CalmLensError-CKwMKV0S.js → CalmLensVersion-DjhsJ1gj.js} +48 -0
- package/cjs/{CalmLensWebhooks-BTPxKvcD.js → CalmLensWebhooks-hVquX5L1.js} +9 -9
- package/cjs/{WebhookPayload-Cgb32aSH.d.ts → WebhookPayload-B7CgSmI5.d.ts} +4996 -3255
- package/cjs/app.d.ts +3 -3
- package/cjs/app.js +10 -10
- package/cjs/{index-BgmWj3KL.d.ts → index-BywTQbRq.d.ts} +3 -1
- package/cjs/index.d.ts +3 -3
- package/cjs/index.js +4 -5
- package/cjs/server.d.ts +12 -4
- package/cjs/server.js +31 -18
- package/cjs/testing.d.ts +6 -1
- package/cjs/testing.js +91 -73
- package/esm/CalmLensConnection-D8VsTdsA.js +192 -0
- package/esm/{CalmLensUtils-Cx32j2zt.js → CalmLensUtils-B4i3Z2He.js} +316 -2
- package/esm/{CalmLensError-CihGG26K.js → CalmLensVersion-DK4BMEST.js} +25 -1
- package/esm/{CalmLensWebhooks-DShmAal4.js → CalmLensWebhooks-DyCoEvpf.js} +2 -2
- package/esm/{WebhookPayload-Cgb32aSH.d.ts → WebhookPayload-B7CgSmI5.d.ts} +4996 -3255
- package/esm/app.d.ts +3 -3
- package/esm/app.js +4 -4
- package/esm/{index-BgmWj3KL.d.ts → index-BywTQbRq.d.ts} +3 -1
- package/esm/index.d.ts +3 -3
- package/esm/index.js +1 -3
- package/esm/server.d.ts +12 -4
- package/esm/server.js +20 -7
- package/esm/testing.d.ts +6 -1
- package/esm/testing.js +59 -41
- package/package.json +1 -1
- package/cjs/CalmLensConnection-CYtBP-vu.js +0 -516
- package/cjs/CalmLensUtils-Dg3s4loH.js +0 -367
- package/cjs/src-Dsc8Pn6v.js +0 -45
- package/esm/CalmLensConnection-wLNy0OIP.js +0 -475
- package/esm/src-CJhrILvM.js +0 -22
package/README.md
CHANGED
|
@@ -197,7 +197,7 @@ const image = await client.cdn.serve({
|
|
|
197
197
|
});
|
|
198
198
|
```
|
|
199
199
|
|
|
200
|
-
For several lenses, use `ensureLenses()` to
|
|
200
|
+
For several lenses, use `ensureLenses()` to ensure them together: new definitions go in one request, or in consecutive requests of up to 100 each, in order. Each lens keeps the same independent versioning rules.
|
|
201
201
|
|
|
202
202
|
```typescript
|
|
203
203
|
const [normal, uhd] = await client.ensureLenses({ lenses: [normalLens, uhdLens] });
|
|
@@ -303,6 +303,19 @@ const asset = await client.assets.retry({ assetId: "asset-uuid" });
|
|
|
303
303
|
// asset.status === "pending"
|
|
304
304
|
```
|
|
305
305
|
|
|
306
|
+
##### assets.waitForDecision(options)
|
|
307
|
+
|
|
308
|
+
Wait for an asset's moderation decision and get the decided asset: `approved`, `rejected` or `error`.
|
|
309
|
+
The SDK checks the asset every few seconds and rejects with `sdk/timeout` if there's no decision within
|
|
310
|
+
`timeoutMs` (default 60000). Pass `signal` to stop waiting early (`sdk/cancelled`). Use it after
|
|
311
|
+
`assets.create`, or with an asset ID you stored earlier. Webhooks report decisions without polling.
|
|
312
|
+
|
|
313
|
+
```typescript
|
|
314
|
+
const queued = await client.assets.create({ name: "photo.jpg", content: { blob, type: "image/jpeg" } });
|
|
315
|
+
const decided = await client.assets.waitForDecision({ assetId: queued.id, timeoutMs: 30_000 });
|
|
316
|
+
if (decided.status === "rejected") hidePhoto(decided.id);
|
|
317
|
+
```
|
|
318
|
+
|
|
306
319
|
##### folders.list()
|
|
307
320
|
|
|
308
321
|
List all folders in the current project.
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
const require_CalmLensVersion = require("./CalmLensVersion-DjhsJ1gj.js");
|
|
2
|
+
const require_CalmLensUtils = require("./CalmLensUtils-vX5H_vMn.js");
|
|
3
|
+
//#region src/client/CalmLensConnection.ts
|
|
4
|
+
const DEFAULT_TIMEOUT_MS = 12e4;
|
|
5
|
+
const DEFAULT_MAX_RETRIES = 2;
|
|
6
|
+
const DEFAULT_MAX_RESPONSE_BYTES = 33554432;
|
|
7
|
+
/**
|
|
8
|
+
* Bytes as a fetch body. They're sent as they are rather than in a Blob: React Native can't
|
|
9
|
+
* build a Blob from bytes, and every fetch accepts a Uint8Array.
|
|
10
|
+
*/
|
|
11
|
+
const toBodyBytes = (value) => value.buffer instanceof ArrayBuffer ? value : Uint8Array.from(value);
|
|
12
|
+
const toRawUploadBody = (payload) => {
|
|
13
|
+
const data = require_CalmLensUtils.assetToData(payload);
|
|
14
|
+
if (data instanceof Response) {
|
|
15
|
+
if (!data.body) throw new require_CalmLensVersion.CalmLensError(require_CalmLensVersion.API_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Response upload data must have a body" });
|
|
16
|
+
return data.body;
|
|
17
|
+
}
|
|
18
|
+
if (data instanceof ArrayBuffer || data instanceof Blob) return data;
|
|
19
|
+
if ("getReader" in data) return data;
|
|
20
|
+
return toBodyBytes(data);
|
|
21
|
+
};
|
|
22
|
+
const setOptionalQueryValue = (params, key, value) => {
|
|
23
|
+
if (value !== void 0 && value !== null) params.set(key, typeof value === "string" ? value : JSON.stringify(value));
|
|
24
|
+
};
|
|
25
|
+
const assertValidUploadMimeType = (mimeType) => {
|
|
26
|
+
if (require_CalmLensUtils.isRejectedUploadMimeType(mimeType)) throw new require_CalmLensVersion.CalmLensError(require_CalmLensVersion.API_ERROR_CODES.INVALID_UPLOAD_MIME_TYPE, {
|
|
27
|
+
message: require_CalmLensUtils.getRejectedUploadMimeTypeError(mimeType),
|
|
28
|
+
details: { mimeType }
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
const isImageMimeType = (mimeType) => mimeType.toLowerCase().startsWith("image/");
|
|
32
|
+
const invalidPayload = (message) => new require_CalmLensVersion.CalmLensError(require_CalmLensVersion.API_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message });
|
|
33
|
+
/** Checks upload options before anything is sent. */
|
|
34
|
+
const prepareSubmitAssetOptions = (options) => {
|
|
35
|
+
if (!options || typeof options !== "object") throw require_CalmLensVersion.invalidArgument("Asset options are required");
|
|
36
|
+
if (!("content" in options) && !("url" in options) && !("part" in options)) throw invalidPayload("Either file content, URL, or multipart part is required");
|
|
37
|
+
if (!options.name) throw invalidPayload("Asset name is required");
|
|
38
|
+
if (!("part" in options) && "isFinal" in options) throw invalidPayload("isFinal is only valid for multipart uploads");
|
|
39
|
+
if ("part" in options && options.part.partNumber && options.part.partNumber > 1) throw new require_CalmLensVersion.CalmLensError(require_CalmLensVersion.API_ERROR_CODES.USE_UPLOAD_PART, { message: "Use uploadPart for multipart upload parts after the first part" });
|
|
40
|
+
if ("content" in options) {
|
|
41
|
+
assertValidUploadMimeType(options.content.type);
|
|
42
|
+
if (!("plainText" in options.content)) {
|
|
43
|
+
const validation = require_CalmLensUtils.validateAssetPayload(options.content);
|
|
44
|
+
if (!validation.ok) throw invalidPayload(validation.error);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if ("part" in options) {
|
|
48
|
+
assertValidUploadMimeType(options.part.type);
|
|
49
|
+
const partSizeInBytes = options.part.sizeInBytes ?? require_CalmLensUtils.getAssetPayloadByteLength(options.part);
|
|
50
|
+
if (isImageMimeType(options.part.type) && partSizeInBytes !== void 0) {
|
|
51
|
+
if (partSizeInBytes > require_CalmLensUtils.MAX_IMAGE_UPLOAD_BYTES) throw new require_CalmLensVersion.CalmLensError(require_CalmLensVersion.API_ERROR_CODES.MULTIPART_IMAGE_TOO_LARGE, { message: "Multipart image uploads cannot exceed 20MB." });
|
|
52
|
+
if (options.part.isFinal && partSizeInBytes < require_CalmLensUtils.MIN_MULTIPART_IMAGE_UPLOAD_BYTES) throw new require_CalmLensVersion.CalmLensError(require_CalmLensVersion.API_ERROR_CODES.MULTIPART_IMAGE_TOO_SMALL, { message: "Multipart image uploads must total at least 10MB." });
|
|
53
|
+
}
|
|
54
|
+
const validation = require_CalmLensUtils.validateAssetPayload(options.part, {
|
|
55
|
+
multipart: true,
|
|
56
|
+
isFinal: options.part.isFinal
|
|
57
|
+
});
|
|
58
|
+
if (!validation.ok) throw invalidPayload(validation.error);
|
|
59
|
+
}
|
|
60
|
+
return options;
|
|
61
|
+
};
|
|
62
|
+
const newIdempotencyKey = () => require_CalmLensUtils.randomUuid();
|
|
63
|
+
/** Text content is small, so it keeps the API deadline; binary files may be arbitrarily large. */
|
|
64
|
+
const isTextPayload = (payload) => "plainText" in payload;
|
|
65
|
+
const isAsset = (data) => !!data && typeof data === "object" && typeof data.id === "string" && !!data.id && typeof data.status === "string";
|
|
66
|
+
const isUploadedAsset = (data) => !!data && typeof data === "object" && typeof data.id === "string" && !!data.id;
|
|
67
|
+
const expectUploadedAsset = (data) => {
|
|
68
|
+
if (!isUploadedAsset(data)) throw new require_CalmLensVersion.CalmLensError(require_CalmLensVersion.SDK_ERROR_CODES.INVALID_RESPONSE, {
|
|
69
|
+
message: "Expected an uploaded asset response",
|
|
70
|
+
status: 200
|
|
71
|
+
});
|
|
72
|
+
return data;
|
|
73
|
+
};
|
|
74
|
+
const expectAsset = (data) => {
|
|
75
|
+
if (!isAsset(data)) throw new require_CalmLensVersion.CalmLensError(require_CalmLensVersion.SDK_ERROR_CODES.INVALID_RESPONSE, {
|
|
76
|
+
message: "Expected an asset response",
|
|
77
|
+
status: 200
|
|
78
|
+
});
|
|
79
|
+
return data;
|
|
80
|
+
};
|
|
81
|
+
/** Fills in the default API base URL and drops trailing slashes. */
|
|
82
|
+
const resolveConnectionOptions = (options) => ({
|
|
83
|
+
...options,
|
|
84
|
+
baseUrl: (options.baseUrl ?? `https://api.calmlens.com/v1`).replace(/\/+$/, "")
|
|
85
|
+
});
|
|
86
|
+
/** One client's transport, base URL and deadlines, and the requests both clients make. */
|
|
87
|
+
var CalmLensConnection = class {
|
|
88
|
+
constructor(options, apiKey) {
|
|
89
|
+
this.options = options;
|
|
90
|
+
const resolveFetch = () => {
|
|
91
|
+
const fetchImpl = this.options.fetch ?? globalThis.fetch;
|
|
92
|
+
if (typeof fetchImpl !== "function") throw new require_CalmLensVersion.CalmLensError(require_CalmLensVersion.SDK_ERROR_CODES.NO_FETCH, { message: "No fetch implementation is available; pass one as the fetch option" });
|
|
93
|
+
return fetchImpl;
|
|
94
|
+
};
|
|
95
|
+
this.transport = new require_CalmLensUtils.CalmLensTransport({
|
|
96
|
+
apiKey,
|
|
97
|
+
fetch: ((input, init) => resolveFetch()(input, init)),
|
|
98
|
+
signal: options.signal,
|
|
99
|
+
maxRetries: options.maxRetries ?? DEFAULT_MAX_RETRIES,
|
|
100
|
+
maxResponseBytes: options.maxResponseBytes ?? DEFAULT_MAX_RESPONSE_BYTES
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
get timeoutMs() {
|
|
104
|
+
return this.options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
105
|
+
}
|
|
106
|
+
url(path, query) {
|
|
107
|
+
const search = query ? require_CalmLensUtils.stringifyQuery(query) : "";
|
|
108
|
+
return `${this.options.baseUrl}${path}${search ? `?${search}` : ""}`;
|
|
109
|
+
}
|
|
110
|
+
/** A JSON request whose deadline covers reading the response. */
|
|
111
|
+
json(method, path, { query, body, headers, idempotencyKey } = {}) {
|
|
112
|
+
return this.transport.json({
|
|
113
|
+
method,
|
|
114
|
+
url: this.url(path, query),
|
|
115
|
+
headers: body === void 0 ? headers : {
|
|
116
|
+
...headers,
|
|
117
|
+
"Content-Type": "application/json"
|
|
118
|
+
},
|
|
119
|
+
body: body === void 0 ? void 0 : JSON.stringify(body),
|
|
120
|
+
idempotencyKey,
|
|
121
|
+
timeoutMs: this.timeoutMs,
|
|
122
|
+
deadline: "body"
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
upload(url, payload, headers, idempotencyKey) {
|
|
126
|
+
return this.transport.json({
|
|
127
|
+
method: "POST",
|
|
128
|
+
url,
|
|
129
|
+
headers,
|
|
130
|
+
body: toRawUploadBody(payload),
|
|
131
|
+
idempotencyKey,
|
|
132
|
+
timeoutMs: isTextPayload(payload) ? this.timeoutMs : this.options.uploadTimeoutMs,
|
|
133
|
+
deadline: "body",
|
|
134
|
+
fetch: this.options.uploadFetch
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Uploads file content or a first multipart part to `uploadsPath` (`.../assets`), as raw
|
|
139
|
+
* bytes with the options in the query.
|
|
140
|
+
*/
|
|
141
|
+
submitRawUpload(uploadsPath, options, queue, authHeaders = {}) {
|
|
142
|
+
const query = new URLSearchParams();
|
|
143
|
+
query.set("name", options.name);
|
|
144
|
+
query.set("queue", String(queue));
|
|
145
|
+
setOptionalQueryValue(query, "description", options.description);
|
|
146
|
+
setOptionalQueryValue(query, "processingOptions", options.processingOptions);
|
|
147
|
+
setOptionalQueryValue(query, "visibility", options.visibility);
|
|
148
|
+
setOptionalQueryValue(query, "metadata", options.metadata);
|
|
149
|
+
setOptionalQueryValue(query, "externalId", options.externalId);
|
|
150
|
+
setOptionalQueryValue(query, "externalUrl", options.externalUrl);
|
|
151
|
+
setOptionalQueryValue(query, "folderId", options.folderId);
|
|
152
|
+
setOptionalQueryValue(query, "uploadPresetId", options.uploadPresetId);
|
|
153
|
+
const isMultipart = "part" in options;
|
|
154
|
+
const payload = isMultipart ? options.part : options.content;
|
|
155
|
+
query.set("contentType", payload.type);
|
|
156
|
+
setOptionalQueryValue(query, "sizeInBytes", payload.sizeInBytes ?? require_CalmLensUtils.getAssetPayloadByteLength(payload));
|
|
157
|
+
if (isMultipart) {
|
|
158
|
+
const part = payload;
|
|
159
|
+
const multipartOptions = options;
|
|
160
|
+
query.set("partNumber", String(part.partNumber ?? 1));
|
|
161
|
+
query.set("isFinal", String(part.isFinal ?? false));
|
|
162
|
+
setOptionalQueryValue(query, "sizeInBytes", part.sizeInBytes ?? multipartOptions.sizeInBytes);
|
|
163
|
+
setOptionalQueryValue(query, "totalSizeInBytes", multipartOptions.totalSizeInBytes);
|
|
164
|
+
}
|
|
165
|
+
return this.upload(this.url(`${uploadsPath}?${query}`), payload, {
|
|
166
|
+
...authHeaders,
|
|
167
|
+
"Content-Type": payload.type,
|
|
168
|
+
"X-CalmLens-Upload": isMultipart ? "part" : "content"
|
|
169
|
+
}, options.idempotencyKey ?? newIdempotencyKey());
|
|
170
|
+
}
|
|
171
|
+
/** Uploads the next part of a multipart upload to `partPath` (`.../assets/{id}/part`). */
|
|
172
|
+
uploadPart(partPath, { part, queue, idempotencyKey }, authHeaders = {}) {
|
|
173
|
+
if (!part) throw require_CalmLensVersion.invalidArgument("Part options are required");
|
|
174
|
+
const validation = require_CalmLensUtils.validateAssetPayload(part, {
|
|
175
|
+
multipart: true,
|
|
176
|
+
isFinal: part.isFinal
|
|
177
|
+
});
|
|
178
|
+
if (!validation.ok) throw invalidPayload(validation.error);
|
|
179
|
+
const query = new URLSearchParams({
|
|
180
|
+
partNumber: String(part.partNumber),
|
|
181
|
+
isFinal: String(part.isFinal ?? false)
|
|
182
|
+
});
|
|
183
|
+
if (queue !== void 0) query.set("queue", String(queue));
|
|
184
|
+
setOptionalQueryValue(query, "sizeInBytes", part.sizeInBytes ?? require_CalmLensUtils.getAssetPayloadByteLength(part));
|
|
185
|
+
return this.upload(this.url(`${partPath}?${query}`), part, {
|
|
186
|
+
...authHeaders,
|
|
187
|
+
"X-CalmLens-Upload": "part"
|
|
188
|
+
}, idempotencyKey ?? newIdempotencyKey());
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
//#endregion
|
|
192
|
+
Object.defineProperty(exports, "CalmLensConnection", {
|
|
193
|
+
enumerable: true,
|
|
194
|
+
get: function() {
|
|
195
|
+
return CalmLensConnection;
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
Object.defineProperty(exports, "expectAsset", {
|
|
199
|
+
enumerable: true,
|
|
200
|
+
get: function() {
|
|
201
|
+
return expectAsset;
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
Object.defineProperty(exports, "expectUploadedAsset", {
|
|
205
|
+
enumerable: true,
|
|
206
|
+
get: function() {
|
|
207
|
+
return expectUploadedAsset;
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
Object.defineProperty(exports, "invalidPayload", {
|
|
211
|
+
enumerable: true,
|
|
212
|
+
get: function() {
|
|
213
|
+
return invalidPayload;
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
Object.defineProperty(exports, "newIdempotencyKey", {
|
|
217
|
+
enumerable: true,
|
|
218
|
+
get: function() {
|
|
219
|
+
return newIdempotencyKey;
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
Object.defineProperty(exports, "prepareSubmitAssetOptions", {
|
|
223
|
+
enumerable: true,
|
|
224
|
+
get: function() {
|
|
225
|
+
return prepareSubmitAssetOptions;
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
Object.defineProperty(exports, "resolveConnectionOptions", {
|
|
229
|
+
enumerable: true,
|
|
230
|
+
get: function() {
|
|
231
|
+
return resolveConnectionOptions;
|
|
232
|
+
}
|
|
233
|
+
});
|