@h3ravel/router 1.7.0 → 1.7.2
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/package.json +8 -5
- package/CHANGELOG.md +0 -274
- package/src/Contracts/.gitkeep +0 -0
- package/src/Controller.ts +0 -1
- package/src/Middleware/.gitkeep +0 -0
- package/src/Providers/AssetsServiceProvider.ts +0 -55
- package/src/Providers/RouteServiceProvider.ts +0 -48
- package/src/Route.ts +0 -250
- package/src/index.ts +0 -8
- package/tests/.gitkeep +0 -0
- package/tsconfig.json +0 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@h3ravel/router",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.2",
|
|
4
4
|
"description": "Route facade, decorators and controller system for H3ravel.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -13,6 +13,9 @@
|
|
|
13
13
|
"types": "./dist/index.d.ts"
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
16
19
|
"publishConfig": {
|
|
17
20
|
"access": "public"
|
|
18
21
|
},
|
|
@@ -34,10 +37,10 @@
|
|
|
34
37
|
],
|
|
35
38
|
"dependencies": {
|
|
36
39
|
"h3": "^2.0.0-beta.1",
|
|
37
|
-
"@h3ravel/
|
|
38
|
-
"@h3ravel/
|
|
39
|
-
"@h3ravel/
|
|
40
|
-
"@h3ravel/
|
|
40
|
+
"@h3ravel/http": "^8.0.4",
|
|
41
|
+
"@h3ravel/core": "^1.4.2",
|
|
42
|
+
"@h3ravel/support": "^0.8.2",
|
|
43
|
+
"@h3ravel/shared": "^0.16.4"
|
|
41
44
|
},
|
|
42
45
|
"scripts": {
|
|
43
46
|
"barrel": "barrelsby --directory src --delete --singleQuotes",
|
package/CHANGELOG.md
DELETED
|
@@ -1,274 +0,0 @@
|
|
|
1
|
-
# @h3ravel/router
|
|
2
|
-
|
|
3
|
-
## 1.7.0
|
|
4
|
-
|
|
5
|
-
### Minor Changes
|
|
6
|
-
|
|
7
|
-
- 79f4045: feat: add add exports to package.json
|
|
8
|
-
|
|
9
|
-
### Patch Changes
|
|
10
|
-
|
|
11
|
-
- Updated dependencies [79f4045]
|
|
12
|
-
- @h3ravel/support@0.8.0
|
|
13
|
-
- @h3ravel/shared@0.16.0
|
|
14
|
-
- @h3ravel/core@1.4.0
|
|
15
|
-
- @h3ravel/http@8.0.0
|
|
16
|
-
|
|
17
|
-
## 1.6.0
|
|
18
|
-
|
|
19
|
-
### Minor Changes
|
|
20
|
-
|
|
21
|
-
- feat: implement full IoC container resolution
|
|
22
|
-
|
|
23
|
-
### Patch Changes
|
|
24
|
-
|
|
25
|
-
- Updated dependencies
|
|
26
|
-
- @h3ravel/core@1.3.0
|
|
27
|
-
- @h3ravel/shared@0.15.4
|
|
28
|
-
- @h3ravel/http@7.0.0
|
|
29
|
-
|
|
30
|
-
## 1.5.4
|
|
31
|
-
|
|
32
|
-
### Patch Changes
|
|
33
|
-
|
|
34
|
-
- feat: add homepage and repository to all packages.
|
|
35
|
-
- Updated dependencies
|
|
36
|
-
- @h3ravel/core@1.2.2
|
|
37
|
-
- @h3ravel/http@6.0.2
|
|
38
|
-
- @h3ravel/shared@0.15.2
|
|
39
|
-
- @h3ravel/support@0.7.5
|
|
40
|
-
|
|
41
|
-
## 1.5.3
|
|
42
|
-
|
|
43
|
-
### Patch Changes
|
|
44
|
-
|
|
45
|
-
- chore: update readme accros all packages
|
|
46
|
-
- Updated dependencies
|
|
47
|
-
- @h3ravel/core@1.2.1
|
|
48
|
-
- @h3ravel/http@6.0.1
|
|
49
|
-
- @h3ravel/shared@0.15.1
|
|
50
|
-
- @h3ravel/support@0.7.4
|
|
51
|
-
|
|
52
|
-
## 1.5.2
|
|
53
|
-
|
|
54
|
-
### Patch Changes
|
|
55
|
-
|
|
56
|
-
- Updated dependencies [d07ff49]
|
|
57
|
-
- @h3ravel/shared@0.15.0
|
|
58
|
-
- @h3ravel/core@1.2.0
|
|
59
|
-
- @h3ravel/http@6.0.0
|
|
60
|
-
- @h3ravel/support@0.7.3
|
|
61
|
-
|
|
62
|
-
## 1.5.1
|
|
63
|
-
|
|
64
|
-
### Patch Changes
|
|
65
|
-
|
|
66
|
-
- chore: require the latest dependencies from the framework
|
|
67
|
-
- Updated dependencies
|
|
68
|
-
- @h3ravel/support@0.7.3
|
|
69
|
-
- @h3ravel/core@1.1.2
|
|
70
|
-
- @h3ravel/http@5.0.2
|
|
71
|
-
|
|
72
|
-
## 1.5.0
|
|
73
|
-
|
|
74
|
-
### Minor Changes
|
|
75
|
-
|
|
76
|
-
- feat: add app to HttpContext interface
|
|
77
|
-
|
|
78
|
-
### Patch Changes
|
|
79
|
-
|
|
80
|
-
- Updated dependencies
|
|
81
|
-
- @h3ravel/shared@0.14.0
|
|
82
|
-
- @h3ravel/core@1.1.1
|
|
83
|
-
- @h3ravel/http@5.0.1
|
|
84
|
-
- @h3ravel/support@0.7.2
|
|
85
|
-
|
|
86
|
-
## 1.4.3
|
|
87
|
-
|
|
88
|
-
### Patch Changes
|
|
89
|
-
|
|
90
|
-
- Updated dependencies [6e249fe]
|
|
91
|
-
- @h3ravel/shared@0.13.0
|
|
92
|
-
- @h3ravel/core@1.1.0
|
|
93
|
-
- @h3ravel/http@5.0.0
|
|
94
|
-
- @h3ravel/support@0.7.2
|
|
95
|
-
|
|
96
|
-
## 1.4.2
|
|
97
|
-
|
|
98
|
-
### Patch Changes
|
|
99
|
-
|
|
100
|
-
- chore: regularize all interfaces.
|
|
101
|
-
- Updated dependencies
|
|
102
|
-
- @h3ravel/core@1.0.9
|
|
103
|
-
- @h3ravel/http@4.2.2
|
|
104
|
-
- @h3ravel/shared@0.12.1
|
|
105
|
-
- @h3ravel/support@0.7.2
|
|
106
|
-
|
|
107
|
-
## 1.4.1
|
|
108
|
-
|
|
109
|
-
### Patch Changes
|
|
110
|
-
|
|
111
|
-
- Updated dependencies
|
|
112
|
-
- @h3ravel/http@4.2.1
|
|
113
|
-
|
|
114
|
-
## 1.4.0
|
|
115
|
-
|
|
116
|
-
### Minor Changes
|
|
117
|
-
|
|
118
|
-
- feat: add the current app instance to the Request and Response object
|
|
119
|
-
|
|
120
|
-
### Patch Changes
|
|
121
|
-
|
|
122
|
-
- Updated dependencies
|
|
123
|
-
- @h3ravel/shared@0.12.0
|
|
124
|
-
- @h3ravel/http@4.2.0
|
|
125
|
-
- @h3ravel/core@1.0.8
|
|
126
|
-
- @h3ravel/support@0.7.1
|
|
127
|
-
|
|
128
|
-
## 1.3.0
|
|
129
|
-
|
|
130
|
-
### Minor Changes
|
|
131
|
-
|
|
132
|
-
- remove 'name' from RouterEnd and only join in apiResource.
|
|
133
|
-
|
|
134
|
-
### Patch Changes
|
|
135
|
-
|
|
136
|
-
- Updated dependencies
|
|
137
|
-
- @h3ravel/shared@0.11.0
|
|
138
|
-
- @h3ravel/core@1.0.7
|
|
139
|
-
- @h3ravel/http@4.1.3
|
|
140
|
-
- @h3ravel/support@0.7.1
|
|
141
|
-
|
|
142
|
-
## 1.2.0
|
|
143
|
-
|
|
144
|
-
### Minor Changes
|
|
145
|
-
|
|
146
|
-
- feat: hide RouterEnd from itslelf to prevent unintentional chaining.
|
|
147
|
-
|
|
148
|
-
### Patch Changes
|
|
149
|
-
|
|
150
|
-
- Updated dependencies
|
|
151
|
-
- @h3ravel/shared@0.10.0
|
|
152
|
-
- @h3ravel/core@1.0.6
|
|
153
|
-
- @h3ravel/http@4.1.2
|
|
154
|
-
- @h3ravel/support@0.7.1
|
|
155
|
-
|
|
156
|
-
## 1.1.0
|
|
157
|
-
|
|
158
|
-
### Minor Changes
|
|
159
|
-
|
|
160
|
-
- refactor!: make route definition more similar to the way it is handled in laravel.
|
|
161
|
-
|
|
162
|
-
### Patch Changes
|
|
163
|
-
|
|
164
|
-
- Updated dependencies
|
|
165
|
-
- @h3ravel/shared@0.9.0
|
|
166
|
-
- @h3ravel/core@1.0.5
|
|
167
|
-
- @h3ravel/http@4.1.1
|
|
168
|
-
- @h3ravel/support@0.7.1
|
|
169
|
-
|
|
170
|
-
## 1.0.4
|
|
171
|
-
|
|
172
|
-
### Patch Changes
|
|
173
|
-
|
|
174
|
-
- Updated dependencies
|
|
175
|
-
- @h3ravel/http@4.1.0
|
|
176
|
-
- @h3ravel/shared@0.8.0
|
|
177
|
-
- @h3ravel/core@1.0.4
|
|
178
|
-
- @h3ravel/support@0.7.1
|
|
179
|
-
|
|
180
|
-
## 1.0.3
|
|
181
|
-
|
|
182
|
-
### Patch Changes
|
|
183
|
-
|
|
184
|
-
- chore: add download count to readme
|
|
185
|
-
- Updated dependencies
|
|
186
|
-
- @h3ravel/core@1.0.3
|
|
187
|
-
- @h3ravel/http@4.0.3
|
|
188
|
-
- @h3ravel/shared@0.7.1
|
|
189
|
-
- @h3ravel/support@0.7.1
|
|
190
|
-
|
|
191
|
-
## 1.0.2
|
|
192
|
-
|
|
193
|
-
### Patch Changes
|
|
194
|
-
|
|
195
|
-
- Updated dependencies [b0d1b7c]
|
|
196
|
-
- Updated dependencies [ce51a92]
|
|
197
|
-
- @h3ravel/support@0.7.0
|
|
198
|
-
- @h3ravel/shared@0.7.0
|
|
199
|
-
- @h3ravel/http@4.0.2
|
|
200
|
-
- @h3ravel/core@1.0.2
|
|
201
|
-
|
|
202
|
-
## 1.0.0
|
|
203
|
-
|
|
204
|
-
### Major Changes
|
|
205
|
-
|
|
206
|
-
- b40caeb: feat: make service providers sortable and unique while only loading the core providers by default.
|
|
207
|
-
Service providers are no longer loaded by default, asides the ones provided by @h3ravel/core
|
|
208
|
-
Service provides are sorted by an optional order and priority property.
|
|
209
|
-
|
|
210
|
-
### Patch Changes
|
|
211
|
-
|
|
212
|
-
- Updated dependencies [b40caeb]
|
|
213
|
-
- @h3ravel/core@1.0.0
|
|
214
|
-
- @h3ravel/http@4.0.0
|
|
215
|
-
- @h3ravel/shared@0.6.0
|
|
216
|
-
- @h3ravel/support@0.6.0
|
|
217
|
-
|
|
218
|
-
## 0.5.0
|
|
219
|
-
|
|
220
|
-
### Minor Changes
|
|
221
|
-
|
|
222
|
-
- rebuild all dependencies
|
|
223
|
-
|
|
224
|
-
### Patch Changes
|
|
225
|
-
|
|
226
|
-
- Updated dependencies
|
|
227
|
-
- @h3ravel/core@0.5.0
|
|
228
|
-
- @h3ravel/http@3.0.0
|
|
229
|
-
- @h3ravel/shared@0.5.0
|
|
230
|
-
- @h3ravel/support@0.5.0
|
|
231
|
-
|
|
232
|
-
## 0.4.0
|
|
233
|
-
|
|
234
|
-
### Minor Changes
|
|
235
|
-
|
|
236
|
-
- 8ceb2c1: implement the Application class directly since it already implements the IClass contract
|
|
237
|
-
|
|
238
|
-
### Patch Changes
|
|
239
|
-
|
|
240
|
-
- a27f452: chore: fix all linting issues.
|
|
241
|
-
- c906050: chore: migrate tests suite to jest
|
|
242
|
-
- Updated dependencies [8ceb2c1]
|
|
243
|
-
- Updated dependencies [a27f452]
|
|
244
|
-
- Updated dependencies [c906050]
|
|
245
|
-
- @h3ravel/core@0.4.0
|
|
246
|
-
- @h3ravel/shared@0.4.0
|
|
247
|
-
- @h3ravel/support@0.4.0
|
|
248
|
-
- @h3ravel/http@2.0.0
|
|
249
|
-
|
|
250
|
-
## 0.3.0
|
|
251
|
-
|
|
252
|
-
### Minor Changes
|
|
253
|
-
|
|
254
|
-
- 3ff97bf: refactor: add a shared package to be extended by others to avoid cyclic dependency issues.
|
|
255
|
-
|
|
256
|
-
### Patch Changes
|
|
257
|
-
|
|
258
|
-
- Updated dependencies [3ff97bf]
|
|
259
|
-
- @h3ravel/core@0.3.0
|
|
260
|
-
- @h3ravel/http@1.0.0
|
|
261
|
-
- @h3ravel/shared@0.3.0
|
|
262
|
-
- @h3ravel/support@0.3.0
|
|
263
|
-
|
|
264
|
-
## 0.2.0
|
|
265
|
-
|
|
266
|
-
### Minor Changes
|
|
267
|
-
|
|
268
|
-
- aea734f: Fix all known bugs and improved interdependecy between packages.
|
|
269
|
-
|
|
270
|
-
### Patch Changes
|
|
271
|
-
|
|
272
|
-
- Updated dependencies [aea734f]
|
|
273
|
-
- @h3ravel/core@0.2.0
|
|
274
|
-
- @h3ravel/support@0.2.0
|
package/src/Contracts/.gitkeep
DELETED
|
File without changes
|
package/src/Controller.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default class { }
|
package/src/Middleware/.gitkeep
DELETED
|
File without changes
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { readFile, stat } from 'node:fs/promises'
|
|
2
|
-
|
|
3
|
-
import { ServiceProvider } from '@h3ravel/core'
|
|
4
|
-
import { before } from '@h3ravel/support'
|
|
5
|
-
import { join } from 'node:path'
|
|
6
|
-
import { serveStatic } from 'h3'
|
|
7
|
-
import { statSync } from 'node:fs'
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Handles public assets loading
|
|
11
|
-
*
|
|
12
|
-
* Auto-Registered
|
|
13
|
-
*/
|
|
14
|
-
export class AssetsServiceProvider extends ServiceProvider {
|
|
15
|
-
public static priority = 996;
|
|
16
|
-
|
|
17
|
-
register () {
|
|
18
|
-
const app = this.app.make('router')
|
|
19
|
-
const config = this.app.make('config')
|
|
20
|
-
const fsconfig = config.get('filesystem')
|
|
21
|
-
const publicPath = this.app.getPath('public')
|
|
22
|
-
|
|
23
|
-
app.middleware(`/${fsconfig.public_mask}/**`, (event) => {
|
|
24
|
-
return serveStatic(event, {
|
|
25
|
-
indexNames: ['/index.html'],
|
|
26
|
-
getContents: (id) => {
|
|
27
|
-
const newId = id.replace(`/${fsconfig.public_mask}/`, '')
|
|
28
|
-
return <never>readFile(join(before(publicPath, newId), newId))
|
|
29
|
-
},
|
|
30
|
-
getMeta: async (id) => {
|
|
31
|
-
const newId = id.replace(`/${fsconfig.public_mask}/`, '')
|
|
32
|
-
const stats = await stat(join(before(publicPath, newId), newId)).catch(() => { })
|
|
33
|
-
if (stats?.isFile()) {
|
|
34
|
-
return {
|
|
35
|
-
size: stats.size,
|
|
36
|
-
mtime: stats.mtimeMs,
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
},
|
|
40
|
-
})
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
this.app.singleton('asset', () => {
|
|
44
|
-
return (key: string, def = '') => {
|
|
45
|
-
try {
|
|
46
|
-
statSync(join(before(publicPath, key), key))
|
|
47
|
-
} catch {
|
|
48
|
-
key = def
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return join(fsconfig.public_mask, key)
|
|
52
|
-
}
|
|
53
|
-
})
|
|
54
|
-
}
|
|
55
|
-
}
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { Router } from '../Route'
|
|
2
|
-
import { ServiceProvider } from '@h3ravel/core'
|
|
3
|
-
import path from 'node:path'
|
|
4
|
-
import { readdir } from 'node:fs/promises'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Handles routing registration
|
|
8
|
-
*
|
|
9
|
-
* Load route files (web.ts, api.ts).
|
|
10
|
-
* Map controllers to routes.
|
|
11
|
-
* Register route-related middleware.
|
|
12
|
-
*
|
|
13
|
-
* Auto-Registered
|
|
14
|
-
*/
|
|
15
|
-
export class RouteServiceProvider extends ServiceProvider {
|
|
16
|
-
public static priority = 997;
|
|
17
|
-
|
|
18
|
-
register () {
|
|
19
|
-
this.app.singleton('router', () => {
|
|
20
|
-
const h3App = this.app.make('http.app')
|
|
21
|
-
return new Router(h3App, this.app)
|
|
22
|
-
})
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Load routes from src/routes
|
|
27
|
-
*/
|
|
28
|
-
async boot () {
|
|
29
|
-
try {
|
|
30
|
-
const routePath = this.app.getPath('routes')
|
|
31
|
-
|
|
32
|
-
const files = (await readdir(routePath)).filter((e) => {
|
|
33
|
-
return !e.includes('.d.ts') && !e.includes('.map')
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
for (let i = 0; i < files.length; i++) {
|
|
37
|
-
const routesModule = await import(path.join(routePath, files[i]))
|
|
38
|
-
|
|
39
|
-
if (typeof routesModule.default === 'function') {
|
|
40
|
-
const router = this.app.make('router')
|
|
41
|
-
routesModule.default(router)
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
} catch (e) {
|
|
45
|
-
console.warn('No web routes found or failed to load:', e)
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
package/src/Route.ts
DELETED
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
import { H3Event, Middleware, MiddlewareOptions, type H3 } from 'h3'
|
|
2
|
-
import { Request, Response } from '@h3ravel/http'
|
|
3
|
-
import { Application, Container, Controller, Kernel } from '@h3ravel/core'
|
|
4
|
-
import { afterLast } from '@h3ravel/support'
|
|
5
|
-
import type { EventHandler, IController, IMiddleware, IRouter, RouterEnd } from '@h3ravel/shared'
|
|
6
|
-
|
|
7
|
-
interface RouteDefinition {
|
|
8
|
-
method: string
|
|
9
|
-
path: string
|
|
10
|
-
name?: string
|
|
11
|
-
handler: EventHandler
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export class Router implements IRouter {
|
|
15
|
-
private routes: RouteDefinition[] = []
|
|
16
|
-
private nameMap: string[] = []
|
|
17
|
-
private groupPrefix = ''
|
|
18
|
-
private middlewareMap: IMiddleware[] = []
|
|
19
|
-
private groupMiddleware: EventHandler[] = []
|
|
20
|
-
|
|
21
|
-
constructor(protected h3App: H3, private app: Application) { }
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Route Resolver
|
|
25
|
-
*
|
|
26
|
-
* @param handler
|
|
27
|
-
* @param middleware
|
|
28
|
-
* @returns
|
|
29
|
-
*/
|
|
30
|
-
private resolveHandler (handler: EventHandler, middleware: IMiddleware[] = []) {
|
|
31
|
-
return async (event: H3Event) => {
|
|
32
|
-
const kernel = new Kernel(() => ({
|
|
33
|
-
app: this.app,
|
|
34
|
-
request: new Request(event, this.app),
|
|
35
|
-
response: new Response(event, this.app)
|
|
36
|
-
}), middleware)
|
|
37
|
-
|
|
38
|
-
return kernel.handle(event, (ctx) => Promise.resolve(handler(ctx)))
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Add a route to the stack
|
|
44
|
-
*
|
|
45
|
-
* @param method
|
|
46
|
-
* @param path
|
|
47
|
-
* @param handler
|
|
48
|
-
* @param name
|
|
49
|
-
* @param middleware
|
|
50
|
-
*/
|
|
51
|
-
private addRoute (
|
|
52
|
-
method: string,
|
|
53
|
-
path: string,
|
|
54
|
-
handler: EventHandler,
|
|
55
|
-
name?: string,
|
|
56
|
-
middleware: IMiddleware[] = []
|
|
57
|
-
) {
|
|
58
|
-
/**
|
|
59
|
-
* Join all defined route names to make a single route name
|
|
60
|
-
*/
|
|
61
|
-
if (this.nameMap.length > 0) {
|
|
62
|
-
name = this.nameMap.join('.')
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Join all defined middlewares
|
|
67
|
-
*/
|
|
68
|
-
if (this.middlewareMap.length > 0) {
|
|
69
|
-
middleware = this.middlewareMap
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const fullPath = `${this.groupPrefix}${path}`.replace(/\/+/g, '/')
|
|
73
|
-
this.routes.push({ method, path: fullPath, name, handler })
|
|
74
|
-
this.h3App[method as 'get'](fullPath, this.resolveHandler(handler, middleware))
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
private resolveControllerOrHandler (
|
|
78
|
-
handler: EventHandler | (new (...args: any[]) => IController),
|
|
79
|
-
methodName?: string
|
|
80
|
-
): EventHandler {
|
|
81
|
-
if (typeof handler === 'function' && (handler as any).prototype instanceof Controller) {
|
|
82
|
-
return (ctx) => {
|
|
83
|
-
let controller: IController
|
|
84
|
-
|
|
85
|
-
if (Container.hasAnyDecorator(handler)) {
|
|
86
|
-
/**
|
|
87
|
-
* If the controller is decorated use the IoC container
|
|
88
|
-
*/
|
|
89
|
-
controller = this.app.make<any, IController>(handler as any)
|
|
90
|
-
} else {
|
|
91
|
-
/**
|
|
92
|
-
* Otherwise instantiate manually so that we can at least
|
|
93
|
-
* pass the app instance
|
|
94
|
-
*/
|
|
95
|
-
controller = new (handler as new (...args: any[]) => IController)(this.app)
|
|
96
|
-
}
|
|
97
|
-
const action = (methodName || 'index') as keyof IController
|
|
98
|
-
|
|
99
|
-
if (typeof controller[action] !== 'function') {
|
|
100
|
-
throw new Error(`Method "${String(action)}" not found on controller ${handler.name}`)
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return controller[action](ctx)
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return handler as EventHandler
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
get (
|
|
111
|
-
path: string,
|
|
112
|
-
definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],
|
|
113
|
-
name?: string,
|
|
114
|
-
middleware: IMiddleware[] = []
|
|
115
|
-
): Omit<this, RouterEnd> {
|
|
116
|
-
const handler = Array.isArray(definition) ? definition[0] : definition
|
|
117
|
-
const methodName = Array.isArray(definition) ? definition[1] : undefined
|
|
118
|
-
this.addRoute('get', path, this.resolveControllerOrHandler(handler, methodName), name, middleware)
|
|
119
|
-
return this
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
post (
|
|
123
|
-
path: string,
|
|
124
|
-
definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],
|
|
125
|
-
name?: string,
|
|
126
|
-
middleware: IMiddleware[] = []
|
|
127
|
-
): Omit<this, RouterEnd> {
|
|
128
|
-
const handler = Array.isArray(definition) ? definition[0] : definition
|
|
129
|
-
const methodName = Array.isArray(definition) ? definition[1] : undefined
|
|
130
|
-
this.addRoute('post', path, this.resolveControllerOrHandler(handler, methodName), name, middleware)
|
|
131
|
-
return this
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
put (
|
|
135
|
-
path: string,
|
|
136
|
-
definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],
|
|
137
|
-
name?: string,
|
|
138
|
-
middleware: IMiddleware[] = []
|
|
139
|
-
): Omit<this, RouterEnd> {
|
|
140
|
-
const handler = Array.isArray(definition) ? definition[0] : definition
|
|
141
|
-
const methodName = Array.isArray(definition) ? definition[1] : undefined
|
|
142
|
-
this.addRoute('put', path, this.resolveControllerOrHandler(handler, methodName), name, middleware)
|
|
143
|
-
return this
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
delete (
|
|
147
|
-
path: string,
|
|
148
|
-
definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],
|
|
149
|
-
name?: string,
|
|
150
|
-
middleware: IMiddleware[] = []
|
|
151
|
-
): Omit<this, RouterEnd> {
|
|
152
|
-
const handler = Array.isArray(definition) ? definition[0] : definition
|
|
153
|
-
const methodName = Array.isArray(definition) ? definition[1] : undefined
|
|
154
|
-
this.addRoute('delete', path, this.resolveControllerOrHandler(handler, methodName), name, middleware)
|
|
155
|
-
return this
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* API Resource support
|
|
160
|
-
*
|
|
161
|
-
* @param path
|
|
162
|
-
* @param controller
|
|
163
|
-
*/
|
|
164
|
-
apiResource (
|
|
165
|
-
path: string,
|
|
166
|
-
Controller: new (app: Application) => IController,
|
|
167
|
-
middleware: IMiddleware[] = []
|
|
168
|
-
): Omit<this, RouterEnd | 'name'> {
|
|
169
|
-
path = path.replace(/\//g, '/')
|
|
170
|
-
|
|
171
|
-
const name = afterLast(path, '/')
|
|
172
|
-
const basePath = `/${path}`.replace(/\/+/g, '/')
|
|
173
|
-
|
|
174
|
-
const controller = new Controller(this.app)
|
|
175
|
-
|
|
176
|
-
this.addRoute('get', basePath, controller.index.bind(controller), `${name}.index`, middleware)
|
|
177
|
-
this.addRoute('post', basePath, controller.store.bind(controller), `${name}.store`, middleware)
|
|
178
|
-
this.addRoute('get', `${basePath}/:id`, controller.show.bind(controller), `${name}.show`, middleware)
|
|
179
|
-
this.addRoute('put', `${basePath}/:id`, controller.update.bind(controller), `${name}.update`, middleware)
|
|
180
|
-
this.addRoute('patch', `${basePath}/:id`, controller.update.bind(controller), `${name}.update`, middleware)
|
|
181
|
-
this.addRoute('delete', `${basePath}/:id`, controller.destroy.bind(controller), `${name}.destroy`, middleware)
|
|
182
|
-
return this
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Named route URL generator
|
|
187
|
-
*
|
|
188
|
-
* @param name
|
|
189
|
-
* @param params
|
|
190
|
-
* @returns
|
|
191
|
-
*/
|
|
192
|
-
route (name: string, params: Record<string, string> = {}): string | undefined {
|
|
193
|
-
const found = this.routes.find(r => r.name === name)
|
|
194
|
-
if (!found) return undefined
|
|
195
|
-
|
|
196
|
-
let url = found.path
|
|
197
|
-
for (const [key, value] of Object.entries(params)) {
|
|
198
|
-
url = url.replace(`:${key}`, value)
|
|
199
|
-
}
|
|
200
|
-
return url
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Grouping
|
|
205
|
-
*
|
|
206
|
-
* @param options
|
|
207
|
-
* @param callback
|
|
208
|
-
*/
|
|
209
|
-
group (options: { prefix?: string; middleware?: EventHandler[] }, callback: () => void) {
|
|
210
|
-
const prevPrefix = this.groupPrefix
|
|
211
|
-
const prevMiddleware = [...this.groupMiddleware]
|
|
212
|
-
|
|
213
|
-
this.groupPrefix += options.prefix || ''
|
|
214
|
-
this.groupMiddleware.push(...(options.middleware || []))
|
|
215
|
-
|
|
216
|
-
callback()
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Restore state after group
|
|
220
|
-
*/
|
|
221
|
-
this.groupPrefix = prevPrefix
|
|
222
|
-
this.groupMiddleware = prevMiddleware
|
|
223
|
-
return this
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* Set the name of the current route
|
|
228
|
-
*
|
|
229
|
-
* @param name
|
|
230
|
-
*/
|
|
231
|
-
name (name: string) {
|
|
232
|
-
this.nameMap.push(name)
|
|
233
|
-
return this
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* Registers middleware for a specific path.
|
|
238
|
-
* @param path - The path to apply the middleware.
|
|
239
|
-
* @param handler - The middleware handler.
|
|
240
|
-
* @param opts - Optional middleware options.
|
|
241
|
-
*/
|
|
242
|
-
middleware (path: string | IMiddleware[], handler: Middleware, opts?: MiddlewareOptions) {
|
|
243
|
-
if (typeof path === 'string') {
|
|
244
|
-
this.h3App.use(path, handler, opts)
|
|
245
|
-
} else {
|
|
246
|
-
this.middlewareMap.concat(path)
|
|
247
|
-
}
|
|
248
|
-
return this
|
|
249
|
-
}
|
|
250
|
-
}
|
package/src/index.ts
DELETED
package/tests/.gitkeep
DELETED
|
File without changes
|