|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { Command } from 'commander'; |
| 4 | +import { startDevTools, sendRequest, runSavedRequest, listRequests } from './index.js'; |
| 5 | +import { startInteractive } from './interactive.js'; |
| 6 | + |
| 7 | +const program = new Command(); |
| 8 | + |
| 9 | +program |
| 10 | + .name('api-devtools') |
| 11 | + .description('API 테스트 & 디버깅용 CLI 개발 도구') |
| 12 | + .version('1.0.0') |
| 13 | + .action(async () => { |
| 14 | + // 인자 없이 실행하면 interactive 모드 |
| 15 | + await startInteractive(); |
| 16 | + }); |
| 17 | + |
| 18 | +// GET 요청 |
| 19 | +program |
| 20 | + .command('get <url>') |
| 21 | + .description('GET 요청을 보냅니다') |
| 22 | + .option('-H, --header <header...>', '헤더 추가 (예: "Authorization: Bearer token")') |
| 23 | + .action(async (url, options) => { |
| 24 | + const headers = parseHeaders(options.header); |
| 25 | + await sendRequest('GET', url, { headers }); |
| 26 | + }); |
| 27 | + |
| 28 | +// POST 요청 |
| 29 | +program |
| 30 | + .command('post <url>') |
| 31 | + .description('POST 요청을 보냅니다') |
| 32 | + .option('-d, --data <data>', '요청 본문 (JSON)') |
| 33 | + .option('-H, --header <header...>', '헤더 추가') |
| 34 | + .action(async (url, options) => { |
| 35 | + const headers = parseHeaders(options.header); |
| 36 | + const body = options.data ? JSON.parse(options.data) : undefined; |
| 37 | + await sendRequest('POST', url, { headers, body }); |
| 38 | + }); |
| 39 | + |
| 40 | +// PUT 요청 |
| 41 | +program |
| 42 | + .command('put <url>') |
| 43 | + .description('PUT 요청을 보냅니다') |
| 44 | + .option('-d, --data <data>', '요청 본문 (JSON)') |
| 45 | + .option('-H, --header <header...>', '헤더 추가') |
| 46 | + .action(async (url, options) => { |
| 47 | + const headers = parseHeaders(options.header); |
| 48 | + const body = options.data ? JSON.parse(options.data) : undefined; |
| 49 | + await sendRequest('PUT', url, { headers, body }); |
| 50 | + }); |
| 51 | + |
| 52 | +// DELETE 요청 |
| 53 | +program |
| 54 | + .command('delete <url>') |
| 55 | + .description('DELETE 요청을 보냅니다') |
| 56 | + .option('-H, --header <header...>', '헤더 추가') |
| 57 | + .action(async (url, options) => { |
| 58 | + const headers = parseHeaders(options.header); |
| 59 | + await sendRequest('DELETE', url, { headers }); |
| 60 | + }); |
| 61 | + |
| 62 | +// 저장된 요청 실행 또는 URL 실행 |
| 63 | +program |
| 64 | + .command('run <nameOrUrl>') |
| 65 | + .description('저장된 요청을 실행하거나 URL로 GET 요청을 보냅니다') |
| 66 | + .action(async nameOrUrl => { |
| 67 | + await runSavedRequest(nameOrUrl); |
| 68 | + }); |
| 69 | + |
| 70 | +// 저장된 요청 목록 |
| 71 | +program |
| 72 | + .command('list') |
| 73 | + .description('저장된 요청 목록을 표시합니다') |
| 74 | + .action(async () => { |
| 75 | + await listRequests(); |
| 76 | + }); |
| 77 | + |
| 78 | +// 프록시 모드 |
| 79 | +program |
| 80 | + .command('proxy') |
| 81 | + .description('프록시 모드로 API DevTools를 시작합니다') |
| 82 | + .option('-p, --port <port>', '프록시 서버 포트', '8888') |
| 83 | + .option('-t, --target <url>', '프록시 타겟 URL', '') |
| 84 | + .action(async options => { |
| 85 | + await startDevTools({ |
| 86 | + port: parseInt(options.port), |
| 87 | + target: options.target, |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | +// 헤더 파싱 헬퍼 |
| 92 | +function parseHeaders(headerArray?: string[]): Record<string, string> | undefined { |
| 93 | + if (!headerArray) return undefined; |
| 94 | + |
| 95 | + const headers: Record<string, string> = {}; |
| 96 | + for (const header of headerArray) { |
| 97 | + const [key, ...valueParts] = header.split(':'); |
| 98 | + if (key && valueParts.length > 0) { |
| 99 | + headers[key.trim()] = valueParts.join(':').trim(); |
| 100 | + } |
| 101 | + } |
| 102 | + return headers; |
| 103 | +} |
| 104 | + |
| 105 | +program.parse(); |
0 commit comments