@mandujs/core 0.7.0 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/bundler/build.ts +131 -18
- package/src/filling/context.ts +438 -464
- package/src/filling/filling.ts +220 -552
- package/src/filling/tmpclaude-2f8d-cwd +1 -0
- package/src/filling/tmpclaude-fb5a-cwd +1 -0
package/src/filling/context.ts
CHANGED
|
@@ -1,464 +1,438 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Mandu Context - 만두 접시 🥟
|
|
3
|
-
* Request/Response를 래핑하여 편리한 API 제공
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type { ZodSchema } from "zod";
|
|
7
|
-
|
|
8
|
-
// ========== Cookie Types ==========
|
|
9
|
-
|
|
10
|
-
export interface CookieOptions {
|
|
11
|
-
/** 쿠키 만료 시간 (Date 객체 또는 문자열) */
|
|
12
|
-
expires?: Date | string;
|
|
13
|
-
/** 쿠키 유효 기간 (초) */
|
|
14
|
-
maxAge?: number;
|
|
15
|
-
/** 쿠키 도메인 */
|
|
16
|
-
domain?: string;
|
|
17
|
-
/** 쿠키 경로 */
|
|
18
|
-
path?: string;
|
|
19
|
-
/** HTTPS에서만 전송 */
|
|
20
|
-
secure?: boolean;
|
|
21
|
-
/** JavaScript에서 접근 불가 */
|
|
22
|
-
httpOnly?: boolean;
|
|
23
|
-
/** Same-Site 정책 */
|
|
24
|
-
sameSite?: "strict" | "lax" | "none";
|
|
25
|
-
/** 파티션 키 (CHIPS) */
|
|
26
|
-
partitioned?: boolean;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Cookie Manager - 쿠키 읽기/쓰기 관리
|
|
31
|
-
*/
|
|
32
|
-
export class CookieManager {
|
|
33
|
-
private requestCookies: Map<string, string>;
|
|
34
|
-
private responseCookies: Map<string, { value: string; options: CookieOptions }>;
|
|
35
|
-
private deletedCookies: Set<string>;
|
|
36
|
-
|
|
37
|
-
constructor(request: Request) {
|
|
38
|
-
this.requestCookies = this.parseRequestCookies(request);
|
|
39
|
-
this.responseCookies = new Map();
|
|
40
|
-
this.deletedCookies = new Set();
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
private parseRequestCookies(request: Request): Map<string, string> {
|
|
44
|
-
const cookies = new Map<string, string>();
|
|
45
|
-
const cookieHeader = request.headers.get("cookie");
|
|
46
|
-
|
|
47
|
-
if (cookieHeader) {
|
|
48
|
-
const pairs = cookieHeader.split(";");
|
|
49
|
-
for (const pair of pairs) {
|
|
50
|
-
const [name, ...rest] = pair.trim().split("=");
|
|
51
|
-
if (name) {
|
|
52
|
-
const rawValue = rest.join("=");
|
|
53
|
-
try {
|
|
54
|
-
cookies.set(name, decodeURIComponent(rawValue));
|
|
55
|
-
} catch {
|
|
56
|
-
// 잘못된 URL 인코딩 시 원본 값 사용
|
|
57
|
-
cookies.set(name, rawValue);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return cookies;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* 쿠키 값 읽기
|
|
68
|
-
* @example
|
|
69
|
-
* const session = ctx.cookies.get('session');
|
|
70
|
-
*/
|
|
71
|
-
get(name: string): string | undefined {
|
|
72
|
-
return this.requestCookies.get(name);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* 쿠키 존재 여부 확인
|
|
77
|
-
*/
|
|
78
|
-
has(name: string): boolean {
|
|
79
|
-
return this.requestCookies.has(name);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* 모든 쿠키 가져오기
|
|
84
|
-
*/
|
|
85
|
-
getAll(): Record<string, string> {
|
|
86
|
-
return Object.fromEntries(this.requestCookies);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* 쿠키 설정
|
|
91
|
-
* @example
|
|
92
|
-
* ctx.cookies.set('session', 'abc123', { httpOnly: true, maxAge: 3600 });
|
|
93
|
-
*/
|
|
94
|
-
set(name: string, value: string, options: CookieOptions = {}): void {
|
|
95
|
-
this.responseCookies.set(name, { value, options });
|
|
96
|
-
this.deletedCookies.delete(name);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* 쿠키 삭제
|
|
101
|
-
* @example
|
|
102
|
-
* ctx.cookies.delete('session');
|
|
103
|
-
*/
|
|
104
|
-
delete(name: string, options: Pick<CookieOptions, "domain" | "path"> = {}): void {
|
|
105
|
-
this.responseCookies.delete(name);
|
|
106
|
-
this.deletedCookies.add(name);
|
|
107
|
-
// 삭제용 쿠키 설정 (maxAge=0)
|
|
108
|
-
this.responseCookies.set(name, {
|
|
109
|
-
value: "",
|
|
110
|
-
options: {
|
|
111
|
-
...options,
|
|
112
|
-
maxAge: 0,
|
|
113
|
-
expires: new Date(0),
|
|
114
|
-
},
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Set-Cookie 헤더 값들 생성
|
|
120
|
-
*/
|
|
121
|
-
getSetCookieHeaders(): string[] {
|
|
122
|
-
const headers: string[] = [];
|
|
123
|
-
|
|
124
|
-
for (const [name, { value, options }] of this.responseCookies) {
|
|
125
|
-
headers.push(this.serializeCookie(name, value, options));
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return headers;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* 쿠키를 Set-Cookie 헤더 형식으로 직렬화
|
|
133
|
-
*/
|
|
134
|
-
private serializeCookie(name: string, value: string, options: CookieOptions): string {
|
|
135
|
-
const parts: string[] = [`${encodeURIComponent(name)}=${encodeURIComponent(value)}`];
|
|
136
|
-
|
|
137
|
-
if (options.maxAge !== undefined) {
|
|
138
|
-
parts.push(`Max-Age=${options.maxAge}`);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (options.expires) {
|
|
142
|
-
const expires =
|
|
143
|
-
options.expires instanceof Date
|
|
144
|
-
? options.expires.toUTCString()
|
|
145
|
-
: options.expires;
|
|
146
|
-
parts.push(`Expires=${expires}`);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (options.domain) {
|
|
150
|
-
parts.push(`Domain=${options.domain}`);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
if (options.path) {
|
|
154
|
-
parts.push(`Path=${options.path}`);
|
|
155
|
-
} else {
|
|
156
|
-
parts.push("Path=/"); // 기본값
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
if (options.secure) {
|
|
160
|
-
parts.push("Secure");
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
if (options.httpOnly) {
|
|
164
|
-
parts.push("HttpOnly");
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (options.sameSite) {
|
|
168
|
-
parts.push(`SameSite=${options.sameSite.charAt(0).toUpperCase() + options.sameSite.slice(1)}`);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
if (options.partitioned) {
|
|
172
|
-
parts.push("Partitioned");
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
return parts.join("; ");
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Response에 Set-Cookie 헤더들 적용
|
|
180
|
-
*/
|
|
181
|
-
applyToResponse(response: Response): Response {
|
|
182
|
-
const setCookieHeaders = this.getSetCookieHeaders();
|
|
183
|
-
|
|
184
|
-
if (setCookieHeaders.length === 0) {
|
|
185
|
-
return response;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// Headers를 복사하여 수정
|
|
189
|
-
const newHeaders = new Headers(response.headers);
|
|
190
|
-
|
|
191
|
-
for (const setCookie of setCookieHeaders) {
|
|
192
|
-
newHeaders.append("Set-Cookie", setCookie);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
return new Response(response.body, {
|
|
196
|
-
status: response.status,
|
|
197
|
-
statusText: response.statusText,
|
|
198
|
-
headers: newHeaders,
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* 응답에 적용할 쿠키가 있는지 확인
|
|
204
|
-
*/
|
|
205
|
-
hasPendingCookies(): boolean {
|
|
206
|
-
return this.responseCookies.size > 0;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// ========== ManduContext ==========
|
|
211
|
-
|
|
212
|
-
export class ManduContext {
|
|
213
|
-
private store: Map<string, unknown> = new Map();
|
|
214
|
-
private _params: Record<string, string>;
|
|
215
|
-
private _query: Record<string, string>;
|
|
216
|
-
private
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
this.
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
// ============================================
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
-
* // 쿠키
|
|
274
|
-
* ctx.cookies.
|
|
275
|
-
*
|
|
276
|
-
* // 쿠키
|
|
277
|
-
* ctx.cookies.
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
*
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
data =
|
|
298
|
-
} else if (contentType.includes("
|
|
299
|
-
const formData = await this.request.formData();
|
|
300
|
-
data = Object.fromEntries(formData.entries());
|
|
301
|
-
} else {
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
this.
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
/** Get early response */
|
|
441
|
-
getResponse(): Response | null {
|
|
442
|
-
return this._response;
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
/** Symbol to indicate continue to next */
|
|
447
|
-
export const NEXT_SYMBOL = Symbol("mandu:next");
|
|
448
|
-
|
|
449
|
-
/** Route context for error reporting */
|
|
450
|
-
export interface ValidationRouteContext {
|
|
451
|
-
routeId: string;
|
|
452
|
-
pattern: string;
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
/** Validation error with details */
|
|
456
|
-
export class ValidationError extends Error {
|
|
457
|
-
constructor(
|
|
458
|
-
public readonly errors: unknown[],
|
|
459
|
-
public readonly routeContext?: ValidationRouteContext
|
|
460
|
-
) {
|
|
461
|
-
super("Validation failed");
|
|
462
|
-
this.name = "ValidationError";
|
|
463
|
-
}
|
|
464
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Mandu Context - 만두 접시 🥟
|
|
3
|
+
* Request/Response를 래핑하여 편리한 API 제공
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { ZodSchema } from "zod";
|
|
7
|
+
|
|
8
|
+
// ========== Cookie Types ==========
|
|
9
|
+
|
|
10
|
+
export interface CookieOptions {
|
|
11
|
+
/** 쿠키 만료 시간 (Date 객체 또는 문자열) */
|
|
12
|
+
expires?: Date | string;
|
|
13
|
+
/** 쿠키 유효 기간 (초) */
|
|
14
|
+
maxAge?: number;
|
|
15
|
+
/** 쿠키 도메인 */
|
|
16
|
+
domain?: string;
|
|
17
|
+
/** 쿠키 경로 */
|
|
18
|
+
path?: string;
|
|
19
|
+
/** HTTPS에서만 전송 */
|
|
20
|
+
secure?: boolean;
|
|
21
|
+
/** JavaScript에서 접근 불가 */
|
|
22
|
+
httpOnly?: boolean;
|
|
23
|
+
/** Same-Site 정책 */
|
|
24
|
+
sameSite?: "strict" | "lax" | "none";
|
|
25
|
+
/** 파티션 키 (CHIPS) */
|
|
26
|
+
partitioned?: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Cookie Manager - 쿠키 읽기/쓰기 관리
|
|
31
|
+
*/
|
|
32
|
+
export class CookieManager {
|
|
33
|
+
private requestCookies: Map<string, string>;
|
|
34
|
+
private responseCookies: Map<string, { value: string; options: CookieOptions }>;
|
|
35
|
+
private deletedCookies: Set<string>;
|
|
36
|
+
|
|
37
|
+
constructor(request: Request) {
|
|
38
|
+
this.requestCookies = this.parseRequestCookies(request);
|
|
39
|
+
this.responseCookies = new Map();
|
|
40
|
+
this.deletedCookies = new Set();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
private parseRequestCookies(request: Request): Map<string, string> {
|
|
44
|
+
const cookies = new Map<string, string>();
|
|
45
|
+
const cookieHeader = request.headers.get("cookie");
|
|
46
|
+
|
|
47
|
+
if (cookieHeader) {
|
|
48
|
+
const pairs = cookieHeader.split(";");
|
|
49
|
+
for (const pair of pairs) {
|
|
50
|
+
const [name, ...rest] = pair.trim().split("=");
|
|
51
|
+
if (name) {
|
|
52
|
+
const rawValue = rest.join("=");
|
|
53
|
+
try {
|
|
54
|
+
cookies.set(name, decodeURIComponent(rawValue));
|
|
55
|
+
} catch {
|
|
56
|
+
// 잘못된 URL 인코딩 시 원본 값 사용
|
|
57
|
+
cookies.set(name, rawValue);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return cookies;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 쿠키 값 읽기
|
|
68
|
+
* @example
|
|
69
|
+
* const session = ctx.cookies.get('session');
|
|
70
|
+
*/
|
|
71
|
+
get(name: string): string | undefined {
|
|
72
|
+
return this.requestCookies.get(name);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 쿠키 존재 여부 확인
|
|
77
|
+
*/
|
|
78
|
+
has(name: string): boolean {
|
|
79
|
+
return this.requestCookies.has(name);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 모든 쿠키 가져오기
|
|
84
|
+
*/
|
|
85
|
+
getAll(): Record<string, string> {
|
|
86
|
+
return Object.fromEntries(this.requestCookies);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 쿠키 설정
|
|
91
|
+
* @example
|
|
92
|
+
* ctx.cookies.set('session', 'abc123', { httpOnly: true, maxAge: 3600 });
|
|
93
|
+
*/
|
|
94
|
+
set(name: string, value: string, options: CookieOptions = {}): void {
|
|
95
|
+
this.responseCookies.set(name, { value, options });
|
|
96
|
+
this.deletedCookies.delete(name);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* 쿠키 삭제
|
|
101
|
+
* @example
|
|
102
|
+
* ctx.cookies.delete('session');
|
|
103
|
+
*/
|
|
104
|
+
delete(name: string, options: Pick<CookieOptions, "domain" | "path"> = {}): void {
|
|
105
|
+
this.responseCookies.delete(name);
|
|
106
|
+
this.deletedCookies.add(name);
|
|
107
|
+
// 삭제용 쿠키 설정 (maxAge=0)
|
|
108
|
+
this.responseCookies.set(name, {
|
|
109
|
+
value: "",
|
|
110
|
+
options: {
|
|
111
|
+
...options,
|
|
112
|
+
maxAge: 0,
|
|
113
|
+
expires: new Date(0),
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Set-Cookie 헤더 값들 생성
|
|
120
|
+
*/
|
|
121
|
+
getSetCookieHeaders(): string[] {
|
|
122
|
+
const headers: string[] = [];
|
|
123
|
+
|
|
124
|
+
for (const [name, { value, options }] of this.responseCookies) {
|
|
125
|
+
headers.push(this.serializeCookie(name, value, options));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return headers;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 쿠키를 Set-Cookie 헤더 형식으로 직렬화
|
|
133
|
+
*/
|
|
134
|
+
private serializeCookie(name: string, value: string, options: CookieOptions): string {
|
|
135
|
+
const parts: string[] = [`${encodeURIComponent(name)}=${encodeURIComponent(value)}`];
|
|
136
|
+
|
|
137
|
+
if (options.maxAge !== undefined) {
|
|
138
|
+
parts.push(`Max-Age=${options.maxAge}`);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (options.expires) {
|
|
142
|
+
const expires =
|
|
143
|
+
options.expires instanceof Date
|
|
144
|
+
? options.expires.toUTCString()
|
|
145
|
+
: options.expires;
|
|
146
|
+
parts.push(`Expires=${expires}`);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (options.domain) {
|
|
150
|
+
parts.push(`Domain=${options.domain}`);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (options.path) {
|
|
154
|
+
parts.push(`Path=${options.path}`);
|
|
155
|
+
} else {
|
|
156
|
+
parts.push("Path=/"); // 기본값
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (options.secure) {
|
|
160
|
+
parts.push("Secure");
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (options.httpOnly) {
|
|
164
|
+
parts.push("HttpOnly");
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (options.sameSite) {
|
|
168
|
+
parts.push(`SameSite=${options.sameSite.charAt(0).toUpperCase() + options.sameSite.slice(1)}`);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (options.partitioned) {
|
|
172
|
+
parts.push("Partitioned");
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return parts.join("; ");
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Response에 Set-Cookie 헤더들 적용
|
|
180
|
+
*/
|
|
181
|
+
applyToResponse(response: Response): Response {
|
|
182
|
+
const setCookieHeaders = this.getSetCookieHeaders();
|
|
183
|
+
|
|
184
|
+
if (setCookieHeaders.length === 0) {
|
|
185
|
+
return response;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Headers를 복사하여 수정
|
|
189
|
+
const newHeaders = new Headers(response.headers);
|
|
190
|
+
|
|
191
|
+
for (const setCookie of setCookieHeaders) {
|
|
192
|
+
newHeaders.append("Set-Cookie", setCookie);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return new Response(response.body, {
|
|
196
|
+
status: response.status,
|
|
197
|
+
statusText: response.statusText,
|
|
198
|
+
headers: newHeaders,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* 응답에 적용할 쿠키가 있는지 확인
|
|
204
|
+
*/
|
|
205
|
+
hasPendingCookies(): boolean {
|
|
206
|
+
return this.responseCookies.size > 0;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// ========== ManduContext ==========
|
|
211
|
+
|
|
212
|
+
export class ManduContext {
|
|
213
|
+
private store: Map<string, unknown> = new Map();
|
|
214
|
+
private _params: Record<string, string>;
|
|
215
|
+
private _query: Record<string, string>;
|
|
216
|
+
private _cookies: CookieManager;
|
|
217
|
+
|
|
218
|
+
constructor(
|
|
219
|
+
public readonly request: Request,
|
|
220
|
+
params: Record<string, string> = {}
|
|
221
|
+
) {
|
|
222
|
+
this._params = params;
|
|
223
|
+
this._query = this.parseQuery();
|
|
224
|
+
this._cookies = new CookieManager(request);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private parseQuery(): Record<string, string> {
|
|
228
|
+
const url = new URL(this.request.url);
|
|
229
|
+
const query: Record<string, string> = {};
|
|
230
|
+
url.searchParams.forEach((value, key) => {
|
|
231
|
+
query[key] = value;
|
|
232
|
+
});
|
|
233
|
+
return query;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// ============================================
|
|
237
|
+
// 🥟 Request 읽기
|
|
238
|
+
// ============================================
|
|
239
|
+
|
|
240
|
+
/** Path parameters (e.g., /users/:id → { id: '123' }) */
|
|
241
|
+
get params(): Record<string, string> {
|
|
242
|
+
return this._params;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** Query parameters (e.g., ?name=mandu → { name: 'mandu' }) */
|
|
246
|
+
get query(): Record<string, string> {
|
|
247
|
+
return this._query;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** Request headers */
|
|
251
|
+
get headers(): Headers {
|
|
252
|
+
return this.request.headers;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** HTTP method */
|
|
256
|
+
get method(): string {
|
|
257
|
+
return this.request.method;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** Request URL */
|
|
261
|
+
get url(): string {
|
|
262
|
+
return this.request.url;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** Shorthand for request */
|
|
266
|
+
get req(): Request {
|
|
267
|
+
return this.request;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Cookie Manager
|
|
272
|
+
* @example
|
|
273
|
+
* // 쿠키 읽기
|
|
274
|
+
* const session = ctx.cookies.get('session');
|
|
275
|
+
*
|
|
276
|
+
* // 쿠키 설정
|
|
277
|
+
* ctx.cookies.set('session', 'abc123', { httpOnly: true, maxAge: 3600 });
|
|
278
|
+
*
|
|
279
|
+
* // 쿠키 삭제
|
|
280
|
+
* ctx.cookies.delete('session');
|
|
281
|
+
*/
|
|
282
|
+
get cookies(): CookieManager {
|
|
283
|
+
return this._cookies;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Parse request body with optional Zod validation
|
|
288
|
+
* @example
|
|
289
|
+
* const data = await ctx.body() // any
|
|
290
|
+
* const data = await ctx.body(UserSchema) // typed & validated
|
|
291
|
+
*/
|
|
292
|
+
async body<T = unknown>(schema?: ZodSchema<T>): Promise<T> {
|
|
293
|
+
const contentType = this.request.headers.get("content-type") || "";
|
|
294
|
+
let data: unknown;
|
|
295
|
+
|
|
296
|
+
if (contentType.includes("application/json")) {
|
|
297
|
+
data = await this.request.json();
|
|
298
|
+
} else if (contentType.includes("application/x-www-form-urlencoded")) {
|
|
299
|
+
const formData = await this.request.formData();
|
|
300
|
+
data = Object.fromEntries(formData.entries());
|
|
301
|
+
} else if (contentType.includes("multipart/form-data")) {
|
|
302
|
+
const formData = await this.request.formData();
|
|
303
|
+
data = Object.fromEntries(formData.entries());
|
|
304
|
+
} else {
|
|
305
|
+
data = await this.request.text();
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (schema) {
|
|
309
|
+
const result = schema.safeParse(data);
|
|
310
|
+
if (!result.success) {
|
|
311
|
+
throw new ValidationError(result.error.errors);
|
|
312
|
+
}
|
|
313
|
+
return result.data;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return data as T;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// ============================================
|
|
320
|
+
// 🥟 Response 보내기
|
|
321
|
+
// ============================================
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Response에 쿠키 헤더 적용 (내부 사용)
|
|
325
|
+
*/
|
|
326
|
+
private withCookies(response: Response): Response {
|
|
327
|
+
if (this._cookies.hasPendingCookies()) {
|
|
328
|
+
return this._cookies.applyToResponse(response);
|
|
329
|
+
}
|
|
330
|
+
return response;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** 200 OK */
|
|
334
|
+
ok<T>(data: T): Response {
|
|
335
|
+
return this.json(data, 200);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** 201 Created */
|
|
339
|
+
created<T>(data: T): Response {
|
|
340
|
+
return this.json(data, 201);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** 204 No Content */
|
|
344
|
+
noContent(): Response {
|
|
345
|
+
return this.withCookies(new Response(null, { status: 204 }));
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** 400 Bad Request */
|
|
349
|
+
error(message: string, details?: unknown): Response {
|
|
350
|
+
return this.json({ status: "error", message, details }, 400);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** 401 Unauthorized */
|
|
354
|
+
unauthorized(message: string = "Unauthorized"): Response {
|
|
355
|
+
return this.json({ status: "error", message }, 401);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** 403 Forbidden */
|
|
359
|
+
forbidden(message: string = "Forbidden"): Response {
|
|
360
|
+
return this.json({ status: "error", message }, 403);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** 404 Not Found */
|
|
364
|
+
notFound(message: string = "Not Found"): Response {
|
|
365
|
+
return this.json({ status: "error", message }, 404);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** 500 Internal Server Error */
|
|
369
|
+
fail(message: string = "Internal Server Error"): Response {
|
|
370
|
+
return this.json({ status: "error", message }, 500);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** Custom JSON response */
|
|
374
|
+
json<T>(data: T, status: number = 200): Response {
|
|
375
|
+
const response = Response.json(data, { status });
|
|
376
|
+
return this.withCookies(response);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** Custom text response */
|
|
380
|
+
text(data: string, status: number = 200): Response {
|
|
381
|
+
const response = new Response(data, {
|
|
382
|
+
status,
|
|
383
|
+
headers: { "Content-Type": "text/plain" },
|
|
384
|
+
});
|
|
385
|
+
return this.withCookies(response);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** Custom HTML response */
|
|
389
|
+
html(data: string, status: number = 200): Response {
|
|
390
|
+
const response = new Response(data, {
|
|
391
|
+
status,
|
|
392
|
+
headers: { "Content-Type": "text/html" },
|
|
393
|
+
});
|
|
394
|
+
return this.withCookies(response);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** Redirect response */
|
|
398
|
+
redirect(url: string, status: 301 | 302 | 307 | 308 = 302): Response {
|
|
399
|
+
const response = Response.redirect(url, status);
|
|
400
|
+
return this.withCookies(response);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// ============================================
|
|
404
|
+
// 🥟 상태 저장 (Lifecycle → Handler 전달)
|
|
405
|
+
// ============================================
|
|
406
|
+
|
|
407
|
+
/** Store value for later use */
|
|
408
|
+
set<T>(key: string, value: T): void {
|
|
409
|
+
this.store.set(key, value);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/** Get stored value */
|
|
413
|
+
get<T>(key: string): T | undefined {
|
|
414
|
+
return this.store.get(key) as T | undefined;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** Check if key exists */
|
|
418
|
+
has(key: string): boolean {
|
|
419
|
+
return this.store.has(key);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/** Route context for error reporting */
|
|
424
|
+
export interface ValidationRouteContext {
|
|
425
|
+
routeId: string;
|
|
426
|
+
pattern: string;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/** Validation error with details */
|
|
430
|
+
export class ValidationError extends Error {
|
|
431
|
+
constructor(
|
|
432
|
+
public readonly errors: unknown[],
|
|
433
|
+
public readonly routeContext?: ValidationRouteContext
|
|
434
|
+
) {
|
|
435
|
+
super("Validation failed");
|
|
436
|
+
this.name = "ValidationError";
|
|
437
|
+
}
|
|
438
|
+
}
|