MCPcopy Create free account
hub / github.com/T-Lab-CUHKSZ/claude-code / handleAPI

Function handleAPI

config-ui/server.ts:192–313  ·  view source on GitHub ↗
(req: Request)

Source from the content-addressed store, hash-verified

190// --- API Router ---
191
192async function handleAPI(req: Request): Promise<Response> {
193 const url = new URL(req.url)
194 const path = url.pathname
195
196 // --- Settings ---
197 if (path === '/api/settings' && req.method === 'GET') {
198 return Response.json({
199 user: readJsonSafe(getUserSettingsPath()),
200 project: readJsonSafe(getProjectSettingsPath()),
201 local: readJsonSafe(getLocalSettingsPath()),
202 })
203 }
204
205 if (path === '/api/settings' && req.method === 'POST') {
206 const body = await req.json() as { scope: string; settings: any }
207 const pathMap: Record<string, string> = {
208 user: getUserSettingsPath(),
209 project: getProjectSettingsPath(),
210 local: getLocalSettingsPath(),
211 }
212 const target = pathMap[body.scope]
213 if (!target) return Response.json({ error: 'Invalid scope' }, { status: 400 })
214 writeJsonSafe(target, body.settings)
215 return Response.json({ ok: true })
216 }
217
218 // --- Feature Flags ---
219 if (path === '/api/features' && req.method === 'GET') {
220 return Response.json(parseFeatureFlags())
221 }
222
223 if (path === '/api/features' && req.method === 'POST') {
224 const flags = await req.json() as Record<string, boolean>
225 writeFeatureFlags(flags)
226 return Response.json({ ok: true })
227 }
228
229 // --- MCP Servers ---
230 if (path === '/api/mcp' && req.method === 'GET') {
231 return Response.json(readJsonSafe(getMcpConfigPath()))
232 }
233
234 if (path === '/api/mcp' && req.method === 'POST') {
235 const config = await req.json()
236 writeJsonSafe(getMcpConfigPath(), config)
237 return Response.json({ ok: true })
238 }
239
240 // --- Agents ---
241 if (path === '/api/agents' && req.method === 'GET') {
242 return Response.json(listMdFiles(getAgentsDir()))
243 }
244
245 if (path === '/api/agents' && req.method === 'POST') {
246 const body = await req.json() as { name: string; content: string }
247 const agentPath = join(getAgentsDir(), body.name.endsWith('.md') ? body.name : `${body.name}.md`)
248 writeTextSafe(agentPath, body.content)
249 return Response.json({ ok: true })

Callers 1

fetchFunction · 0.85

Calls 15

existsSyncFunction · 0.90
unlinkSyncFunction · 0.90
readJsonSafeFunction · 0.85
getUserSettingsPathFunction · 0.85
getProjectSettingsPathFunction · 0.85
getLocalSettingsPathFunction · 0.85
writeJsonSafeFunction · 0.85
parseFeatureFlagsFunction · 0.85
writeFeatureFlagsFunction · 0.85
getMcpConfigPathFunction · 0.85
listMdFilesFunction · 0.85
getAgentsDirFunction · 0.85

Tested by

no test coverage detected