calmlens 0.4.0 → 0.5.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 +61 -39
- package/bin/calmlens.js +217 -80
- package/cjs/CalmLensTypes-DG_kHVqY.d.ts +3521 -0
- package/cjs/{CalmLensUtils-CjdO9v--.js → CalmLensUtils-0QaxbI3A.js} +95 -93
- package/cjs/index.d.ts +2602 -24
- package/cjs/index.js +533 -124
- package/cjs/testing.d.ts +2 -2
- package/cjs/testing.js +6 -27
- package/esm/CalmLensTypes-DG_kHVqY.d.ts +3521 -0
- package/esm/{CalmLensUtils-B3Eiz2Ro.js → CalmLensUtils-BQEgxGWk.js} +68 -88
- package/esm/index.d.ts +2602 -24
- package/esm/index.js +526 -124
- package/esm/testing.d.ts +2 -2
- package/esm/testing.js +5 -4
- package/package.json +2 -2
- package/cjs/CalmLensTypes-DkqoKBwF.d.ts +0 -1761
- package/esm/CalmLensTypes-DkqoKBwF.d.ts +0 -1761
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
> This package is the same as [`@calmlens/
|
|
1
|
+
> This package is the same as [`@calmlens/sdk`](https://www.npmjs.com/package/@calmlens/sdk), published as `calmlens` so `npx calmlens` runs the CLI without installing anything. In code, depend on `@calmlens/sdk`.
|
|
2
2
|
|
|
3
|
-
# @calmlens/
|
|
3
|
+
# @calmlens/sdk
|
|
4
4
|
|
|
5
5
|
Official JavaScript/TypeScript SDK for CalmLens API
|
|
6
6
|
|
|
@@ -9,7 +9,7 @@ CalmLens provides content moderation and classification for text, images, docume
|
|
|
9
9
|
## Installation
|
|
10
10
|
|
|
11
11
|
```bash
|
|
12
|
-
npm install @calmlens/
|
|
12
|
+
npm install @calmlens/sdk
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
Every public SDK operation accepts one options object. This keeps call sites stable as optional fields are added.
|
|
@@ -19,7 +19,7 @@ Every public SDK operation accepts one options object. This keeps call sites sta
|
|
|
19
19
|
Create an API key under **Developer → API Keys** in the CalmLens portal. The same page shows your **Project ID**. Keep both on your server.
|
|
20
20
|
|
|
21
21
|
```typescript
|
|
22
|
-
import { CalmLensClient } from "@calmlens/
|
|
22
|
+
import { CalmLensClient } from "@calmlens/sdk";
|
|
23
23
|
|
|
24
24
|
const client = new CalmLensClient({
|
|
25
25
|
apiKey: process.env.CALMLENS_API_KEY!,
|
|
@@ -27,10 +27,7 @@ const client = new CalmLensClient({
|
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
// Text (comments, captions, bios): waits for the decision
|
|
30
|
-
const comment = await client.text.moderate({
|
|
31
|
-
text: "Great photo!",
|
|
32
|
-
externalId: "comment-42", // your own ID
|
|
33
|
-
});
|
|
30
|
+
const comment = await client.text.moderate({ text: "Great photo!", externalId: "comment-42" });
|
|
34
31
|
|
|
35
32
|
if (comment.status === "approved") {
|
|
36
33
|
// publish it; if comment.report?.flagged is true, also queue it for review
|
|
@@ -77,21 +74,6 @@ new CalmLensClient(options: CalmLensClientOptions)
|
|
|
77
74
|
|
|
78
75
|
#### Methods
|
|
79
76
|
|
|
80
|
-
##### text.moderate(options)
|
|
81
|
-
|
|
82
|
-
Moderate a comment, caption, bio or message and wait for the decision.
|
|
83
|
-
|
|
84
|
-
```typescript
|
|
85
|
-
async text.moderate(options: ModerateTextOptions): Promise<Asset>
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
**Options:**
|
|
89
|
-
- `text` (string, required): The text to moderate
|
|
90
|
-
- `name` (string, optional): Asset name shown in the portal. Defaults to `"text"`
|
|
91
|
-
- `externalId`, `metadata`, `folderId`, `processingOptions` (optional): As for `assets.create`
|
|
92
|
-
|
|
93
|
-
This is shorthand for `assets.analyze` with `content: { plainText: text, type: "text/plain" }`.
|
|
94
|
-
|
|
95
77
|
##### assets.create(options)
|
|
96
78
|
|
|
97
79
|
Queue an asset for processing and return the created asset immediately.
|
|
@@ -126,11 +108,17 @@ const asset = await client.assets.create({
|
|
|
126
108
|
const asset = await client.assets.create({
|
|
127
109
|
name: "website-content",
|
|
128
110
|
url: "https://example.com/page",
|
|
129
|
-
description: "Web page to
|
|
111
|
+
description: "Web page to moderate"
|
|
130
112
|
});
|
|
131
113
|
```
|
|
132
114
|
|
|
133
|
-
|
|
115
|
+
##### assets.moderate(options)
|
|
116
|
+
|
|
117
|
+
Submit an asset and wait for classification to finish. Accepts the same options as `assets.create()` for text, images, documents, and URLs.
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
async assets.moderate(options: SubmitAssetOptions): Promise<Asset>
|
|
121
|
+
```
|
|
134
122
|
|
|
135
123
|
##### assets.retrieve(options)
|
|
136
124
|
|
|
@@ -399,7 +387,7 @@ const completed = await client.assets.uploadPart({
|
|
|
399
387
|
|
|
400
388
|
**Example (single-call synchronous analysis):**
|
|
401
389
|
```typescript
|
|
402
|
-
const analyzed = await client.assets.
|
|
390
|
+
const analyzed = await client.assets.moderate({
|
|
403
391
|
name: "large-image.png",
|
|
404
392
|
part: {
|
|
405
393
|
base64: finalPartBase64,
|
|
@@ -414,30 +402,36 @@ console.log(analyzed.status);
|
|
|
414
402
|
|
|
415
403
|
##### verifyWebhookSignature(options)
|
|
416
404
|
|
|
417
|
-
Verify webhook
|
|
405
|
+
Verify that a webhook delivery came from CalmLens and is fresh. Also exported as a standalone `verifyWebhookSignature` function.
|
|
418
406
|
|
|
419
407
|
```typescript
|
|
420
408
|
static async verifyWebhookSignature(options: {
|
|
421
|
-
payload: string | object;
|
|
422
|
-
|
|
409
|
+
payload: string | object; // raw request body (pass the string, not parsed JSON)
|
|
410
|
+
signature: string | null | undefined; // x-calmlens-signature header
|
|
411
|
+
timestamp: string | number | null | undefined; // x-calmlens-timestamp header
|
|
423
412
|
secret: string;
|
|
413
|
+
toleranceSeconds?: number; // default 300
|
|
414
|
+
now?: Date | number; // default Date.now(), for tests
|
|
424
415
|
}): Promise<boolean>
|
|
425
416
|
```
|
|
426
417
|
|
|
418
|
+
It returns `true` only when the `x-calmlens-signature` header contains a `v1=` HMAC-SHA256 of `"<timestamp>.<raw body>"` (one match in a comma-separated list is enough) and the timestamp is within `toleranceSeconds` of now.
|
|
419
|
+
|
|
427
420
|
**Example:**
|
|
428
421
|
```typescript
|
|
429
|
-
// In your webhook handler
|
|
430
|
-
const
|
|
431
|
-
const payload = req.body;
|
|
422
|
+
// In your webhook handler (Fetch API Request)
|
|
423
|
+
const payload = await request.text();
|
|
432
424
|
const isValid = await CalmLensClient.verifyWebhookSignature({
|
|
433
425
|
payload,
|
|
434
|
-
|
|
426
|
+
signature: request.headers.get('x-calmlens-signature'),
|
|
427
|
+
timestamp: request.headers.get('x-calmlens-timestamp'),
|
|
435
428
|
secret: WEBHOOK_SECRET,
|
|
436
429
|
});
|
|
437
430
|
|
|
438
431
|
if (!isValid) {
|
|
439
|
-
return
|
|
432
|
+
return new Response('Unauthorized', { status: 401 });
|
|
440
433
|
}
|
|
434
|
+
const event: WebhookEventPayload = JSON.parse(payload);
|
|
441
435
|
```
|
|
442
436
|
|
|
443
437
|
## Asset Types
|
|
@@ -499,12 +493,12 @@ Public API failures are normalized to `CalmLensError`. Use its stable `code` pro
|
|
|
499
493
|
|
|
500
494
|
## Testing
|
|
501
495
|
|
|
502
|
-
`@calmlens/
|
|
496
|
+
`@calmlens/sdk/testing` exports a matching object-parameter `MockCalmLensClient`, in-memory storage implementations, and mock API key helpers for local unit testing without network requests.
|
|
503
497
|
|
|
504
|
-
The main `@calmlens/
|
|
498
|
+
The main `@calmlens/sdk` entry point does not load or export testing helpers to keep production bundles minimal. Keep `/testing` imports in test files.
|
|
505
499
|
|
|
506
500
|
```typescript
|
|
507
|
-
import { MockCalmLensClient, createMockApiKey } from "@calmlens/
|
|
501
|
+
import { MockCalmLensClient, createMockApiKey } from "@calmlens/sdk/testing";
|
|
508
502
|
|
|
509
503
|
const client = new MockCalmLensClient({
|
|
510
504
|
apiKey: createMockApiKey(),
|
|
@@ -516,11 +510,31 @@ For configuration options (`mockOptions`), deterministic timestamps and IDs, sto
|
|
|
516
510
|
|
|
517
511
|
## Webhooks
|
|
518
512
|
|
|
519
|
-
|
|
513
|
+
A `webhook` connector POSTs each event to your URL as JSON:
|
|
514
|
+
|
|
515
|
+
```json
|
|
516
|
+
{
|
|
517
|
+
"id": "…",
|
|
518
|
+
"type": "asset.flagged",
|
|
519
|
+
"createdAt": 1760000000000,
|
|
520
|
+
"projectId": "…",
|
|
521
|
+
"data": { "severity": "high", "title": "…", "description": null, "assetId": "…", "asset": null, "metadata": null }
|
|
522
|
+
}
|
|
523
|
+
```
|
|
524
|
+
|
|
525
|
+
Each delivery has these headers:
|
|
526
|
+
|
|
527
|
+
| Header | Value |
|
|
528
|
+
|---|---|
|
|
529
|
+
| `x-calmlens-delivery` | Delivery ID (the same across retries) |
|
|
530
|
+
| `x-calmlens-timestamp` | Unix time in seconds when the delivery was signed |
|
|
531
|
+
| `x-calmlens-signature` | `v1=<lowercase hex HMAC-SHA256(secret, "<timestamp>.<raw body>")>` |
|
|
532
|
+
|
|
533
|
+
Verify the exact raw request body with `verifyWebhookSignature` before parsing it; re-serializing parsed JSON can change the signed bytes. Deliveries more than 5 minutes old (or ahead of your clock) are rejected to prevent replays. The body type is exported as `WebhookEventPayload`; narrow it on `type` to get that event type's typed `data.facts` (`EventType` and `EventFacts<Type>` are exported too, and [the webhooks guide](https://calmlens.com/docs/guides/webhooks#event-types) lists every type). The header names are exported as `WEBHOOK_SIGNATURE_HEADER`, `WEBHOOK_TIMESTAMP_HEADER` and `WEBHOOK_DELIVERY_HEADER`.
|
|
520
534
|
|
|
521
535
|
## Command-line tool
|
|
522
536
|
|
|
523
|
-
The SDK installs a `calmlens` command. Webhook
|
|
537
|
+
The SDK installs a `calmlens` command. Webhook connectors must use public HTTPS URLs, so during development use it to forward events to a local handler instead. Each event is sent with the same body, headers and signature as a real delivery:
|
|
524
538
|
|
|
525
539
|
```bash
|
|
526
540
|
npx calmlens listen --forward-to http://localhost:3000/api/webhooks/calmlens --env-file .env.local
|
|
@@ -533,3 +547,11 @@ The same feed is available in code: `client.events.list({ after, limit })` retur
|
|
|
533
547
|
## Releasing
|
|
534
548
|
|
|
535
549
|
Stable releases are published automatically from `main` when packed SDK contents differ from npm `latest` and an SDK Changeset is present. Contributors must run `bun run changeset` to choose the release type and write release notes. CI updates and commits this package's `CHANGELOG.md` automatically. See the repository's [.changeset release guide](../../.changeset/README.md).
|
|
550
|
+
|
|
551
|
+
### Request budgets and errors
|
|
552
|
+
|
|
553
|
+
Set `timeoutMs` (default 120000) or `signal` on the client. Use `client.withRequestOptions({ signal, timeoutMs: 30000 })` to scope them to an operation without changing the original client. Multipart state is shared between scoped clients. Deadlines include response-body reads; authenticated default REST, upload and CDN requests reject redirects. Custom non-Fetch backends own their redirect/cancellation policies.
|
|
554
|
+
|
|
555
|
+
`CalmLensError` exposes `code`, optional HTTP `status`, `details` and native `cause`. Transport failures use `sdk/transport-error`, deadlines `sdk/timeout`, cancellation `sdk/cancelled`, malformed successful uploads `sdk/invalid-response`, and unstructured HTTP failures `sdk/http-error`. API error codes/messages are preserved. Core model types (`Asset`, `Folder`, `Lens`, `AssetPage`, `EventsPage`, `ClassificationReport`) and request types are exported from the package root.
|
|
556
|
+
|
|
557
|
+
`text.moderate` is a convenience wrapper around asset moderation: it supplies `text/plain` content and defaults the asset name to `text`. `assets.create` queues processing; `assets.moderate` waits for classification. The old asset analyze method is removed.
|