@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/dist/index.cjs ADDED
@@ -0,0 +1,402 @@
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/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ VettlyAuthError: () => VettlyAuthError,
24
+ VettlyError: () => VettlyError,
25
+ VettlyRateLimitError: () => VettlyRateLimitError,
26
+ VettlyValidationError: () => VettlyValidationError,
27
+ createClient: () => createClient,
28
+ createModerationHandler: () => createModerationHandler,
29
+ moderate: () => moderate,
30
+ moderateImage: () => moderateImage,
31
+ withModeration: () => withModeration
32
+ });
33
+ module.exports = __toCommonJS(index_exports);
34
+
35
+ // src/types.ts
36
+ var VettlyError = class extends Error {
37
+ code;
38
+ statusCode;
39
+ responseBody;
40
+ constructor(message, code, statusCode, responseBody) {
41
+ super(message);
42
+ this.name = "VettlyError";
43
+ this.code = code;
44
+ this.statusCode = statusCode;
45
+ this.responseBody = responseBody;
46
+ }
47
+ };
48
+ var VettlyAuthError = class extends VettlyError {
49
+ constructor(message = "Invalid API key") {
50
+ super(message, "AUTH_ERROR", 401);
51
+ this.name = "VettlyAuthError";
52
+ }
53
+ };
54
+ var VettlyRateLimitError = class extends VettlyError {
55
+ retryAfter;
56
+ constructor(message = "Rate limit exceeded", retryAfter) {
57
+ super(message, "RATE_LIMIT", 429);
58
+ this.name = "VettlyRateLimitError";
59
+ this.retryAfter = retryAfter;
60
+ }
61
+ };
62
+ var VettlyValidationError = class extends VettlyError {
63
+ errors;
64
+ constructor(message, errors) {
65
+ super(message, "VALIDATION_ERROR", 422);
66
+ this.name = "VettlyValidationError";
67
+ this.errors = errors;
68
+ }
69
+ };
70
+
71
+ // src/client.ts
72
+ var DEFAULT_API_URL = "https://api.vettly.dev";
73
+ var DEFAULT_TIMEOUT = 3e4;
74
+ var SDK_VERSION = "0.1.0";
75
+ function createClient(config) {
76
+ const apiUrl = config.apiUrl ?? DEFAULT_API_URL;
77
+ const timeout = config.timeout ?? DEFAULT_TIMEOUT;
78
+ const apiKey = config.apiKey;
79
+ if (!apiKey) {
80
+ throw new VettlyError(
81
+ "API key is required",
82
+ "CONFIG_ERROR",
83
+ 500
84
+ );
85
+ }
86
+ async function request(method, path, body) {
87
+ const controller = new AbortController();
88
+ const timeoutId = setTimeout(() => controller.abort(), timeout);
89
+ try {
90
+ const response = await fetch(`${apiUrl}${path}`, {
91
+ method,
92
+ headers: {
93
+ "Content-Type": "application/json",
94
+ Authorization: `Bearer ${apiKey}`,
95
+ "User-Agent": `@vettly/supabase/${SDK_VERSION}`,
96
+ "X-Vettly-SDK-Version": SDK_VERSION
97
+ },
98
+ body: body ? JSON.stringify(body) : void 0,
99
+ signal: controller.signal
100
+ });
101
+ clearTimeout(timeoutId);
102
+ if (!response.ok) {
103
+ const errorBody = await response.json().catch(() => ({}));
104
+ switch (response.status) {
105
+ case 401:
106
+ throw new VettlyAuthError(errorBody.error || "Invalid API key");
107
+ case 429: {
108
+ const retryAfter = response.headers.get("Retry-After");
109
+ throw new VettlyRateLimitError(
110
+ errorBody.error || "Rate limit exceeded",
111
+ retryAfter ? parseInt(retryAfter, 10) : void 0
112
+ );
113
+ }
114
+ case 422:
115
+ throw new VettlyValidationError(
116
+ errorBody.error || "Validation error",
117
+ Array.isArray(errorBody.details) ? errorBody.details : []
118
+ );
119
+ default:
120
+ throw new VettlyError(
121
+ errorBody.error || `Request failed with status ${response.status}`,
122
+ errorBody.code || "API_ERROR",
123
+ response.status,
124
+ errorBody
125
+ );
126
+ }
127
+ }
128
+ return response.json();
129
+ } catch (error) {
130
+ clearTimeout(timeoutId);
131
+ if (error instanceof VettlyError) {
132
+ throw error;
133
+ }
134
+ if (error instanceof Error && error.name === "AbortError") {
135
+ throw new VettlyError(
136
+ `Request timed out after ${timeout}ms`,
137
+ "TIMEOUT",
138
+ 408
139
+ );
140
+ }
141
+ throw new VettlyError(
142
+ error instanceof Error ? error.message : "Unknown error",
143
+ "NETWORK_ERROR",
144
+ 500
145
+ );
146
+ }
147
+ }
148
+ return {
149
+ /**
150
+ * Moderate text content
151
+ */
152
+ async check(content, options = {}) {
153
+ return request("POST", "/v1/moderate", {
154
+ content,
155
+ policyId: options.policyId ?? "default",
156
+ contentType: options.contentType ?? "text",
157
+ language: options.language,
158
+ metadata: options.metadata,
159
+ requestId: options.requestId
160
+ });
161
+ },
162
+ /**
163
+ * Moderate an image
164
+ */
165
+ async checkImage(imageUrl, options = {}) {
166
+ return request("POST", "/v1/moderate", {
167
+ content: imageUrl,
168
+ policyId: options.policyId ?? "default",
169
+ contentType: "image",
170
+ metadata: options.metadata,
171
+ requestId: options.requestId
172
+ });
173
+ }
174
+ };
175
+ }
176
+ var defaultClient = null;
177
+ function getDefaultClient() {
178
+ if (defaultClient) {
179
+ return defaultClient;
180
+ }
181
+ const apiKey = typeof Deno !== "undefined" ? Deno.env.get("VETTLY_API_KEY") : typeof process !== "undefined" ? process.env.VETTLY_API_KEY : void 0;
182
+ if (!apiKey) {
183
+ throw new VettlyError(
184
+ "VETTLY_API_KEY environment variable is not set",
185
+ "CONFIG_ERROR",
186
+ 500
187
+ );
188
+ }
189
+ defaultClient = createClient({ apiKey });
190
+ return defaultClient;
191
+ }
192
+ async function moderate(content, options = {}) {
193
+ return getDefaultClient().check(content, options);
194
+ }
195
+ async function moderateImage(imageUrl, options = {}) {
196
+ return getDefaultClient().checkImage(imageUrl, options);
197
+ }
198
+
199
+ // src/edge.ts
200
+ async function defaultGetContent(req) {
201
+ try {
202
+ const body = await req.json();
203
+ return typeof body.content === "string" ? body.content : null;
204
+ } catch {
205
+ return null;
206
+ }
207
+ }
208
+ function createModerationHandler(config = {}) {
209
+ const {
210
+ apiKey,
211
+ policyId = "default",
212
+ getContent = defaultGetContent,
213
+ onBlock,
214
+ onFlag,
215
+ onWarn,
216
+ onAllow,
217
+ onError
218
+ } = config;
219
+ const resolvedApiKey = apiKey ?? (typeof Deno !== "undefined" ? Deno.env.get("VETTLY_API_KEY") : typeof process !== "undefined" ? process.env.VETTLY_API_KEY : void 0);
220
+ if (!resolvedApiKey) {
221
+ throw new VettlyError(
222
+ "API key is required. Set VETTLY_API_KEY env var or pass apiKey in config.",
223
+ "CONFIG_ERROR",
224
+ 500
225
+ );
226
+ }
227
+ const client = createClient({ apiKey: resolvedApiKey });
228
+ return async (req) => {
229
+ if (req.method === "OPTIONS") {
230
+ return new Response(null, {
231
+ status: 204,
232
+ headers: {
233
+ "Access-Control-Allow-Origin": "*",
234
+ "Access-Control-Allow-Methods": "POST, OPTIONS",
235
+ "Access-Control-Allow-Headers": "Content-Type, Authorization"
236
+ }
237
+ });
238
+ }
239
+ if (req.method !== "POST") {
240
+ return new Response(
241
+ JSON.stringify({ error: "Method not allowed" }),
242
+ {
243
+ status: 405,
244
+ headers: { "Content-Type": "application/json" }
245
+ }
246
+ );
247
+ }
248
+ try {
249
+ const content = await getContent(req);
250
+ if (!content) {
251
+ return new Response(
252
+ JSON.stringify({ error: "No content to moderate" }),
253
+ {
254
+ status: 400,
255
+ headers: { "Content-Type": "application/json" }
256
+ }
257
+ );
258
+ }
259
+ const result = await client.check(content, { policyId });
260
+ if (result.action === "block") {
261
+ if (onBlock) {
262
+ return onBlock(result, req);
263
+ }
264
+ return new Response(
265
+ JSON.stringify({
266
+ error: "Content blocked",
267
+ categories: result.categories.filter((c) => c.flagged)
268
+ }),
269
+ {
270
+ status: 403,
271
+ headers: { "Content-Type": "application/json" }
272
+ }
273
+ );
274
+ }
275
+ if (result.action === "flag" && onFlag) {
276
+ await onFlag(result, req);
277
+ }
278
+ if (result.action === "warn" && onWarn) {
279
+ await onWarn(result, req);
280
+ }
281
+ if (onAllow) {
282
+ return onAllow(result, req);
283
+ }
284
+ return new Response(
285
+ JSON.stringify({
286
+ success: true,
287
+ moderation: {
288
+ safe: result.safe,
289
+ action: result.action,
290
+ decisionId: result.decisionId
291
+ }
292
+ }),
293
+ {
294
+ status: 200,
295
+ headers: { "Content-Type": "application/json" }
296
+ }
297
+ );
298
+ } catch (error) {
299
+ if (onError) {
300
+ return onError(error instanceof Error ? error : new Error(String(error)), req);
301
+ }
302
+ console.error("[Vettly] Moderation error:", error);
303
+ return new Response(
304
+ JSON.stringify({
305
+ error: "Moderation service error",
306
+ message: error instanceof Error ? error.message : "Unknown error"
307
+ }),
308
+ {
309
+ status: 500,
310
+ headers: { "Content-Type": "application/json" }
311
+ }
312
+ );
313
+ }
314
+ };
315
+ }
316
+ function withModeration(handler, config = {}) {
317
+ const {
318
+ apiKey,
319
+ policyId = "default",
320
+ getContent = defaultGetContent,
321
+ onBlock,
322
+ onError
323
+ } = config;
324
+ const resolvedApiKey = apiKey ?? (typeof Deno !== "undefined" ? Deno.env.get("VETTLY_API_KEY") : typeof process !== "undefined" ? process.env.VETTLY_API_KEY : void 0);
325
+ if (!resolvedApiKey) {
326
+ throw new VettlyError(
327
+ "API key is required. Set VETTLY_API_KEY env var or pass apiKey in config.",
328
+ "CONFIG_ERROR",
329
+ 500
330
+ );
331
+ }
332
+ const client = createClient({ apiKey: resolvedApiKey });
333
+ return async (req) => {
334
+ if (req.method === "OPTIONS") {
335
+ return new Response(null, {
336
+ status: 204,
337
+ headers: {
338
+ "Access-Control-Allow-Origin": "*",
339
+ "Access-Control-Allow-Methods": "POST, OPTIONS",
340
+ "Access-Control-Allow-Headers": "Content-Type, Authorization"
341
+ }
342
+ });
343
+ }
344
+ try {
345
+ const clonedReq = req.clone();
346
+ const content = await getContent(clonedReq);
347
+ if (!content) {
348
+ return new Response(
349
+ JSON.stringify({ error: "No content to moderate" }),
350
+ {
351
+ status: 400,
352
+ headers: { "Content-Type": "application/json" }
353
+ }
354
+ );
355
+ }
356
+ const result = await client.check(content, { policyId });
357
+ if (result.action === "block") {
358
+ if (onBlock) {
359
+ return onBlock(result, req);
360
+ }
361
+ return new Response(
362
+ JSON.stringify({
363
+ error: "Content blocked",
364
+ categories: result.categories.filter((c) => c.flagged)
365
+ }),
366
+ {
367
+ status: 403,
368
+ headers: { "Content-Type": "application/json" }
369
+ }
370
+ );
371
+ }
372
+ return handler(req, result);
373
+ } catch (error) {
374
+ if (onError) {
375
+ return onError(error instanceof Error ? error : new Error(String(error)), req);
376
+ }
377
+ console.error("[Vettly] Moderation error:", error);
378
+ return new Response(
379
+ JSON.stringify({
380
+ error: "Moderation service error",
381
+ message: error instanceof Error ? error.message : "Unknown error"
382
+ }),
383
+ {
384
+ status: 500,
385
+ headers: { "Content-Type": "application/json" }
386
+ }
387
+ );
388
+ }
389
+ };
390
+ }
391
+ // Annotate the CommonJS export names for ESM import in node:
392
+ 0 && (module.exports = {
393
+ VettlyAuthError,
394
+ VettlyError,
395
+ VettlyRateLimitError,
396
+ VettlyValidationError,
397
+ createClient,
398
+ createModerationHandler,
399
+ moderate,
400
+ moderateImage,
401
+ withModeration
402
+ });
@@ -0,0 +1,31 @@
1
+ import { V as VettlyConfig, M as ModerationOptions, a as ModerationResult } from './edge-CPse0nUh.cjs';
2
+ export { A as Action, b as Category, d as CategoryScore, C as ContentType, E as EdgeHandlerOptions, e as ModerationHandlerConfig, g as VettlyAuthError, f as VettlyError, h as VettlyRateLimitError, i as VettlyValidationError, c as createModerationHandler, w as withModeration } from './edge-CPse0nUh.cjs';
3
+
4
+ /**
5
+ * Vettly Client for Supabase Edge Functions
6
+ * Fetch-based, Deno-compatible client
7
+ */
8
+
9
+ /**
10
+ * Create a configured Vettly client
11
+ */
12
+ declare function createClient(config: VettlyConfig): {
13
+ /**
14
+ * Moderate text content
15
+ */
16
+ check(content: string, options?: ModerationOptions): Promise<ModerationResult>;
17
+ /**
18
+ * Moderate an image
19
+ */
20
+ checkImage(imageUrl: string, options?: ModerationOptions): Promise<ModerationResult>;
21
+ };
22
+ /**
23
+ * Moderate text content using the default client
24
+ */
25
+ declare function moderate(content: string, options?: ModerationOptions): Promise<ModerationResult>;
26
+ /**
27
+ * Moderate an image using the default client
28
+ */
29
+ declare function moderateImage(imageUrl: string, options?: ModerationOptions): Promise<ModerationResult>;
30
+
31
+ export { ModerationOptions, ModerationResult, VettlyConfig, createClient, moderate, moderateImage };
@@ -0,0 +1,31 @@
1
+ import { V as VettlyConfig, M as ModerationOptions, a as ModerationResult } from './edge-CPse0nUh.js';
2
+ export { A as Action, b as Category, d as CategoryScore, C as ContentType, E as EdgeHandlerOptions, e as ModerationHandlerConfig, g as VettlyAuthError, f as VettlyError, h as VettlyRateLimitError, i as VettlyValidationError, c as createModerationHandler, w as withModeration } from './edge-CPse0nUh.js';
3
+
4
+ /**
5
+ * Vettly Client for Supabase Edge Functions
6
+ * Fetch-based, Deno-compatible client
7
+ */
8
+
9
+ /**
10
+ * Create a configured Vettly client
11
+ */
12
+ declare function createClient(config: VettlyConfig): {
13
+ /**
14
+ * Moderate text content
15
+ */
16
+ check(content: string, options?: ModerationOptions): Promise<ModerationResult>;
17
+ /**
18
+ * Moderate an image
19
+ */
20
+ checkImage(imageUrl: string, options?: ModerationOptions): Promise<ModerationResult>;
21
+ };
22
+ /**
23
+ * Moderate text content using the default client
24
+ */
25
+ declare function moderate(content: string, options?: ModerationOptions): Promise<ModerationResult>;
26
+ /**
27
+ * Moderate an image using the default client
28
+ */
29
+ declare function moderateImage(imageUrl: string, options?: ModerationOptions): Promise<ModerationResult>;
30
+
31
+ export { ModerationOptions, ModerationResult, VettlyConfig, createClient, moderate, moderateImage };
package/dist/index.js ADDED
@@ -0,0 +1,22 @@
1
+ import {
2
+ VettlyAuthError,
3
+ VettlyError,
4
+ VettlyRateLimitError,
5
+ VettlyValidationError,
6
+ createClient,
7
+ createModerationHandler,
8
+ moderate,
9
+ moderateImage,
10
+ withModeration
11
+ } from "./chunk-LTC3D75K.js";
12
+ export {
13
+ VettlyAuthError,
14
+ VettlyError,
15
+ VettlyRateLimitError,
16
+ VettlyValidationError,
17
+ createClient,
18
+ createModerationHandler,
19
+ moderate,
20
+ moderateImage,
21
+ withModeration
22
+ };
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@vettly/supabase",
3
+ "version": "0.1.0",
4
+ "description": "Vettly content moderation for Supabase Edge Functions",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js",
12
+ "require": "./dist/index.cjs"
13
+ },
14
+ "./edge": {
15
+ "types": "./dist/edge.d.ts",
16
+ "import": "./dist/edge.js",
17
+ "require": "./dist/edge.cjs"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "README.md"
23
+ ],
24
+ "scripts": {
25
+ "build": "tsup src/index.ts src/edge.ts --format esm,cjs --dts --clean",
26
+ "test": "bun test",
27
+ "prepublishOnly": "bun run build"
28
+ },
29
+ "keywords": [
30
+ "supabase",
31
+ "edge-functions",
32
+ "deno",
33
+ "content-moderation",
34
+ "moderation",
35
+ "vettly",
36
+ "trust-safety"
37
+ ],
38
+ "author": "Vettly",
39
+ "license": "MIT",
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "https://github.com/brian-nextaura/vettly-docs.git",
43
+ "directory": "packages/supabase"
44
+ },
45
+ "homepage": "https://vettly.dev",
46
+ "publishConfig": {
47
+ "access": "public"
48
+ },
49
+ "bugs": {
50
+ "url": "https://github.com/brian-nextaura/vettly-docs/issues"
51
+ },
52
+ "devDependencies": {
53
+ "@types/bun": "latest",
54
+ "tsup": "^8.0.0",
55
+ "typescript": "^5"
56
+ }
57
+ }