@kood/claude-code 0.1.7 → 0.1.10
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/dist/index.js +137 -3
- package/package.json +8 -2
- package/templates/hono/CLAUDE.md +53 -326
- package/templates/hono/docs/architecture/architecture.md +93 -747
- package/templates/hono/docs/deployment/cloudflare.md +59 -513
- package/templates/hono/docs/deployment/docker.md +41 -356
- package/templates/hono/docs/deployment/index.md +49 -190
- package/templates/hono/docs/deployment/railway.md +36 -306
- package/templates/hono/docs/deployment/vercel.md +49 -434
- package/templates/hono/docs/library/ai-sdk/index.md +53 -290
- package/templates/hono/docs/library/ai-sdk/openrouter.md +19 -387
- package/templates/hono/docs/library/ai-sdk/providers.md +28 -394
- package/templates/hono/docs/library/ai-sdk/streaming.md +52 -353
- package/templates/hono/docs/library/ai-sdk/structured-output.md +63 -395
- package/templates/hono/docs/library/ai-sdk/tools.md +62 -431
- package/templates/hono/docs/library/hono/env-setup.md +24 -313
- package/templates/hono/docs/library/hono/error-handling.md +34 -295
- package/templates/hono/docs/library/hono/index.md +24 -122
- package/templates/hono/docs/library/hono/middleware.md +21 -188
- package/templates/hono/docs/library/hono/rpc.md +40 -341
- package/templates/hono/docs/library/hono/validation.md +35 -195
- package/templates/hono/docs/library/pino/index.md +42 -333
- package/templates/hono/docs/library/prisma/cloudflare-d1.md +64 -367
- package/templates/hono/docs/library/prisma/config.md +19 -260
- package/templates/hono/docs/library/prisma/index.md +64 -320
- package/templates/hono/docs/library/zod/index.md +53 -257
- package/templates/npx/CLAUDE.md +58 -276
- package/templates/npx/docs/references/patterns.md +160 -0
- package/templates/tanstack-start/CLAUDE.md +0 -4
- package/templates/tanstack-start/docs/architecture/architecture.md +44 -589
- package/templates/tanstack-start/docs/design/index.md +119 -12
- package/templates/tanstack-start/docs/guides/conventions.md +103 -0
- package/templates/tanstack-start/docs/guides/env-setup.md +34 -340
- package/templates/tanstack-start/docs/guides/getting-started.md +22 -209
- package/templates/tanstack-start/docs/guides/hooks.md +166 -0
- package/templates/tanstack-start/docs/guides/routes.md +166 -0
- package/templates/tanstack-start/docs/guides/services.md +143 -0
- package/templates/tanstack-start/docs/library/tanstack-query/index.md +18 -2
- package/templates/tanstack-start/docs/library/zod/index.md +16 -1
- package/templates/tanstack-start/docs/design/accessibility.md +0 -163
- package/templates/tanstack-start/docs/design/color.md +0 -93
- package/templates/tanstack-start/docs/design/spacing.md +0 -122
- package/templates/tanstack-start/docs/design/typography.md +0 -80
- package/templates/tanstack-start/docs/guides/best-practices.md +0 -950
- package/templates/tanstack-start/docs/guides/husky-lint-staged.md +0 -303
- package/templates/tanstack-start/docs/guides/prettier.md +0 -189
- package/templates/tanstack-start/docs/guides/project-templates.md +0 -710
- package/templates/tanstack-start/docs/library/tanstack-query/setup.md +0 -48
- package/templates/tanstack-start/docs/library/zod/basic-types.md +0 -74
|
@@ -6,16 +6,10 @@
|
|
|
6
6
|
|
|
7
7
|
## HTTPException
|
|
8
8
|
|
|
9
|
-
### 기본 사용
|
|
10
|
-
|
|
11
9
|
```typescript
|
|
12
|
-
import { Hono } from 'hono'
|
|
13
10
|
import { HTTPException } from 'hono/http-exception'
|
|
14
11
|
|
|
15
|
-
const app = new Hono()
|
|
16
|
-
|
|
17
12
|
app.get('/users/:id', async (c) => {
|
|
18
|
-
const id = c.req.param('id')
|
|
19
13
|
const user = await prisma.user.findUnique({ where: { id } })
|
|
20
14
|
|
|
21
15
|
if (!user) {
|
|
@@ -26,49 +20,22 @@ app.get('/users/:id', async (c) => {
|
|
|
26
20
|
})
|
|
27
21
|
```
|
|
28
22
|
|
|
29
|
-
###
|
|
23
|
+
### 일반적인 상태 코드
|
|
30
24
|
|
|
31
25
|
```typescript
|
|
32
|
-
throw new HTTPException(400, {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
```typescript
|
|
41
|
-
// 400 Bad Request - 잘못된 요청
|
|
42
|
-
throw new HTTPException(400, { message: 'Invalid input' })
|
|
43
|
-
|
|
44
|
-
// 401 Unauthorized - 인증 필요
|
|
45
|
-
throw new HTTPException(401, { message: 'Authentication required' })
|
|
46
|
-
|
|
47
|
-
// 403 Forbidden - 권한 없음
|
|
48
|
-
throw new HTTPException(403, { message: 'Access denied' })
|
|
49
|
-
|
|
50
|
-
// 404 Not Found - 리소스 없음
|
|
51
|
-
throw new HTTPException(404, { message: 'Resource not found' })
|
|
52
|
-
|
|
53
|
-
// 409 Conflict - 충돌
|
|
54
|
-
throw new HTTPException(409, { message: 'Resource already exists' })
|
|
55
|
-
|
|
56
|
-
// 422 Unprocessable Entity - 검증 실패
|
|
57
|
-
throw new HTTPException(422, { message: 'Validation failed' })
|
|
58
|
-
|
|
59
|
-
// 429 Too Many Requests - 요청 제한 초과
|
|
60
|
-
throw new HTTPException(429, { message: 'Rate limit exceeded' })
|
|
61
|
-
|
|
62
|
-
// 500 Internal Server Error - 서버 에러
|
|
63
|
-
throw new HTTPException(500, { message: 'Internal server error' })
|
|
26
|
+
throw new HTTPException(400, { message: 'Invalid input' }) // Bad Request
|
|
27
|
+
throw new HTTPException(401, { message: 'Unauthorized' }) // 인증 필요
|
|
28
|
+
throw new HTTPException(403, { message: 'Access denied' }) // 권한 없음
|
|
29
|
+
throw new HTTPException(404, { message: 'Not found' }) // 리소스 없음
|
|
30
|
+
throw new HTTPException(409, { message: 'Already exists' }) // 충돌
|
|
31
|
+
throw new HTTPException(422, { message: 'Validation failed' }) // 검증 실패
|
|
32
|
+
throw new HTTPException(429, { message: 'Rate limit exceeded' })// 요청 제한
|
|
64
33
|
```
|
|
65
34
|
|
|
66
35
|
---
|
|
67
36
|
|
|
68
37
|
## 글로벌 에러 핸들러
|
|
69
38
|
|
|
70
|
-
### onError
|
|
71
|
-
|
|
72
39
|
```typescript
|
|
73
40
|
import { Hono } from 'hono'
|
|
74
41
|
import { HTTPException } from 'hono/http-exception'
|
|
@@ -76,144 +43,45 @@ import { HTTPException } from 'hono/http-exception'
|
|
|
76
43
|
const app = new Hono()
|
|
77
44
|
|
|
78
45
|
app.onError((err, c) => {
|
|
79
|
-
console.error(
|
|
80
|
-
|
|
81
|
-
// HTTPException 처리
|
|
82
|
-
if (err instanceof HTTPException) {
|
|
83
|
-
return c.json(
|
|
84
|
-
{
|
|
85
|
-
error: err.message,
|
|
86
|
-
status: err.status,
|
|
87
|
-
},
|
|
88
|
-
err.status
|
|
89
|
-
)
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// 기타 에러
|
|
93
|
-
return c.json(
|
|
94
|
-
{
|
|
95
|
-
error: 'Internal Server Error',
|
|
96
|
-
message: err.message,
|
|
97
|
-
},
|
|
98
|
-
500
|
|
99
|
-
)
|
|
100
|
-
})
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
### 상세 에러 응답
|
|
104
|
-
|
|
105
|
-
```typescript
|
|
106
|
-
app.onError((err, c) => {
|
|
107
|
-
const requestId = c.get('requestId')
|
|
46
|
+
console.error(err)
|
|
108
47
|
|
|
109
|
-
// HTTPException
|
|
110
48
|
if (err instanceof HTTPException) {
|
|
111
|
-
return c.json(
|
|
112
|
-
{
|
|
113
|
-
success: false,
|
|
114
|
-
error: {
|
|
115
|
-
code: err.status,
|
|
116
|
-
message: err.message,
|
|
117
|
-
requestId,
|
|
118
|
-
},
|
|
119
|
-
},
|
|
120
|
-
err.status
|
|
121
|
-
)
|
|
49
|
+
return c.json({ error: err.message, status: err.status }, err.status)
|
|
122
50
|
}
|
|
123
51
|
|
|
124
|
-
|
|
125
|
-
if (err.name === 'ZodError') {
|
|
126
|
-
return c.json(
|
|
127
|
-
{
|
|
128
|
-
success: false,
|
|
129
|
-
error: {
|
|
130
|
-
code: 400,
|
|
131
|
-
message: 'Validation failed',
|
|
132
|
-
details: err.flatten(),
|
|
133
|
-
requestId,
|
|
134
|
-
},
|
|
135
|
-
},
|
|
136
|
-
400
|
|
137
|
-
)
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// 개발 환경에서만 스택 트레이스 포함
|
|
141
|
-
const isDev = c.env.NODE_ENV === 'development'
|
|
142
|
-
|
|
143
|
-
return c.json(
|
|
144
|
-
{
|
|
145
|
-
success: false,
|
|
146
|
-
error: {
|
|
147
|
-
code: 500,
|
|
148
|
-
message: 'Internal Server Error',
|
|
149
|
-
...(isDev && { stack: err.stack }),
|
|
150
|
-
requestId,
|
|
151
|
-
},
|
|
152
|
-
},
|
|
153
|
-
500
|
|
154
|
-
)
|
|
52
|
+
return c.json({ error: 'Internal Server Error' }, 500)
|
|
155
53
|
})
|
|
156
|
-
```
|
|
157
54
|
|
|
158
|
-
---
|
|
159
|
-
|
|
160
|
-
## 404 핸들러
|
|
161
|
-
|
|
162
|
-
### notFound
|
|
163
|
-
|
|
164
|
-
```typescript
|
|
165
55
|
app.notFound((c) => {
|
|
166
|
-
return c.json(
|
|
167
|
-
{
|
|
168
|
-
error: 'Not Found',
|
|
169
|
-
path: c.req.path,
|
|
170
|
-
method: c.req.method,
|
|
171
|
-
},
|
|
172
|
-
404
|
|
173
|
-
)
|
|
56
|
+
return c.json({ error: 'Not Found', path: c.req.path }, 404)
|
|
174
57
|
})
|
|
175
58
|
```
|
|
176
59
|
|
|
177
60
|
---
|
|
178
61
|
|
|
179
|
-
##
|
|
62
|
+
## 상세 에러 응답
|
|
180
63
|
|
|
181
64
|
```typescript
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
api.onError((err, c) => {
|
|
186
|
-
console.log('API-specific error handler')
|
|
65
|
+
app.onError((err, c) => {
|
|
66
|
+
const requestId = c.get('requestId')
|
|
67
|
+
const isDev = c.env.NODE_ENV === 'development'
|
|
187
68
|
|
|
188
69
|
if (err instanceof HTTPException) {
|
|
189
|
-
return c.json({
|
|
70
|
+
return c.json({
|
|
71
|
+
success: false,
|
|
72
|
+
error: { status: err.status, message: err.message, requestId },
|
|
73
|
+
}, err.status)
|
|
190
74
|
}
|
|
191
75
|
|
|
192
|
-
return c.json({
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
---
|
|
203
|
-
|
|
204
|
-
## 미들웨어에서 에러 접근
|
|
205
|
-
|
|
206
|
-
### c.error 사용
|
|
207
|
-
|
|
208
|
-
```typescript
|
|
209
|
-
app.use(async (c, next) => {
|
|
210
|
-
await next()
|
|
211
|
-
|
|
212
|
-
// 핸들러에서 발생한 에러 접근
|
|
213
|
-
if (c.error) {
|
|
214
|
-
// 로깅, 모니터링 등
|
|
215
|
-
console.error('Error occurred:', c.error)
|
|
216
|
-
}
|
|
76
|
+
return c.json({
|
|
77
|
+
success: false,
|
|
78
|
+
error: {
|
|
79
|
+
status: 500,
|
|
80
|
+
message: 'Internal Server Error',
|
|
81
|
+
requestId,
|
|
82
|
+
...(isDev && { stack: err.stack }),
|
|
83
|
+
},
|
|
84
|
+
}, 500)
|
|
217
85
|
})
|
|
218
86
|
```
|
|
219
87
|
|
|
@@ -221,7 +89,7 @@ app.use(async (c, next) => {
|
|
|
221
89
|
|
|
222
90
|
## 커스텀 에러 클래스
|
|
223
91
|
|
|
224
|
-
### errors.ts
|
|
92
|
+
### lib/errors.ts
|
|
225
93
|
|
|
226
94
|
```typescript
|
|
227
95
|
import { HTTPException } from 'hono/http-exception'
|
|
@@ -238,18 +106,6 @@ export class UnauthorizedError extends HTTPException {
|
|
|
238
106
|
}
|
|
239
107
|
}
|
|
240
108
|
|
|
241
|
-
export class ForbiddenError extends HTTPException {
|
|
242
|
-
constructor(message = 'Access denied') {
|
|
243
|
-
super(403, { message })
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
export class ValidationError extends HTTPException {
|
|
248
|
-
constructor(message: string, public details?: unknown) {
|
|
249
|
-
super(422, { message })
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
|
|
253
109
|
export class ConflictError extends HTTPException {
|
|
254
110
|
constructor(resource: string) {
|
|
255
111
|
super(409, { message: `${resource} already exists` })
|
|
@@ -264,137 +120,20 @@ import { NotFoundError, ConflictError } from '@/lib/errors'
|
|
|
264
120
|
|
|
265
121
|
app.get('/users/:id', async (c) => {
|
|
266
122
|
const user = await prisma.user.findUnique({ where: { id } })
|
|
267
|
-
|
|
268
|
-
if (!user) {
|
|
269
|
-
throw new NotFoundError('User')
|
|
270
|
-
}
|
|
271
|
-
|
|
123
|
+
if (!user) throw new NotFoundError('User')
|
|
272
124
|
return c.json({ user })
|
|
273
125
|
})
|
|
274
126
|
|
|
275
127
|
app.post('/users', async (c) => {
|
|
276
|
-
const
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
})
|
|
280
|
-
|
|
281
|
-
if (existing) {
|
|
282
|
-
throw new ConflictError('User with this email')
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
const user = await prisma.user.create({ data })
|
|
286
|
-
return c.json({ user }, 201)
|
|
287
|
-
})
|
|
288
|
-
```
|
|
289
|
-
|
|
290
|
-
---
|
|
291
|
-
|
|
292
|
-
## 스트리밍 에러 처리
|
|
293
|
-
|
|
294
|
-
```typescript
|
|
295
|
-
import { stream } from 'hono/streaming'
|
|
296
|
-
|
|
297
|
-
app.get('/stream', (c) => {
|
|
298
|
-
return stream(
|
|
299
|
-
c,
|
|
300
|
-
async (stream) => {
|
|
301
|
-
stream.onAbort(() => {
|
|
302
|
-
console.log('Stream aborted')
|
|
303
|
-
})
|
|
304
|
-
|
|
305
|
-
await stream.write(new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f]))
|
|
306
|
-
},
|
|
307
|
-
// 에러 핸들러 (선택적)
|
|
308
|
-
(err, stream) => {
|
|
309
|
-
stream.writeln('An error occurred!')
|
|
310
|
-
console.error(err)
|
|
311
|
-
}
|
|
312
|
-
)
|
|
128
|
+
const existing = await prisma.user.findUnique({ where: { email } })
|
|
129
|
+
if (existing) throw new ConflictError('User with this email')
|
|
130
|
+
// ...
|
|
313
131
|
})
|
|
314
132
|
```
|
|
315
133
|
|
|
316
134
|
---
|
|
317
135
|
|
|
318
|
-
## 완전한 에러 처리 설정
|
|
319
|
-
|
|
320
|
-
```typescript
|
|
321
|
-
import { Hono } from 'hono'
|
|
322
|
-
import { HTTPException } from 'hono/http-exception'
|
|
323
|
-
import { logger } from 'hono/logger'
|
|
324
|
-
import { requestId } from 'hono/request-id'
|
|
325
|
-
|
|
326
|
-
type Env = {
|
|
327
|
-
Bindings: {
|
|
328
|
-
NODE_ENV: string
|
|
329
|
-
}
|
|
330
|
-
Variables: {
|
|
331
|
-
requestId: string
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
const app = new Hono<Env>()
|
|
336
|
-
|
|
337
|
-
// 미들웨어
|
|
338
|
-
app.use(logger())
|
|
339
|
-
app.use(requestId())
|
|
340
|
-
|
|
341
|
-
// 글로벌 에러 핸들러
|
|
342
|
-
app.onError((err, c) => {
|
|
343
|
-
const reqId = c.get('requestId')
|
|
344
|
-
const isDev = c.env.NODE_ENV === 'development'
|
|
345
|
-
|
|
346
|
-
console.error(`[${reqId}] Error:`, err)
|
|
347
|
-
|
|
348
|
-
if (err instanceof HTTPException) {
|
|
349
|
-
return c.json(
|
|
350
|
-
{
|
|
351
|
-
success: false,
|
|
352
|
-
error: {
|
|
353
|
-
status: err.status,
|
|
354
|
-
message: err.message,
|
|
355
|
-
requestId: reqId,
|
|
356
|
-
},
|
|
357
|
-
},
|
|
358
|
-
err.status
|
|
359
|
-
)
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
return c.json(
|
|
363
|
-
{
|
|
364
|
-
success: false,
|
|
365
|
-
error: {
|
|
366
|
-
status: 500,
|
|
367
|
-
message: 'Internal Server Error',
|
|
368
|
-
requestId: reqId,
|
|
369
|
-
...(isDev && { stack: err.stack }),
|
|
370
|
-
},
|
|
371
|
-
},
|
|
372
|
-
500
|
|
373
|
-
)
|
|
374
|
-
})
|
|
375
|
-
|
|
376
|
-
// 404 핸들러
|
|
377
|
-
app.notFound((c) => {
|
|
378
|
-
return c.json(
|
|
379
|
-
{
|
|
380
|
-
success: false,
|
|
381
|
-
error: {
|
|
382
|
-
status: 404,
|
|
383
|
-
message: 'Not Found',
|
|
384
|
-
path: c.req.path,
|
|
385
|
-
},
|
|
386
|
-
},
|
|
387
|
-
404
|
|
388
|
-
)
|
|
389
|
-
})
|
|
390
|
-
|
|
391
|
-
export default app
|
|
392
|
-
```
|
|
393
|
-
|
|
394
|
-
---
|
|
395
|
-
|
|
396
136
|
## 관련 문서
|
|
397
137
|
|
|
398
138
|
- [기본 사용법](./index.md)
|
|
399
139
|
- [미들웨어](./middleware.md)
|
|
400
|
-
- [Zod 검증](./validation.md)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Hono - Web Framework
|
|
2
2
|
|
|
3
|
-
> Web Standards 기반
|
|
3
|
+
> Web Standards 기반 초경량 서버 프레임워크
|
|
4
4
|
|
|
5
5
|
@env-setup.md
|
|
6
6
|
@middleware.md
|
|
@@ -10,70 +10,23 @@
|
|
|
10
10
|
|
|
11
11
|
---
|
|
12
12
|
|
|
13
|
-
## 개요
|
|
14
|
-
|
|
15
|
-
Hono는 Cloudflare Workers, Deno, Bun, Node.js 등 모든 JavaScript 런타임에서 동작하는 서버 프레임워크입니다.
|
|
16
|
-
|
|
17
|
-
---
|
|
18
|
-
|
|
19
13
|
## 설치
|
|
20
14
|
|
|
21
15
|
```bash
|
|
22
|
-
# npm
|
|
23
16
|
npm install hono
|
|
24
|
-
|
|
25
|
-
# bun
|
|
26
|
-
bun add hono
|
|
27
|
-
|
|
28
|
-
# yarn
|
|
29
|
-
yarn add hono
|
|
30
17
|
```
|
|
31
18
|
|
|
32
19
|
---
|
|
33
20
|
|
|
34
21
|
## 기본 사용법
|
|
35
22
|
|
|
36
|
-
### App 생성
|
|
37
|
-
|
|
38
23
|
```typescript
|
|
39
24
|
import { Hono } from 'hono'
|
|
40
25
|
|
|
41
26
|
const app = new Hono()
|
|
42
27
|
|
|
43
|
-
app.get('/', (c) =>
|
|
44
|
-
return c.text('Hello Hono!')
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
export default app
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
### HTTP 메서드
|
|
51
|
-
|
|
52
|
-
```typescript
|
|
53
|
-
const app = new Hono()
|
|
54
|
-
|
|
55
|
-
app.get('/', (c) => c.text('GET /'))
|
|
28
|
+
app.get('/', (c) => c.text('Hello Hono!'))
|
|
56
29
|
app.post('/', (c) => c.text('POST /'))
|
|
57
|
-
app.put('/', (c) => c.text('PUT /'))
|
|
58
|
-
app.delete('/', (c) => c.text('DELETE /'))
|
|
59
|
-
app.patch('/', (c) => c.text('PATCH /'))
|
|
60
|
-
|
|
61
|
-
// 모든 메서드
|
|
62
|
-
app.all('/hello', (c) => c.text('Any Method /hello'))
|
|
63
|
-
|
|
64
|
-
// 여러 메서드
|
|
65
|
-
app.on(['PUT', 'DELETE'], '/post', (c) => c.text('PUT or DELETE /post'))
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
---
|
|
69
|
-
|
|
70
|
-
## 라우팅
|
|
71
|
-
|
|
72
|
-
### 기본 라우팅
|
|
73
|
-
|
|
74
|
-
```typescript
|
|
75
|
-
// 정적 경로
|
|
76
|
-
app.get('/users', (c) => c.json({ users: [] }))
|
|
77
30
|
|
|
78
31
|
// 동적 파라미터
|
|
79
32
|
app.get('/users/:id', (c) => {
|
|
@@ -86,34 +39,22 @@ app.get('/posts/:postId/comments/:commentId', (c) => {
|
|
|
86
39
|
const { postId, commentId } = c.req.param()
|
|
87
40
|
return c.json({ postId, commentId })
|
|
88
41
|
})
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
### 와일드카드
|
|
92
|
-
|
|
93
|
-
```typescript
|
|
94
|
-
// 와일드카드 매칭
|
|
95
|
-
app.get('/wild/*/card', (c) => {
|
|
96
|
-
return c.text('GET /wild/*/card')
|
|
97
|
-
})
|
|
98
42
|
|
|
99
|
-
|
|
100
|
-
app.get('/api/*', (c) => {
|
|
101
|
-
return c.text('API catch-all')
|
|
102
|
-
})
|
|
43
|
+
export default app
|
|
103
44
|
```
|
|
104
45
|
|
|
105
|
-
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## 라우트 그룹화
|
|
106
49
|
|
|
107
50
|
```typescript
|
|
108
51
|
const app = new Hono()
|
|
109
52
|
|
|
110
|
-
// 서브 라우트
|
|
111
53
|
const users = new Hono()
|
|
112
54
|
users.get('/', (c) => c.json({ users: [] }))
|
|
113
55
|
users.get('/:id', (c) => c.json({ id: c.req.param('id') }))
|
|
114
56
|
users.post('/', (c) => c.json({ created: true }, 201))
|
|
115
57
|
|
|
116
|
-
// 마운트
|
|
117
58
|
app.route('/users', users)
|
|
118
59
|
```
|
|
119
60
|
|
|
@@ -121,26 +62,16 @@ app.route('/users', users)
|
|
|
121
62
|
|
|
122
63
|
## Context (c)
|
|
123
64
|
|
|
124
|
-
### Request
|
|
65
|
+
### Request
|
|
125
66
|
|
|
126
67
|
```typescript
|
|
127
68
|
app.get('/info', (c) => {
|
|
128
|
-
// URL 정보
|
|
129
69
|
const url = c.req.url
|
|
130
70
|
const path = c.req.path
|
|
131
71
|
const method = c.req.method
|
|
132
|
-
|
|
133
|
-
// 파라미터
|
|
134
72
|
const id = c.req.param('id')
|
|
135
|
-
const params = c.req.param() // 모든 파라미터
|
|
136
|
-
|
|
137
|
-
// 쿼리 스트링
|
|
138
73
|
const page = c.req.query('page')
|
|
139
|
-
const queries = c.req.query() // 모든 쿼리
|
|
140
|
-
|
|
141
|
-
// 헤더
|
|
142
74
|
const auth = c.req.header('Authorization')
|
|
143
|
-
|
|
144
75
|
return c.json({ url, path, method })
|
|
145
76
|
})
|
|
146
77
|
```
|
|
@@ -148,86 +79,57 @@ app.get('/info', (c) => {
|
|
|
148
79
|
### Request Body
|
|
149
80
|
|
|
150
81
|
```typescript
|
|
151
|
-
// JSON
|
|
152
82
|
app.post('/json', async (c) => {
|
|
153
83
|
const body = await c.req.json()
|
|
154
84
|
return c.json(body)
|
|
155
85
|
})
|
|
156
86
|
|
|
157
|
-
// Form Data
|
|
158
87
|
app.post('/form', async (c) => {
|
|
159
88
|
const body = await c.req.parseBody()
|
|
160
89
|
return c.json(body)
|
|
161
90
|
})
|
|
162
|
-
|
|
163
|
-
// Raw Text
|
|
164
|
-
app.post('/text', async (c) => {
|
|
165
|
-
const text = await c.req.text()
|
|
166
|
-
return c.text(text)
|
|
167
|
-
})
|
|
168
91
|
```
|
|
169
92
|
|
|
170
|
-
### Response
|
|
93
|
+
### Response
|
|
171
94
|
|
|
172
95
|
```typescript
|
|
173
|
-
// Text
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
//
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
// JSON with status
|
|
180
|
-
app.post('/created', (c) => c.json({ id: 1 }, 201))
|
|
181
|
-
|
|
182
|
-
// HTML
|
|
183
|
-
app.get('/html', (c) => c.html('<h1>Hello</h1>'))
|
|
184
|
-
|
|
185
|
-
// Redirect
|
|
186
|
-
app.get('/old', (c) => c.redirect('/new'))
|
|
187
|
-
|
|
188
|
-
// Not Found
|
|
189
|
-
app.get('/404', (c) => c.notFound())
|
|
96
|
+
c.text('Hello') // Text
|
|
97
|
+
c.json({ message: 'Hello' }) // JSON
|
|
98
|
+
c.json({ id: 1 }, 201) // JSON + status
|
|
99
|
+
c.html('<h1>Hello</h1>') // HTML
|
|
100
|
+
c.redirect('/new') // Redirect
|
|
101
|
+
c.notFound() // 404
|
|
190
102
|
```
|
|
191
103
|
|
|
192
104
|
---
|
|
193
105
|
|
|
194
|
-
##
|
|
106
|
+
## Bindings & Variables
|
|
195
107
|
|
|
196
108
|
```typescript
|
|
197
109
|
type Bindings = {
|
|
198
110
|
DATABASE_URL: string
|
|
199
111
|
JWT_SECRET: string
|
|
200
|
-
MY_BUCKET: R2Bucket // Cloudflare R2
|
|
201
112
|
}
|
|
202
113
|
|
|
203
|
-
const app = new Hono<{ Bindings: Bindings }>()
|
|
204
|
-
|
|
205
|
-
app.get('/', (c) => {
|
|
206
|
-
const dbUrl = c.env.DATABASE_URL
|
|
207
|
-
const secret = c.env.JWT_SECRET
|
|
208
|
-
return c.json({ connected: true })
|
|
209
|
-
})
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
---
|
|
213
|
-
|
|
214
|
-
## Variables (상태 공유)
|
|
215
|
-
|
|
216
|
-
```typescript
|
|
217
114
|
type Variables = {
|
|
218
115
|
userId: string
|
|
219
116
|
user: { id: string; name: string }
|
|
220
117
|
}
|
|
221
118
|
|
|
222
|
-
const app = new Hono<{ Variables: Variables }>()
|
|
119
|
+
const app = new Hono<{ Bindings: Bindings; Variables: Variables }>()
|
|
120
|
+
|
|
121
|
+
// 환경 변수 접근
|
|
122
|
+
app.get('/', (c) => {
|
|
123
|
+
const dbUrl = c.env.DATABASE_URL
|
|
124
|
+
return c.json({ connected: true })
|
|
125
|
+
})
|
|
223
126
|
|
|
224
|
-
//
|
|
127
|
+
// Variables 설정/사용
|
|
225
128
|
app.use(async (c, next) => {
|
|
226
129
|
c.set('userId', '123')
|
|
227
130
|
await next()
|
|
228
131
|
})
|
|
229
132
|
|
|
230
|
-
// 핸들러에서 사용
|
|
231
133
|
app.get('/me', (c) => {
|
|
232
134
|
const userId = c.get('userId')
|
|
233
135
|
return c.json({ userId })
|
|
@@ -238,7 +140,7 @@ app.get('/me', (c) => {
|
|
|
238
140
|
|
|
239
141
|
## 관련 문서
|
|
240
142
|
|
|
241
|
-
- [환경 변수 설정](./env-setup.md)
|
|
143
|
+
- [환경 변수 설정](./env-setup.md)
|
|
242
144
|
- [미들웨어](./middleware.md)
|
|
243
145
|
- [Zod 검증](./validation.md)
|
|
244
146
|
- [에러 처리](./error-handling.md)
|