tambahannnya

This commit is contained in:
bipproduction
2025-11-14 10:24:49 +08:00
parent e0bef23eab
commit 77cbb6062b
2 changed files with 49 additions and 68 deletions

View File

@@ -1,5 +1,6 @@
import { Elysia } from "elysia"; import { Elysia } from "elysia";
import { getMcpTools } from "../lib/mcp_tool_convert"; import { getMcpTools } from "../lib/mcp_tool_convert";
import _ from "lodash";
var tools = [] as any[]; var tools = [] as any[];
const OPENAPI_URL = process.env.BUN_PUBLIC_BASE_URL + "/docs/json"; const OPENAPI_URL = process.env.BUN_PUBLIC_BASE_URL + "/docs/json";
@@ -118,16 +119,16 @@ async function handleMCPRequestAsync(
const baseUrl = const baseUrl =
process.env.BUN_PUBLIC_BASE_URL || "http://localhost:3000"; process.env.BUN_PUBLIC_BASE_URL || "http://localhost:3000";
const result = await executeTool(tool, params?.arguments || {}, baseUrl); const result = await executeTool(tool, params?.arguments || {}, baseUrl);
const isObject = typeof result === "object" && result !== null; const data = result.data.data;
const isObject = _.isObject(data);
return { return {
jsonrpc: "2.0", jsonrpc: "2.0",
id, id,
result: { result: {
content: [ content: [
isObject isObject
? { type: "json", data: result } // prefer JSON jika client support ? { type: "json", data: data }
: { type: "text", text: String(result) }, : { type: "text", text: String(data) },
], ],
}, },
}; };

100
xx.ts
View File

@@ -1,65 +1,45 @@
/* eslint-disable @typescript-eslint/no-explicit-any */ {
import { Elysia } from 'elysia' "response": [
import jwt, { type JWTPayloadSpec } from '@elysiajs/jwt' {
import bearer from '@elysiajs/bearer' "type": "json",
import { prisma } from '../lib/prisma' "data": {
"success": true,
// ========================================================= "status": 200,
// JWT Secret Validation "method": "GET",
// ========================================================= "path": "/api/pengaduan/category",
const secret = process.env.JWT_SECRET "data": {
if (!secret) throw new Error('JWT_SECRET environment variable is missing') "data": [
{
// ========================================================= "id": "infrastruktur",
// Auth Middleware Plugin "name": "Infrastruktur"
// ========================================================= },
export default function apiAuth(app: Elysia) { {
if (!secret) throw new Error('JWT_SECRET environment variable is missing') "id": "cmhslcvcy0000mg0810l7zx8x",
return app "name": "keamanan"
// Register Bearer and JWT plugins },
.use(bearer()) // ✅ Extracts Bearer token automatically (case-insensitive) {
.use( "id": "keamanan",
jwt({ "name": "Keamanan"
name: 'jwt', },
secret, {
}) "id": "kebersihan",
) "name": "Kebersihan"
},
// Derive user from JWT or cookie {
.derive(async ({ bearer, cookie, jwt }) => { "id": "lainnya",
// Normalize token type to string or undefined "name": "Lainnya"
const token = },
(typeof bearer === 'string' ? bearer : undefined) ?? {
(typeof cookie?.token?.value === 'string' ? cookie.token.value : undefined) "id": "pelayanan",
"name": "Pelayanan"
let user: Awaited<ReturnType<typeof prisma.user.findUnique>> | null = null },
{
if (token) { "id": "cmhsl5ijj0000mg08pru6kom4",
try { "name": "sampah"
const decoded = (await jwt.verify(token)) as JWTPayloadSpec
if (decoded?.sub && typeof decoded.sub === 'string') {
user = await prisma.user.findUnique({
where: { id: decoded.sub },
})
} }
} catch (err) { ]
console.warn('[SERVER][apiAuth] Invalid token:', (err as Error).message)
} }
} }
return { user }
})
// Protect all routes by default
.onBeforeHandle(({ user, set, request }) => {
// Whitelist public routes if needed
const publicPaths = ['/auth/login', '/auth/register', '/public']
if (publicPaths.some((path) => request.url.includes(path))) return
if (!user) {
set.status = 401
return { error: 'Unauthorized' }
} }
}) ]
} }