@vettly/supabase 0.1.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 +266 -0
- package/dist/chunk-LTC3D75K.js +368 -0
- package/dist/edge-CPse0nUh.d.cts +137 -0
- package/dist/edge-CPse0nUh.d.ts +137 -0
- package/dist/edge.cjs +366 -0
- package/dist/edge.d.cts +1 -0
- package/dist/edge.d.ts +1 -0
- package/dist/edge.js +8 -0
- package/dist/index.cjs +402 -0
- package/dist/index.d.cts +31 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +22 -0
- package/package.json +57 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vettly Supabase Types
|
|
3
|
+
* Self-contained types for Deno Edge Function compatibility
|
|
4
|
+
*/
|
|
5
|
+
type ContentType = 'text' | 'image' | 'video';
|
|
6
|
+
type Action = 'block' | 'warn' | 'flag' | 'allow';
|
|
7
|
+
type Category = 'hate_speech' | 'harassment' | 'violence' | 'self_harm' | 'sexual' | 'spam' | 'profanity' | 'scam' | 'illegal';
|
|
8
|
+
interface CategoryScore {
|
|
9
|
+
category: Category;
|
|
10
|
+
score: number;
|
|
11
|
+
flagged: boolean;
|
|
12
|
+
}
|
|
13
|
+
interface ModerationResult {
|
|
14
|
+
decisionId: string;
|
|
15
|
+
safe: boolean;
|
|
16
|
+
flagged: boolean;
|
|
17
|
+
action: Action;
|
|
18
|
+
categories: CategoryScore[];
|
|
19
|
+
provider: string;
|
|
20
|
+
latency: number;
|
|
21
|
+
cost: number;
|
|
22
|
+
}
|
|
23
|
+
interface ModerationOptions {
|
|
24
|
+
/** Policy ID to use for moderation (defaults to 'default') */
|
|
25
|
+
policyId?: string;
|
|
26
|
+
/** Content type hint */
|
|
27
|
+
contentType?: ContentType;
|
|
28
|
+
/** Language hint (ISO 639-1) */
|
|
29
|
+
language?: string;
|
|
30
|
+
/** Request metadata */
|
|
31
|
+
metadata?: Record<string, unknown>;
|
|
32
|
+
/** Idempotency key */
|
|
33
|
+
requestId?: string;
|
|
34
|
+
}
|
|
35
|
+
interface VettlyConfig {
|
|
36
|
+
/** Your Vettly API key */
|
|
37
|
+
apiKey: string;
|
|
38
|
+
/** API base URL (defaults to https://api.vettly.dev) */
|
|
39
|
+
apiUrl?: string;
|
|
40
|
+
/** Request timeout in milliseconds (defaults to 30000) */
|
|
41
|
+
timeout?: number;
|
|
42
|
+
}
|
|
43
|
+
declare class VettlyError extends Error {
|
|
44
|
+
code: string;
|
|
45
|
+
statusCode: number;
|
|
46
|
+
responseBody?: unknown;
|
|
47
|
+
constructor(message: string, code: string, statusCode: number, responseBody?: unknown);
|
|
48
|
+
}
|
|
49
|
+
declare class VettlyAuthError extends VettlyError {
|
|
50
|
+
constructor(message?: string);
|
|
51
|
+
}
|
|
52
|
+
declare class VettlyRateLimitError extends VettlyError {
|
|
53
|
+
retryAfter?: number;
|
|
54
|
+
constructor(message?: string, retryAfter?: number);
|
|
55
|
+
}
|
|
56
|
+
declare class VettlyValidationError extends VettlyError {
|
|
57
|
+
errors: Array<{
|
|
58
|
+
field: string;
|
|
59
|
+
message: string;
|
|
60
|
+
}>;
|
|
61
|
+
constructor(message: string, errors: Array<{
|
|
62
|
+
field: string;
|
|
63
|
+
message: string;
|
|
64
|
+
}>);
|
|
65
|
+
}
|
|
66
|
+
interface EdgeHandlerOptions {
|
|
67
|
+
/** Policy ID to use for moderation */
|
|
68
|
+
policyId?: string;
|
|
69
|
+
/** Function to extract content from request (default: reads JSON body.content) */
|
|
70
|
+
getContent?: (req: Request) => Promise<string | null>;
|
|
71
|
+
/** Called when content is blocked */
|
|
72
|
+
onBlock?: (result: ModerationResult, req: Request) => Response | Promise<Response>;
|
|
73
|
+
/** Called when content is flagged (but allowed) */
|
|
74
|
+
onFlag?: (result: ModerationResult, req: Request) => void | Promise<void>;
|
|
75
|
+
/** Called when content is warned (but allowed) */
|
|
76
|
+
onWarn?: (result: ModerationResult, req: Request) => void | Promise<void>;
|
|
77
|
+
/** Called when moderation passes */
|
|
78
|
+
onAllow?: (result: ModerationResult, req: Request) => Response | Promise<Response>;
|
|
79
|
+
/** Called on error (default: returns 500) */
|
|
80
|
+
onError?: (error: Error, req: Request) => Response | Promise<Response>;
|
|
81
|
+
}
|
|
82
|
+
interface ModerationHandlerConfig extends EdgeHandlerOptions {
|
|
83
|
+
/** Your Vettly API key (or set VETTLY_API_KEY env var) */
|
|
84
|
+
apiKey?: string;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Vettly Edge Function Helpers
|
|
89
|
+
* Deno-compatible handlers for Supabase Edge Functions
|
|
90
|
+
*/
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Create an Edge Function handler that moderates incoming requests
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* // Simple one-liner
|
|
98
|
+
* Deno.serve(createModerationHandler({
|
|
99
|
+
* policyId: 'strict',
|
|
100
|
+
* onBlock: () => new Response('Content blocked', { status: 403 })
|
|
101
|
+
* }))
|
|
102
|
+
*
|
|
103
|
+
* // With custom content extraction
|
|
104
|
+
* Deno.serve(createModerationHandler({
|
|
105
|
+
* getContent: async (req) => {
|
|
106
|
+
* const { message } = await req.json()
|
|
107
|
+
* return message
|
|
108
|
+
* },
|
|
109
|
+
* onBlock: (result) => Response.json({
|
|
110
|
+
* error: 'blocked',
|
|
111
|
+
* categories: result.categories.filter(c => c.flagged)
|
|
112
|
+
* }, { status: 403 })
|
|
113
|
+
* }))
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
declare function createModerationHandler(config?: ModerationHandlerConfig): (req: Request) => Promise<Response>;
|
|
117
|
+
/**
|
|
118
|
+
* Create a middleware-style handler that moderates content before passing to your handler
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```typescript
|
|
122
|
+
* const handler = withModeration(
|
|
123
|
+
* async (req, moderationResult) => {
|
|
124
|
+
* // Content is already moderated, safe to process
|
|
125
|
+
* const { content, userId } = await req.json()
|
|
126
|
+
* // Insert into database...
|
|
127
|
+
* return Response.json({ success: true })
|
|
128
|
+
* },
|
|
129
|
+
* { policyId: 'user-content' }
|
|
130
|
+
* )
|
|
131
|
+
*
|
|
132
|
+
* Deno.serve(handler)
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
declare function withModeration(handler: (req: Request, result: ModerationResult) => Promise<Response>, config?: ModerationHandlerConfig): (req: Request) => Promise<Response>;
|
|
136
|
+
|
|
137
|
+
export { type Action as A, type ContentType as C, type EdgeHandlerOptions as E, type ModerationOptions as M, type VettlyConfig as V, type ModerationResult as a, type Category as b, createModerationHandler as c, type CategoryScore as d, type ModerationHandlerConfig as e, VettlyError as f, VettlyAuthError as g, VettlyRateLimitError as h, VettlyValidationError as i, withModeration as w };
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vettly Supabase Types
|
|
3
|
+
* Self-contained types for Deno Edge Function compatibility
|
|
4
|
+
*/
|
|
5
|
+
type ContentType = 'text' | 'image' | 'video';
|
|
6
|
+
type Action = 'block' | 'warn' | 'flag' | 'allow';
|
|
7
|
+
type Category = 'hate_speech' | 'harassment' | 'violence' | 'self_harm' | 'sexual' | 'spam' | 'profanity' | 'scam' | 'illegal';
|
|
8
|
+
interface CategoryScore {
|
|
9
|
+
category: Category;
|
|
10
|
+
score: number;
|
|
11
|
+
flagged: boolean;
|
|
12
|
+
}
|
|
13
|
+
interface ModerationResult {
|
|
14
|
+
decisionId: string;
|
|
15
|
+
safe: boolean;
|
|
16
|
+
flagged: boolean;
|
|
17
|
+
action: Action;
|
|
18
|
+
categories: CategoryScore[];
|
|
19
|
+
provider: string;
|
|
20
|
+
latency: number;
|
|
21
|
+
cost: number;
|
|
22
|
+
}
|
|
23
|
+
interface ModerationOptions {
|
|
24
|
+
/** Policy ID to use for moderation (defaults to 'default') */
|
|
25
|
+
policyId?: string;
|
|
26
|
+
/** Content type hint */
|
|
27
|
+
contentType?: ContentType;
|
|
28
|
+
/** Language hint (ISO 639-1) */
|
|
29
|
+
language?: string;
|
|
30
|
+
/** Request metadata */
|
|
31
|
+
metadata?: Record<string, unknown>;
|
|
32
|
+
/** Idempotency key */
|
|
33
|
+
requestId?: string;
|
|
34
|
+
}
|
|
35
|
+
interface VettlyConfig {
|
|
36
|
+
/** Your Vettly API key */
|
|
37
|
+
apiKey: string;
|
|
38
|
+
/** API base URL (defaults to https://api.vettly.dev) */
|
|
39
|
+
apiUrl?: string;
|
|
40
|
+
/** Request timeout in milliseconds (defaults to 30000) */
|
|
41
|
+
timeout?: number;
|
|
42
|
+
}
|
|
43
|
+
declare class VettlyError extends Error {
|
|
44
|
+
code: string;
|
|
45
|
+
statusCode: number;
|
|
46
|
+
responseBody?: unknown;
|
|
47
|
+
constructor(message: string, code: string, statusCode: number, responseBody?: unknown);
|
|
48
|
+
}
|
|
49
|
+
declare class VettlyAuthError extends VettlyError {
|
|
50
|
+
constructor(message?: string);
|
|
51
|
+
}
|
|
52
|
+
declare class VettlyRateLimitError extends VettlyError {
|
|
53
|
+
retryAfter?: number;
|
|
54
|
+
constructor(message?: string, retryAfter?: number);
|
|
55
|
+
}
|
|
56
|
+
declare class VettlyValidationError extends VettlyError {
|
|
57
|
+
errors: Array<{
|
|
58
|
+
field: string;
|
|
59
|
+
message: string;
|
|
60
|
+
}>;
|
|
61
|
+
constructor(message: string, errors: Array<{
|
|
62
|
+
field: string;
|
|
63
|
+
message: string;
|
|
64
|
+
}>);
|
|
65
|
+
}
|
|
66
|
+
interface EdgeHandlerOptions {
|
|
67
|
+
/** Policy ID to use for moderation */
|
|
68
|
+
policyId?: string;
|
|
69
|
+
/** Function to extract content from request (default: reads JSON body.content) */
|
|
70
|
+
getContent?: (req: Request) => Promise<string | null>;
|
|
71
|
+
/** Called when content is blocked */
|
|
72
|
+
onBlock?: (result: ModerationResult, req: Request) => Response | Promise<Response>;
|
|
73
|
+
/** Called when content is flagged (but allowed) */
|
|
74
|
+
onFlag?: (result: ModerationResult, req: Request) => void | Promise<void>;
|
|
75
|
+
/** Called when content is warned (but allowed) */
|
|
76
|
+
onWarn?: (result: ModerationResult, req: Request) => void | Promise<void>;
|
|
77
|
+
/** Called when moderation passes */
|
|
78
|
+
onAllow?: (result: ModerationResult, req: Request) => Response | Promise<Response>;
|
|
79
|
+
/** Called on error (default: returns 500) */
|
|
80
|
+
onError?: (error: Error, req: Request) => Response | Promise<Response>;
|
|
81
|
+
}
|
|
82
|
+
interface ModerationHandlerConfig extends EdgeHandlerOptions {
|
|
83
|
+
/** Your Vettly API key (or set VETTLY_API_KEY env var) */
|
|
84
|
+
apiKey?: string;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Vettly Edge Function Helpers
|
|
89
|
+
* Deno-compatible handlers for Supabase Edge Functions
|
|
90
|
+
*/
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Create an Edge Function handler that moderates incoming requests
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* // Simple one-liner
|
|
98
|
+
* Deno.serve(createModerationHandler({
|
|
99
|
+
* policyId: 'strict',
|
|
100
|
+
* onBlock: () => new Response('Content blocked', { status: 403 })
|
|
101
|
+
* }))
|
|
102
|
+
*
|
|
103
|
+
* // With custom content extraction
|
|
104
|
+
* Deno.serve(createModerationHandler({
|
|
105
|
+
* getContent: async (req) => {
|
|
106
|
+
* const { message } = await req.json()
|
|
107
|
+
* return message
|
|
108
|
+
* },
|
|
109
|
+
* onBlock: (result) => Response.json({
|
|
110
|
+
* error: 'blocked',
|
|
111
|
+
* categories: result.categories.filter(c => c.flagged)
|
|
112
|
+
* }, { status: 403 })
|
|
113
|
+
* }))
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
declare function createModerationHandler(config?: ModerationHandlerConfig): (req: Request) => Promise<Response>;
|
|
117
|
+
/**
|
|
118
|
+
* Create a middleware-style handler that moderates content before passing to your handler
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```typescript
|
|
122
|
+
* const handler = withModeration(
|
|
123
|
+
* async (req, moderationResult) => {
|
|
124
|
+
* // Content is already moderated, safe to process
|
|
125
|
+
* const { content, userId } = await req.json()
|
|
126
|
+
* // Insert into database...
|
|
127
|
+
* return Response.json({ success: true })
|
|
128
|
+
* },
|
|
129
|
+
* { policyId: 'user-content' }
|
|
130
|
+
* )
|
|
131
|
+
*
|
|
132
|
+
* Deno.serve(handler)
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
declare function withModeration(handler: (req: Request, result: ModerationResult) => Promise<Response>, config?: ModerationHandlerConfig): (req: Request) => Promise<Response>;
|
|
136
|
+
|
|
137
|
+
export { type Action as A, type ContentType as C, type EdgeHandlerOptions as E, type ModerationOptions as M, type VettlyConfig as V, type ModerationResult as a, type Category as b, createModerationHandler as c, type CategoryScore as d, type ModerationHandlerConfig as e, VettlyError as f, VettlyAuthError as g, VettlyRateLimitError as h, VettlyValidationError as i, withModeration as w };
|
package/dist/edge.cjs
ADDED
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/edge.ts
|
|
21
|
+
var edge_exports = {};
|
|
22
|
+
__export(edge_exports, {
|
|
23
|
+
createModerationHandler: () => createModerationHandler,
|
|
24
|
+
withModeration: () => withModeration
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(edge_exports);
|
|
27
|
+
|
|
28
|
+
// src/types.ts
|
|
29
|
+
var VettlyError = class extends Error {
|
|
30
|
+
code;
|
|
31
|
+
statusCode;
|
|
32
|
+
responseBody;
|
|
33
|
+
constructor(message, code, statusCode, responseBody) {
|
|
34
|
+
super(message);
|
|
35
|
+
this.name = "VettlyError";
|
|
36
|
+
this.code = code;
|
|
37
|
+
this.statusCode = statusCode;
|
|
38
|
+
this.responseBody = responseBody;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
var VettlyAuthError = class extends VettlyError {
|
|
42
|
+
constructor(message = "Invalid API key") {
|
|
43
|
+
super(message, "AUTH_ERROR", 401);
|
|
44
|
+
this.name = "VettlyAuthError";
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
var VettlyRateLimitError = class extends VettlyError {
|
|
48
|
+
retryAfter;
|
|
49
|
+
constructor(message = "Rate limit exceeded", retryAfter) {
|
|
50
|
+
super(message, "RATE_LIMIT", 429);
|
|
51
|
+
this.name = "VettlyRateLimitError";
|
|
52
|
+
this.retryAfter = retryAfter;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
var VettlyValidationError = class extends VettlyError {
|
|
56
|
+
errors;
|
|
57
|
+
constructor(message, errors) {
|
|
58
|
+
super(message, "VALIDATION_ERROR", 422);
|
|
59
|
+
this.name = "VettlyValidationError";
|
|
60
|
+
this.errors = errors;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// src/client.ts
|
|
65
|
+
var DEFAULT_API_URL = "https://api.vettly.dev";
|
|
66
|
+
var DEFAULT_TIMEOUT = 3e4;
|
|
67
|
+
var SDK_VERSION = "0.1.0";
|
|
68
|
+
function createClient(config) {
|
|
69
|
+
const apiUrl = config.apiUrl ?? DEFAULT_API_URL;
|
|
70
|
+
const timeout = config.timeout ?? DEFAULT_TIMEOUT;
|
|
71
|
+
const apiKey = config.apiKey;
|
|
72
|
+
if (!apiKey) {
|
|
73
|
+
throw new VettlyError(
|
|
74
|
+
"API key is required",
|
|
75
|
+
"CONFIG_ERROR",
|
|
76
|
+
500
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
async function request(method, path, body) {
|
|
80
|
+
const controller = new AbortController();
|
|
81
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
82
|
+
try {
|
|
83
|
+
const response = await fetch(`${apiUrl}${path}`, {
|
|
84
|
+
method,
|
|
85
|
+
headers: {
|
|
86
|
+
"Content-Type": "application/json",
|
|
87
|
+
Authorization: `Bearer ${apiKey}`,
|
|
88
|
+
"User-Agent": `@vettly/supabase/${SDK_VERSION}`,
|
|
89
|
+
"X-Vettly-SDK-Version": SDK_VERSION
|
|
90
|
+
},
|
|
91
|
+
body: body ? JSON.stringify(body) : void 0,
|
|
92
|
+
signal: controller.signal
|
|
93
|
+
});
|
|
94
|
+
clearTimeout(timeoutId);
|
|
95
|
+
if (!response.ok) {
|
|
96
|
+
const errorBody = await response.json().catch(() => ({}));
|
|
97
|
+
switch (response.status) {
|
|
98
|
+
case 401:
|
|
99
|
+
throw new VettlyAuthError(errorBody.error || "Invalid API key");
|
|
100
|
+
case 429: {
|
|
101
|
+
const retryAfter = response.headers.get("Retry-After");
|
|
102
|
+
throw new VettlyRateLimitError(
|
|
103
|
+
errorBody.error || "Rate limit exceeded",
|
|
104
|
+
retryAfter ? parseInt(retryAfter, 10) : void 0
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
case 422:
|
|
108
|
+
throw new VettlyValidationError(
|
|
109
|
+
errorBody.error || "Validation error",
|
|
110
|
+
Array.isArray(errorBody.details) ? errorBody.details : []
|
|
111
|
+
);
|
|
112
|
+
default:
|
|
113
|
+
throw new VettlyError(
|
|
114
|
+
errorBody.error || `Request failed with status ${response.status}`,
|
|
115
|
+
errorBody.code || "API_ERROR",
|
|
116
|
+
response.status,
|
|
117
|
+
errorBody
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return response.json();
|
|
122
|
+
} catch (error) {
|
|
123
|
+
clearTimeout(timeoutId);
|
|
124
|
+
if (error instanceof VettlyError) {
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
128
|
+
throw new VettlyError(
|
|
129
|
+
`Request timed out after ${timeout}ms`,
|
|
130
|
+
"TIMEOUT",
|
|
131
|
+
408
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
throw new VettlyError(
|
|
135
|
+
error instanceof Error ? error.message : "Unknown error",
|
|
136
|
+
"NETWORK_ERROR",
|
|
137
|
+
500
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
/**
|
|
143
|
+
* Moderate text content
|
|
144
|
+
*/
|
|
145
|
+
async check(content, options = {}) {
|
|
146
|
+
return request("POST", "/v1/moderate", {
|
|
147
|
+
content,
|
|
148
|
+
policyId: options.policyId ?? "default",
|
|
149
|
+
contentType: options.contentType ?? "text",
|
|
150
|
+
language: options.language,
|
|
151
|
+
metadata: options.metadata,
|
|
152
|
+
requestId: options.requestId
|
|
153
|
+
});
|
|
154
|
+
},
|
|
155
|
+
/**
|
|
156
|
+
* Moderate an image
|
|
157
|
+
*/
|
|
158
|
+
async checkImage(imageUrl, options = {}) {
|
|
159
|
+
return request("POST", "/v1/moderate", {
|
|
160
|
+
content: imageUrl,
|
|
161
|
+
policyId: options.policyId ?? "default",
|
|
162
|
+
contentType: "image",
|
|
163
|
+
metadata: options.metadata,
|
|
164
|
+
requestId: options.requestId
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// src/edge.ts
|
|
171
|
+
async function defaultGetContent(req) {
|
|
172
|
+
try {
|
|
173
|
+
const body = await req.json();
|
|
174
|
+
return typeof body.content === "string" ? body.content : null;
|
|
175
|
+
} catch {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
function createModerationHandler(config = {}) {
|
|
180
|
+
const {
|
|
181
|
+
apiKey,
|
|
182
|
+
policyId = "default",
|
|
183
|
+
getContent = defaultGetContent,
|
|
184
|
+
onBlock,
|
|
185
|
+
onFlag,
|
|
186
|
+
onWarn,
|
|
187
|
+
onAllow,
|
|
188
|
+
onError
|
|
189
|
+
} = config;
|
|
190
|
+
const resolvedApiKey = apiKey ?? (typeof Deno !== "undefined" ? Deno.env.get("VETTLY_API_KEY") : typeof process !== "undefined" ? process.env.VETTLY_API_KEY : void 0);
|
|
191
|
+
if (!resolvedApiKey) {
|
|
192
|
+
throw new VettlyError(
|
|
193
|
+
"API key is required. Set VETTLY_API_KEY env var or pass apiKey in config.",
|
|
194
|
+
"CONFIG_ERROR",
|
|
195
|
+
500
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
const client = createClient({ apiKey: resolvedApiKey });
|
|
199
|
+
return async (req) => {
|
|
200
|
+
if (req.method === "OPTIONS") {
|
|
201
|
+
return new Response(null, {
|
|
202
|
+
status: 204,
|
|
203
|
+
headers: {
|
|
204
|
+
"Access-Control-Allow-Origin": "*",
|
|
205
|
+
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
|
206
|
+
"Access-Control-Allow-Headers": "Content-Type, Authorization"
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
if (req.method !== "POST") {
|
|
211
|
+
return new Response(
|
|
212
|
+
JSON.stringify({ error: "Method not allowed" }),
|
|
213
|
+
{
|
|
214
|
+
status: 405,
|
|
215
|
+
headers: { "Content-Type": "application/json" }
|
|
216
|
+
}
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
try {
|
|
220
|
+
const content = await getContent(req);
|
|
221
|
+
if (!content) {
|
|
222
|
+
return new Response(
|
|
223
|
+
JSON.stringify({ error: "No content to moderate" }),
|
|
224
|
+
{
|
|
225
|
+
status: 400,
|
|
226
|
+
headers: { "Content-Type": "application/json" }
|
|
227
|
+
}
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
const result = await client.check(content, { policyId });
|
|
231
|
+
if (result.action === "block") {
|
|
232
|
+
if (onBlock) {
|
|
233
|
+
return onBlock(result, req);
|
|
234
|
+
}
|
|
235
|
+
return new Response(
|
|
236
|
+
JSON.stringify({
|
|
237
|
+
error: "Content blocked",
|
|
238
|
+
categories: result.categories.filter((c) => c.flagged)
|
|
239
|
+
}),
|
|
240
|
+
{
|
|
241
|
+
status: 403,
|
|
242
|
+
headers: { "Content-Type": "application/json" }
|
|
243
|
+
}
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
if (result.action === "flag" && onFlag) {
|
|
247
|
+
await onFlag(result, req);
|
|
248
|
+
}
|
|
249
|
+
if (result.action === "warn" && onWarn) {
|
|
250
|
+
await onWarn(result, req);
|
|
251
|
+
}
|
|
252
|
+
if (onAllow) {
|
|
253
|
+
return onAllow(result, req);
|
|
254
|
+
}
|
|
255
|
+
return new Response(
|
|
256
|
+
JSON.stringify({
|
|
257
|
+
success: true,
|
|
258
|
+
moderation: {
|
|
259
|
+
safe: result.safe,
|
|
260
|
+
action: result.action,
|
|
261
|
+
decisionId: result.decisionId
|
|
262
|
+
}
|
|
263
|
+
}),
|
|
264
|
+
{
|
|
265
|
+
status: 200,
|
|
266
|
+
headers: { "Content-Type": "application/json" }
|
|
267
|
+
}
|
|
268
|
+
);
|
|
269
|
+
} catch (error) {
|
|
270
|
+
if (onError) {
|
|
271
|
+
return onError(error instanceof Error ? error : new Error(String(error)), req);
|
|
272
|
+
}
|
|
273
|
+
console.error("[Vettly] Moderation error:", error);
|
|
274
|
+
return new Response(
|
|
275
|
+
JSON.stringify({
|
|
276
|
+
error: "Moderation service error",
|
|
277
|
+
message: error instanceof Error ? error.message : "Unknown error"
|
|
278
|
+
}),
|
|
279
|
+
{
|
|
280
|
+
status: 500,
|
|
281
|
+
headers: { "Content-Type": "application/json" }
|
|
282
|
+
}
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
function withModeration(handler, config = {}) {
|
|
288
|
+
const {
|
|
289
|
+
apiKey,
|
|
290
|
+
policyId = "default",
|
|
291
|
+
getContent = defaultGetContent,
|
|
292
|
+
onBlock,
|
|
293
|
+
onError
|
|
294
|
+
} = config;
|
|
295
|
+
const resolvedApiKey = apiKey ?? (typeof Deno !== "undefined" ? Deno.env.get("VETTLY_API_KEY") : typeof process !== "undefined" ? process.env.VETTLY_API_KEY : void 0);
|
|
296
|
+
if (!resolvedApiKey) {
|
|
297
|
+
throw new VettlyError(
|
|
298
|
+
"API key is required. Set VETTLY_API_KEY env var or pass apiKey in config.",
|
|
299
|
+
"CONFIG_ERROR",
|
|
300
|
+
500
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
const client = createClient({ apiKey: resolvedApiKey });
|
|
304
|
+
return async (req) => {
|
|
305
|
+
if (req.method === "OPTIONS") {
|
|
306
|
+
return new Response(null, {
|
|
307
|
+
status: 204,
|
|
308
|
+
headers: {
|
|
309
|
+
"Access-Control-Allow-Origin": "*",
|
|
310
|
+
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
|
311
|
+
"Access-Control-Allow-Headers": "Content-Type, Authorization"
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
try {
|
|
316
|
+
const clonedReq = req.clone();
|
|
317
|
+
const content = await getContent(clonedReq);
|
|
318
|
+
if (!content) {
|
|
319
|
+
return new Response(
|
|
320
|
+
JSON.stringify({ error: "No content to moderate" }),
|
|
321
|
+
{
|
|
322
|
+
status: 400,
|
|
323
|
+
headers: { "Content-Type": "application/json" }
|
|
324
|
+
}
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
const result = await client.check(content, { policyId });
|
|
328
|
+
if (result.action === "block") {
|
|
329
|
+
if (onBlock) {
|
|
330
|
+
return onBlock(result, req);
|
|
331
|
+
}
|
|
332
|
+
return new Response(
|
|
333
|
+
JSON.stringify({
|
|
334
|
+
error: "Content blocked",
|
|
335
|
+
categories: result.categories.filter((c) => c.flagged)
|
|
336
|
+
}),
|
|
337
|
+
{
|
|
338
|
+
status: 403,
|
|
339
|
+
headers: { "Content-Type": "application/json" }
|
|
340
|
+
}
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
return handler(req, result);
|
|
344
|
+
} catch (error) {
|
|
345
|
+
if (onError) {
|
|
346
|
+
return onError(error instanceof Error ? error : new Error(String(error)), req);
|
|
347
|
+
}
|
|
348
|
+
console.error("[Vettly] Moderation error:", error);
|
|
349
|
+
return new Response(
|
|
350
|
+
JSON.stringify({
|
|
351
|
+
error: "Moderation service error",
|
|
352
|
+
message: error instanceof Error ? error.message : "Unknown error"
|
|
353
|
+
}),
|
|
354
|
+
{
|
|
355
|
+
status: 500,
|
|
356
|
+
headers: { "Content-Type": "application/json" }
|
|
357
|
+
}
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
363
|
+
0 && (module.exports = {
|
|
364
|
+
createModerationHandler,
|
|
365
|
+
withModeration
|
|
366
|
+
});
|
package/dist/edge.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { E as EdgeHandlerOptions, e as ModerationHandlerConfig, a as ModerationResult, c as createModerationHandler, w as withModeration } from './edge-CPse0nUh.cjs';
|
package/dist/edge.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { E as EdgeHandlerOptions, e as ModerationHandlerConfig, a as ModerationResult, c as createModerationHandler, w as withModeration } from './edge-CPse0nUh.js';
|