clear-router 2.7.7 → 2.8.1
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/{Request-BhTJDR_5.d.mts → Request-Ci0UQ-Vl.d.mts} +27 -0
- 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 +150 -0
- package/dist/ResourceRoutes.d.cts +96 -0
- package/dist/ResourceRoutes.d.mts +96 -0
- package/dist/ResourceRoutes.mjs +150 -0
- package/dist/Route.cjs +32 -0
- package/dist/Route.d.cts +27 -0
- package/dist/Route.d.mts +27 -0
- package/dist/Route.mjs +32 -0
- package/dist/core/helpers.cjs +9 -0
- package/dist/core/helpers.d.cts +4 -0
- package/dist/core/helpers.d.mts +4 -0
- package/dist/core/helpers.mjs +8 -0
- package/dist/core/index.cjs +8 -0
- package/dist/core/index.d.cts +2 -1
- package/dist/core/index.d.mts +2 -1
- package/dist/core/index.mjs +2 -1
- package/dist/core/router.cjs +66 -37
- package/dist/core/router.d.cts +17 -4
- package/dist/core/router.d.mts +17 -4
- package/dist/core/router.mjs +66 -37
- package/dist/decorators/setup.d.mts +0 -1
- package/dist/express/router.cjs +2 -2
- package/dist/express/router.d.cts +5 -4
- package/dist/express/router.d.mts +5 -4
- package/dist/express/router.mjs +2 -2
- package/dist/fastify/router.cjs +2 -2
- package/dist/fastify/router.d.cts +5 -4
- package/dist/fastify/router.d.mts +5 -4
- package/dist/fastify/router.mjs +2 -2
- package/dist/h3/router.cjs +2 -2
- package/dist/h3/router.d.cts +22 -21
- package/dist/h3/router.d.mts +22 -21
- package/dist/h3/router.mjs +2 -2
- package/dist/hono/router.cjs +2 -2
- package/dist/hono/router.d.cts +5 -4
- package/dist/hono/router.d.mts +5 -4
- package/dist/hono/router.mjs +2 -2
- package/dist/index.cjs +3 -1
- package/dist/index.d.cts +2 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.mjs +2 -1
- package/dist/koa/router.cjs +2 -2
- package/dist/koa/router.d.cts +5 -4
- package/dist/koa/router.d.mts +5 -4
- package/dist/koa/router.mjs +2 -2
- package/dist/types/basic.d.cts +13 -4
- package/dist/types/basic.d.mts +13 -4
- package/dist/types/express.d.cts +6 -2
- package/dist/types/express.d.mts +6 -2
- package/dist/types/fastify.d.cts +3 -2
- package/dist/types/fastify.d.mts +3 -2
- package/dist/types/h3.d.cts +7 -2
- package/dist/types/h3.d.mts +7 -2
- package/dist/types/hono.d.cts +4 -3
- package/dist/types/hono.d.mts +4 -3
- package/dist/types/koa.d.cts +3 -2
- package/dist/types/koa.d.mts +3 -2
- package/package.json +1 -1
|
@@ -40,6 +40,12 @@ declare class Response {
|
|
|
40
40
|
}
|
|
41
41
|
//#endregion
|
|
42
42
|
//#region src/Route.d.ts
|
|
43
|
+
/**
|
|
44
|
+
* @class clear-router Route
|
|
45
|
+
* @description A route describes a single enpoint on clear-router
|
|
46
|
+
* @author 3m1n3nc3
|
|
47
|
+
* @repository https://github.com/arkstack-tmp/clear-router
|
|
48
|
+
*/
|
|
43
49
|
declare class Route<X = any, M = Middleware$1 | Middleware$4, H = any> {
|
|
44
50
|
ctx: X;
|
|
45
51
|
body: RequestData;
|
|
@@ -61,9 +67,30 @@ declare class Route<X = any, M = Middleware$1 | Middleware$4, H = any> {
|
|
|
61
67
|
registrationPaths?: string[];
|
|
62
68
|
parameters?: RouteParameter[];
|
|
63
69
|
onName?: (name: string, route: Route<X, M, H>, previousName?: string) => void;
|
|
70
|
+
normalizeMiddleware?: (middleware: M) => M;
|
|
64
71
|
});
|
|
65
72
|
private onName?;
|
|
73
|
+
private normalizeMiddleware?;
|
|
74
|
+
/**
|
|
75
|
+
* Set the route name
|
|
76
|
+
*
|
|
77
|
+
* @param name
|
|
78
|
+
* @returns
|
|
79
|
+
*/
|
|
66
80
|
name(name: string): this;
|
|
81
|
+
/**
|
|
82
|
+
* Register one or more middleware that will be executed before the route.
|
|
83
|
+
*
|
|
84
|
+
* @param middlewares
|
|
85
|
+
* @returns
|
|
86
|
+
*/
|
|
87
|
+
middleware(middlewares: M[] | M): this;
|
|
88
|
+
/**
|
|
89
|
+
* Get the path generated and accessible by this route
|
|
90
|
+
*
|
|
91
|
+
* @param params
|
|
92
|
+
* @returns
|
|
93
|
+
*/
|
|
67
94
|
toPath(params?: RequestData): string;
|
|
68
95
|
}
|
|
69
96
|
//#endregion
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/ResourceRouteSelection.ts
|
|
3
|
+
var ResourceRouteSelection = class {
|
|
4
|
+
constructor(routes) {
|
|
5
|
+
this.routes = routes;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Register one or more middleware that will be executed before the route.
|
|
9
|
+
*
|
|
10
|
+
* @param middlewares
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
middleware(middlewares) {
|
|
14
|
+
for (const route of this.routes) route.middleware(middlewares);
|
|
15
|
+
return this;
|
|
16
|
+
}
|
|
17
|
+
all() {
|
|
18
|
+
return this.routes;
|
|
19
|
+
}
|
|
20
|
+
first() {
|
|
21
|
+
return this.routes[0];
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
exports.ResourceRouteSelection = ResourceRouteSelection;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Middleware } from "./types/express.cjs";
|
|
2
|
+
import { Middleware as Middleware$1 } from "./types/h3.cjs";
|
|
3
|
+
import { Route } from "./Route.cjs";
|
|
4
|
+
|
|
5
|
+
//#region src/ResourceRouteSelection.d.ts
|
|
6
|
+
declare class ResourceRouteSelection<X = any, M = Middleware$1 | Middleware, H = any> {
|
|
7
|
+
readonly routes: Array<Route<X, M, H>>;
|
|
8
|
+
constructor(routes: Array<Route<X, M, H>>);
|
|
9
|
+
/**
|
|
10
|
+
* Register one or more middleware that will be executed before the route.
|
|
11
|
+
*
|
|
12
|
+
* @param middlewares
|
|
13
|
+
* @returns
|
|
14
|
+
*/
|
|
15
|
+
middleware(middlewares: M[] | M): this;
|
|
16
|
+
all(): Array<Route<X, M, H>>;
|
|
17
|
+
first(): Route<X, M, H> | undefined;
|
|
18
|
+
}
|
|
19
|
+
//#endregion
|
|
20
|
+
export { ResourceRouteSelection };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Middleware } from "./types/express.mjs";
|
|
2
|
+
import { Middleware as Middleware$1 } from "./types/h3.mjs";
|
|
3
|
+
import { Route } from "./Route.mjs";
|
|
4
|
+
|
|
5
|
+
//#region src/ResourceRouteSelection.d.ts
|
|
6
|
+
declare class ResourceRouteSelection<X = any, M = Middleware$1 | Middleware, H = any> {
|
|
7
|
+
readonly routes: Array<Route<X, M, H>>;
|
|
8
|
+
constructor(routes: Array<Route<X, M, H>>);
|
|
9
|
+
/**
|
|
10
|
+
* Register one or more middleware that will be executed before the route.
|
|
11
|
+
*
|
|
12
|
+
* @param middlewares
|
|
13
|
+
* @returns
|
|
14
|
+
*/
|
|
15
|
+
middleware(middlewares: M[] | M): this;
|
|
16
|
+
all(): Array<Route<X, M, H>>;
|
|
17
|
+
first(): Route<X, M, H> | undefined;
|
|
18
|
+
}
|
|
19
|
+
//#endregion
|
|
20
|
+
export { ResourceRouteSelection };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//#region src/ResourceRouteSelection.ts
|
|
2
|
+
var ResourceRouteSelection = class {
|
|
3
|
+
constructor(routes) {
|
|
4
|
+
this.routes = routes;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Register one or more middleware that will be executed before the route.
|
|
8
|
+
*
|
|
9
|
+
* @param middlewares
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
middleware(middlewares) {
|
|
13
|
+
for (const route of this.routes) route.middleware(middlewares);
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
all() {
|
|
17
|
+
return this.routes;
|
|
18
|
+
}
|
|
19
|
+
first() {
|
|
20
|
+
return this.routes[0];
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
export { ResourceRouteSelection };
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
const require_ResourceRouteSelection = require('./ResourceRouteSelection.cjs');
|
|
2
|
+
const require_helpers = require('./core/helpers.cjs');
|
|
3
|
+
require('./core/index.cjs');
|
|
4
|
+
|
|
5
|
+
//#region src/ResourceRoutes.ts
|
|
6
|
+
/**
|
|
7
|
+
* @class clear-router ResourceRoutes
|
|
8
|
+
* @description A ResourceRoutes creates a collection of resourceful routes in a single call
|
|
9
|
+
* @author 3m1n3nc3
|
|
10
|
+
* @repository https://github.com/arkstack-tmp/clear-router
|
|
11
|
+
*/
|
|
12
|
+
var ResourceRoutes = class ResourceRoutes {
|
|
13
|
+
static actions = {
|
|
14
|
+
index: {
|
|
15
|
+
method: "get",
|
|
16
|
+
path: "/"
|
|
17
|
+
},
|
|
18
|
+
show: {
|
|
19
|
+
method: "get",
|
|
20
|
+
path: "/:{param}"
|
|
21
|
+
},
|
|
22
|
+
create: {
|
|
23
|
+
method: "post",
|
|
24
|
+
path: "/"
|
|
25
|
+
},
|
|
26
|
+
update: {
|
|
27
|
+
method: "put",
|
|
28
|
+
path: "/:{param}"
|
|
29
|
+
},
|
|
30
|
+
destroy: {
|
|
31
|
+
method: "delete",
|
|
32
|
+
path: "/:{param}"
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
routes = {};
|
|
36
|
+
options;
|
|
37
|
+
chainedMiddlewares = [];
|
|
38
|
+
constructor(basePath, controller, paramName, options, registerRoute, removeRoute) {
|
|
39
|
+
this.basePath = basePath;
|
|
40
|
+
this.controller = controller;
|
|
41
|
+
this.paramName = paramName;
|
|
42
|
+
this.registerRoute = registerRoute;
|
|
43
|
+
this.removeRoute = removeRoute;
|
|
44
|
+
this.options = options ?? {};
|
|
45
|
+
}
|
|
46
|
+
register() {
|
|
47
|
+
this.clear();
|
|
48
|
+
const preController = typeof this.controller === "function" ? new this.controller() : this.controller;
|
|
49
|
+
for (const action of this.selectedActions()) {
|
|
50
|
+
if (typeof preController[action] !== "function") continue;
|
|
51
|
+
const definition = this.definitionFor(action);
|
|
52
|
+
const route = this.registerRoute({
|
|
53
|
+
action,
|
|
54
|
+
method: definition.method,
|
|
55
|
+
path: `${this.basePath}${definition.path}`,
|
|
56
|
+
handler: [this.controller, action],
|
|
57
|
+
middlewares: this.resolveActionMiddlewares(action),
|
|
58
|
+
name: `${this.nameFor(definition.path)}.${action.toLowerCase()}`
|
|
59
|
+
});
|
|
60
|
+
this.routes[action] = route;
|
|
61
|
+
}
|
|
62
|
+
for (const middlewares of this.chainedMiddlewares) this.middleware(middlewares, false);
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Only register routes for the provided actions.
|
|
67
|
+
*/
|
|
68
|
+
only(action, ...actions) {
|
|
69
|
+
this.options.only = Array.from(new Set(require_helpers.wrap(action).concat(actions)));
|
|
70
|
+
return this.register();
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Register all resource routes except the provided actions.
|
|
74
|
+
*/
|
|
75
|
+
except(action, ...actions) {
|
|
76
|
+
this.options.except = Array.from(new Set(require_helpers.wrap(action).concat(actions)));
|
|
77
|
+
return this.register();
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Register one or more middleware that will be executed before the route.
|
|
81
|
+
*/
|
|
82
|
+
middleware(middlewares, remember = true) {
|
|
83
|
+
if (remember) this.chainedMiddlewares.push(middlewares);
|
|
84
|
+
for (const route of Object.values(this.routes)) route?.middleware(middlewares);
|
|
85
|
+
return this;
|
|
86
|
+
}
|
|
87
|
+
action(action) {
|
|
88
|
+
return this.routes[action];
|
|
89
|
+
}
|
|
90
|
+
index() {
|
|
91
|
+
return this.routes.index;
|
|
92
|
+
}
|
|
93
|
+
show() {
|
|
94
|
+
return this.routes.show;
|
|
95
|
+
}
|
|
96
|
+
create() {
|
|
97
|
+
return this.routes.create;
|
|
98
|
+
}
|
|
99
|
+
update() {
|
|
100
|
+
return this.routes.update;
|
|
101
|
+
}
|
|
102
|
+
destroy() {
|
|
103
|
+
return this.routes.destroy;
|
|
104
|
+
}
|
|
105
|
+
get() {
|
|
106
|
+
return this.byMethod("get");
|
|
107
|
+
}
|
|
108
|
+
post() {
|
|
109
|
+
return this.byMethod("post");
|
|
110
|
+
}
|
|
111
|
+
put() {
|
|
112
|
+
return this.byMethod("put");
|
|
113
|
+
}
|
|
114
|
+
delete() {
|
|
115
|
+
return this.byMethod("delete");
|
|
116
|
+
}
|
|
117
|
+
selectedActions() {
|
|
118
|
+
const actions = this.options.only?.length ? this.options.only : Object.keys(ResourceRoutes.actions);
|
|
119
|
+
const except = new Set(this.options.except ?? []);
|
|
120
|
+
return actions.filter((action) => !except.has(action));
|
|
121
|
+
}
|
|
122
|
+
definitionFor(action) {
|
|
123
|
+
const definition = ResourceRoutes.actions[action];
|
|
124
|
+
return {
|
|
125
|
+
method: definition.method,
|
|
126
|
+
path: definition.path.replace("{param}", this.paramName)
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
resolveActionMiddlewares(action) {
|
|
130
|
+
const middlewares = this.isActionMiddlewareMap(this.options.middlewares) ? this.options.middlewares[action] : this.options.middlewares;
|
|
131
|
+
return Array.isArray(middlewares) ? middlewares : middlewares ? [middlewares] : void 0;
|
|
132
|
+
}
|
|
133
|
+
isActionMiddlewareMap(middlewares) {
|
|
134
|
+
if (!middlewares || Array.isArray(middlewares) || typeof middlewares !== "object") return false;
|
|
135
|
+
return Object.keys(ResourceRoutes.actions).some((action) => action in middlewares);
|
|
136
|
+
}
|
|
137
|
+
nameFor(path) {
|
|
138
|
+
return `${this.basePath}${path}`.replace(/\/:[^/]+|\/\{[^}]+\}/g, "").replace(/\{(\w+):[^}]+\}/g, "$1").replace(/\/|:|[{}]/g, ".").replace(/\.{2,}/g, ".").replace(/^\.|\.$/g, "");
|
|
139
|
+
}
|
|
140
|
+
byMethod(method) {
|
|
141
|
+
return new require_ResourceRouteSelection.ResourceRouteSelection(Object.values(this.routes).filter((route) => Boolean(route?.methods.includes(method))));
|
|
142
|
+
}
|
|
143
|
+
clear() {
|
|
144
|
+
for (const route of Object.values(this.routes)) if (route) this.removeRoute(route);
|
|
145
|
+
for (const key of Object.keys(this.routes)) delete this.routes[key];
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
//#endregion
|
|
150
|
+
exports.ResourceRoutes = ResourceRoutes;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { ApiResourceMiddleware, HttpMethod, ResourceAction } from "./types/basic.cjs";
|
|
2
|
+
import { Middleware } from "./types/express.cjs";
|
|
3
|
+
import { Middleware as Middleware$1 } from "./types/h3.cjs";
|
|
4
|
+
import { Route } from "./Route.cjs";
|
|
5
|
+
import { ResourceRouteSelection } from "./ResourceRouteSelection.cjs";
|
|
6
|
+
|
|
7
|
+
//#region src/ResourceRoutes.d.ts
|
|
8
|
+
type ResourceRouteDefinition = {
|
|
9
|
+
method: HttpMethod;
|
|
10
|
+
path: string;
|
|
11
|
+
};
|
|
12
|
+
type ResourceRouteRegistrar<X, M, H> = (input: {
|
|
13
|
+
action: ResourceAction;
|
|
14
|
+
method: HttpMethod;
|
|
15
|
+
path: string;
|
|
16
|
+
handler: any;
|
|
17
|
+
middlewares?: M[];
|
|
18
|
+
name: string;
|
|
19
|
+
}) => Route<X, M, H>;
|
|
20
|
+
type ResourceRouteRemover<X, M, H> = (route: Route<X, M, H>) => void;
|
|
21
|
+
type ResourceRoutesOptions<M = any> = {
|
|
22
|
+
only?: ResourceAction[];
|
|
23
|
+
except?: ResourceAction[];
|
|
24
|
+
middlewares?: ApiResourceMiddleware<M>;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* @class clear-router ResourceRoutes
|
|
28
|
+
* @description A ResourceRoutes creates a collection of resourceful routes in a single call
|
|
29
|
+
* @author 3m1n3nc3
|
|
30
|
+
* @repository https://github.com/arkstack-tmp/clear-router
|
|
31
|
+
*/
|
|
32
|
+
declare class ResourceRoutes<X = any, M = Middleware$1 | Middleware, H = any> {
|
|
33
|
+
readonly basePath: string;
|
|
34
|
+
readonly controller: any;
|
|
35
|
+
readonly paramName: string;
|
|
36
|
+
protected readonly registerRoute: ResourceRouteRegistrar<X, M, H>;
|
|
37
|
+
protected readonly removeRoute: ResourceRouteRemover<X, M, H>;
|
|
38
|
+
static actions: {
|
|
39
|
+
readonly index: {
|
|
40
|
+
readonly method: "get";
|
|
41
|
+
readonly path: "/";
|
|
42
|
+
};
|
|
43
|
+
readonly show: {
|
|
44
|
+
readonly method: "get";
|
|
45
|
+
readonly path: "/:{param}";
|
|
46
|
+
};
|
|
47
|
+
readonly create: {
|
|
48
|
+
readonly method: "post";
|
|
49
|
+
readonly path: "/";
|
|
50
|
+
};
|
|
51
|
+
readonly update: {
|
|
52
|
+
readonly method: "put";
|
|
53
|
+
readonly path: "/:{param}";
|
|
54
|
+
};
|
|
55
|
+
readonly destroy: {
|
|
56
|
+
readonly method: "delete";
|
|
57
|
+
readonly path: "/:{param}";
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
readonly routes: Partial<Record<ResourceAction, Route<X, M, H>>>;
|
|
61
|
+
protected options: ResourceRoutesOptions<M>;
|
|
62
|
+
protected chainedMiddlewares: Array<M[] | M>;
|
|
63
|
+
constructor(basePath: string, controller: any, paramName: string, options: ResourceRoutesOptions<M> | undefined, registerRoute: ResourceRouteRegistrar<X, M, H>, removeRoute: ResourceRouteRemover<X, M, H>);
|
|
64
|
+
register(): this;
|
|
65
|
+
/**
|
|
66
|
+
* Only register routes for the provided actions.
|
|
67
|
+
*/
|
|
68
|
+
only(action: ResourceAction | ResourceAction[], ...actions: ResourceAction[]): this;
|
|
69
|
+
/**
|
|
70
|
+
* Register all resource routes except the provided actions.
|
|
71
|
+
*/
|
|
72
|
+
except(action: ResourceAction | ResourceAction[], ...actions: ResourceAction[]): this;
|
|
73
|
+
/**
|
|
74
|
+
* Register one or more middleware that will be executed before the route.
|
|
75
|
+
*/
|
|
76
|
+
middleware(middlewares: M[] | M, remember?: boolean): this;
|
|
77
|
+
action(action: ResourceAction): Route<X, M, H> | undefined;
|
|
78
|
+
index(): Route<X, M, H> | undefined;
|
|
79
|
+
show(): Route<X, M, H> | undefined;
|
|
80
|
+
create(): Route<X, M, H> | undefined;
|
|
81
|
+
update(): Route<X, M, H> | undefined;
|
|
82
|
+
destroy(): Route<X, M, H> | undefined;
|
|
83
|
+
get(): ResourceRouteSelection<X, M, H>;
|
|
84
|
+
post(): ResourceRouteSelection<X, M, H>;
|
|
85
|
+
put(): ResourceRouteSelection<X, M, H>;
|
|
86
|
+
delete(): ResourceRouteSelection<X, M, H>;
|
|
87
|
+
protected selectedActions(): ResourceAction[];
|
|
88
|
+
protected definitionFor(action: ResourceAction): ResourceRouteDefinition;
|
|
89
|
+
protected resolveActionMiddlewares(action: ResourceAction): M[] | undefined;
|
|
90
|
+
protected isActionMiddlewareMap(middlewares: ApiResourceMiddleware<M> | undefined): middlewares is { [K in ResourceAction]?: M | M[] };
|
|
91
|
+
protected nameFor(path: string): string;
|
|
92
|
+
protected byMethod(method: HttpMethod): ResourceRouteSelection<X, M, H>;
|
|
93
|
+
protected clear(): void;
|
|
94
|
+
}
|
|
95
|
+
//#endregion
|
|
96
|
+
export { ResourceRoutes };
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { ApiResourceMiddleware, HttpMethod, ResourceAction } from "./types/basic.mjs";
|
|
2
|
+
import { Middleware } from "./types/express.mjs";
|
|
3
|
+
import { Middleware as Middleware$1 } from "./types/h3.mjs";
|
|
4
|
+
import { Route } from "./Route.mjs";
|
|
5
|
+
import { ResourceRouteSelection } from "./ResourceRouteSelection.mjs";
|
|
6
|
+
|
|
7
|
+
//#region src/ResourceRoutes.d.ts
|
|
8
|
+
type ResourceRouteDefinition = {
|
|
9
|
+
method: HttpMethod;
|
|
10
|
+
path: string;
|
|
11
|
+
};
|
|
12
|
+
type ResourceRouteRegistrar<X, M, H> = (input: {
|
|
13
|
+
action: ResourceAction;
|
|
14
|
+
method: HttpMethod;
|
|
15
|
+
path: string;
|
|
16
|
+
handler: any;
|
|
17
|
+
middlewares?: M[];
|
|
18
|
+
name: string;
|
|
19
|
+
}) => Route<X, M, H>;
|
|
20
|
+
type ResourceRouteRemover<X, M, H> = (route: Route<X, M, H>) => void;
|
|
21
|
+
type ResourceRoutesOptions<M = any> = {
|
|
22
|
+
only?: ResourceAction[];
|
|
23
|
+
except?: ResourceAction[];
|
|
24
|
+
middlewares?: ApiResourceMiddleware<M>;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* @class clear-router ResourceRoutes
|
|
28
|
+
* @description A ResourceRoutes creates a collection of resourceful routes in a single call
|
|
29
|
+
* @author 3m1n3nc3
|
|
30
|
+
* @repository https://github.com/arkstack-tmp/clear-router
|
|
31
|
+
*/
|
|
32
|
+
declare class ResourceRoutes<X = any, M = Middleware$1 | Middleware, H = any> {
|
|
33
|
+
readonly basePath: string;
|
|
34
|
+
readonly controller: any;
|
|
35
|
+
readonly paramName: string;
|
|
36
|
+
protected readonly registerRoute: ResourceRouteRegistrar<X, M, H>;
|
|
37
|
+
protected readonly removeRoute: ResourceRouteRemover<X, M, H>;
|
|
38
|
+
static actions: {
|
|
39
|
+
readonly index: {
|
|
40
|
+
readonly method: "get";
|
|
41
|
+
readonly path: "/";
|
|
42
|
+
};
|
|
43
|
+
readonly show: {
|
|
44
|
+
readonly method: "get";
|
|
45
|
+
readonly path: "/:{param}";
|
|
46
|
+
};
|
|
47
|
+
readonly create: {
|
|
48
|
+
readonly method: "post";
|
|
49
|
+
readonly path: "/";
|
|
50
|
+
};
|
|
51
|
+
readonly update: {
|
|
52
|
+
readonly method: "put";
|
|
53
|
+
readonly path: "/:{param}";
|
|
54
|
+
};
|
|
55
|
+
readonly destroy: {
|
|
56
|
+
readonly method: "delete";
|
|
57
|
+
readonly path: "/:{param}";
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
readonly routes: Partial<Record<ResourceAction, Route<X, M, H>>>;
|
|
61
|
+
protected options: ResourceRoutesOptions<M>;
|
|
62
|
+
protected chainedMiddlewares: Array<M[] | M>;
|
|
63
|
+
constructor(basePath: string, controller: any, paramName: string, options: ResourceRoutesOptions<M> | undefined, registerRoute: ResourceRouteRegistrar<X, M, H>, removeRoute: ResourceRouteRemover<X, M, H>);
|
|
64
|
+
register(): this;
|
|
65
|
+
/**
|
|
66
|
+
* Only register routes for the provided actions.
|
|
67
|
+
*/
|
|
68
|
+
only(action: ResourceAction | ResourceAction[], ...actions: ResourceAction[]): this;
|
|
69
|
+
/**
|
|
70
|
+
* Register all resource routes except the provided actions.
|
|
71
|
+
*/
|
|
72
|
+
except(action: ResourceAction | ResourceAction[], ...actions: ResourceAction[]): this;
|
|
73
|
+
/**
|
|
74
|
+
* Register one or more middleware that will be executed before the route.
|
|
75
|
+
*/
|
|
76
|
+
middleware(middlewares: M[] | M, remember?: boolean): this;
|
|
77
|
+
action(action: ResourceAction): Route<X, M, H> | undefined;
|
|
78
|
+
index(): Route<X, M, H> | undefined;
|
|
79
|
+
show(): Route<X, M, H> | undefined;
|
|
80
|
+
create(): Route<X, M, H> | undefined;
|
|
81
|
+
update(): Route<X, M, H> | undefined;
|
|
82
|
+
destroy(): Route<X, M, H> | undefined;
|
|
83
|
+
get(): ResourceRouteSelection<X, M, H>;
|
|
84
|
+
post(): ResourceRouteSelection<X, M, H>;
|
|
85
|
+
put(): ResourceRouteSelection<X, M, H>;
|
|
86
|
+
delete(): ResourceRouteSelection<X, M, H>;
|
|
87
|
+
protected selectedActions(): ResourceAction[];
|
|
88
|
+
protected definitionFor(action: ResourceAction): ResourceRouteDefinition;
|
|
89
|
+
protected resolveActionMiddlewares(action: ResourceAction): M[] | undefined;
|
|
90
|
+
protected isActionMiddlewareMap(middlewares: ApiResourceMiddleware<M> | undefined): middlewares is { [K in ResourceAction]?: M | M[] };
|
|
91
|
+
protected nameFor(path: string): string;
|
|
92
|
+
protected byMethod(method: HttpMethod): ResourceRouteSelection<X, M, H>;
|
|
93
|
+
protected clear(): void;
|
|
94
|
+
}
|
|
95
|
+
//#endregion
|
|
96
|
+
export { ResourceRoutes };
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { ResourceRouteSelection } from "./ResourceRouteSelection.mjs";
|
|
2
|
+
import { wrap } from "./core/helpers.mjs";
|
|
3
|
+
import "./core/index.mjs";
|
|
4
|
+
|
|
5
|
+
//#region src/ResourceRoutes.ts
|
|
6
|
+
/**
|
|
7
|
+
* @class clear-router ResourceRoutes
|
|
8
|
+
* @description A ResourceRoutes creates a collection of resourceful routes in a single call
|
|
9
|
+
* @author 3m1n3nc3
|
|
10
|
+
* @repository https://github.com/arkstack-tmp/clear-router
|
|
11
|
+
*/
|
|
12
|
+
var ResourceRoutes = class ResourceRoutes {
|
|
13
|
+
static actions = {
|
|
14
|
+
index: {
|
|
15
|
+
method: "get",
|
|
16
|
+
path: "/"
|
|
17
|
+
},
|
|
18
|
+
show: {
|
|
19
|
+
method: "get",
|
|
20
|
+
path: "/:{param}"
|
|
21
|
+
},
|
|
22
|
+
create: {
|
|
23
|
+
method: "post",
|
|
24
|
+
path: "/"
|
|
25
|
+
},
|
|
26
|
+
update: {
|
|
27
|
+
method: "put",
|
|
28
|
+
path: "/:{param}"
|
|
29
|
+
},
|
|
30
|
+
destroy: {
|
|
31
|
+
method: "delete",
|
|
32
|
+
path: "/:{param}"
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
routes = {};
|
|
36
|
+
options;
|
|
37
|
+
chainedMiddlewares = [];
|
|
38
|
+
constructor(basePath, controller, paramName, options, registerRoute, removeRoute) {
|
|
39
|
+
this.basePath = basePath;
|
|
40
|
+
this.controller = controller;
|
|
41
|
+
this.paramName = paramName;
|
|
42
|
+
this.registerRoute = registerRoute;
|
|
43
|
+
this.removeRoute = removeRoute;
|
|
44
|
+
this.options = options ?? {};
|
|
45
|
+
}
|
|
46
|
+
register() {
|
|
47
|
+
this.clear();
|
|
48
|
+
const preController = typeof this.controller === "function" ? new this.controller() : this.controller;
|
|
49
|
+
for (const action of this.selectedActions()) {
|
|
50
|
+
if (typeof preController[action] !== "function") continue;
|
|
51
|
+
const definition = this.definitionFor(action);
|
|
52
|
+
const route = this.registerRoute({
|
|
53
|
+
action,
|
|
54
|
+
method: definition.method,
|
|
55
|
+
path: `${this.basePath}${definition.path}`,
|
|
56
|
+
handler: [this.controller, action],
|
|
57
|
+
middlewares: this.resolveActionMiddlewares(action),
|
|
58
|
+
name: `${this.nameFor(definition.path)}.${action.toLowerCase()}`
|
|
59
|
+
});
|
|
60
|
+
this.routes[action] = route;
|
|
61
|
+
}
|
|
62
|
+
for (const middlewares of this.chainedMiddlewares) this.middleware(middlewares, false);
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Only register routes for the provided actions.
|
|
67
|
+
*/
|
|
68
|
+
only(action, ...actions) {
|
|
69
|
+
this.options.only = Array.from(new Set(wrap(action).concat(actions)));
|
|
70
|
+
return this.register();
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Register all resource routes except the provided actions.
|
|
74
|
+
*/
|
|
75
|
+
except(action, ...actions) {
|
|
76
|
+
this.options.except = Array.from(new Set(wrap(action).concat(actions)));
|
|
77
|
+
return this.register();
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Register one or more middleware that will be executed before the route.
|
|
81
|
+
*/
|
|
82
|
+
middleware(middlewares, remember = true) {
|
|
83
|
+
if (remember) this.chainedMiddlewares.push(middlewares);
|
|
84
|
+
for (const route of Object.values(this.routes)) route?.middleware(middlewares);
|
|
85
|
+
return this;
|
|
86
|
+
}
|
|
87
|
+
action(action) {
|
|
88
|
+
return this.routes[action];
|
|
89
|
+
}
|
|
90
|
+
index() {
|
|
91
|
+
return this.routes.index;
|
|
92
|
+
}
|
|
93
|
+
show() {
|
|
94
|
+
return this.routes.show;
|
|
95
|
+
}
|
|
96
|
+
create() {
|
|
97
|
+
return this.routes.create;
|
|
98
|
+
}
|
|
99
|
+
update() {
|
|
100
|
+
return this.routes.update;
|
|
101
|
+
}
|
|
102
|
+
destroy() {
|
|
103
|
+
return this.routes.destroy;
|
|
104
|
+
}
|
|
105
|
+
get() {
|
|
106
|
+
return this.byMethod("get");
|
|
107
|
+
}
|
|
108
|
+
post() {
|
|
109
|
+
return this.byMethod("post");
|
|
110
|
+
}
|
|
111
|
+
put() {
|
|
112
|
+
return this.byMethod("put");
|
|
113
|
+
}
|
|
114
|
+
delete() {
|
|
115
|
+
return this.byMethod("delete");
|
|
116
|
+
}
|
|
117
|
+
selectedActions() {
|
|
118
|
+
const actions = this.options.only?.length ? this.options.only : Object.keys(ResourceRoutes.actions);
|
|
119
|
+
const except = new Set(this.options.except ?? []);
|
|
120
|
+
return actions.filter((action) => !except.has(action));
|
|
121
|
+
}
|
|
122
|
+
definitionFor(action) {
|
|
123
|
+
const definition = ResourceRoutes.actions[action];
|
|
124
|
+
return {
|
|
125
|
+
method: definition.method,
|
|
126
|
+
path: definition.path.replace("{param}", this.paramName)
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
resolveActionMiddlewares(action) {
|
|
130
|
+
const middlewares = this.isActionMiddlewareMap(this.options.middlewares) ? this.options.middlewares[action] : this.options.middlewares;
|
|
131
|
+
return Array.isArray(middlewares) ? middlewares : middlewares ? [middlewares] : void 0;
|
|
132
|
+
}
|
|
133
|
+
isActionMiddlewareMap(middlewares) {
|
|
134
|
+
if (!middlewares || Array.isArray(middlewares) || typeof middlewares !== "object") return false;
|
|
135
|
+
return Object.keys(ResourceRoutes.actions).some((action) => action in middlewares);
|
|
136
|
+
}
|
|
137
|
+
nameFor(path) {
|
|
138
|
+
return `${this.basePath}${path}`.replace(/\/:[^/]+|\/\{[^}]+\}/g, "").replace(/\{(\w+):[^}]+\}/g, "$1").replace(/\/|:|[{}]/g, ".").replace(/\.{2,}/g, ".").replace(/^\.|\.$/g, "");
|
|
139
|
+
}
|
|
140
|
+
byMethod(method) {
|
|
141
|
+
return new ResourceRouteSelection(Object.values(this.routes).filter((route) => Boolean(route?.methods.includes(method))));
|
|
142
|
+
}
|
|
143
|
+
clear() {
|
|
144
|
+
for (const route of Object.values(this.routes)) if (route) this.removeRoute(route);
|
|
145
|
+
for (const key of Object.keys(this.routes)) delete this.routes[key];
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
//#endregion
|
|
150
|
+
export { ResourceRoutes };
|
package/dist/Route.cjs
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
|
|
2
2
|
//#region src/Route.ts
|
|
3
|
+
/**
|
|
4
|
+
* @class clear-router Route
|
|
5
|
+
* @description A route describes a single enpoint on clear-router
|
|
6
|
+
* @author 3m1n3nc3
|
|
7
|
+
* @repository https://github.com/arkstack-tmp/clear-router
|
|
8
|
+
*/
|
|
3
9
|
var Route = class {
|
|
4
10
|
ctx;
|
|
5
11
|
body = {};
|
|
@@ -29,14 +35,40 @@ var Route = class {
|
|
|
29
35
|
this.controllerName = Array.isArray(handler) ? handler[0]?.name : void 0;
|
|
30
36
|
this.actionName = Array.isArray(handler) ? handler[1] : typeof handler === "function" ? handler.constructor.name ?? handler.name : void 0;
|
|
31
37
|
this.onName = options.onName;
|
|
38
|
+
this.normalizeMiddleware = options.normalizeMiddleware;
|
|
32
39
|
}
|
|
33
40
|
onName;
|
|
41
|
+
normalizeMiddleware;
|
|
42
|
+
/**
|
|
43
|
+
* Set the route name
|
|
44
|
+
*
|
|
45
|
+
* @param name
|
|
46
|
+
* @returns
|
|
47
|
+
*/
|
|
34
48
|
name(name) {
|
|
35
49
|
const previousName = this.routeName;
|
|
36
50
|
this.routeName = name;
|
|
37
51
|
this.onName?.(name, this, previousName);
|
|
38
52
|
return this;
|
|
39
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Register one or more middleware that will be executed before the route.
|
|
56
|
+
*
|
|
57
|
+
* @param middlewares
|
|
58
|
+
* @returns
|
|
59
|
+
*/
|
|
60
|
+
middleware(middlewares) {
|
|
61
|
+
const normalized = (Array.isArray(middlewares) ? middlewares : [middlewares]).map((middleware) => this.normalizeMiddleware?.(middleware) ?? middleware);
|
|
62
|
+
this.middlewares.push(...normalized);
|
|
63
|
+
this.middlewareCount = this.middlewares.length;
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Get the path generated and accessible by this route
|
|
68
|
+
*
|
|
69
|
+
* @param params
|
|
70
|
+
* @returns
|
|
71
|
+
*/
|
|
40
72
|
toPath(params = {}) {
|
|
41
73
|
return this.path.replace(/\/?\{([^{}]+)\}/g, (segment, raw) => {
|
|
42
74
|
const optional = raw.endsWith("?");
|