@calmlens/sdk 0.11.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 +73 -16
- package/cjs/CalmLensConnection-DzRDCMOP.js +233 -0
- package/cjs/CalmLensUtils-vX5H_vMn.js +699 -0
- package/cjs/CalmLensVersion-DjhsJ1gj.js +410 -0
- package/cjs/CalmLensWebhooks-hVquX5L1.js +201 -0
- package/cjs/WebhookPayload-B7CgSmI5.d.ts +11928 -0
- package/cjs/app.d.ts +43 -0
- package/cjs/app.js +91 -0
- package/cjs/index-BywTQbRq.d.ts +352 -0
- package/cjs/index.d.ts +3 -382
- package/cjs/index.js +4 -816
- package/cjs/server.d.ts +210 -0
- package/cjs/server.js +384 -0
- package/cjs/testing.d.ts +59 -18
- package/cjs/testing.js +245 -99
- package/esm/CalmLensConnection-D8VsTdsA.js +192 -0
- package/esm/CalmLensUtils-B4i3Z2He.js +574 -0
- package/esm/CalmLensVersion-DK4BMEST.js +345 -0
- package/esm/CalmLensWebhooks-DyCoEvpf.js +148 -0
- package/esm/WebhookPayload-B7CgSmI5.d.ts +11928 -0
- package/esm/app.d.ts +43 -0
- package/esm/app.js +87 -0
- package/esm/index-BywTQbRq.d.ts +352 -0
- package/esm/index.d.ts +3 -382
- package/esm/index.js +1 -807
- package/esm/server.d.ts +210 -0
- package/esm/server.js +374 -0
- package/esm/testing.d.ts +59 -18
- package/esm/testing.js +195 -49
- package/package.json +23 -1
- package/cjs/CalmLensTypes-BZ-nxhr_.d.ts +0 -8613
- package/cjs/CalmLensWebhooks-CH2PaPs0.js +0 -784
- package/esm/CalmLensTypes-BZ-nxhr_.d.ts +0 -8613
- package/esm/CalmLensWebhooks-3Otbubyf.js +0 -593
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { c as invalidArgument, o as CalmLensError, s as SDK_ERROR_CODES, u as API_ERROR_CODES } from "./CalmLensVersion-DK4BMEST.js";
|
|
2
|
+
import { a as stringifyQuery, b as getRejectedUploadMimeTypeError, d as CalmLensTransport, g as validateAssetPayload, h as getAssetPayloadByteLength, i as randomUuid, m as assetToData, v as MAX_IMAGE_UPLOAD_BYTES, x as isRejectedUploadMimeType, y as MIN_MULTIPART_IMAGE_UPLOAD_BYTES } from "./CalmLensUtils-B4i3Z2He.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 = assetToData(payload);
|
|
14
|
+
if (data instanceof Response) {
|
|
15
|
+
if (!data.body) throw new CalmLensError(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 (isRejectedUploadMimeType(mimeType)) throw new CalmLensError(API_ERROR_CODES.INVALID_UPLOAD_MIME_TYPE, {
|
|
27
|
+
message: getRejectedUploadMimeTypeError(mimeType),
|
|
28
|
+
details: { mimeType }
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
const isImageMimeType = (mimeType) => mimeType.toLowerCase().startsWith("image/");
|
|
32
|
+
const invalidPayload = (message) => new CalmLensError(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 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 CalmLensError(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 = 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 ?? getAssetPayloadByteLength(options.part);
|
|
50
|
+
if (isImageMimeType(options.part.type) && partSizeInBytes !== void 0) {
|
|
51
|
+
if (partSizeInBytes > MAX_IMAGE_UPLOAD_BYTES) throw new CalmLensError(API_ERROR_CODES.MULTIPART_IMAGE_TOO_LARGE, { message: "Multipart image uploads cannot exceed 20MB." });
|
|
52
|
+
if (options.part.isFinal && partSizeInBytes < MIN_MULTIPART_IMAGE_UPLOAD_BYTES) throw new CalmLensError(API_ERROR_CODES.MULTIPART_IMAGE_TOO_SMALL, { message: "Multipart image uploads must total at least 10MB." });
|
|
53
|
+
}
|
|
54
|
+
const validation = 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 = () => 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 CalmLensError(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 CalmLensError(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 CalmLensError(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 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 ? 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 ?? 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 invalidArgument("Part options are required");
|
|
174
|
+
const validation = 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 ?? 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
|
+
export { newIdempotencyKey as a, invalidPayload as i, expectAsset as n, prepareSubmitAssetOptions as o, expectUploadedAsset as r, resolveConnectionOptions as s, CalmLensConnection as t };
|