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
/
extensions.ts
157 lines (149 loc) · 4.36 KB
/
extensions.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
// Copyright 2023 Samuel Kopp. All rights reserved. Apache-2.0 license.
import { ZodString, ZodUnion } from 'zod'
import { Method } from './base.ts'
import { Handler, ObjectType } from './handler.ts'
import { AppContext, Context } from './mod.ts'
type HasRequired<T> = Partial<T> extends T ? false : true
type Req = Response | void | undefined
type Res = void | undefined
type ExtensionContext = {
/** The prefix of the routes to which this extension is assigned. */
prefix: string
}
export type Extension<
Config extends Record<string, unknown> | unknown = never,
> = {
__config: Config | undefined
onPlugIn: HasRequired<Config> extends true ? ((
context: ExtensionContext & {
env: AppContext['env']
routes: AppContext['routes']
runtime: AppContext['runtime']
setRoute: <
Pathname extends `/${string}`,
// deno-lint-ignore no-explicit-any
ValidatedBody extends ObjectType | ZodString | ZodUnion<any>,
ValidatedCookies extends ObjectType,
ValidatedHeaders extends ObjectType,
ValidatedQuery extends ObjectType,
>(
method: Method,
pathname: Pathname,
...handler: (
| {
body?: ValidatedBody
cookies?: ValidatedCookies
headers?: ValidatedHeaders
query?: ValidatedQuery
cors?: string
}
| Handler<
Pathname,
ValidatedBody,
ValidatedCookies,
ValidatedHeaders,
ValidatedQuery
>
)[]
) => void | Promise<void>
settings: Config
},
) => void | Promise<void>)
: ((
context: ExtensionContext & {
env: AppContext['env']
routes: AppContext['routes']
runtime: AppContext['runtime']
setRoute: <
Pathname extends `/${string}`,
// deno-lint-ignore no-explicit-any
ValidatedBody extends ObjectType | ZodString | ZodUnion<any>,
ValidatedCookies extends ObjectType,
ValidatedHeaders extends ObjectType,
ValidatedQuery extends ObjectType,
>(
method: Method,
pathname: Pathname,
...handlers: (
| {
body?: ValidatedBody
cookies?: ValidatedCookies
headers?: ValidatedHeaders
query?: ValidatedQuery
cors?: string
}
| Handler<
Pathname,
ValidatedBody,
ValidatedCookies,
ValidatedHeaders,
ValidatedQuery
>
)[]
) => void
settings?: Config
},
) => void | Promise<void>)
onRequest?: HasRequired<Config> extends true ? ((
context: ExtensionContext & {
app: AppContext
req: Request
_: Config
},
) => Req | Promise<Req>)
: ((
context: ExtensionContext & {
app: AppContext
req: Request
_?: Config
},
) => Req | Promise<Req>)
onResponse?: HasRequired<Config> extends true ? ((
context: ExtensionContext & {
app: AppContext
c: Context
_: Config
},
) => Res | Promise<Res>)
: ((
context: ExtensionContext & {
app: AppContext
c: Context
_?: Config
},
) => Res | Promise<Res>)
}
type ReturnFunction<
Config extends Record<string, unknown> | unknown = unknown,
> = Config extends Record<string, unknown>
? (HasRequired<Config> extends true ? ((config: Config) => Extension<Config>)
: ((config?: Config) => Extension<Config>))
: (() => Extension<Config>)
export function validExtension(ext: Record<string, unknown>) {
const symbol = Object.getOwnPropertySymbols(ext).find((s) =>
s.description === 'cheetah.extension'
)
// @ts-ignore:
return symbol !== undefined && ext[symbol] === 'v1.0'
}
export function createExtension<
Config extends Record<string, unknown> | unknown = unknown,
>({
onPlugIn,
onRequest,
onResponse,
}: {
onPlugIn?: Extension<Config>['onPlugIn']
onRequest?: Extension<Config>['onRequest']
onResponse?: Extension<Config>['onResponse']
}) {
return ((__config?: Config) => {
return {
__config,
onPlugIn,
onRequest,
onResponse,
[Symbol('cheetah.extension')]: 'v1.0',
}
}) as ReturnFunction<Config>
}