Repository navigation
Expand file tree
/
Copy pathcliRenderer.ts
More file actions
174 lines (142 loc) · 5.13 KB
/
Copy pathcliRenderer.ts
File metadata and controls
174 lines (142 loc) · 5.13 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* CLI 응답 렌더러
*/
interface ApiEnvelope {
success?: boolean;
data?: unknown;
meta?: unknown;
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null;
}
function toText(value: unknown): string {
if (value === null || value === undefined) {
return '-';
}
if (typeof value === 'string') {
return value;
}
if (typeof value === 'number' || typeof value === 'boolean') {
return String(value);
}
return JSON.stringify(value);
}
function formatMeta(meta: unknown): string[] {
if (!isRecord(meta)) {
return [];
}
const pairs = Object.entries(meta)
.filter(([, value]) => typeof value === 'string' || typeof value === 'number')
.map(([key, value]) => `${key}=${value}`);
if (pairs.length === 0) {
return [];
}
return [`메타: ${pairs.join(', ')}`];
}
function detectName(item: Record<string, unknown>): string {
const keys = ['name', 'productName', 'storeName', 'theaterName', 'movieName'];
for (const key of keys) {
if (typeof item[key] === 'string' && item[key].trim().length > 0) {
return item[key] as string;
}
}
return '이름 없음';
}
function formatCollection(title: string, items: unknown): string[] {
if (!Array.isArray(items)) {
return [];
}
const lines = [`${title}: ${items.length}건`];
const preview = items.slice(0, 5);
for (const entry of preview) {
if (!isRecord(entry)) {
lines.push(`- ${toText(entry)}`);
continue;
}
const name = detectName(entry);
const id =
entry.id ?? entry.productId ?? entry.PD_NO ?? entry.storeCode ?? entry.theaterId ?? entry.movieId;
const price = entry.price ?? entry.prc ?? entry.PD_PRC;
let detail = `- ${name}`;
if (id !== undefined) {
detail += ` [${toText(id)}]`;
}
if (price !== undefined) {
detail += ` / ${toText(price)}원`;
}
lines.push(detail);
}
if (items.length > preview.length) {
lines.push(`...외 ${items.length - preview.length}건`);
}
return lines;
}
function formatProductDetail(data: Record<string, unknown>): string[] {
const id = data.id ?? data.productId ?? '-';
const name = data.name ?? data.productName ?? '-';
const price = data.price ?? '-';
const soldOut = data.soldOut;
const lines = [`제품 상세: ${toText(name)} (${toText(id)})`, `가격: ${toText(price)}원`];
if (typeof soldOut === 'boolean') {
lines.push(`품절 여부: ${soldOut ? '품절' : '재고 있음'}`);
}
return lines;
}
function formatInventory(data: Record<string, unknown>): string[] {
const lines: string[] = [];
const >
if (onlineStock !== undefined) {
lines.push(`온라인 재고: ${toText(onlineStock)}`);
}
const storeInventory = data.storeInventory;
if (isRecord(storeInventory)) {
const total = storeInventory.totalStores;
const inStock = storeInventory.inStockCount;
lines.push(`매장 재고: 재고보유 ${toText(inStock)} / 전체 ${toText(total)}`);
lines.push(...formatCollection('매장 목록', storeInventory.stores));
}
return lines;
}
export function renderApiEnvelope(command: string, url: URL, payload: unknown): string {
if (!isRecord(payload)) {
return `요청 성공: ${url.pathname}\n${toText(payload)}\n\n원본 JSON은 --json 옵션으로 확인하세요.`;
}
const envelope = payload as ApiEnvelope;
if (envelope.success !== true) {
return JSON.stringify(payload, null, 2);
}
const lines: string[] = [`요청 성공: ${url.pathname}`];
lines.push(...formatMeta(envelope.meta));
if (!isRecord(envelope.data)) {
lines.push(`응답 데이터: ${toText(envelope.data)}`);
lines.push('', '원본 JSON은 --json 옵션으로 확인하세요.');
return lines.join('\n');
}
const data = envelope.data;
if (command === 'product') {
lines.push(...formatProductDetail(data));
} else if (command === 'inventory') {
lines.push(...formatInventory(data));
} else if (command === 'cu-inventory') {
lines.push(...formatCollection('매장 목록', isRecord(data.nearbyStores) ? data.nearbyStores.stores : undefined));
lines.push(...formatCollection('재고 항목', isRecord(data.inventory) ? data.inventory.items : undefined));
} else {
lines.push(...formatCollection('제품 목록', data.products));
lines.push(...formatCollection('매장 목록', data.stores));
lines.push(...formatCollection('극장 목록', data.theaters));
lines.push(...formatCollection('영화 목록', data.movies));
lines.push(...formatCollection('시간표', data.timetable));
lines.push(...formatCollection('좌석 목록', data.seats));
lines.push(...formatCollection('상영 목록', data.showtimes));
if (lines.length <= 2) {
const summaryEntries = Object.entries(data)
.filter(([, value]) => typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean')
.slice(0, 8);
for (const [key, value] of summaryEntries) {
lines.push(`${key}: ${toText(value)}`);
}
}
}
lines.push('', '원본 JSON은 --json 옵션으로 확인하세요.');
return lines.join('\n');
}