-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrequest-headers.ts
More file actions
132 lines (114 loc) · 3.52 KB
/
request-headers.ts
File metadata and controls
132 lines (114 loc) · 3.52 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
/** AUTOGENERATED - DO NOT EDIT **/
/* tslint:disable */
/* eslint-disable */
import {
t_GetHeadersRequestHeaderSchema,
t_getHeadersRequestJson200Response,
t_getHeadersUndeclaredJson200Response,
} from "../models"
import {
s_getHeadersRequestJson200Response,
s_getHeadersUndeclaredJson200Response,
} from "../schemas"
import KoaRouter, { RouterContext } from "@koa/router"
import {
KoaRuntimeError,
RequestInputType,
} from "@nahkies/typescript-koa-runtime/errors"
import {
KoaRuntimeResponse,
Params,
Response,
b,
} from "@nahkies/typescript-koa-runtime/server"
import { parseRequestInput } from "@nahkies/typescript-koa-runtime/zod"
import { z } from "zod"
const getHeadersUndeclared = b((r) => ({
with200: r.with200<t_getHeadersUndeclaredJson200Response>(
s_getHeadersUndeclaredJson200Response,
),
withStatus: r.withStatus,
}))
export type GetHeadersUndeclared = (
params: Params<void, void, void, void>,
respond: (typeof getHeadersUndeclared)["responder"],
ctx: RouterContext,
) => Promise<
| KoaRuntimeResponse<unknown>
| Response<200, t_getHeadersUndeclaredJson200Response>
>
const getHeadersRequest = b((r) => ({
with200: r.with200<t_getHeadersRequestJson200Response>(
s_getHeadersRequestJson200Response,
),
withStatus: r.withStatus,
}))
export type GetHeadersRequest = (
params: Params<void, void, void, t_GetHeadersRequestHeaderSchema>,
respond: (typeof getHeadersRequest)["responder"],
ctx: RouterContext,
) => Promise<
| KoaRuntimeResponse<unknown>
| Response<200, t_getHeadersRequestJson200Response>
>
export type RequestHeadersImplementation = {
getHeadersUndeclared: GetHeadersUndeclared
getHeadersRequest: GetHeadersRequest
}
export function createRequestHeadersRouter(
implementation: RequestHeadersImplementation,
): KoaRouter {
const router = new KoaRouter()
router.get(
"getHeadersUndeclared",
"/headers/undeclared",
async (ctx, next) => {
const input = {
params: undefined,
query: undefined,
body: undefined,
headers: undefined,
}
const response = await implementation
.getHeadersUndeclared(input, getHeadersUndeclared.responder, ctx)
.catch((err) => {
throw KoaRuntimeError.HandlerError(err)
})
const { status, body } =
response instanceof KoaRuntimeResponse ? response.unpack() : response
ctx.body = getHeadersUndeclared.validator(status, body)
ctx.status = status
return next()
},
)
const getHeadersRequestHeaderSchema = z.object({
"route-level-header": z.string().optional(),
"number-header": z.coerce.number().optional(),
authorization: z.string().optional(),
})
router.get("getHeadersRequest", "/headers/request", async (ctx, next) => {
const input = {
params: undefined,
query: undefined,
body: undefined,
headers: parseRequestInput(
getHeadersRequestHeaderSchema,
Reflect.get(ctx.request, "headers"),
RequestInputType.RequestHeader,
),
}
const response = await implementation
.getHeadersRequest(input, getHeadersRequest.responder, ctx)
.catch((err) => {
throw KoaRuntimeError.HandlerError(err)
})
const { status, body } =
response instanceof KoaRuntimeResponse ? response.unpack() : response
ctx.body = getHeadersRequest.validator(status, body)
ctx.status = status
return next()
})
return router
}
export { createRequestHeadersRouter as createRouter }
export type { RequestHeadersImplementation as Implementation }