-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
62 lines (53 loc) · 1.74 KB
/
server.ts
File metadata and controls
62 lines (53 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import express from 'express';
import { Moderyo } from '@moderyo/sdk';
import dotenv from 'dotenv';
dotenv.config();
const app = express();
app.use(express.json());
// ─── Initialize Moderyo Client ───
const client = new Moderyo({
apiKey: process.env.MODERYO_API_KEY || '',
});
// ─── POST /api/moderate ───
app.post('/api/moderate', async (req, res) => {
try {
const { input, mode, risk, playerId, skipProfanity, skipThreat, skipMaskedWord, longTextMode, longTextThreshold } = req.body;
if (!input) {
return res.status(400).json({ error: 'input field is required' });
}
const result = await client.moderate(
{
input,
skipProfanity,
skipThreat,
skipMaskedWord,
longTextMode,
longTextThreshold,
},
{ mode, risk, playerId }
);
return res.json({
blocked: result.action === 'block',
flagged: result.action === 'flag' || result.flagged,
decision: result.policyDecision?.decision,
reason: result.policyDecision?.reason,
scores: result.scores,
detectedPhrases: result.detectedPhrases,
categories: result.categories,
});
} catch (err: unknown) {
const message = err instanceof Error ? err.message : 'Unknown error';
return res.status(500).json({ error: message });
}
});
// ─── GET /health ───
app.get('/health', (_req, res) => {
res.json({ status: 'ok', sdk: 'moderyo-js', version: '2.0.7' });
});
// ─── Start ───
const PORT = process.env.PORT || 4001;
app.listen(PORT, () => {
console.log(`Node.js example server running on http://localhost:${PORT}`);
console.log('POST /api/moderate — Moderate text content');
console.log('GET /health — Health check');
});