@calmlens/js-sdk 0.0.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 +37 -0
- package/cjs/Asset.d.ts +109 -0
- package/cjs/Asset.js +39 -0
- package/cjs/CalmLensClient.d.ts +41 -0
- package/cjs/CalmLensClient.js +199 -0
- package/cjs/CalmLensTypes.d.ts +5 -0
- package/cjs/CalmLensTypes.js +2 -0
- package/cjs/CalmLensUtils.d.ts +1 -0
- package/cjs/CalmLensUtils.js +68 -0
- package/cjs/Classification.d.ts +154 -0
- package/cjs/Classification.js +117 -0
- package/cjs/SchemaUtils.d.ts +11 -0
- package/cjs/SchemaUtils.js +63 -0
- package/cjs/SharedTypes.d.ts +13 -0
- package/cjs/SharedTypes.js +2 -0
- package/cjs/ValidationUtils.d.ts +1 -0
- package/cjs/ValidationUtils.js +33 -0
- package/cjs/index.d.ts +3 -0
- package/cjs/index.js +8 -0
- package/esm/Asset.d.ts +109 -0
- package/esm/Asset.js +36 -0
- package/esm/CalmLensClient.d.ts +41 -0
- package/esm/CalmLensClient.js +142 -0
- package/esm/CalmLensTypes.d.ts +5 -0
- package/esm/CalmLensTypes.js +1 -0
- package/esm/CalmLensUtils.d.ts +1 -0
- package/esm/CalmLensUtils.js +28 -0
- package/esm/Classification.d.ts +154 -0
- package/esm/Classification.js +113 -0
- package/esm/SchemaUtils.d.ts +11 -0
- package/esm/SchemaUtils.js +46 -0
- package/esm/SharedTypes.d.ts +13 -0
- package/esm/SharedTypes.js +1 -0
- package/esm/ValidationUtils.d.ts +1 -0
- package/esm/ValidationUtils.js +29 -0
- package/esm/index.d.ts +3 -0
- package/esm/index.js +3 -0
- package/package.json +71 -0
package/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# [api-def](https://github.com/Censkh/api-def/) · [](https://github.com/Censkh/api-def/blob/master/LICENSE) [](https://www.npmjs.com/package/api-def) [](https://github.com/Censkh/api-def/actions)
|
|
2
|
+
|
|
3
|
+
Typed APIs with middleware support
|
|
4
|
+
|
|
5
|
+
API def provides a unified way to type your endpoints allowing for compile time checking of query, body, response and even url parameters
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm i api-def
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
- [Documentation](https://censkh.github.io/api-def/)
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { Api } from "api-def";
|
|
15
|
+
|
|
16
|
+
const api = new Api({
|
|
17
|
+
baseUrl: "https://my-api/",
|
|
18
|
+
name: "My API",
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const fetchData = api
|
|
22
|
+
.endpoint()
|
|
23
|
+
.queryOf<{ includeAwesome: boolean }>()
|
|
24
|
+
.responseOf<{ data: { awesome: boolean } }>()
|
|
25
|
+
.build({
|
|
26
|
+
id: "fetch_data",
|
|
27
|
+
method: "get",
|
|
28
|
+
path: "/data",
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// calls GET https://my-api/data?includeAwesome=true
|
|
32
|
+
const res = await fetchData.submit({
|
|
33
|
+
query: { includeAwesome: true },
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
console.log(res.data); // { data: { awesome: true } }
|
|
37
|
+
```
|
package/cjs/Asset.d.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import * as zod from "zod/v4";
|
|
2
|
+
export declare const ASSET_STATUS_SCHEMA: zod.ZodEnum<{
|
|
3
|
+
error: "error";
|
|
4
|
+
pending: "pending";
|
|
5
|
+
approved: "approved";
|
|
6
|
+
rejected: "rejected";
|
|
7
|
+
}>;
|
|
8
|
+
export type AssetStatus = zod.infer<typeof ASSET_STATUS_SCHEMA>;
|
|
9
|
+
export declare const ASSET_TYPE_SCHEMA: zod.ZodEnum<{
|
|
10
|
+
image: "image";
|
|
11
|
+
video: "video";
|
|
12
|
+
audio: "audio";
|
|
13
|
+
document: "document";
|
|
14
|
+
website: "website";
|
|
15
|
+
}>;
|
|
16
|
+
export type AssetType = zod.infer<typeof ASSET_TYPE_SCHEMA>;
|
|
17
|
+
export declare const ASSET_VISIBILITY_SCHEMA: zod.ZodEnum<{
|
|
18
|
+
public: "public";
|
|
19
|
+
private: "private";
|
|
20
|
+
}>;
|
|
21
|
+
export type AssetVisibility = zod.infer<typeof ASSET_VISIBILITY_SCHEMA>;
|
|
22
|
+
export declare const ASSET_SCHEMA: zod.ZodObject<{
|
|
23
|
+
id: zod.ZodString;
|
|
24
|
+
projectId: zod.ZodString;
|
|
25
|
+
sizeInBytes: zod.ZodOptional<zod.ZodNullable<zod.ZodNumber>>;
|
|
26
|
+
userId: zod.ZodOptional<zod.ZodNullable<zod.ZodPipe<zod.ZodString, zod.ZodTransform<string, string>>>>;
|
|
27
|
+
parentId: zod.ZodOptional<zod.ZodNullable<zod.ZodString>>;
|
|
28
|
+
createdAt: zod.ZodNumber;
|
|
29
|
+
updatedAt: zod.ZodOptional<zod.ZodNumber>;
|
|
30
|
+
name: zod.ZodPipe<zod.ZodString, zod.ZodTransform<string, string>>;
|
|
31
|
+
fileFormat: zod.ZodOptional<zod.ZodNullable<zod.ZodPipe<zod.ZodString, zod.ZodTransform<string, string>>>>;
|
|
32
|
+
fileExtension: zod.ZodOptional<zod.ZodNullable<zod.ZodPipe<zod.ZodString, zod.ZodTransform<string, string>>>>;
|
|
33
|
+
description: zod.ZodOptional<zod.ZodNullable<zod.ZodPipe<zod.ZodString, zod.ZodTransform<string, string>>>>;
|
|
34
|
+
externalId: zod.ZodOptional<zod.ZodNullable<zod.ZodPipe<zod.ZodString, zod.ZodTransform<string, string>>>>;
|
|
35
|
+
externalUrl: zod.ZodOptional<zod.ZodNullable<zod.ZodString>>;
|
|
36
|
+
previewImageId: zod.ZodOptional<zod.ZodNullable<zod.ZodPipe<zod.ZodString, zod.ZodTransform<string, string>>>>;
|
|
37
|
+
storageId: zod.ZodOptional<zod.ZodNullable<zod.ZodPipe<zod.ZodString, zod.ZodTransform<string, string>>>>;
|
|
38
|
+
averageColor: zod.ZodOptional<zod.ZodNullable<zod.ZodPipe<zod.ZodString, zod.ZodTransform<string, string>>>>;
|
|
39
|
+
type: zod.ZodEnum<{
|
|
40
|
+
image: "image";
|
|
41
|
+
video: "video";
|
|
42
|
+
audio: "audio";
|
|
43
|
+
document: "document";
|
|
44
|
+
website: "website";
|
|
45
|
+
}>;
|
|
46
|
+
previewHash: zod.ZodOptional<zod.ZodNullable<zod.ZodPipe<zod.ZodString, zod.ZodTransform<string, string>>>>;
|
|
47
|
+
metadata: zod.ZodOptional<zod.ZodNullable<zod.ZodAny>>;
|
|
48
|
+
tags: zod.ZodOptional<zod.ZodNullable<zod.ZodArray<zod.ZodPipe<zod.ZodString, zod.ZodTransform<string, string>>>>>;
|
|
49
|
+
report: zod.ZodOptional<zod.ZodNullable<zod.ZodObject<{
|
|
50
|
+
processedAt: zod.ZodNumber;
|
|
51
|
+
categories: zod.ZodRecord<zod.ZodEnum<{
|
|
52
|
+
sexual: "sexual";
|
|
53
|
+
violence: "violence";
|
|
54
|
+
gore: "gore";
|
|
55
|
+
"self-harm": "self-harm";
|
|
56
|
+
harassment: "harassment";
|
|
57
|
+
hate: "hate";
|
|
58
|
+
illicit: "illicit";
|
|
59
|
+
"child-exploitation": "child-exploitation";
|
|
60
|
+
}> & zod.z.core.$partial, zod.ZodObject<{
|
|
61
|
+
category: zod.ZodEnum<{
|
|
62
|
+
sexual: "sexual";
|
|
63
|
+
violence: "violence";
|
|
64
|
+
gore: "gore";
|
|
65
|
+
"self-harm": "self-harm";
|
|
66
|
+
harassment: "harassment";
|
|
67
|
+
hate: "hate";
|
|
68
|
+
illicit: "illicit";
|
|
69
|
+
"child-exploitation": "child-exploitation";
|
|
70
|
+
}>;
|
|
71
|
+
likelihood: zod.ZodEnum<{
|
|
72
|
+
VERY_UNLIKELY: "VERY_UNLIKELY";
|
|
73
|
+
UNLIKELY: "UNLIKELY";
|
|
74
|
+
POSSIBLE: "POSSIBLE";
|
|
75
|
+
LIKELY: "LIKELY";
|
|
76
|
+
VERY_LIKELY: "VERY_LIKELY";
|
|
77
|
+
}>;
|
|
78
|
+
score: zod.ZodNumber;
|
|
79
|
+
}, zod.z.core.$strip>>;
|
|
80
|
+
flagged: zod.ZodBoolean;
|
|
81
|
+
score: zod.ZodNumber;
|
|
82
|
+
notes: zod.ZodOptional<zod.ZodNullable<zod.ZodPipe<zod.ZodString, zod.ZodTransform<string, string>>>>;
|
|
83
|
+
labels: zod.ZodOptional<zod.ZodNullable<zod.ZodArray<zod.ZodObject<{
|
|
84
|
+
description: zod.ZodPipe<zod.ZodString, zod.ZodTransform<string, string>>;
|
|
85
|
+
score: zod.ZodNumber;
|
|
86
|
+
topicalityScore: zod.ZodOptional<zod.ZodNullable<zod.ZodNumber>>;
|
|
87
|
+
likelihood: zod.ZodEnum<{
|
|
88
|
+
VERY_UNLIKELY: "VERY_UNLIKELY";
|
|
89
|
+
UNLIKELY: "UNLIKELY";
|
|
90
|
+
POSSIBLE: "POSSIBLE";
|
|
91
|
+
LIKELY: "LIKELY";
|
|
92
|
+
VERY_LIKELY: "VERY_LIKELY";
|
|
93
|
+
}>;
|
|
94
|
+
knowledgeBaseId: zod.ZodOptional<zod.ZodNullable<zod.ZodPipe<zod.ZodString, zod.ZodTransform<string, string>>>>;
|
|
95
|
+
}, zod.z.core.$strip>>>>;
|
|
96
|
+
}, zod.z.core.$strip>>>;
|
|
97
|
+
status: zod.ZodEnum<{
|
|
98
|
+
error: "error";
|
|
99
|
+
pending: "pending";
|
|
100
|
+
approved: "approved";
|
|
101
|
+
rejected: "rejected";
|
|
102
|
+
}>;
|
|
103
|
+
keepAfterProcessing: zod.ZodOptional<zod.ZodNullable<zod.ZodBoolean>>;
|
|
104
|
+
visibility: zod.ZodOptional<zod.ZodNullable<zod.ZodEnum<{
|
|
105
|
+
public: "public";
|
|
106
|
+
private: "private";
|
|
107
|
+
}>>>;
|
|
108
|
+
}, zod.z.core.$strip>;
|
|
109
|
+
export type Asset = zod.infer<typeof ASSET_SCHEMA>;
|
package/cjs/Asset.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ASSET_SCHEMA = exports.ASSET_VISIBILITY_SCHEMA = exports.ASSET_TYPE_SCHEMA = exports.ASSET_STATUS_SCHEMA = void 0;
|
|
4
|
+
var zod = require("zod/v4");
|
|
5
|
+
var Classification_1 = require("./Classification");
|
|
6
|
+
var SchemaUtils_1 = require("./SchemaUtils");
|
|
7
|
+
exports.ASSET_STATUS_SCHEMA = zod.enum(["pending", "approved", "rejected", "error"]);
|
|
8
|
+
exports.ASSET_TYPE_SCHEMA = zod.enum(["image", "video", "audio", "document", "website"]);
|
|
9
|
+
exports.ASSET_VISIBILITY_SCHEMA = zod.enum(["public", "private"]);
|
|
10
|
+
exports.ASSET_SCHEMA = zod.object({
|
|
11
|
+
id: (0, SchemaUtils_1.primaryUuidField)(),
|
|
12
|
+
projectId: (0, SchemaUtils_1.uuidField)(),
|
|
13
|
+
sizeInBytes: zod.number().nullish(),
|
|
14
|
+
userId: (0, SchemaUtils_1.saneStringField)().nullish(),
|
|
15
|
+
parentId: (0, SchemaUtils_1.uuidField)().nullish(),
|
|
16
|
+
createdAt: (0, SchemaUtils_1.createdAtField)(),
|
|
17
|
+
updatedAt: (0, SchemaUtils_1.updatedAtField)(),
|
|
18
|
+
name: (0, SchemaUtils_1.saneStringField)({
|
|
19
|
+
type: "large",
|
|
20
|
+
}),
|
|
21
|
+
fileFormat: (0, SchemaUtils_1.saneStringField)().nullish(),
|
|
22
|
+
fileExtension: (0, SchemaUtils_1.saneStringField)().nullish(),
|
|
23
|
+
description: (0, SchemaUtils_1.saneStringField)({
|
|
24
|
+
type: "large",
|
|
25
|
+
}).nullish(),
|
|
26
|
+
externalId: (0, SchemaUtils_1.saneStringField)().nullish(),
|
|
27
|
+
externalUrl: zod.string().url().nullish(),
|
|
28
|
+
previewImageId: (0, SchemaUtils_1.saneStringField)().nullish(),
|
|
29
|
+
storageId: (0, SchemaUtils_1.saneStringField)().nullish(),
|
|
30
|
+
averageColor: (0, SchemaUtils_1.saneStringField)().nullish(),
|
|
31
|
+
type: exports.ASSET_TYPE_SCHEMA,
|
|
32
|
+
previewHash: (0, SchemaUtils_1.saneStringField)().nullish(),
|
|
33
|
+
metadata: zod.any().nullish(),
|
|
34
|
+
tags: zod.array((0, SchemaUtils_1.saneStringField)()).nullish(),
|
|
35
|
+
report: Classification_1.CLASSIFICATION_REPORT_SCHEMA.nullish(),
|
|
36
|
+
status: exports.ASSET_STATUS_SCHEMA,
|
|
37
|
+
keepAfterProcessing: zod.boolean().nullish(),
|
|
38
|
+
visibility: exports.ASSET_VISIBILITY_SCHEMA.nullish(),
|
|
39
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Asset } from "./Asset";
|
|
2
|
+
import type { SubmitAssetOptions } from "./SharedTypes";
|
|
3
|
+
import type { CalmLensClientOptions } from "./CalmLensTypes";
|
|
4
|
+
export default class CalmLensClient {
|
|
5
|
+
private options;
|
|
6
|
+
private readonly api;
|
|
7
|
+
private readonly endpoints;
|
|
8
|
+
/**
|
|
9
|
+
* Validates a webhook signature to ensure the webhook payload is authentic.
|
|
10
|
+
*
|
|
11
|
+
* @param payload - The webhook payload (string or object)
|
|
12
|
+
* @param signatureHex - The signature from the 'x-calmlens-signature' header
|
|
13
|
+
* @param secret - The webhook secret used to sign the payload
|
|
14
|
+
* @returns Promise<boolean> - True if the signature is valid, false otherwise
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```javascript
|
|
18
|
+
* // In your webhook handler
|
|
19
|
+
* const signature = req.headers['x-calmlens-signature'];
|
|
20
|
+
* const payload = req.body;
|
|
21
|
+
* const isValid = await CalmLensClient.verifyWebhookSignature(payload, signature, WEBHOOK_SECRET);
|
|
22
|
+
*
|
|
23
|
+
* if (!isValid) {
|
|
24
|
+
* return res.status(401).send('Unauthorized');
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
static verifyWebhookSignature(payload: string | object, signatureHex: string, secret: string): Promise<boolean>;
|
|
29
|
+
constructor(options: CalmLensClientOptions);
|
|
30
|
+
submitAsset(options: SubmitAssetOptions): Promise<Asset>;
|
|
31
|
+
getAsset(assetId: string): Promise<Asset>;
|
|
32
|
+
listAssets(options?: {
|
|
33
|
+
pageIndex?: number;
|
|
34
|
+
pageSize?: number;
|
|
35
|
+
}): Promise<{
|
|
36
|
+
items: Asset[];
|
|
37
|
+
total: number;
|
|
38
|
+
pageIndex: number;
|
|
39
|
+
pageSize: number;
|
|
40
|
+
}>;
|
|
41
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
var api_def_1 = require("api-def");
|
|
40
|
+
var CalmLensUtils_1 = require("./CalmLensUtils");
|
|
41
|
+
var createApi = function (options) {
|
|
42
|
+
var api = new api_def_1.Api({
|
|
43
|
+
name: "Calm Lens",
|
|
44
|
+
baseUrl: options.baseUrl,
|
|
45
|
+
middleware: [
|
|
46
|
+
{
|
|
47
|
+
beforeSend: function (context) {
|
|
48
|
+
context.updateHeaders({
|
|
49
|
+
"X-Api-Key": options.apiKey,
|
|
50
|
+
});
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
],
|
|
54
|
+
});
|
|
55
|
+
return api;
|
|
56
|
+
};
|
|
57
|
+
// API endpoint definitions
|
|
58
|
+
var createEndpoints = function (api) {
|
|
59
|
+
var postUploadAsset = api
|
|
60
|
+
.endpoint()
|
|
61
|
+
.bodyOf()
|
|
62
|
+
.paramsOf()
|
|
63
|
+
.responseOf()
|
|
64
|
+
.build({
|
|
65
|
+
id: "postUploadAsset",
|
|
66
|
+
method: "post",
|
|
67
|
+
path: "/projects/:projectId/assets",
|
|
68
|
+
});
|
|
69
|
+
var getAsset = api.endpoint().paramsOf().responseOf().build({
|
|
70
|
+
id: "getAsset",
|
|
71
|
+
method: "get",
|
|
72
|
+
path: "/projects/:projectId/assets/:assetId",
|
|
73
|
+
});
|
|
74
|
+
var getAssetsPage = api
|
|
75
|
+
.endpoint()
|
|
76
|
+
.queryOf()
|
|
77
|
+
.paramsOf()
|
|
78
|
+
.responseOf()
|
|
79
|
+
.build({
|
|
80
|
+
id: "getAssetsPage",
|
|
81
|
+
method: "get",
|
|
82
|
+
path: "/projects/:projectId/assets/page",
|
|
83
|
+
});
|
|
84
|
+
return {
|
|
85
|
+
postUploadAsset: postUploadAsset,
|
|
86
|
+
getAsset: getAsset,
|
|
87
|
+
getAssetsPage: getAssetsPage,
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
var CalmLensClient = /** @class */ (function () {
|
|
91
|
+
function CalmLensClient(options) {
|
|
92
|
+
var _a;
|
|
93
|
+
this.options = options;
|
|
94
|
+
if (!options.apiKey) {
|
|
95
|
+
throw new Error("API key is required");
|
|
96
|
+
}
|
|
97
|
+
if (!options.projectId) {
|
|
98
|
+
throw new Error("Project ID is required");
|
|
99
|
+
}
|
|
100
|
+
this.options.baseUrl = (_a = this.options.baseUrl) !== null && _a !== void 0 ? _a : "https://api.calmlens.com";
|
|
101
|
+
this.api = createApi(this.options);
|
|
102
|
+
this.endpoints = createEndpoints(this.api);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Validates a webhook signature to ensure the webhook payload is authentic.
|
|
106
|
+
*
|
|
107
|
+
* @param payload - The webhook payload (string or object)
|
|
108
|
+
* @param signatureHex - The signature from the 'x-calmlens-signature' header
|
|
109
|
+
* @param secret - The webhook secret used to sign the payload
|
|
110
|
+
* @returns Promise<boolean> - True if the signature is valid, false otherwise
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```javascript
|
|
114
|
+
* // In your webhook handler
|
|
115
|
+
* const signature = req.headers['x-calmlens-signature'];
|
|
116
|
+
* const payload = req.body;
|
|
117
|
+
* const isValid = await CalmLensClient.verifyWebhookSignature(payload, signature, WEBHOOK_SECRET);
|
|
118
|
+
*
|
|
119
|
+
* if (!isValid) {
|
|
120
|
+
* return res.status(401).send('Unauthorized');
|
|
121
|
+
* }
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
CalmLensClient.verifyWebhookSignature = function (payload, signatureHex, secret) {
|
|
125
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
126
|
+
return __generator(this, function (_a) {
|
|
127
|
+
return [2 /*return*/, (0, CalmLensUtils_1.verifyWebhookSignature)(payload, signatureHex, secret)];
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
CalmLensClient.prototype.submitAsset = function (options) {
|
|
132
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
133
|
+
var result;
|
|
134
|
+
return __generator(this, function (_a) {
|
|
135
|
+
switch (_a.label) {
|
|
136
|
+
case 0:
|
|
137
|
+
if (!("file" in options) && !("url" in options)) {
|
|
138
|
+
throw new Error("Either file content or URL is required");
|
|
139
|
+
}
|
|
140
|
+
if (!options.name) {
|
|
141
|
+
throw new Error("Asset name is required");
|
|
142
|
+
}
|
|
143
|
+
return [4 /*yield*/, this.endpoints.postUploadAsset.submit({
|
|
144
|
+
body: options,
|
|
145
|
+
params: {
|
|
146
|
+
projectId: this.options.projectId,
|
|
147
|
+
},
|
|
148
|
+
})];
|
|
149
|
+
case 1:
|
|
150
|
+
result = _a.sent();
|
|
151
|
+
return [2 /*return*/, result.data];
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
};
|
|
156
|
+
CalmLensClient.prototype.getAsset = function (assetId) {
|
|
157
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
158
|
+
var result;
|
|
159
|
+
return __generator(this, function (_a) {
|
|
160
|
+
switch (_a.label) {
|
|
161
|
+
case 0:
|
|
162
|
+
if (!assetId) {
|
|
163
|
+
throw new Error("Asset ID is required");
|
|
164
|
+
}
|
|
165
|
+
return [4 /*yield*/, this.endpoints.getAsset.submit({
|
|
166
|
+
params: {
|
|
167
|
+
projectId: this.options.projectId,
|
|
168
|
+
assetId: assetId,
|
|
169
|
+
},
|
|
170
|
+
})];
|
|
171
|
+
case 1:
|
|
172
|
+
result = _a.sent();
|
|
173
|
+
return [2 /*return*/, result.data];
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
};
|
|
178
|
+
CalmLensClient.prototype.listAssets = function () {
|
|
179
|
+
return __awaiter(this, arguments, void 0, function (options) {
|
|
180
|
+
var result;
|
|
181
|
+
if (options === void 0) { options = {}; }
|
|
182
|
+
return __generator(this, function (_a) {
|
|
183
|
+
switch (_a.label) {
|
|
184
|
+
case 0: return [4 /*yield*/, this.endpoints.getAssetsPage.submit({
|
|
185
|
+
params: {
|
|
186
|
+
projectId: this.options.projectId,
|
|
187
|
+
},
|
|
188
|
+
query: options,
|
|
189
|
+
})];
|
|
190
|
+
case 1:
|
|
191
|
+
result = _a.sent();
|
|
192
|
+
return [2 /*return*/, result.data];
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
};
|
|
197
|
+
return CalmLensClient;
|
|
198
|
+
}());
|
|
199
|
+
exports.default = CalmLensClient;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function verifyWebhookSignature(payload: string | object, signatureHex: string, secret: string): Promise<boolean>;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.verifyWebhookSignature = verifyWebhookSignature;
|
|
40
|
+
function verifyWebhookSignature(payload, signatureHex, secret) {
|
|
41
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
42
|
+
var encoder, normalizePayload, key, sig, hex, diff, i;
|
|
43
|
+
return __generator(this, function (_a) {
|
|
44
|
+
switch (_a.label) {
|
|
45
|
+
case 0:
|
|
46
|
+
encoder = new TextEncoder();
|
|
47
|
+
normalizePayload = typeof payload === "string" ? payload : JSON.stringify(payload);
|
|
48
|
+
return [4 /*yield*/, crypto.subtle.importKey("raw", encoder.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["sign"])];
|
|
49
|
+
case 1:
|
|
50
|
+
key = _a.sent();
|
|
51
|
+
return [4 /*yield*/, crypto.subtle.sign("HMAC", key, encoder.encode(normalizePayload))];
|
|
52
|
+
case 2:
|
|
53
|
+
sig = _a.sent();
|
|
54
|
+
hex = Array.from(new Uint8Array(sig))
|
|
55
|
+
.map(function (b) { return b.toString(16).padStart(2, "0"); })
|
|
56
|
+
.join("");
|
|
57
|
+
// Timing-safe equality not available in Node <20 cross-platform via subtle; compare length and constant-time loop
|
|
58
|
+
if (hex.length !== signatureHex.length)
|
|
59
|
+
return [2 /*return*/, false];
|
|
60
|
+
diff = 0;
|
|
61
|
+
for (i = 0; i < hex.length; i++) {
|
|
62
|
+
diff |= hex.charCodeAt(i) ^ signatureHex.charCodeAt(i);
|
|
63
|
+
}
|
|
64
|
+
return [2 /*return*/, diff === 0];
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import * as zod from "zod/v4";
|
|
2
|
+
export declare const CLASSIFICATION_CATEGORIES: readonly ["sexual", "violence", "gore", "self-harm", "harassment", "hate", "illicit", "child-exploitation"];
|
|
3
|
+
export declare const CLASSIFICATION_CATEGORY_SCHEMA: zod.ZodEnum<{
|
|
4
|
+
sexual: "sexual";
|
|
5
|
+
violence: "violence";
|
|
6
|
+
gore: "gore";
|
|
7
|
+
"self-harm": "self-harm";
|
|
8
|
+
harassment: "harassment";
|
|
9
|
+
hate: "hate";
|
|
10
|
+
illicit: "illicit";
|
|
11
|
+
"child-exploitation": "child-exploitation";
|
|
12
|
+
}>;
|
|
13
|
+
export type ClassificationCategory = zod.infer<typeof CLASSIFICATION_CATEGORY_SCHEMA>;
|
|
14
|
+
export declare const CLASSIFICATION_CATEGORY_INFO_SCHEMA: zod.ZodObject<{
|
|
15
|
+
category: zod.ZodEnum<{
|
|
16
|
+
sexual: "sexual";
|
|
17
|
+
violence: "violence";
|
|
18
|
+
gore: "gore";
|
|
19
|
+
"self-harm": "self-harm";
|
|
20
|
+
harassment: "harassment";
|
|
21
|
+
hate: "hate";
|
|
22
|
+
illicit: "illicit";
|
|
23
|
+
"child-exploitation": "child-exploitation";
|
|
24
|
+
}>;
|
|
25
|
+
displayName: zod.ZodPipe<zod.ZodString, zod.ZodTransform<string, string>>;
|
|
26
|
+
description: zod.ZodPipe<zod.ZodString, zod.ZodTransform<string, string>>;
|
|
27
|
+
textOnly: zod.ZodBoolean;
|
|
28
|
+
supportsImages: zod.ZodBoolean;
|
|
29
|
+
}, zod.z.core.$strip>;
|
|
30
|
+
export type ClassificationCategoryInfo = zod.infer<typeof CLASSIFICATION_CATEGORY_INFO_SCHEMA>;
|
|
31
|
+
export declare const CLASSIFICATION_CATEGORY_INFO: Record<ClassificationCategory, ClassificationCategoryInfo>;
|
|
32
|
+
export declare const getCategoryInfo: (category: ClassificationCategory) => ClassificationCategoryInfo;
|
|
33
|
+
export declare const LIKELIHOOD_SCHEMA: zod.ZodEnum<{
|
|
34
|
+
VERY_UNLIKELY: "VERY_UNLIKELY";
|
|
35
|
+
UNLIKELY: "UNLIKELY";
|
|
36
|
+
POSSIBLE: "POSSIBLE";
|
|
37
|
+
LIKELY: "LIKELY";
|
|
38
|
+
VERY_LIKELY: "VERY_LIKELY";
|
|
39
|
+
}>;
|
|
40
|
+
export type Likelihood = zod.infer<typeof LIKELIHOOD_SCHEMA>;
|
|
41
|
+
export declare const CLASSIFICATION_CATEGORY_REPORT_SCHEMA: zod.ZodObject<{
|
|
42
|
+
category: zod.ZodEnum<{
|
|
43
|
+
sexual: "sexual";
|
|
44
|
+
violence: "violence";
|
|
45
|
+
gore: "gore";
|
|
46
|
+
"self-harm": "self-harm";
|
|
47
|
+
harassment: "harassment";
|
|
48
|
+
hate: "hate";
|
|
49
|
+
illicit: "illicit";
|
|
50
|
+
"child-exploitation": "child-exploitation";
|
|
51
|
+
}>;
|
|
52
|
+
likelihood: zod.ZodEnum<{
|
|
53
|
+
VERY_UNLIKELY: "VERY_UNLIKELY";
|
|
54
|
+
UNLIKELY: "UNLIKELY";
|
|
55
|
+
POSSIBLE: "POSSIBLE";
|
|
56
|
+
LIKELY: "LIKELY";
|
|
57
|
+
VERY_LIKELY: "VERY_LIKELY";
|
|
58
|
+
}>;
|
|
59
|
+
score: zod.ZodNumber;
|
|
60
|
+
}, zod.z.core.$strip>;
|
|
61
|
+
export type ClassificationCategoryReport = zod.infer<typeof CLASSIFICATION_CATEGORY_REPORT_SCHEMA>;
|
|
62
|
+
export declare const CATEGORIES_SCHEMA: zod.ZodRecord<zod.ZodEnum<{
|
|
63
|
+
sexual: "sexual";
|
|
64
|
+
violence: "violence";
|
|
65
|
+
gore: "gore";
|
|
66
|
+
"self-harm": "self-harm";
|
|
67
|
+
harassment: "harassment";
|
|
68
|
+
hate: "hate";
|
|
69
|
+
illicit: "illicit";
|
|
70
|
+
"child-exploitation": "child-exploitation";
|
|
71
|
+
}> & zod.z.core.$partial, zod.ZodObject<{
|
|
72
|
+
category: zod.ZodEnum<{
|
|
73
|
+
sexual: "sexual";
|
|
74
|
+
violence: "violence";
|
|
75
|
+
gore: "gore";
|
|
76
|
+
"self-harm": "self-harm";
|
|
77
|
+
harassment: "harassment";
|
|
78
|
+
hate: "hate";
|
|
79
|
+
illicit: "illicit";
|
|
80
|
+
"child-exploitation": "child-exploitation";
|
|
81
|
+
}>;
|
|
82
|
+
likelihood: zod.ZodEnum<{
|
|
83
|
+
VERY_UNLIKELY: "VERY_UNLIKELY";
|
|
84
|
+
UNLIKELY: "UNLIKELY";
|
|
85
|
+
POSSIBLE: "POSSIBLE";
|
|
86
|
+
LIKELY: "LIKELY";
|
|
87
|
+
VERY_LIKELY: "VERY_LIKELY";
|
|
88
|
+
}>;
|
|
89
|
+
score: zod.ZodNumber;
|
|
90
|
+
}, zod.z.core.$strip>>;
|
|
91
|
+
export type ClassificationCategories = zod.infer<typeof CATEGORIES_SCHEMA>;
|
|
92
|
+
export declare const CLASSIFICATION_LABEL_SCHEMA: zod.ZodObject<{
|
|
93
|
+
description: zod.ZodPipe<zod.ZodString, zod.ZodTransform<string, string>>;
|
|
94
|
+
score: zod.ZodNumber;
|
|
95
|
+
topicalityScore: zod.ZodOptional<zod.ZodNullable<zod.ZodNumber>>;
|
|
96
|
+
likelihood: zod.ZodEnum<{
|
|
97
|
+
VERY_UNLIKELY: "VERY_UNLIKELY";
|
|
98
|
+
UNLIKELY: "UNLIKELY";
|
|
99
|
+
POSSIBLE: "POSSIBLE";
|
|
100
|
+
LIKELY: "LIKELY";
|
|
101
|
+
VERY_LIKELY: "VERY_LIKELY";
|
|
102
|
+
}>;
|
|
103
|
+
knowledgeBaseId: zod.ZodOptional<zod.ZodNullable<zod.ZodPipe<zod.ZodString, zod.ZodTransform<string, string>>>>;
|
|
104
|
+
}, zod.z.core.$strip>;
|
|
105
|
+
export type ClassificationLabel = zod.infer<typeof CLASSIFICATION_LABEL_SCHEMA>;
|
|
106
|
+
export declare const CLASSIFICATION_REPORT_SCHEMA: zod.ZodObject<{
|
|
107
|
+
processedAt: zod.ZodNumber;
|
|
108
|
+
categories: zod.ZodRecord<zod.ZodEnum<{
|
|
109
|
+
sexual: "sexual";
|
|
110
|
+
violence: "violence";
|
|
111
|
+
gore: "gore";
|
|
112
|
+
"self-harm": "self-harm";
|
|
113
|
+
harassment: "harassment";
|
|
114
|
+
hate: "hate";
|
|
115
|
+
illicit: "illicit";
|
|
116
|
+
"child-exploitation": "child-exploitation";
|
|
117
|
+
}> & zod.z.core.$partial, zod.ZodObject<{
|
|
118
|
+
category: zod.ZodEnum<{
|
|
119
|
+
sexual: "sexual";
|
|
120
|
+
violence: "violence";
|
|
121
|
+
gore: "gore";
|
|
122
|
+
"self-harm": "self-harm";
|
|
123
|
+
harassment: "harassment";
|
|
124
|
+
hate: "hate";
|
|
125
|
+
illicit: "illicit";
|
|
126
|
+
"child-exploitation": "child-exploitation";
|
|
127
|
+
}>;
|
|
128
|
+
likelihood: zod.ZodEnum<{
|
|
129
|
+
VERY_UNLIKELY: "VERY_UNLIKELY";
|
|
130
|
+
UNLIKELY: "UNLIKELY";
|
|
131
|
+
POSSIBLE: "POSSIBLE";
|
|
132
|
+
LIKELY: "LIKELY";
|
|
133
|
+
VERY_LIKELY: "VERY_LIKELY";
|
|
134
|
+
}>;
|
|
135
|
+
score: zod.ZodNumber;
|
|
136
|
+
}, zod.z.core.$strip>>;
|
|
137
|
+
flagged: zod.ZodBoolean;
|
|
138
|
+
score: zod.ZodNumber;
|
|
139
|
+
notes: zod.ZodOptional<zod.ZodNullable<zod.ZodPipe<zod.ZodString, zod.ZodTransform<string, string>>>>;
|
|
140
|
+
labels: zod.ZodOptional<zod.ZodNullable<zod.ZodArray<zod.ZodObject<{
|
|
141
|
+
description: zod.ZodPipe<zod.ZodString, zod.ZodTransform<string, string>>;
|
|
142
|
+
score: zod.ZodNumber;
|
|
143
|
+
topicalityScore: zod.ZodOptional<zod.ZodNullable<zod.ZodNumber>>;
|
|
144
|
+
likelihood: zod.ZodEnum<{
|
|
145
|
+
VERY_UNLIKELY: "VERY_UNLIKELY";
|
|
146
|
+
UNLIKELY: "UNLIKELY";
|
|
147
|
+
POSSIBLE: "POSSIBLE";
|
|
148
|
+
LIKELY: "LIKELY";
|
|
149
|
+
VERY_LIKELY: "VERY_LIKELY";
|
|
150
|
+
}>;
|
|
151
|
+
knowledgeBaseId: zod.ZodOptional<zod.ZodNullable<zod.ZodPipe<zod.ZodString, zod.ZodTransform<string, string>>>>;
|
|
152
|
+
}, zod.z.core.$strip>>>>;
|
|
153
|
+
}, zod.z.core.$strip>;
|
|
154
|
+
export type ClassificationReport = zod.infer<typeof CLASSIFICATION_REPORT_SCHEMA>;
|