clear-router 2.7.6 → 2.8.0
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/dist/ClearRequest.cjs +23 -0
- package/dist/ClearRequest.d.cts +28 -0
- package/dist/ClearRequest.d.mts +28 -0
- package/dist/ClearRequest.mjs +22 -0
- package/dist/Contracts.d.cts +12 -0
- package/dist/Contracts.d.mts +12 -0
- package/dist/Controller.cjs +12 -0
- package/dist/Controller.d.cts +14 -0
- package/dist/Controller.d.mts +14 -0
- package/dist/Controller.mjs +11 -0
- package/dist/{Request-DKXwa_W0.d.mts → Request-Ci0UQ-Vl.d.mts} +32 -5
- package/dist/ResourceRouteSelection.cjs +26 -0
- package/dist/ResourceRouteSelection.d.cts +20 -0
- package/dist/ResourceRouteSelection.d.mts +20 -0
- package/dist/ResourceRouteSelection.mjs +25 -0
- package/dist/ResourceRoutes.cjs +60 -0
- package/dist/ResourceRoutes.d.cts +37 -0
- package/dist/ResourceRoutes.d.mts +37 -0
- package/dist/ResourceRoutes.mjs +60 -0
- package/dist/Route.cjs +90 -0
- package/dist/Route.d.cts +62 -0
- package/dist/Route.d.mts +62 -0
- package/dist/Route.mjs +89 -0
- package/dist/core/Request.cjs +35 -0
- package/dist/core/Request.d.cts +25 -0
- package/dist/core/Request.d.mts +25 -0
- package/dist/core/Request.mjs +35 -0
- package/dist/core/Response.cjs +59 -0
- package/dist/core/Response.d.cts +24 -0
- package/dist/core/Response.d.mts +24 -0
- package/dist/core/Response.mjs +58 -0
- package/dist/{bindings-CLsZjOEy.cjs → core/bindings.cjs} +10 -160
- package/dist/{bindings-CNL7bpz5.d.mts → core/bindings.d.cts} +1 -1
- package/dist/{bindings-CxvtC8XS.d.cts → core/bindings.d.mts} +1 -1
- package/dist/{bindings-XLDXFpHZ.mjs → core/bindings.mjs} +3 -110
- package/dist/core/index.cjs +7 -17
- package/dist/core/index.d.cts +4 -1
- package/dist/core/index.d.mts +4 -1
- package/dist/core/index.mjs +4 -14
- package/dist/core/plugins.cjs +14 -0
- package/dist/core/plugins.d.cts +109 -0
- package/dist/core/plugins.d.mts +109 -0
- package/dist/core/plugins.mjs +13 -0
- package/dist/{responses-Bvnk0uvc.cjs → core/responses.cjs} +5 -20
- package/dist/{responses-BvETUeDL.mjs → core/responses.mjs} +2 -2
- package/dist/{router-C6W-k6sS.cjs → core/router.cjs} +67 -72
- package/dist/core/router.d.cts +286 -0
- package/dist/core/router.d.mts +286 -0
- package/dist/{router-Dc9w86Wn.mjs → core/router.mjs} +62 -62
- package/dist/decorators/index.cjs +1 -1
- package/dist/decorators/index.d.cts +1 -1
- package/dist/decorators/index.d.mts +1 -1
- package/dist/decorators/index.mjs +1 -1
- package/dist/decorators/setup.cjs +2 -2
- package/dist/decorators/setup.d.cts +1 -1
- package/dist/decorators/setup.d.mts +1 -2
- package/dist/decorators/setup.mjs +2 -2
- package/dist/express/index.cjs +2 -265
- package/dist/express/index.d.cts +1 -127
- package/dist/express/index.d.mts +1 -127
- package/dist/express/index.mjs +1 -264
- package/dist/express/router.cjs +265 -0
- package/dist/express/router.d.cts +132 -0
- package/dist/express/router.d.mts +132 -0
- package/dist/express/router.mjs +265 -0
- package/dist/fastify/index.cjs +2 -254
- package/dist/fastify/index.d.cts +1 -125
- package/dist/fastify/index.d.mts +1 -125
- package/dist/fastify/index.mjs +1 -253
- package/dist/fastify/router.cjs +254 -0
- package/dist/fastify/router.d.cts +130 -0
- package/dist/fastify/router.d.mts +130 -0
- package/dist/fastify/router.mjs +254 -0
- package/dist/h3/index.cjs +2 -260
- package/dist/h3/index.d.cts +1 -128
- package/dist/h3/index.d.mts +1 -128
- package/dist/h3/index.mjs +1 -259
- package/dist/h3/router.cjs +260 -0
- package/dist/h3/router.d.cts +133 -0
- package/dist/h3/router.d.mts +133 -0
- package/dist/h3/router.mjs +260 -0
- package/dist/hono/index.cjs +2 -251
- package/dist/hono/index.d.cts +1 -130
- package/dist/hono/index.d.mts +1 -130
- package/dist/hono/index.mjs +1 -250
- package/dist/hono/router.cjs +251 -0
- package/dist/hono/router.d.cts +135 -0
- package/dist/hono/router.d.mts +135 -0
- package/dist/hono/router.mjs +251 -0
- package/dist/index.cjs +16 -1097
- package/dist/index.d.cts +9 -563
- package/dist/index.d.mts +9 -563
- package/dist/index.mjs +8 -1089
- package/dist/koa/index.cjs +2 -261
- package/dist/koa/index.d.cts +1 -131
- package/dist/koa/index.d.mts +1 -131
- package/dist/koa/index.mjs +1 -260
- package/dist/koa/router.cjs +261 -0
- package/dist/koa/router.d.cts +136 -0
- package/dist/koa/router.d.mts +136 -0
- package/dist/koa/router.mjs +261 -0
- package/dist/types/basic.d.cts +53 -0
- package/dist/types/basic.d.mts +11 -1
- package/dist/types/express.d.cts +48 -0
- package/dist/types/express.d.mts +10 -4
- package/dist/types/fastify.d.cts +24 -0
- package/dist/types/fastify.d.mts +7 -4
- package/dist/types/h3.d.cts +46 -0
- package/dist/types/h3.d.mts +10 -5
- package/dist/types/hono.d.cts +22 -0
- package/dist/types/hono.d.mts +8 -6
- package/dist/types/koa.d.cts +26 -0
- package/dist/types/koa.d.mts +7 -5
- package/package.json +1 -1
- package/dist/router-BEgAxp5A.d.cts +0 -649
- package/dist/router-DKKYx23P.d.mts +0 -649
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { ApiResourceMiddleware, ControllerAction, HttpMethod, RouterConfig } from "../types/basic.mjs";
|
|
2
|
+
import { Response } from "./Response.mjs";
|
|
3
|
+
import { Route } from "../Route.mjs";
|
|
4
|
+
import { Request } from "./Request.mjs";
|
|
5
|
+
import { ClearRouterPluginArgumentsContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, PluginArgumentsResolver, PluginBind } from "./plugins.mjs";
|
|
6
|
+
import { Controller } from "../Controller.mjs";
|
|
7
|
+
import { ResourceRoutes } from "../ResourceRoutes.mjs";
|
|
8
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
9
|
+
|
|
10
|
+
//#region src/core/router.d.ts
|
|
11
|
+
/**
|
|
12
|
+
* @class clear-router CoreRouter
|
|
13
|
+
* @description Core routing logic for clear-router, shared between all supported adapters (Express.js, H3, etc.)
|
|
14
|
+
* @author 3m1n3nc3
|
|
15
|
+
* @repository https://github.com/arkstack-tmp/clear-router
|
|
16
|
+
*/
|
|
17
|
+
declare abstract class CoreRouter {
|
|
18
|
+
protected static routerStateNamespace: string;
|
|
19
|
+
private static readonly stateStoreKey;
|
|
20
|
+
private static readonly stateBoundKey;
|
|
21
|
+
private static readonly defaultConfigKey;
|
|
22
|
+
private static readonly pluginStoreKey;
|
|
23
|
+
private static readonly pluginPendingKey;
|
|
24
|
+
private static readonly pluginHttpCtxResolversKey;
|
|
25
|
+
private static readonly pluginArgumentResolversKey;
|
|
26
|
+
private static requestProvider?;
|
|
27
|
+
private static responseProvider?;
|
|
28
|
+
static config: RouterConfig;
|
|
29
|
+
protected static groupContext: AsyncLocalStorage<{
|
|
30
|
+
prefix: string;
|
|
31
|
+
groupMiddlewares: any[];
|
|
32
|
+
}>;
|
|
33
|
+
protected static pluginRequestContext: AsyncLocalStorage<ClearRouterPluginRequestContext<any>>;
|
|
34
|
+
static routes: Set<Route<any, any, any>>;
|
|
35
|
+
static routesByPathMethod: Map<string, Route<any, any, any>>;
|
|
36
|
+
static routesByMethod: Map<"GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS" | "HEAD", Route<any, any, any>[]>;
|
|
37
|
+
static routesByName: Map<string, Route<any, any, any>>;
|
|
38
|
+
static prefix: string;
|
|
39
|
+
static groupMiddlewares: any[];
|
|
40
|
+
static globalMiddlewares: any[];
|
|
41
|
+
/**
|
|
42
|
+
* Resolve middlewares before assigning to adapter
|
|
43
|
+
*
|
|
44
|
+
* @param middleware
|
|
45
|
+
* @returns
|
|
46
|
+
*/
|
|
47
|
+
protected static resolveMiddleware(middleware: any): any;
|
|
48
|
+
protected static resolveMiddlewares(middlewares?: any[]): any[];
|
|
49
|
+
protected static routeSpecificity(route: Route<any, any, any>): [number, number, number];
|
|
50
|
+
protected static orderedRoutes(): Array<Route<any, any, any>>;
|
|
51
|
+
protected static removeRouteMethod(route: Route<any, any, any>, method: HttpMethod, path: string): void;
|
|
52
|
+
/**
|
|
53
|
+
* Resets the router to it's default state
|
|
54
|
+
*/
|
|
55
|
+
static reset(): typeof CoreRouter;
|
|
56
|
+
protected static createBaseConfig(): RouterConfig;
|
|
57
|
+
protected static mergeConfig(target: RouterConfig, source?: RouterConfig): RouterConfig;
|
|
58
|
+
protected static getDefaultConfig(): RouterConfig;
|
|
59
|
+
protected static resolveStateNamespace(): string;
|
|
60
|
+
protected static getStateStore(): Record<string, any>;
|
|
61
|
+
protected static getPluginStore(): Set<string>;
|
|
62
|
+
protected static getPluginPendingStore(): Set<Promise<void>>;
|
|
63
|
+
protected static getPluginArgumentResolvers(): Set<PluginArgumentsResolver>;
|
|
64
|
+
protected static getPluginHttpCtxResolvers(): Set<PluginArgumentsResolver>;
|
|
65
|
+
protected static createDefaultState(): {
|
|
66
|
+
config: RouterConfig;
|
|
67
|
+
groupContext: AsyncLocalStorage<{
|
|
68
|
+
prefix: string;
|
|
69
|
+
groupMiddlewares: any[];
|
|
70
|
+
}>;
|
|
71
|
+
routes: Set<never>;
|
|
72
|
+
routesByPathMethod: Map<any, any>;
|
|
73
|
+
routesByMethod: Map<any, any>;
|
|
74
|
+
routesByName: Map<any, any>;
|
|
75
|
+
prefix: string;
|
|
76
|
+
groupMiddlewares: any[];
|
|
77
|
+
globalMiddlewares: any[];
|
|
78
|
+
};
|
|
79
|
+
protected static bindStateAccessors(): void;
|
|
80
|
+
protected static createDefaultOptionsHandler(): any;
|
|
81
|
+
/**
|
|
82
|
+
* Default configuration used for everytime the router is reset
|
|
83
|
+
*
|
|
84
|
+
* @param options
|
|
85
|
+
*/
|
|
86
|
+
static configureDefaults(options?: RouterConfig): void;
|
|
87
|
+
/**
|
|
88
|
+
* Use a registered plugin
|
|
89
|
+
*
|
|
90
|
+
* @param this
|
|
91
|
+
* @param plugin
|
|
92
|
+
* @param options
|
|
93
|
+
* @returns
|
|
94
|
+
*/
|
|
95
|
+
static use<Options = any>(plugin: ClearRouterPluginInput<Options>, options?: Options): Promise<void>;
|
|
96
|
+
protected static pluginsReady(): Promise<void>;
|
|
97
|
+
protected static getCurrentPluginRequestContext(): ClearRouterPluginRequestContext | undefined;
|
|
98
|
+
protected static createPluginRequestContext(ctx: any): ClearRouterPluginRequestContext;
|
|
99
|
+
protected static createPluginBind(): PluginBind;
|
|
100
|
+
protected static resolvePluginArguments(ctx: any, routeContext: Omit<ClearRouterPluginArgumentsContext, keyof ClearRouterPluginRequestContext>): Promise<any[] | undefined>;
|
|
101
|
+
protected static resolvePluginHttpCtx(ctx: any): Promise<void>;
|
|
102
|
+
protected static ensureState(): void;
|
|
103
|
+
/**
|
|
104
|
+
* Normalizes a path by ensuring it starts with a single slash and does not have trailing
|
|
105
|
+
* slashes, while preserving dynamic segments and parameters.
|
|
106
|
+
*
|
|
107
|
+
* @param path The path to normalize.
|
|
108
|
+
* @returns The normalized path.
|
|
109
|
+
*/
|
|
110
|
+
static normalizePath(path: string): string;
|
|
111
|
+
protected static parseRouteParameters(path: string): Array<{
|
|
112
|
+
name: string;
|
|
113
|
+
field?: string;
|
|
114
|
+
optional: boolean;
|
|
115
|
+
}>;
|
|
116
|
+
protected static expandRoutePath(path: string): string[];
|
|
117
|
+
protected static routeRegistrationPaths(path: string): string[];
|
|
118
|
+
/**
|
|
119
|
+
* Configures the router with the given options, such as method override settings.
|
|
120
|
+
*
|
|
121
|
+
* @param this
|
|
122
|
+
* @param options
|
|
123
|
+
* @returns
|
|
124
|
+
*/
|
|
125
|
+
static configure(this: any, options?: RouterConfig): void;
|
|
126
|
+
protected static resolveMethodOverride(method: string, headers: Headers | Record<string, any>, body: unknown): HttpMethod | null;
|
|
127
|
+
/**
|
|
128
|
+
* Adds a new route to the router.
|
|
129
|
+
*
|
|
130
|
+
* @param this
|
|
131
|
+
* @param methods
|
|
132
|
+
* @param path
|
|
133
|
+
* @param handler
|
|
134
|
+
* @param middlewares
|
|
135
|
+
*/
|
|
136
|
+
static add(methods: HttpMethod | HttpMethod[], path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
|
|
137
|
+
/**
|
|
138
|
+
* Define a resourceful API controller with standard CRUD routes.
|
|
139
|
+
*
|
|
140
|
+
* @param this
|
|
141
|
+
* @param basePath
|
|
142
|
+
* @param controller
|
|
143
|
+
* @param options
|
|
144
|
+
*/
|
|
145
|
+
static apiResource(basePath: string, controller: any, options?: {
|
|
146
|
+
only?: ControllerAction[];
|
|
147
|
+
except?: ControllerAction[];
|
|
148
|
+
middlewares?: ApiResourceMiddleware<any>;
|
|
149
|
+
}): ResourceRoutes<any, any, any>;
|
|
150
|
+
/**
|
|
151
|
+
* Adds a new GET route to the router.
|
|
152
|
+
*
|
|
153
|
+
* @param this The router instance.
|
|
154
|
+
* @param path The path for the GET route.
|
|
155
|
+
* @param handler The handler function for the GET route.
|
|
156
|
+
* @param middlewares Optional middlewares to apply to the GET route.
|
|
157
|
+
*/
|
|
158
|
+
static get(path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
|
|
159
|
+
/**
|
|
160
|
+
* Adds a new POST route to the router.
|
|
161
|
+
*
|
|
162
|
+
* @param this
|
|
163
|
+
* @param path
|
|
164
|
+
* @param handler
|
|
165
|
+
* @param middlewares
|
|
166
|
+
*/
|
|
167
|
+
static post(path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
|
|
168
|
+
/**
|
|
169
|
+
* Adds a new PUT route to the router.
|
|
170
|
+
*
|
|
171
|
+
* @param this
|
|
172
|
+
* @param path
|
|
173
|
+
* @param handler
|
|
174
|
+
* @param middlewares
|
|
175
|
+
*/
|
|
176
|
+
static put(path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
|
|
177
|
+
/**
|
|
178
|
+
* Adds a new DELETE route to the router.
|
|
179
|
+
*
|
|
180
|
+
* @param this
|
|
181
|
+
* @param path
|
|
182
|
+
* @param handler
|
|
183
|
+
* @param middlewares
|
|
184
|
+
*/
|
|
185
|
+
static delete(path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
|
|
186
|
+
/**
|
|
187
|
+
* Adds a new PATCH route to the router.
|
|
188
|
+
*
|
|
189
|
+
* @param this
|
|
190
|
+
* @param path
|
|
191
|
+
* @param handler
|
|
192
|
+
* @param middlewares
|
|
193
|
+
*/
|
|
194
|
+
static patch(path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
|
|
195
|
+
/**
|
|
196
|
+
* Adds a new OPTIONS route to the router.
|
|
197
|
+
*
|
|
198
|
+
* @param this
|
|
199
|
+
* @param path
|
|
200
|
+
* @param handler
|
|
201
|
+
* @param middlewares
|
|
202
|
+
*/
|
|
203
|
+
static options(path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
|
|
204
|
+
/**
|
|
205
|
+
* Adds a new HEAD route to the router.
|
|
206
|
+
*
|
|
207
|
+
* @param this
|
|
208
|
+
* @param path
|
|
209
|
+
* @param handler
|
|
210
|
+
* @param middlewares
|
|
211
|
+
*/
|
|
212
|
+
static head(path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
|
|
213
|
+
/**
|
|
214
|
+
* Defines a group of routes with a common prefix.
|
|
215
|
+
*
|
|
216
|
+
* @param this
|
|
217
|
+
* @param prefix
|
|
218
|
+
* @param callback
|
|
219
|
+
* @param middlewares
|
|
220
|
+
*/
|
|
221
|
+
static group(prefix: string, callback: () => void | Promise<void>, middlewares?: any[]): Promise<void>;
|
|
222
|
+
/**
|
|
223
|
+
* Adds global middlewares to the router, which will be applied to all routes.
|
|
224
|
+
*
|
|
225
|
+
* @param this
|
|
226
|
+
* @param middlewares
|
|
227
|
+
* @param callback
|
|
228
|
+
*/
|
|
229
|
+
static middleware(middlewares: any[], callback: () => void): void;
|
|
230
|
+
/**
|
|
231
|
+
* Retrieves all registered routes in the router, optionally organized by path or method
|
|
232
|
+
* for easier access and management.
|
|
233
|
+
*
|
|
234
|
+
* @param this
|
|
235
|
+
*/
|
|
236
|
+
static allRoutes(): Array<Route<any, any, any>>;
|
|
237
|
+
/**
|
|
238
|
+
* @param this
|
|
239
|
+
* @param type - 'path' to get routes organized by path
|
|
240
|
+
*/
|
|
241
|
+
static allRoutes(type: 'path'): Record<string, Route<any, any, any>>;
|
|
242
|
+
/**
|
|
243
|
+
* @param this
|
|
244
|
+
* @param type - 'method' to get routes organized by method
|
|
245
|
+
*/
|
|
246
|
+
static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<any, any, any>> };
|
|
247
|
+
static allRoutes(type: 'name'): Record<string, Route<any, any, any>>;
|
|
248
|
+
static route(name: string): Route<any, any, any> | undefined;
|
|
249
|
+
static url(name: string, params?: Record<string, any>): string | undefined;
|
|
250
|
+
/**
|
|
251
|
+
* Provide a class that will overide the base Request instance
|
|
252
|
+
*
|
|
253
|
+
* @param provider
|
|
254
|
+
*/
|
|
255
|
+
static setRequestProvider(provider: typeof Request): void;
|
|
256
|
+
/**
|
|
257
|
+
* Provide a class that will overide the base Response instance
|
|
258
|
+
*
|
|
259
|
+
* @param provider
|
|
260
|
+
*/
|
|
261
|
+
static setResponseProvider(provider: typeof Response): void;
|
|
262
|
+
private static hasPackageInstalled;
|
|
263
|
+
/**
|
|
264
|
+
* Provide a class that will overide the base Response instance
|
|
265
|
+
*
|
|
266
|
+
* @param provider
|
|
267
|
+
*/
|
|
268
|
+
private static initializeInstance;
|
|
269
|
+
protected static resolveHandler(route: Route<any, any, any>): {
|
|
270
|
+
handlerFunction: ((ctx: any, req: Request) => any | Promise<any>) | null;
|
|
271
|
+
instance: Controller<any> | null;
|
|
272
|
+
bindingTarget?: object;
|
|
273
|
+
bindingMethod?: PropertyKey;
|
|
274
|
+
bindingHandler?: object;
|
|
275
|
+
bindingMetadata?: object;
|
|
276
|
+
};
|
|
277
|
+
protected static callHandler(handlerFunction: (ctx: any, req: Request) => any | Promise<any>, ctx: any, bindingTarget?: object, bindingMethod?: PropertyKey, bindingHandler?: object, bindingMetadata?: object): Promise<any>;
|
|
278
|
+
protected static bindRequestToInstance(ctx: any, instance: Controller<any> | Route<any, any, any> | null, route: Route<any, any, any>, payload: {
|
|
279
|
+
body: Record<string, any>;
|
|
280
|
+
query: Record<string, any>;
|
|
281
|
+
params: Record<string, any>;
|
|
282
|
+
method?: HttpMethod | string;
|
|
283
|
+
}): void;
|
|
284
|
+
}
|
|
285
|
+
//#endregion
|
|
286
|
+
export { CoreRouter };
|
|
@@ -1,63 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Request } from "./Request.mjs";
|
|
2
|
+
import { Response } from "./Response.mjs";
|
|
3
|
+
import { Container, getBindingMetadataFromTargets, getDesignParamTypes, getStandardMetadata, isClass } from "./bindings.mjs";
|
|
4
|
+
import { ResourceRoutes } from "../ResourceRoutes.mjs";
|
|
5
|
+
import { Route } from "../Route.mjs";
|
|
2
6
|
import { createRequire } from "node:module";
|
|
3
7
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
4
8
|
|
|
5
|
-
//#region src/Route.ts
|
|
6
|
-
var Route = class {
|
|
7
|
-
ctx;
|
|
8
|
-
body = {};
|
|
9
|
-
query = {};
|
|
10
|
-
params = {};
|
|
11
|
-
clearRequest;
|
|
12
|
-
methods;
|
|
13
|
-
path;
|
|
14
|
-
registrationPaths;
|
|
15
|
-
parameters;
|
|
16
|
-
routeName;
|
|
17
|
-
handler;
|
|
18
|
-
middlewares;
|
|
19
|
-
controllerName;
|
|
20
|
-
actionName;
|
|
21
|
-
handlerType;
|
|
22
|
-
middlewareCount;
|
|
23
|
-
constructor(methods, path, handler, middlewares = [], options = {}) {
|
|
24
|
-
this.methods = methods;
|
|
25
|
-
this.path = path;
|
|
26
|
-
this.registrationPaths = options.registrationPaths || [path];
|
|
27
|
-
this.parameters = options.parameters || [];
|
|
28
|
-
this.handler = handler;
|
|
29
|
-
this.middlewares = middlewares;
|
|
30
|
-
this.handlerType = Array.isArray(handler) ? "controller" : "function";
|
|
31
|
-
this.middlewareCount = middlewares.length;
|
|
32
|
-
this.controllerName = Array.isArray(handler) ? handler[0]?.name : void 0;
|
|
33
|
-
this.actionName = Array.isArray(handler) ? handler[1] : typeof handler === "function" ? handler.constructor.name ?? handler.name : void 0;
|
|
34
|
-
this.onName = options.onName;
|
|
35
|
-
}
|
|
36
|
-
onName;
|
|
37
|
-
name(name) {
|
|
38
|
-
const previousName = this.routeName;
|
|
39
|
-
this.routeName = name;
|
|
40
|
-
this.onName?.(name, this, previousName);
|
|
41
|
-
return this;
|
|
42
|
-
}
|
|
43
|
-
toPath(params = {}) {
|
|
44
|
-
return this.path.replace(/\/?\{([^{}]+)\}/g, (segment, raw) => {
|
|
45
|
-
const optional = raw.endsWith("?");
|
|
46
|
-
const [rawName, rawField] = (optional ? raw.slice(0, -1) : raw).split(":", 2);
|
|
47
|
-
const name = rawName.trim();
|
|
48
|
-
const field = rawField?.trim();
|
|
49
|
-
const value = params[name];
|
|
50
|
-
const resolved = field && value && typeof value === "object" ? value[field] : value;
|
|
51
|
-
if (typeof resolved === "undefined" || resolved === null || resolved === "") {
|
|
52
|
-
if (optional) return "";
|
|
53
|
-
throw new Error(`Missing required route parameter: ${name}`);
|
|
54
|
-
}
|
|
55
|
-
return `${segment.startsWith("/") ? "/" : ""}${encodeURIComponent(String(resolved))}`;
|
|
56
|
-
}) || "/";
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
//#endregion
|
|
61
9
|
//#region src/core/router.ts
|
|
62
10
|
/**
|
|
63
11
|
* @class clear-router CoreRouter
|
|
@@ -98,6 +46,51 @@ var CoreRouter = class {
|
|
|
98
46
|
static groupMiddlewares = [];
|
|
99
47
|
static globalMiddlewares = [];
|
|
100
48
|
/**
|
|
49
|
+
* Resolve middlewares before assigning to adapter
|
|
50
|
+
*
|
|
51
|
+
* @param middleware
|
|
52
|
+
* @returns
|
|
53
|
+
*/
|
|
54
|
+
static resolveMiddleware(middleware) {
|
|
55
|
+
if (!middleware || typeof middleware === "function" && !isClass(middleware)) return middleware;
|
|
56
|
+
const instance = isClass(middleware) ? new middleware() : middleware;
|
|
57
|
+
if (instance && typeof instance.handle === "function") return instance.handle.bind(instance);
|
|
58
|
+
return middleware;
|
|
59
|
+
}
|
|
60
|
+
static resolveMiddlewares(middlewares = []) {
|
|
61
|
+
return middlewares.map((middleware) => this.resolveMiddleware(middleware));
|
|
62
|
+
}
|
|
63
|
+
static routeSpecificity(route) {
|
|
64
|
+
const path = route.registrationPaths.slice().sort((left, right) => right.length - left.length)[0] ?? route.path;
|
|
65
|
+
const segments = this.normalizePath(path).split("/").filter(Boolean);
|
|
66
|
+
return [
|
|
67
|
+
segments.filter((segment) => !segment.startsWith(":")).length,
|
|
68
|
+
segments.length,
|
|
69
|
+
path.length
|
|
70
|
+
];
|
|
71
|
+
}
|
|
72
|
+
static orderedRoutes() {
|
|
73
|
+
return Array.from(this.routes).sort((left, right) => {
|
|
74
|
+
const leftScore = this.routeSpecificity(left);
|
|
75
|
+
const rightScore = this.routeSpecificity(right);
|
|
76
|
+
for (let index = 0; index < leftScore.length; index++) {
|
|
77
|
+
const difference = rightScore[index] - leftScore[index];
|
|
78
|
+
if (difference !== 0) return difference;
|
|
79
|
+
}
|
|
80
|
+
return 0;
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
static removeRouteMethod(route, method, path) {
|
|
84
|
+
route.methods = route.methods.filter((existingMethod) => existingMethod !== method);
|
|
85
|
+
this.routesByPathMethod.delete(`${method.toUpperCase()} ${path}`);
|
|
86
|
+
const methodKey = method.toUpperCase();
|
|
87
|
+
this.routesByMethod.set(methodKey, (this.routesByMethod.get(methodKey) ?? []).filter((existingRoute) => existingRoute !== route));
|
|
88
|
+
if (!route.methods.some((existingMethod) => existingMethod !== "options")) {
|
|
89
|
+
this.routes.delete(route);
|
|
90
|
+
if (route.routeName && this.routesByName.get(route.routeName) === route) this.routesByName.delete(route.routeName);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
101
94
|
* Resets the router to it's default state
|
|
102
95
|
*/
|
|
103
96
|
static reset() {
|
|
@@ -509,17 +502,22 @@ var CoreRouter = class {
|
|
|
509
502
|
const fullPath = this.normalizePath(`${activePrefix}/${path}`);
|
|
510
503
|
const registrationPaths = this.routeRegistrationPaths(fullPath);
|
|
511
504
|
const parameters = this.parseRouteParameters(fullPath);
|
|
512
|
-
|
|
505
|
+
for (const method of methods) {
|
|
506
|
+
const existing = this.routesByPathMethod.get(`${method.toUpperCase()} ${fullPath}`);
|
|
507
|
+
if (existing) this.removeRouteMethod(existing, method, fullPath);
|
|
508
|
+
}
|
|
509
|
+
const route = new Route(methods.includes("options") ? methods : methods.concat("options"), fullPath, handler, this.resolveMiddlewares([
|
|
513
510
|
...this.globalMiddlewares,
|
|
514
511
|
...activeGroupMiddlewares,
|
|
515
512
|
...middlewares || []
|
|
516
|
-
], {
|
|
513
|
+
]), {
|
|
517
514
|
registrationPaths,
|
|
518
515
|
parameters,
|
|
519
516
|
onName: (name, route, previousName) => {
|
|
520
517
|
if (previousName && this.routesByName.get(previousName) === route) this.routesByName.delete(previousName);
|
|
521
518
|
this.routesByName.set(name, route);
|
|
522
|
-
}
|
|
519
|
+
},
|
|
520
|
+
normalizeMiddleware: (middleware) => this.resolveMiddleware(middleware)
|
|
523
521
|
});
|
|
524
522
|
if (!methods.includes("options") && !this.routesByPathMethod.get(`OPTIONS ${fullPath}`)) this.options(path, this.createDefaultOptionsHandler());
|
|
525
523
|
this.routes.add(route);
|
|
@@ -539,6 +537,7 @@ var CoreRouter = class {
|
|
|
539
537
|
* @param options
|
|
540
538
|
*/
|
|
541
539
|
static apiResource(basePath, controller, options) {
|
|
540
|
+
const resourceRoutes = {};
|
|
542
541
|
let paramName = "id";
|
|
543
542
|
if (!!this.config.inferParamName && this.hasPackageInstalled("@h3ravel/support")) {
|
|
544
543
|
const { str } = createRequire(import.meta.url)("@h3ravel/support");
|
|
@@ -575,9 +574,10 @@ var CoreRouter = class {
|
|
|
575
574
|
const { method, path } = actions[action];
|
|
576
575
|
const actionMiddlewares = typeof options?.middlewares === "object" && !Array.isArray(options.middlewares) ? options.middlewares[action] : options?.middlewares;
|
|
577
576
|
const name = `${basePath}${path}`.replace(/\/:[^/]+|\/\{[^}]+\}/g, "").replace(/\{(\w+):[^}]+\}/g, "$1").replace(/\/|:|[{}]/g, ".").replace(/\.{2,}/g, ".").replace(/^\.|\.$/g, "");
|
|
578
|
-
this.add(method, `${basePath}${path}`, [controller, action], Array.isArray(actionMiddlewares) ? actionMiddlewares : actionMiddlewares ? [actionMiddlewares] : void 0).name(name + "." + action.toLowerCase());
|
|
577
|
+
resourceRoutes[action] = this.add(method, `${basePath}${path}`, [controller, action], Array.isArray(actionMiddlewares) ? actionMiddlewares : actionMiddlewares ? [actionMiddlewares] : void 0).name(name + "." + action.toLowerCase());
|
|
579
578
|
}
|
|
580
579
|
}
|
|
580
|
+
return new ResourceRoutes(resourceRoutes);
|
|
581
581
|
}
|
|
582
582
|
/**
|
|
583
583
|
* Adds a new GET route to the router.
|
|
@@ -856,4 +856,4 @@ var CoreRouter = class {
|
|
|
856
856
|
};
|
|
857
857
|
|
|
858
858
|
//#endregion
|
|
859
|
-
export { CoreRouter
|
|
859
|
+
export { CoreRouter };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_bindings = require('../bindings
|
|
2
|
+
const require_bindings = require('../core/bindings.cjs');
|
|
3
3
|
|
|
4
4
|
exports.Bind = require_bindings.Bind;
|
|
5
5
|
exports.Container = require_bindings.Container;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Bind, BindDecorator, BindFactory, BindToken, BindValue, Container } from "../core/bindings.cjs";
|
|
2
2
|
export { Bind, type BindDecorator, type BindFactory, type BindToken, type BindValue, Container };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Bind, BindDecorator, BindFactory, BindToken, BindValue, Container } from "../core/bindings.mjs";
|
|
2
2
|
export { Bind, type BindDecorator, type BindFactory, type BindToken, type BindValue, Container };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_bindings = require('../bindings
|
|
3
|
-
const require_router = require('../router
|
|
2
|
+
const require_bindings = require('../core/bindings.cjs');
|
|
3
|
+
const require_router = require('../core/router.cjs');
|
|
4
4
|
require('./index.cjs');
|
|
5
5
|
require("reflect-metadata");
|
|
6
6
|
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Bind, BindDecorator, BindFactory, BindToken, BindValue, Container } from "../core/bindings.cjs";
|
|
2
2
|
export { Bind, BindDecorator, BindFactory, BindToken, BindValue, Container };
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import "reflect-metadata";
|
|
1
|
+
import { Bind, BindDecorator, BindFactory, BindToken, BindValue, Container } from "../core/bindings.mjs";
|
|
3
2
|
export { Bind, BindDecorator, BindFactory, BindToken, BindValue, Container };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { Bind, Container } from "../core/bindings.mjs";
|
|
2
|
+
import { CoreRouter } from "../core/router.mjs";
|
|
3
3
|
import "./index.mjs";
|
|
4
4
|
import "reflect-metadata";
|
|
5
5
|
|