refactor: code quality improvements (P3)

## Type Safety
- Add zod runtime validation for external API responses
  * Namecheap API responses (domain-register.ts)
  * n8n webhook responses (n8n-service.ts)
  * User request bodies (routes/api.ts)
  * Replaced unsafe type assertions with safeParse()
  * Proper error handling and logging

## Dead Code Removal
- Remove unused callDepositAgent function (127 lines)
  * Legacy Assistants API code no longer needed
  * Now using direct code execution
  * File reduced from 469 → 345 lines (26.4% reduction)

## Configuration Management
- Extract hardcoded URLs to environment variables
  * Added 7 new vars in wrangler.toml:
    OPENAI_API_BASE, NAMECHEAP_API_URL, WHOIS_API_URL,
    CONTEXT7_API_BASE, BRAVE_API_BASE, WTTR_IN_URL, HOSTING_SITE_URL
  * Updated Env interface in types.ts
  * All URLs have fallback to current production values
  * Enables environment-specific configuration (dev/staging/prod)

## Dependencies
- Add zod 4.3.5 for runtime type validation

## Files Modified
- Configuration: wrangler.toml, types.ts, package.json
- Services: 11 TypeScript files with URL/validation updates
- Total: 15 files, +196/-189 lines

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
kappa
2026-01-19 22:06:01 +09:00
parent 4f68dd3ebb
commit 45e0677ab0
15 changed files with 196 additions and 189 deletions

View File

@@ -1,4 +1,14 @@
import { z } from 'zod';
import { Env, IntentAnalysis, N8nResponse } from './types';
import { createLogger } from './utils/logger';
const logger = createLogger('n8n-service');
// Zod schema for N8n webhook response validation
const N8nResponseSchema = z.object({
reply: z.string().optional(),
error: z.string().optional(),
});
// n8n으로 처리할 기능 목록 (참고용)
// - weather: 날씨
@@ -104,8 +114,15 @@ export async function callN8n(
return { error: `n8n 호출 실패 (${response.status})` };
}
const data = await response.json() as N8nResponse;
return data;
const jsonData = await response.json();
const parseResult = N8nResponseSchema.safeParse(jsonData);
if (!parseResult.success) {
logger.error('N8n response schema validation failed', parseResult.error);
return { error: 'n8n 응답 형식 오류' };
}
return parseResult.data;
} catch (error) {
console.error('n8n fetch error:', error);
return { error: 'n8n 연결 실패' };