clear-router 2.8.0 → 2.8.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/dist/ResourceRoutes.cjs +97 -7
- package/dist/ResourceRoutes.d.cts +65 -6
- package/dist/ResourceRoutes.d.mts +65 -6
- package/dist/ResourceRoutes.mjs +97 -7
- package/dist/core/helpers.cjs +32 -0
- package/dist/core/helpers.d.cts +18 -0
- package/dist/core/helpers.d.mts +17 -0
- package/dist/core/helpers.mjs +31 -0
- package/dist/core/index.cjs +1 -6
- 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 +43 -38
- package/dist/core/router.d.cts +7 -4
- package/dist/core/router.d.mts +7 -4
- package/dist/core/router.mjs +43 -38
- package/dist/express/router.cjs +2 -2
- package/dist/express/router.d.cts +4 -4
- package/dist/express/router.d.mts +4 -4
- package/dist/express/router.mjs +2 -2
- package/dist/fastify/router.cjs +2 -2
- package/dist/fastify/router.d.cts +4 -4
- package/dist/fastify/router.d.mts +4 -4
- package/dist/fastify/router.mjs +2 -2
- package/dist/h3/router.cjs +2 -2
- package/dist/h3/router.d.cts +4 -4
- package/dist/h3/router.d.mts +4 -4
- package/dist/h3/router.mjs +2 -2
- package/dist/hono/router.cjs +2 -2
- package/dist/hono/router.d.cts +4 -4
- package/dist/hono/router.d.mts +4 -4
- package/dist/hono/router.mjs +2 -2
- package/dist/index.cjs +4 -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 +4 -4
- package/dist/koa/router.d.mts +4 -4
- package/dist/koa/router.mjs +2 -2
- package/dist/types/basic.d.cts +15 -5
- package/dist/types/basic.d.mts +15 -5
- package/dist/types/index.d.mts +1 -0
- package/package.json +5 -2
package/dist/ResourceRoutes.cjs
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
const require_ResourceRouteSelection = require('./ResourceRouteSelection.cjs');
|
|
2
|
+
const require_helpers = require('./core/helpers.cjs');
|
|
3
|
+
require('./core/index.cjs');
|
|
2
4
|
|
|
3
5
|
//#region src/ResourceRoutes.ts
|
|
4
6
|
/**
|
|
@@ -7,17 +9,78 @@ const require_ResourceRouteSelection = require('./ResourceRouteSelection.cjs');
|
|
|
7
9
|
* @author 3m1n3nc3
|
|
8
10
|
* @repository https://github.com/arkstack-tmp/clear-router
|
|
9
11
|
*/
|
|
10
|
-
var ResourceRoutes = class {
|
|
11
|
-
|
|
12
|
-
|
|
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();
|
|
13
78
|
}
|
|
14
79
|
/**
|
|
15
80
|
* Register one or more middleware that will be executed before the route.
|
|
16
|
-
*
|
|
17
|
-
* @param middlewares
|
|
18
|
-
* @returns
|
|
19
81
|
*/
|
|
20
|
-
middleware(middlewares) {
|
|
82
|
+
middleware(middlewares, remember = true) {
|
|
83
|
+
if (remember) this.chainedMiddlewares.push(middlewares);
|
|
21
84
|
for (const route of Object.values(this.routes)) route?.middleware(middlewares);
|
|
22
85
|
return this;
|
|
23
86
|
}
|
|
@@ -51,9 +114,36 @@ var ResourceRoutes = class {
|
|
|
51
114
|
delete() {
|
|
52
115
|
return this.byMethod("delete");
|
|
53
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
|
+
}
|
|
54
140
|
byMethod(method) {
|
|
55
141
|
return new require_ResourceRouteSelection.ResourceRouteSelection(Object.values(this.routes).filter((route) => Boolean(route?.methods.includes(method))));
|
|
56
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
|
+
}
|
|
57
147
|
};
|
|
58
148
|
|
|
59
149
|
//#endregion
|
|
@@ -1,10 +1,28 @@
|
|
|
1
|
-
import { HttpMethod, ResourceAction } from "./types/basic.cjs";
|
|
1
|
+
import { ApiResourceMiddleware, HttpMethod, ResourceAction } from "./types/basic.cjs";
|
|
2
2
|
import { Middleware } from "./types/express.cjs";
|
|
3
3
|
import { Middleware as Middleware$1 } from "./types/h3.cjs";
|
|
4
4
|
import { Route } from "./Route.cjs";
|
|
5
5
|
import { ResourceRouteSelection } from "./ResourceRouteSelection.cjs";
|
|
6
6
|
|
|
7
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
|
+
};
|
|
8
26
|
/**
|
|
9
27
|
* @class clear-router ResourceRoutes
|
|
10
28
|
* @description A ResourceRoutes creates a collection of resourceful routes in a single call
|
|
@@ -12,15 +30,50 @@ import { ResourceRouteSelection } from "./ResourceRouteSelection.cjs";
|
|
|
12
30
|
* @repository https://github.com/arkstack-tmp/clear-router
|
|
13
31
|
*/
|
|
14
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
|
+
};
|
|
15
60
|
readonly routes: Partial<Record<ResourceAction, Route<X, M, H>>>;
|
|
16
|
-
|
|
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;
|
|
17
73
|
/**
|
|
18
74
|
* Register one or more middleware that will be executed before the route.
|
|
19
|
-
*
|
|
20
|
-
* @param middlewares
|
|
21
|
-
* @returns
|
|
22
75
|
*/
|
|
23
|
-
middleware(middlewares: M[] | M): this;
|
|
76
|
+
middleware(middlewares: M[] | M, remember?: boolean): this;
|
|
24
77
|
action(action: ResourceAction): Route<X, M, H> | undefined;
|
|
25
78
|
index(): Route<X, M, H> | undefined;
|
|
26
79
|
show(): Route<X, M, H> | undefined;
|
|
@@ -31,7 +84,13 @@ declare class ResourceRoutes<X = any, M = Middleware$1 | Middleware, H = any> {
|
|
|
31
84
|
post(): ResourceRouteSelection<X, M, H>;
|
|
32
85
|
put(): ResourceRouteSelection<X, M, H>;
|
|
33
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;
|
|
34
92
|
protected byMethod(method: HttpMethod): ResourceRouteSelection<X, M, H>;
|
|
93
|
+
protected clear(): void;
|
|
35
94
|
}
|
|
36
95
|
//#endregion
|
|
37
96
|
export { ResourceRoutes };
|
|
@@ -1,10 +1,28 @@
|
|
|
1
|
-
import { HttpMethod, ResourceAction } from "./types/basic.mjs";
|
|
1
|
+
import { ApiResourceMiddleware, HttpMethod, ResourceAction } from "./types/basic.mjs";
|
|
2
2
|
import { Middleware } from "./types/express.mjs";
|
|
3
3
|
import { Middleware as Middleware$1 } from "./types/h3.mjs";
|
|
4
4
|
import { Route } from "./Route.mjs";
|
|
5
5
|
import { ResourceRouteSelection } from "./ResourceRouteSelection.mjs";
|
|
6
6
|
|
|
7
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
|
+
};
|
|
8
26
|
/**
|
|
9
27
|
* @class clear-router ResourceRoutes
|
|
10
28
|
* @description A ResourceRoutes creates a collection of resourceful routes in a single call
|
|
@@ -12,15 +30,50 @@ import { ResourceRouteSelection } from "./ResourceRouteSelection.mjs";
|
|
|
12
30
|
* @repository https://github.com/arkstack-tmp/clear-router
|
|
13
31
|
*/
|
|
14
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
|
+
};
|
|
15
60
|
readonly routes: Partial<Record<ResourceAction, Route<X, M, H>>>;
|
|
16
|
-
|
|
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;
|
|
17
73
|
/**
|
|
18
74
|
* Register one or more middleware that will be executed before the route.
|
|
19
|
-
*
|
|
20
|
-
* @param middlewares
|
|
21
|
-
* @returns
|
|
22
75
|
*/
|
|
23
|
-
middleware(middlewares: M[] | M): this;
|
|
76
|
+
middleware(middlewares: M[] | M, remember?: boolean): this;
|
|
24
77
|
action(action: ResourceAction): Route<X, M, H> | undefined;
|
|
25
78
|
index(): Route<X, M, H> | undefined;
|
|
26
79
|
show(): Route<X, M, H> | undefined;
|
|
@@ -31,7 +84,13 @@ declare class ResourceRoutes<X = any, M = Middleware$1 | Middleware, H = any> {
|
|
|
31
84
|
post(): ResourceRouteSelection<X, M, H>;
|
|
32
85
|
put(): ResourceRouteSelection<X, M, H>;
|
|
33
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;
|
|
34
92
|
protected byMethod(method: HttpMethod): ResourceRouteSelection<X, M, H>;
|
|
93
|
+
protected clear(): void;
|
|
35
94
|
}
|
|
36
95
|
//#endregion
|
|
37
96
|
export { ResourceRoutes };
|
package/dist/ResourceRoutes.mjs
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { ResourceRouteSelection } from "./ResourceRouteSelection.mjs";
|
|
2
|
+
import { wrap } from "./core/helpers.mjs";
|
|
3
|
+
import "./core/index.mjs";
|
|
2
4
|
|
|
3
5
|
//#region src/ResourceRoutes.ts
|
|
4
6
|
/**
|
|
@@ -7,17 +9,78 @@ import { ResourceRouteSelection } from "./ResourceRouteSelection.mjs";
|
|
|
7
9
|
* @author 3m1n3nc3
|
|
8
10
|
* @repository https://github.com/arkstack-tmp/clear-router
|
|
9
11
|
*/
|
|
10
|
-
var ResourceRoutes = class {
|
|
11
|
-
|
|
12
|
-
|
|
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();
|
|
13
78
|
}
|
|
14
79
|
/**
|
|
15
80
|
* Register one or more middleware that will be executed before the route.
|
|
16
|
-
*
|
|
17
|
-
* @param middlewares
|
|
18
|
-
* @returns
|
|
19
81
|
*/
|
|
20
|
-
middleware(middlewares) {
|
|
82
|
+
middleware(middlewares, remember = true) {
|
|
83
|
+
if (remember) this.chainedMiddlewares.push(middlewares);
|
|
21
84
|
for (const route of Object.values(this.routes)) route?.middleware(middlewares);
|
|
22
85
|
return this;
|
|
23
86
|
}
|
|
@@ -51,9 +114,36 @@ var ResourceRoutes = class {
|
|
|
51
114
|
delete() {
|
|
52
115
|
return this.byMethod("delete");
|
|
53
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
|
+
}
|
|
54
140
|
byMethod(method) {
|
|
55
141
|
return new ResourceRouteSelection(Object.values(this.routes).filter((route) => Boolean(route?.methods.includes(method))));
|
|
56
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
|
+
}
|
|
57
147
|
};
|
|
58
148
|
|
|
59
149
|
//#endregion
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
let node_path = require("node:path");
|
|
2
|
+
let jiti = require("jiti");
|
|
3
|
+
let node_url = require("node:url");
|
|
4
|
+
|
|
5
|
+
//#region src/core/helpers.ts
|
|
6
|
+
const wrap = (value) => {
|
|
7
|
+
if (value === null || value === void 0) return [];
|
|
8
|
+
return Array.isArray(value) ? value : [value];
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
*
|
|
12
|
+
* Dynamically imports a file at the given path with full TypeScript support,
|
|
13
|
+
* including `tsconfig.json` path aliases.
|
|
14
|
+
*
|
|
15
|
+
* @param filePath - The path to the file to import.
|
|
16
|
+
* @returns The imported module typed as `T`.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* const config = await importFile<AppConfig>('./config/app.ts')
|
|
20
|
+
*/
|
|
21
|
+
const importFile = async (filePath, userOptions, resolveOptions) => {
|
|
22
|
+
const resolvedPath = (0, node_path.resolve)(filePath);
|
|
23
|
+
return await (0, jiti.createJiti)((0, node_url.pathToFileURL)(resolvedPath).href, {
|
|
24
|
+
...userOptions,
|
|
25
|
+
interopDefault: false,
|
|
26
|
+
tsconfigPaths: true
|
|
27
|
+
}).import(resolvedPath, resolveOptions);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
//#endregion
|
|
31
|
+
exports.importFile = importFile;
|
|
32
|
+
exports.wrap = wrap;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { FileImporter } from "../types/basic.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/core/helpers.d.ts
|
|
4
|
+
declare const wrap: <T>(value: T | T[] | null | undefined) => T[];
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* Dynamically imports a file at the given path with full TypeScript support,
|
|
8
|
+
* including `tsconfig.json` path aliases.
|
|
9
|
+
*
|
|
10
|
+
* @param filePath - The path to the file to import.
|
|
11
|
+
* @returns The imported module typed as `T`.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const config = await importFile<AppConfig>('./config/app.ts')
|
|
15
|
+
*/
|
|
16
|
+
declare const importFile: FileImporter;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { importFile, wrap };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { FileImporter } from "../types/basic.mjs";
|
|
2
|
+
//#region src/core/helpers.d.ts
|
|
3
|
+
declare const wrap: <T>(value: T | T[] | null | undefined) => T[];
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* Dynamically imports a file at the given path with full TypeScript support,
|
|
7
|
+
* including `tsconfig.json` path aliases.
|
|
8
|
+
*
|
|
9
|
+
* @param filePath - The path to the file to import.
|
|
10
|
+
* @returns The imported module typed as `T`.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const config = await importFile<AppConfig>('./config/app.ts')
|
|
14
|
+
*/
|
|
15
|
+
declare const importFile: FileImporter;
|
|
16
|
+
//#endregion
|
|
17
|
+
export { importFile, wrap };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import { createJiti } from "jiti";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
|
|
5
|
+
//#region src/core/helpers.ts
|
|
6
|
+
const wrap = (value) => {
|
|
7
|
+
if (value === null || value === void 0) return [];
|
|
8
|
+
return Array.isArray(value) ? value : [value];
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
*
|
|
12
|
+
* Dynamically imports a file at the given path with full TypeScript support,
|
|
13
|
+
* including `tsconfig.json` path aliases.
|
|
14
|
+
*
|
|
15
|
+
* @param filePath - The path to the file to import.
|
|
16
|
+
* @returns The imported module typed as `T`.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* const config = await importFile<AppConfig>('./config/app.ts')
|
|
20
|
+
*/
|
|
21
|
+
const importFile = async (filePath, userOptions, resolveOptions) => {
|
|
22
|
+
const resolvedPath = resolve(filePath);
|
|
23
|
+
return await createJiti(pathToFileURL(resolvedPath).href, {
|
|
24
|
+
...userOptions,
|
|
25
|
+
interopDefault: false,
|
|
26
|
+
tsconfigPaths: true
|
|
27
|
+
}).import(resolvedPath, resolveOptions);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
//#endregion
|
|
31
|
+
export { importFile, wrap };
|
package/dist/core/index.cjs
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
1
|
const require_Request = require('./Request.cjs');
|
|
3
2
|
const require_Response = require('./Response.cjs');
|
|
4
3
|
const require_plugins = require('./plugins.cjs');
|
|
4
|
+
const require_helpers = require('./helpers.cjs');
|
|
5
5
|
const require_router = require('./router.cjs');
|
|
6
|
-
|
|
7
|
-
exports.CoreRouter = require_router.CoreRouter;
|
|
8
|
-
exports.Request = require_Request.Request;
|
|
9
|
-
exports.Response = require_Response.Response;
|
|
10
|
-
exports.definePlugin = require_plugins.definePlugin;
|
package/dist/core/index.d.cts
CHANGED
|
@@ -2,4 +2,5 @@ import { Response } from "./Response.cjs";
|
|
|
2
2
|
import { Request } from "./Request.cjs";
|
|
3
3
|
import { ClearRouterPlugin, ClearRouterPluginArgumentsContext, ClearRouterPluginContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, PluginArgumentsResolver, PluginBind, PluginBindFactory, PluginBindValue, PluginSetupResult, definePlugin } from "./plugins.cjs";
|
|
4
4
|
import { CoreRouter } from "./router.cjs";
|
|
5
|
-
|
|
5
|
+
import { importFile, wrap } from "./helpers.cjs";
|
|
6
|
+
export { ClearRouterPlugin, ClearRouterPluginArgumentsContext, ClearRouterPluginContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, CoreRouter, PluginArgumentsResolver, PluginBind, PluginBindFactory, PluginBindValue, PluginSetupResult, Request, Response, definePlugin, importFile, wrap };
|
package/dist/core/index.d.mts
CHANGED
|
@@ -2,4 +2,5 @@ import { Response } from "./Response.mjs";
|
|
|
2
2
|
import { Request } from "./Request.mjs";
|
|
3
3
|
import { ClearRouterPlugin, ClearRouterPluginArgumentsContext, ClearRouterPluginContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, PluginArgumentsResolver, PluginBind, PluginBindFactory, PluginBindValue, PluginSetupResult, definePlugin } from "./plugins.mjs";
|
|
4
4
|
import { CoreRouter } from "./router.mjs";
|
|
5
|
-
|
|
5
|
+
import { importFile, wrap } from "./helpers.mjs";
|
|
6
|
+
export { ClearRouterPlugin, ClearRouterPluginArgumentsContext, ClearRouterPluginContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, CoreRouter, PluginArgumentsResolver, PluginBind, PluginBindFactory, PluginBindValue, PluginSetupResult, Request, Response, definePlugin, importFile, wrap };
|
package/dist/core/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Request } from "./Request.mjs";
|
|
2
2
|
import { Response } from "./Response.mjs";
|
|
3
3
|
import { definePlugin } from "./plugins.mjs";
|
|
4
|
+
import { importFile, wrap } from "./helpers.mjs";
|
|
4
5
|
import { CoreRouter } from "./router.mjs";
|
|
5
6
|
|
|
6
|
-
export { CoreRouter, Request, Response, definePlugin };
|
|
7
|
+
export { CoreRouter, Request, Response, definePlugin, importFile, wrap };
|