rajt 0.0.29 → 0.0.30
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 +1 -1
- package/src/create-app.ts +2 -3
- package/src/register.ts +5 -16
- package/src/utils/resolve.ts +4 -4
package/package.json
CHANGED
package/src/create-app.ts
CHANGED
|
@@ -95,12 +95,11 @@ export const createApp = <E extends Env>(options?: ServerOptions<E>) => {
|
|
|
95
95
|
const routes = options?.routes || []
|
|
96
96
|
for (const route of routes) {
|
|
97
97
|
if (Array.isArray(route)) {
|
|
98
|
-
const handle = getHandler(route[3])
|
|
99
98
|
// @ts-ignore
|
|
100
|
-
app[route[0]](route[1], ...mw(route[2], route[3]), ...resolve(
|
|
99
|
+
app[route[0]](route[1], ...mw(route[2], route[3]), ...resolve(getHandler(route[3]), route[3]))
|
|
101
100
|
} else {
|
|
102
101
|
// @ts-ignore
|
|
103
|
-
app[route.method](route.path, ...mw(route.middlewares, route.name), ...resolve(route.handle))
|
|
102
|
+
app[route.method](route.path, ...mw(route.middlewares, route.name), ...resolve(route.handle, route.name))
|
|
104
103
|
}
|
|
105
104
|
}
|
|
106
105
|
|
package/src/register.ts
CHANGED
|
@@ -1,24 +1,13 @@
|
|
|
1
|
-
export const handlers = {}
|
|
1
|
+
export const handlers: Record<string, Function> = {}
|
|
2
2
|
|
|
3
3
|
export function registerHandler(id: string, handler: any) {
|
|
4
|
-
if (
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const instance = new handler()
|
|
9
|
-
// @ts-ignore
|
|
10
|
-
handlers[id] = instance.handle.bind(instance)
|
|
11
|
-
} else if (handler.run) {
|
|
12
|
-
const instance = new handler()
|
|
13
|
-
// @ts-ignore
|
|
14
|
-
handlers[id] = instance.run.bind(instance)
|
|
15
|
-
} else {
|
|
16
|
-
console.warn(`Handler ${id} could not be registered - unsupported type`)
|
|
17
|
-
}
|
|
4
|
+
if (id in handlers)
|
|
5
|
+
console.warn(`Handler "${id}" has already been registered`)
|
|
6
|
+
|
|
7
|
+
handlers[id] = handler
|
|
18
8
|
}
|
|
19
9
|
|
|
20
10
|
export function getHandler(id: string): Function {
|
|
21
|
-
// @ts-ignore
|
|
22
11
|
const handler = handlers[id] || null
|
|
23
12
|
if (!handler) throw new Error(`Handler ${id} not registered`)
|
|
24
13
|
return handler
|
package/src/utils/resolve.ts
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
import Action, { ActionType } from '../action'
|
|
2
2
|
import { MiddlewareType } from '../middleware'
|
|
3
3
|
|
|
4
|
-
export function resolve(obj: ActionType) {
|
|
4
|
+
export function resolve(obj: ActionType, id: string) {
|
|
5
5
|
if (typeof obj === 'function' && obj?.length === 2)
|
|
6
6
|
return [obj]
|
|
7
7
|
|
|
8
|
-
if (obj
|
|
8
|
+
if (obj?.run)
|
|
9
9
|
return obj.run()
|
|
10
10
|
|
|
11
11
|
const instance = new (obj as new () => Action)()
|
|
12
|
-
if (
|
|
12
|
+
if (obj?.prototype?.run)
|
|
13
13
|
return instance.run()
|
|
14
14
|
|
|
15
15
|
if (obj?.prototype?.handle)
|
|
16
16
|
return [instance.handle]
|
|
17
17
|
|
|
18
|
-
throw new Error(
|
|
18
|
+
throw new Error(`Invalid action "${id}" - unsupported type`)
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
export function resolveMiddleware(obj: MiddlewareType) {
|