This repository has been archived by the owner on Jun 27, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.ts
169 lines (156 loc) · 4.18 KB
/
handler.ts
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
// Copyright 2023 Samuel Kopp. All rights reserved. Apache-2.0 license.
import {
ZodObject,
ZodRecord,
ZodString,
ZodType,
ZodTypeDef,
ZodUnion,
} from 'zod'
import { Context } from './mod.ts'
export type ObjectType =
// deno-lint-ignore no-explicit-any
| ZodObject<any>
| ZodRecord
export type BaseType<T extends ZodTypeDef = ZodTypeDef> =
// deno-lint-ignore no-explicit-any
ZodType<any, T, any>
type ExtractParam<Path, NextPart> = Path extends `:${infer Param}`
? Record<Param, string> & NextPart
: NextPart
type ExtractParams<Path> = Path extends `${infer Segment}/${infer Rest}`
? ExtractParam<Segment, ExtractParams<Rest>>
// deno-lint-ignore ban-types
: ExtractParam<Path, {}>
export type Payload =
| ArrayBuffer
| Blob
| FormData
| ReadableStream<unknown>
| Record<string, unknown>
| Uint8Array
| string
| undefined
| void
type Number = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
export type Version = `v${
| Exclude<Number, 0>
| `${Exclude<Number, 0>}${Number}`}`
export type VersionRange =
| Version // exact version
| `> ${Version}` // greater than version
| `< ${Version}` // smaller than version
| `>= ${Version}` // greater than or equal to version
| `<= ${Version}` // smaller than or equal to version
| `${Version}...${Version}` // from (min) ... to (max)
export type Handler<
Pathname extends `/${string}` | unknown,
// deno-lint-ignore no-explicit-any
ParsedBody extends ObjectType | ZodString | ZodUnion<any> = never,
ParsedCookies extends ObjectType = never,
ParsedHeaders extends ObjectType = never,
ParsedQuery extends ObjectType = never,
> = (
c: Context<
ExtractParams<Pathname>,
ParsedBody,
ParsedCookies,
ParsedHeaders,
ParsedQuery
>,
next: () => void,
) => Payload | Promise<Payload>
export function handler<T>() {
return <
Pathname extends `/${string}`,
// deno-lint-ignore no-explicit-any
ValidatedBody extends ObjectType | ZodString | ZodUnion<any> = never,
ValidatedCookies extends ObjectType = never,
ValidatedHeaders extends ObjectType = never,
ValidatedQuery extends ObjectType = never,
>(
// deno-lint-ignore no-unused-vars
pathname: Pathname,
// deno-lint-ignore no-unused-vars
...handler: (
| ({
body?: ValidatedBody
cookies?: ValidatedCookies
headers?: ValidatedHeaders
query?: ValidatedQuery
cors?: string
params?: Partial<Record<keyof ExtractParams<Pathname>, ZodType>>
gateway?: VersionRange
})
| Handler<
Pathname,
ValidatedBody,
ValidatedCookies,
ValidatedHeaders,
ValidatedQuery
>
)[]
): T => {
return null as T
}
}
export type BodylessHandler<
Pathname extends `/${string}` | unknown,
// deno-lint-ignore no-explicit-any
ParsedBody extends ObjectType | ZodString | ZodUnion<any> = never,
ParsedCookies extends ObjectType = never,
ParsedHeaders extends ObjectType = never,
ParsedQuery extends ObjectType = never,
> = (
c: Context<
ExtractParams<Pathname>,
ParsedBody,
ParsedCookies,
ParsedHeaders,
ParsedQuery
>,
next: () => void,
) => Payload | Promise<Payload>
export function bodylessHandler<T>() {
return <
Pathname extends `/${string}`,
ValidatedCookies extends ObjectType = never,
ValidatedHeaders extends ObjectType = never,
ValidatedQuery extends ObjectType = never,
>(
// deno-lint-ignore no-unused-vars
pathname: Pathname,
// deno-lint-ignore no-unused-vars
...handler: (
| {
cookies?: ValidatedCookies
headers?: ValidatedHeaders
query?: ValidatedQuery
cors?: string
params?: Partial<Record<keyof ExtractParams<Pathname>, ZodType>>
gateway?: VersionRange
}
| BodylessHandler<
Pathname,
never,
ValidatedCookies,
ValidatedHeaders,
ValidatedQuery
>
)[]
): T => {
return null as T
}
}
export type HandlerOrSchema =
| {
body?: ZodType
cookies?: ObjectType
headers?: ObjectType
query?: ObjectType
cors?: string
params?: Record<string, ZodType>
gateway?: VersionRange
}
| Handler<unknown>
| BodylessHandler<unknown>