/
route.ts
223 lines (195 loc) · 4.22 KB
/
route.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* @adonisjs/http-server
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import type Middleware from '@poppinss/middleware'
import type { ContainerResolver } from '@adonisjs/fold'
import type { Constructor, LazyImport } from './base.js'
import type { HttpContext } from '../http_context/main.js'
import type { MiddlewareFn, ParsedGlobalMiddleware } from './middleware.js'
import { ServerErrorHandler } from './server.js'
/**
* Returns a union of methods from a controller that accepts
* the context as the first argument.
*/
export type GetControllerHandlers<Controller extends Constructor<any>> = {
[K in keyof InstanceType<Controller>]: InstanceType<Controller>[K] extends (
ctx: HttpContext,
...args: any[]
) => any
? K
: never
}[keyof InstanceType<Controller>]
/**
* Route token stored by matchit library
*/
export type MatchItRouteToken = RouteMatcher & {
old: string
type: 0 | 1 | 2 | 3
val: string
end: string
}
/**
* Route handler defined as a function
*/
export type RouteFn = (ctx: HttpContext) => any
/**
* Route handler persisted with the route store
*/
export type StoreRouteHandler =
| RouteFn
| {
reference: string | [LazyImport<Constructor<any>> | Constructor<any>, any?]
handle: (
resolver: ContainerResolver<any>,
...args: [ctx: HttpContext, ...injections: any[]]
) => any
}
/**
* The middleware persisted with the route store
*/
export type StoreRouteMiddleware =
| MiddlewareFn
| ({ name?: string; args?: any[] } & ParsedGlobalMiddleware)
/**
* Route node persisted within the routes store
*/
export type StoreRouteNode = {
/**
* The execute function to execute the route middleware
* and the handler
*/
execute: (
route: StoreRouteNode,
resolver: ContainerResolver<any>,
ctx: HttpContext,
errorResponder: ServerErrorHandler['handle']
) => any
/**
* A unique name for the route
*/
name?: string
/**
* Route URI pattern
*/
pattern: string
/**
* Route handler
*/
handler: StoreRouteHandler
/**
* Route middleware
*/
middleware: Middleware<StoreRouteMiddleware>
/**
* Additional metadata associated with the route
*/
meta: Record<string, any>
}
/**
* An object of routes for a given HTTP method
*/
export type StoreMethodNode = {
tokens: MatchItRouteToken[][]
routeKeys: {
[pattern: string]: string
}
routes: {
[pattern: string]: StoreRouteNode
}
}
/**
* Each domain node container an object of methods. Each method
* object has nested routes.
*/
export type StoreDomainNode = {
[method: string]: StoreMethodNode
}
/**
* Routes tree stored within the routes store
*/
export type StoreRoutesTree = {
tokens: MatchItRouteToken[][]
domains: {
[domain: string]: StoreDomainNode
}
}
/**
* Shape of the matched route for a pattern, method and domain.
*/
export type MatchedRoute = {
route: StoreRouteNode
/**
* A unique key for the looked up route
*/
routeKey: string
/**
* Route params
*/
params: Record<string, any>
/**
* Route subdomains (if part of a subdomain)
*/
subdomains: Record<string, any>
}
/**
* Shape of a route param matcher
*/
export type RouteMatcher = {
match?: RegExp
cast?: (value: string) => any
}
/**
* A collection of route matchers
*/
export type RouteMatchers = {
[param: string]: RouteMatcher
}
/**
* Representation of a route as JSON
*/
export type RouteJSON = StoreRouteNode & {
/**
* HTTP methods, the route responds to.
*/
methods: string[]
/**
* The domain for which the route is registered.
*/
domain: string
/**
* Matchers for route params.
*/
matchers: RouteMatchers
}
/**
* Resource action names
*/
export type ResourceActionNames =
| 'create'
| 'index'
| 'store'
| 'show'
| 'edit'
| 'update'
| 'destroy'
/**
* Options accepted by makeUrl method
*/
export type MakeUrlOptions = {
qs?: Record<string, any>
domain?: string
prefixUrl?: string
disableRouteLookup?: boolean
}
/**
* Options accepted by makeSignedUrl method
*/
export type MakeSignedUrlOptions = MakeUrlOptions & {
expiresIn?: string | number
purpose?: string
}