clear-router 2.2.0 → 2.3.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/README.md +15 -5
- package/dist/core/index.cjs +1 -1
- package/dist/core/index.d.cts +1 -1
- package/dist/core/index.d.mts +1 -1
- package/dist/core/index.mjs +1 -1
- package/dist/express/index.cjs +1 -1
- package/dist/express/index.d.cts +1 -1
- package/dist/express/index.d.mts +1 -1
- package/dist/express/index.mjs +1 -1
- package/dist/fastify/index.cjs +213 -0
- package/dist/fastify/index.d.cts +137 -0
- package/dist/fastify/index.d.mts +137 -0
- package/dist/fastify/index.mjs +212 -0
- package/dist/h3/index.cjs +1 -1
- package/dist/h3/index.d.cts +1 -1
- package/dist/h3/index.d.mts +1 -1
- package/dist/h3/index.mjs +1 -1
- package/dist/hono/index.cjs +227 -0
- package/dist/hono/index.d.cts +141 -0
- package/dist/hono/index.d.mts +141 -0
- package/dist/hono/index.mjs +226 -0
- package/dist/index.cjs +26 -11
- package/dist/index.d.cts +14 -5
- package/dist/index.d.mts +14 -5
- package/dist/index.mjs +26 -11
- package/dist/{router-BNVIrTi3.cjs → router-Ba2MVNn-.cjs} +26 -11
- package/dist/{router-BiCuy5TZ.mjs → router-Bug2IE_u.mjs} +26 -11
- package/dist/{router-C1jVRytA.d.mts → router-DLmimm_U.d.cts} +82 -73
- package/dist/{router-CZIh1ZPJ.d.cts → router-cWYmcfTX.d.mts} +83 -74
- package/dist/types/ClearRequest.d.mts +1 -1
- package/dist/types/Route.d.mts +1 -1
- package/dist/types/basic.d.mts +1 -4
- package/dist/types/express.d.mts +1 -1
- package/dist/types/fastify.d.mts +19 -0
- package/dist/types/fastify.mjs +1 -0
- package/dist/types/h3.d.mts +1 -1
- package/dist/types/hono.d.mts +18 -0
- package/dist/types/hono.mjs +1 -0
- package/package.json +23 -3
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
[](https://github.com/arkstack-hq/clear-router/actions/workflows/npm-publish.yml)
|
|
7
7
|
[](https://github.com/arkstack-hq/clear-router/actions/workflows/ci.yml)
|
|
8
8
|
|
|
9
|
-
Laravel-style routing
|
|
9
|
+
Laravel-style routing for Node.js with support for Express, H3, Fastify, and Hono, including CommonJS, ESM, and TypeScript support.
|
|
10
10
|
|
|
11
11
|
## Installation
|
|
12
12
|
|
|
@@ -51,20 +51,30 @@ yarn add clear-router express
|
|
|
51
51
|
- `clearRequest` is passed as second handler argument for controller handlers
|
|
52
52
|
- Auto-binds controller methods
|
|
53
53
|
- Full CommonJS, ESM, and TypeScript support
|
|
54
|
-
- Error handling delegated to Express | H3
|
|
54
|
+
- Error handling delegated to Express | H3 | Fastify | Hono
|
|
55
55
|
- Route inspection with the `allRoutes` method
|
|
56
56
|
- Fully Express-compatible
|
|
57
57
|
- Fully H3-compatible
|
|
58
|
+
- Fully Fastify-compatible
|
|
59
|
+
- Fully Hono-compatible
|
|
58
60
|
|
|
59
61
|
## Quick Start
|
|
60
62
|
|
|
61
63
|
### Express JS
|
|
62
64
|
|
|
63
|
-
See the [Express JS documentation](https://arkstack-hq.github.io/clear-router/express) for details.
|
|
65
|
+
See the [Express JS documentation](https://arkstack-hq.github.io/clear-router/guide/express) for details.
|
|
64
66
|
|
|
65
67
|
### H3
|
|
66
68
|
|
|
67
|
-
See the [H3 documentation](https://arkstack-hq.github.io/clear-router/h3) for details.
|
|
69
|
+
See the [H3 documentation](https://arkstack-hq.github.io/clear-router/guide/h3) for details.
|
|
70
|
+
|
|
71
|
+
### Fastify
|
|
72
|
+
|
|
73
|
+
See the [Fastify documentation](https://arkstack-hq.github.io/clear-router/guide/fastify) for details.
|
|
74
|
+
|
|
75
|
+
### Hono
|
|
76
|
+
|
|
77
|
+
See the [Hono documentation](https://arkstack-hq.github.io/clear-router/guide/hono) for details.
|
|
68
78
|
|
|
69
79
|
## API Reference
|
|
70
80
|
|
|
@@ -92,7 +102,7 @@ npm run test:esm # Test ESM
|
|
|
92
102
|
npm run test:ts # Test TypeScript
|
|
93
103
|
```
|
|
94
104
|
|
|
95
|
-
See [TESTING.md](https://arkstack-hq.github.io/clear-router/
|
|
105
|
+
See [TESTING.md](https://arkstack-hq.github.io/clear-router/testing) for a detailed testing guide.
|
|
96
106
|
|
|
97
107
|
## Examples
|
|
98
108
|
|
package/dist/core/index.cjs
CHANGED
package/dist/core/index.d.cts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as CoreRouter } from "../router-
|
|
1
|
+
import { t as CoreRouter } from "../router-DLmimm_U.cjs";
|
|
2
2
|
export { CoreRouter };
|
package/dist/core/index.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as CoreRouter } from "../router-
|
|
1
|
+
import { t as CoreRouter } from "../router-cWYmcfTX.mjs";
|
|
2
2
|
export { CoreRouter };
|
package/dist/core/index.mjs
CHANGED
package/dist/express/index.cjs
CHANGED
package/dist/express/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { c as Handler, d as ApiResourceMiddleware, f as ControllerAction, l as HttpContext, m as HttpMethod, r as Route, t as CoreRouter, u as Middleware } from "../router-DLmimm_U.cjs";
|
|
2
2
|
import { Router as Router$1 } from "express";
|
|
3
3
|
|
|
4
4
|
//#region src/express/router.d.ts
|
package/dist/express/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { c as Handler, d as ApiResourceMiddleware, f as ControllerAction, l as HttpContext, m as HttpMethod, r as Route, t as CoreRouter, u as Middleware } from "../router-cWYmcfTX.mjs";
|
|
2
2
|
import { Router as Router$1 } from "express";
|
|
3
3
|
|
|
4
4
|
//#region src/express/router.d.ts
|
package/dist/express/index.mjs
CHANGED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_router = require('../router-Ba2MVNn-.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/fastify/router.ts
|
|
5
|
+
/**
|
|
6
|
+
* @class clear-router Fastify Router
|
|
7
|
+
* @description Laravel-style routing system for Fastify using shared clear-router core
|
|
8
|
+
* @author 3m1n3nc3
|
|
9
|
+
* @repository https://github.com/toneflix/clear-router
|
|
10
|
+
*/
|
|
11
|
+
var Router = class Router extends require_router.CoreRouter {
|
|
12
|
+
static ensureRequestBodyAccessor(req) {
|
|
13
|
+
if (typeof req.getBody !== "function") req.getBody = () => req.body ?? {};
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Add a route to the router
|
|
17
|
+
*
|
|
18
|
+
* @param methods HTTP methods for the route
|
|
19
|
+
* @param path Route path
|
|
20
|
+
* @param handler Route handler function
|
|
21
|
+
* @param middlewares Optional middlewares for the route
|
|
22
|
+
*/
|
|
23
|
+
static add(methods, path, handler, middlewares) {
|
|
24
|
+
super.add(methods, path, handler, middlewares);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Define a resourceful API controller with standard CRUD routes
|
|
28
|
+
*
|
|
29
|
+
* @param basePath Base path for the resource
|
|
30
|
+
* @param controller Controller class or instance
|
|
31
|
+
* @param options Optional configuration for the resource
|
|
32
|
+
*/
|
|
33
|
+
static apiResource(basePath, controller, options) {
|
|
34
|
+
super.apiResource(basePath, controller, options);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Define a GET route
|
|
38
|
+
*
|
|
39
|
+
* @param path
|
|
40
|
+
* @param handler
|
|
41
|
+
* @param middlewares
|
|
42
|
+
*/
|
|
43
|
+
static get(path, handler, middlewares) {
|
|
44
|
+
super.get(path, handler, middlewares);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Define a POST route
|
|
48
|
+
*
|
|
49
|
+
* @param path
|
|
50
|
+
* @param handler
|
|
51
|
+
* @param middlewares
|
|
52
|
+
*/
|
|
53
|
+
static post(path, handler, middlewares) {
|
|
54
|
+
super.post(path, handler, middlewares);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Define a PUT route
|
|
58
|
+
*
|
|
59
|
+
* @param path
|
|
60
|
+
* @param handler
|
|
61
|
+
* @param middlewares
|
|
62
|
+
*/
|
|
63
|
+
static put(path, handler, middlewares) {
|
|
64
|
+
super.put(path, handler, middlewares);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Define a DELETE route
|
|
68
|
+
*
|
|
69
|
+
* @param path
|
|
70
|
+
* @param handler
|
|
71
|
+
* @param middlewares
|
|
72
|
+
*/
|
|
73
|
+
static delete(path, handler, middlewares) {
|
|
74
|
+
super.delete(path, handler, middlewares);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Define a PATCH route
|
|
78
|
+
*
|
|
79
|
+
* @param path
|
|
80
|
+
* @param handler
|
|
81
|
+
* @param middlewares
|
|
82
|
+
*/
|
|
83
|
+
static patch(path, handler, middlewares) {
|
|
84
|
+
super.patch(path, handler, middlewares);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Define an OPTIONS route
|
|
88
|
+
*
|
|
89
|
+
* @param path
|
|
90
|
+
* @param handler
|
|
91
|
+
* @param middlewares
|
|
92
|
+
*/
|
|
93
|
+
static options(path, handler, middlewares) {
|
|
94
|
+
super.options(path, handler, middlewares);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Define a HEAD route
|
|
98
|
+
*
|
|
99
|
+
* @param path
|
|
100
|
+
* @param handler
|
|
101
|
+
* @param middlewares
|
|
102
|
+
*/
|
|
103
|
+
static head(path, handler, middlewares) {
|
|
104
|
+
super.head(path, handler, middlewares);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Define a group of routes with a common prefix and optional middlewares
|
|
108
|
+
*
|
|
109
|
+
* @param prefix
|
|
110
|
+
* @param callback
|
|
111
|
+
* @param middlewares
|
|
112
|
+
*/
|
|
113
|
+
static async group(prefix, callback, middlewares) {
|
|
114
|
+
await super.group(prefix, callback, middlewares);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Apply middlewares to a group of routes defined within the callback
|
|
118
|
+
*
|
|
119
|
+
* @param middlewares
|
|
120
|
+
* @param callback
|
|
121
|
+
*/
|
|
122
|
+
static middleware(middlewares, callback) {
|
|
123
|
+
super.middleware(middlewares, callback);
|
|
124
|
+
}
|
|
125
|
+
static allRoutes(type) {
|
|
126
|
+
return super.allRoutes(type);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Apply the defined routes to a Fastify application instance
|
|
130
|
+
*
|
|
131
|
+
* @param app - The Fastify application instance
|
|
132
|
+
* @returns The Fastify application instance with the applied routes
|
|
133
|
+
*/
|
|
134
|
+
static apply(app) {
|
|
135
|
+
for (const route of this.routes) {
|
|
136
|
+
let handlerFunction = null;
|
|
137
|
+
let instance = null;
|
|
138
|
+
try {
|
|
139
|
+
const resolved = this.resolveHandler(route);
|
|
140
|
+
handlerFunction = resolved.handlerFunction;
|
|
141
|
+
instance = resolved.instance;
|
|
142
|
+
} catch (error) {
|
|
143
|
+
console.error(`[ROUTES] Error setting up route ${route.path}:`, error.message);
|
|
144
|
+
throw error;
|
|
145
|
+
}
|
|
146
|
+
if (!handlerFunction) continue;
|
|
147
|
+
for (const method of route.methods) {
|
|
148
|
+
const allowedMethods = [
|
|
149
|
+
"get",
|
|
150
|
+
"post",
|
|
151
|
+
"put",
|
|
152
|
+
"delete",
|
|
153
|
+
"patch",
|
|
154
|
+
"options",
|
|
155
|
+
"head"
|
|
156
|
+
];
|
|
157
|
+
if (method === "options" && route.methods.length > 1) continue;
|
|
158
|
+
if (!allowedMethods.includes(method)) throw new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
|
|
159
|
+
app.route({
|
|
160
|
+
method: method.toUpperCase(),
|
|
161
|
+
url: route.path,
|
|
162
|
+
preHandler: route.middlewares,
|
|
163
|
+
handler: async (req, reply) => {
|
|
164
|
+
Router.ensureRequestBodyAccessor(req);
|
|
165
|
+
const override = Router.resolveMethodOverride(req.method, req.headers, req.body);
|
|
166
|
+
if (method === "post" && override && override !== "post") return reply.code(404).send();
|
|
167
|
+
const ctx = {
|
|
168
|
+
req,
|
|
169
|
+
reply
|
|
170
|
+
};
|
|
171
|
+
const inst = instance ?? route;
|
|
172
|
+
Router.bindRequestToInstance(ctx, inst, route, {
|
|
173
|
+
body: ctx.req.getBody(),
|
|
174
|
+
query: ctx.req.query ?? {},
|
|
175
|
+
params: ctx.req.params ?? {}
|
|
176
|
+
});
|
|
177
|
+
const result = handlerFunction(ctx, inst.clearRequest);
|
|
178
|
+
return await Promise.resolve(result);
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
if ([
|
|
182
|
+
"put",
|
|
183
|
+
"patch",
|
|
184
|
+
"delete"
|
|
185
|
+
].includes(method)) app.route({
|
|
186
|
+
method: "POST",
|
|
187
|
+
url: route.path,
|
|
188
|
+
preHandler: route.middlewares,
|
|
189
|
+
handler: async (req, reply) => {
|
|
190
|
+
Router.ensureRequestBodyAccessor(req);
|
|
191
|
+
if (Router.resolveMethodOverride(req.method, req.headers, req.body) !== method) return reply.code(404).send();
|
|
192
|
+
const ctx = {
|
|
193
|
+
req,
|
|
194
|
+
reply
|
|
195
|
+
};
|
|
196
|
+
const inst = instance ?? route;
|
|
197
|
+
Router.bindRequestToInstance(ctx, inst, route, {
|
|
198
|
+
body: ctx.req.getBody(),
|
|
199
|
+
query: ctx.req.query ?? {},
|
|
200
|
+
params: ctx.req.params ?? {}
|
|
201
|
+
});
|
|
202
|
+
const result = handlerFunction(ctx, inst.clearRequest);
|
|
203
|
+
return await Promise.resolve(result);
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return app;
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
//#endregion
|
|
213
|
+
exports.Router = Router;
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { d as ApiResourceMiddleware, f as ControllerAction, m as HttpMethod, n as ClearRequest, p as ControllerHandler, r as Route, t as CoreRouter } from "../router-DLmimm_U.cjs";
|
|
2
|
+
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
|
3
|
+
|
|
4
|
+
//#region types/fastify.d.ts
|
|
5
|
+
interface RequestWithGetBody extends FastifyRequest {
|
|
6
|
+
getBody: () => Record<string, any>;
|
|
7
|
+
}
|
|
8
|
+
interface HttpContext {
|
|
9
|
+
req: RequestWithGetBody;
|
|
10
|
+
reply: FastifyReply;
|
|
11
|
+
}
|
|
12
|
+
type RouteHandler = (ctx: HttpContext, req: ClearRequest) => any | Promise<any>;
|
|
13
|
+
type Handler = RouteHandler | ControllerHandler;
|
|
14
|
+
type NextFunction = (err?: Error) => void;
|
|
15
|
+
type Middleware = (req: RequestWithGetBody, reply: FastifyReply, next: NextFunction) => any | Promise<any>;
|
|
16
|
+
type FastifyApp = FastifyInstance;
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/fastify/router.d.ts
|
|
19
|
+
/**
|
|
20
|
+
* @class clear-router Fastify Router
|
|
21
|
+
* @description Laravel-style routing system for Fastify using shared clear-router core
|
|
22
|
+
* @author 3m1n3nc3
|
|
23
|
+
* @repository https://github.com/toneflix/clear-router
|
|
24
|
+
*/
|
|
25
|
+
declare class Router extends CoreRouter {
|
|
26
|
+
private static ensureRequestBodyAccessor;
|
|
27
|
+
/**
|
|
28
|
+
* Add a route to the router
|
|
29
|
+
*
|
|
30
|
+
* @param methods HTTP methods for the route
|
|
31
|
+
* @param path Route path
|
|
32
|
+
* @param handler Route handler function
|
|
33
|
+
* @param middlewares Optional middlewares for the route
|
|
34
|
+
*/
|
|
35
|
+
static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
36
|
+
/**
|
|
37
|
+
* Define a resourceful API controller with standard CRUD routes
|
|
38
|
+
*
|
|
39
|
+
* @param basePath Base path for the resource
|
|
40
|
+
* @param controller Controller class or instance
|
|
41
|
+
* @param options Optional configuration for the resource
|
|
42
|
+
*/
|
|
43
|
+
static apiResource(basePath: string, controller: any, options?: {
|
|
44
|
+
only?: ControllerAction[];
|
|
45
|
+
except?: ControllerAction[];
|
|
46
|
+
middlewares?: ApiResourceMiddleware<Middleware>;
|
|
47
|
+
}): void;
|
|
48
|
+
/**
|
|
49
|
+
* Define a GET route
|
|
50
|
+
*
|
|
51
|
+
* @param path
|
|
52
|
+
* @param handler
|
|
53
|
+
* @param middlewares
|
|
54
|
+
*/
|
|
55
|
+
static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
56
|
+
/**
|
|
57
|
+
* Define a POST route
|
|
58
|
+
*
|
|
59
|
+
* @param path
|
|
60
|
+
* @param handler
|
|
61
|
+
* @param middlewares
|
|
62
|
+
*/
|
|
63
|
+
static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
64
|
+
/**
|
|
65
|
+
* Define a PUT route
|
|
66
|
+
*
|
|
67
|
+
* @param path
|
|
68
|
+
* @param handler
|
|
69
|
+
* @param middlewares
|
|
70
|
+
*/
|
|
71
|
+
static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
72
|
+
/**
|
|
73
|
+
* Define a DELETE route
|
|
74
|
+
*
|
|
75
|
+
* @param path
|
|
76
|
+
* @param handler
|
|
77
|
+
* @param middlewares
|
|
78
|
+
*/
|
|
79
|
+
static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
80
|
+
/**
|
|
81
|
+
* Define a PATCH route
|
|
82
|
+
*
|
|
83
|
+
* @param path
|
|
84
|
+
* @param handler
|
|
85
|
+
* @param middlewares
|
|
86
|
+
*/
|
|
87
|
+
static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
88
|
+
/**
|
|
89
|
+
* Define an OPTIONS route
|
|
90
|
+
*
|
|
91
|
+
* @param path
|
|
92
|
+
* @param handler
|
|
93
|
+
* @param middlewares
|
|
94
|
+
*/
|
|
95
|
+
static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
96
|
+
/**
|
|
97
|
+
* Define a HEAD route
|
|
98
|
+
*
|
|
99
|
+
* @param path
|
|
100
|
+
* @param handler
|
|
101
|
+
* @param middlewares
|
|
102
|
+
*/
|
|
103
|
+
static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
104
|
+
/**
|
|
105
|
+
* Define a group of routes with a common prefix and optional middlewares
|
|
106
|
+
*
|
|
107
|
+
* @param prefix
|
|
108
|
+
* @param callback
|
|
109
|
+
* @param middlewares
|
|
110
|
+
*/
|
|
111
|
+
static group(prefix: string, callback: () => void | Promise<void>, middlewares?: Middleware[]): Promise<void>;
|
|
112
|
+
/**
|
|
113
|
+
* Apply middlewares to a group of routes defined within the callback
|
|
114
|
+
*
|
|
115
|
+
* @param middlewares
|
|
116
|
+
* @param callback
|
|
117
|
+
*/
|
|
118
|
+
static middleware(middlewares: Middleware[], callback: () => void): void;
|
|
119
|
+
static allRoutes(): Array<Route<HttpContext, Middleware, Handler>>;
|
|
120
|
+
/**
|
|
121
|
+
* @param type - 'path' to get routes organized by path
|
|
122
|
+
*/
|
|
123
|
+
static allRoutes(type: 'path'): Record<string, Route<HttpContext, Middleware, Handler>>;
|
|
124
|
+
/**
|
|
125
|
+
* @param type - 'method' to get routes organized by method
|
|
126
|
+
*/
|
|
127
|
+
static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware, Handler>> };
|
|
128
|
+
/**
|
|
129
|
+
* Apply the defined routes to a Fastify application instance
|
|
130
|
+
*
|
|
131
|
+
* @param app - The Fastify application instance
|
|
132
|
+
* @returns The Fastify application instance with the applied routes
|
|
133
|
+
*/
|
|
134
|
+
static apply(app: FastifyApp): FastifyApp;
|
|
135
|
+
}
|
|
136
|
+
//#endregion
|
|
137
|
+
export { Router };
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { d as ApiResourceMiddleware, f as ControllerAction, m as HttpMethod, n as ClearRequest, p as ControllerHandler, r as Route, t as CoreRouter } from "../router-cWYmcfTX.mjs";
|
|
2
|
+
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
|
3
|
+
|
|
4
|
+
//#region types/fastify.d.ts
|
|
5
|
+
interface RequestWithGetBody extends FastifyRequest {
|
|
6
|
+
getBody: () => Record<string, any>;
|
|
7
|
+
}
|
|
8
|
+
interface HttpContext {
|
|
9
|
+
req: RequestWithGetBody;
|
|
10
|
+
reply: FastifyReply;
|
|
11
|
+
}
|
|
12
|
+
type RouteHandler = (ctx: HttpContext, req: ClearRequest) => any | Promise<any>;
|
|
13
|
+
type Handler = RouteHandler | ControllerHandler;
|
|
14
|
+
type NextFunction = (err?: Error) => void;
|
|
15
|
+
type Middleware = (req: RequestWithGetBody, reply: FastifyReply, next: NextFunction) => any | Promise<any>;
|
|
16
|
+
type FastifyApp = FastifyInstance;
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/fastify/router.d.ts
|
|
19
|
+
/**
|
|
20
|
+
* @class clear-router Fastify Router
|
|
21
|
+
* @description Laravel-style routing system for Fastify using shared clear-router core
|
|
22
|
+
* @author 3m1n3nc3
|
|
23
|
+
* @repository https://github.com/toneflix/clear-router
|
|
24
|
+
*/
|
|
25
|
+
declare class Router extends CoreRouter {
|
|
26
|
+
private static ensureRequestBodyAccessor;
|
|
27
|
+
/**
|
|
28
|
+
* Add a route to the router
|
|
29
|
+
*
|
|
30
|
+
* @param methods HTTP methods for the route
|
|
31
|
+
* @param path Route path
|
|
32
|
+
* @param handler Route handler function
|
|
33
|
+
* @param middlewares Optional middlewares for the route
|
|
34
|
+
*/
|
|
35
|
+
static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
36
|
+
/**
|
|
37
|
+
* Define a resourceful API controller with standard CRUD routes
|
|
38
|
+
*
|
|
39
|
+
* @param basePath Base path for the resource
|
|
40
|
+
* @param controller Controller class or instance
|
|
41
|
+
* @param options Optional configuration for the resource
|
|
42
|
+
*/
|
|
43
|
+
static apiResource(basePath: string, controller: any, options?: {
|
|
44
|
+
only?: ControllerAction[];
|
|
45
|
+
except?: ControllerAction[];
|
|
46
|
+
middlewares?: ApiResourceMiddleware<Middleware>;
|
|
47
|
+
}): void;
|
|
48
|
+
/**
|
|
49
|
+
* Define a GET route
|
|
50
|
+
*
|
|
51
|
+
* @param path
|
|
52
|
+
* @param handler
|
|
53
|
+
* @param middlewares
|
|
54
|
+
*/
|
|
55
|
+
static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
56
|
+
/**
|
|
57
|
+
* Define a POST route
|
|
58
|
+
*
|
|
59
|
+
* @param path
|
|
60
|
+
* @param handler
|
|
61
|
+
* @param middlewares
|
|
62
|
+
*/
|
|
63
|
+
static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
64
|
+
/**
|
|
65
|
+
* Define a PUT route
|
|
66
|
+
*
|
|
67
|
+
* @param path
|
|
68
|
+
* @param handler
|
|
69
|
+
* @param middlewares
|
|
70
|
+
*/
|
|
71
|
+
static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
72
|
+
/**
|
|
73
|
+
* Define a DELETE route
|
|
74
|
+
*
|
|
75
|
+
* @param path
|
|
76
|
+
* @param handler
|
|
77
|
+
* @param middlewares
|
|
78
|
+
*/
|
|
79
|
+
static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
80
|
+
/**
|
|
81
|
+
* Define a PATCH route
|
|
82
|
+
*
|
|
83
|
+
* @param path
|
|
84
|
+
* @param handler
|
|
85
|
+
* @param middlewares
|
|
86
|
+
*/
|
|
87
|
+
static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
88
|
+
/**
|
|
89
|
+
* Define an OPTIONS route
|
|
90
|
+
*
|
|
91
|
+
* @param path
|
|
92
|
+
* @param handler
|
|
93
|
+
* @param middlewares
|
|
94
|
+
*/
|
|
95
|
+
static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
96
|
+
/**
|
|
97
|
+
* Define a HEAD route
|
|
98
|
+
*
|
|
99
|
+
* @param path
|
|
100
|
+
* @param handler
|
|
101
|
+
* @param middlewares
|
|
102
|
+
*/
|
|
103
|
+
static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
104
|
+
/**
|
|
105
|
+
* Define a group of routes with a common prefix and optional middlewares
|
|
106
|
+
*
|
|
107
|
+
* @param prefix
|
|
108
|
+
* @param callback
|
|
109
|
+
* @param middlewares
|
|
110
|
+
*/
|
|
111
|
+
static group(prefix: string, callback: () => void | Promise<void>, middlewares?: Middleware[]): Promise<void>;
|
|
112
|
+
/**
|
|
113
|
+
* Apply middlewares to a group of routes defined within the callback
|
|
114
|
+
*
|
|
115
|
+
* @param middlewares
|
|
116
|
+
* @param callback
|
|
117
|
+
*/
|
|
118
|
+
static middleware(middlewares: Middleware[], callback: () => void): void;
|
|
119
|
+
static allRoutes(): Array<Route<HttpContext, Middleware, Handler>>;
|
|
120
|
+
/**
|
|
121
|
+
* @param type - 'path' to get routes organized by path
|
|
122
|
+
*/
|
|
123
|
+
static allRoutes(type: 'path'): Record<string, Route<HttpContext, Middleware, Handler>>;
|
|
124
|
+
/**
|
|
125
|
+
* @param type - 'method' to get routes organized by method
|
|
126
|
+
*/
|
|
127
|
+
static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware, Handler>> };
|
|
128
|
+
/**
|
|
129
|
+
* Apply the defined routes to a Fastify application instance
|
|
130
|
+
*
|
|
131
|
+
* @param app - The Fastify application instance
|
|
132
|
+
* @returns The Fastify application instance with the applied routes
|
|
133
|
+
*/
|
|
134
|
+
static apply(app: FastifyApp): FastifyApp;
|
|
135
|
+
}
|
|
136
|
+
//#endregion
|
|
137
|
+
export { Router };
|