@bepalo/spine 1.1.9 → 1.2.10
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 +84 -5
- package/dist/auth-middlewares.d.ts +2 -2
- package/dist/auth-middlewares.js +2 -2
- package/dist/cjs/auth-middlewares.d.ts +2 -2
- package/dist/cjs/auth-middlewares.js +2 -2
- package/dist/cjs/generator.d.ts +4 -0
- package/dist/cjs/generator.d.ts.map +1 -0
- package/dist/cjs/generator.js +166 -0
- package/dist/cjs/generator.js.map +1 -0
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/middlewares.d.ts +1 -1
- package/dist/cjs/middlewares.js +1 -1
- package/dist/cjs/router.d.ts +80 -73
- package/dist/cjs/router.d.ts.map +1 -1
- package/dist/cjs/router.js +279 -278
- package/dist/cjs/router.js.map +1 -1
- package/dist/cjs/types.d.ts +6 -2
- package/dist/cjs/types.d.ts.map +1 -1
- package/dist/cjs/types.js +1 -1
- package/dist/cjs/types.js.map +1 -1
- package/dist/generator.d.ts +4 -0
- package/dist/generator.d.ts.map +1 -0
- package/dist/generator.js +166 -0
- package/dist/generator.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/middlewares.d.ts +1 -1
- package/dist/middlewares.js +1 -1
- package/dist/router.d.ts +80 -73
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +279 -278
- package/dist/router.js.map +1 -1
- package/dist/types.d.ts +6 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -79,7 +79,7 @@ Average 10.05k 8.13k 7.65k ┌────────
|
|
|
79
79
|
|
|
80
80
|
## 📑 Table of Contents
|
|
81
81
|
|
|
82
|
-
- [
|
|
82
|
+
- [What is new?](#what-is-new)
|
|
83
83
|
- [Quick Start](#quick-start)
|
|
84
84
|
- [Routing](#routing)
|
|
85
85
|
- [Parameters](#parameters)
|
|
@@ -106,6 +106,85 @@ Average 10.05k 8.13k 7.65k ┌────────
|
|
|
106
106
|
- [Thanks and Enjoy](#️-thanks-and-enjoy)
|
|
107
107
|
- [Be a Sponsor](#-be-a-sponsor)
|
|
108
108
|
|
|
109
|
+
## What is new v1.2.10?
|
|
110
|
+
|
|
111
|
+
- Implemented static route generator function `generateStaticRoutesImporter` to use with nextjs, cloudflare workers and more which do not do accept dynamic imports.
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
// src/server/utils/gen-routes.ts
|
|
115
|
+
import Router from "@bepalo/spine";
|
|
116
|
+
import generateStaticRoutesImporter from "@bepalo/spine";
|
|
117
|
+
|
|
118
|
+
await generateStaticRoutesImporter(
|
|
119
|
+
new Router(),
|
|
120
|
+
"./src/server/routes",
|
|
121
|
+
"./routes/",
|
|
122
|
+
).then((content) =>
|
|
123
|
+
Bun.write("./src/server/index.ts", "// src/server/index.ts\n" + content),
|
|
124
|
+
);
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
// src/server/index.ts
|
|
129
|
+
// This is auto-generated for static importing of @bepalo/spine file-based routes
|
|
130
|
+
import {
|
|
131
|
+
Router,
|
|
132
|
+
Handler,
|
|
133
|
+
HandlerRegisterPiplineOptions,
|
|
134
|
+
Pipeline,
|
|
135
|
+
} from "@bepalo/spine";
|
|
136
|
+
import * as route__index from "./routes/_index";
|
|
137
|
+
import * as route_api_$$$path from "./routes/api/[## path]";
|
|
138
|
+
import * as route_api_$ from "./routes/api/[[#]]";
|
|
139
|
+
import * as route_api_fruits from "./routes/api/fruits";
|
|
140
|
+
import * as route_api_users_$$$usersPath from "./routes/api/users/[## usersPath]";
|
|
141
|
+
import * as route_api_users_$$$id from "./routes/api/users/[id]";
|
|
142
|
+
import * as route_pages_$$ from "./routes/pages/[[##]]";
|
|
143
|
+
|
|
144
|
+
const getOptions = <ExtendContext extends Record<string, unknown>>(
|
|
145
|
+
def: {
|
|
146
|
+
pipe: Handler<ExtendContext> | Pipeline<ExtendContext>;
|
|
147
|
+
} & HandlerRegisterPiplineOptions,
|
|
148
|
+
) => {
|
|
149
|
+
const { pipe, ...options } = def;
|
|
150
|
+
return options;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
export const setRoutes = async <ExtendContext extends Record<string, unknown>>(
|
|
154
|
+
router: Router<ExtendContext>,
|
|
155
|
+
) => {
|
|
156
|
+
// /
|
|
157
|
+
router.handleGet(
|
|
158
|
+
"/_index",
|
|
159
|
+
route__index.Get.pipe,
|
|
160
|
+
getOptions(route__index.Get),
|
|
161
|
+
);
|
|
162
|
+
router.filterGet("/_index", route__index.Get_Filter);
|
|
163
|
+
// /api
|
|
164
|
+
router.filterGet("/api/::path", route_api_$$$path.Get_Filter);
|
|
165
|
+
router.filterGet("/api/*!", route_api_$.Get_Filter);
|
|
166
|
+
router.handleDelete("/api/fruits", route_api_fruits.Delete);
|
|
167
|
+
router.handleGet("/api/fruits", route_api_fruits.Get);
|
|
168
|
+
router.handlePatch("/api/fruits", route_api_fruits.Patch);
|
|
169
|
+
router.handlePost("/api/fruits", route_api_fruits.Post);
|
|
170
|
+
// /api/users
|
|
171
|
+
router.filterGet(
|
|
172
|
+
"/api/users/::usersPath",
|
|
173
|
+
route_api_users_$$$usersPath.Get_Filter,
|
|
174
|
+
);
|
|
175
|
+
router.handleGet(
|
|
176
|
+
"/api/users/:id",
|
|
177
|
+
route_api_users_$$$id.Get.pipe,
|
|
178
|
+
getOptions(route_api_users_$$$id.Get),
|
|
179
|
+
);
|
|
180
|
+
// /pages
|
|
181
|
+
router.handleGet("/pages/**!", route_pages_$$.Get);
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
// Use this to set the routes in your router
|
|
185
|
+
export default setRoutes;
|
|
186
|
+
```
|
|
187
|
+
|
|
109
188
|
## Quick Start
|
|
110
189
|
|
|
111
190
|
Install
|
|
@@ -336,7 +415,7 @@ Spine separates request processing into explicit phases:
|
|
|
336
415
|
|
|
337
416
|
```text
|
|
338
417
|
( @Bepalo/spine )
|
|
339
|
-
router
|
|
418
|
+
router pipe
|
|
340
419
|
┌───────────────────────┐
|
|
341
420
|
▼ │
|
|
342
421
|
┌──────┴───────┐ │
|
|
@@ -410,7 +489,7 @@ spine.catchGet("/api/**", ({ error }) =>
|
|
|
410
489
|
|
|
411
490
|
spine.afterGet("/api/**", ({ response }) => {
|
|
412
491
|
console.log(response.status);
|
|
413
|
-
// even the response after a caught error will pass through the after-
|
|
492
|
+
// even the response after a caught error will pass through the after-pipe
|
|
414
493
|
// error thrown here is not caught.
|
|
415
494
|
// afters are best used for logging or modifying the final response
|
|
416
495
|
});
|
|
@@ -422,7 +501,7 @@ Handlers can also be composed into pipelines:
|
|
|
422
501
|
spine.post("/users", [parseBody(), validateUser(), createUser()]);
|
|
423
502
|
```
|
|
424
503
|
|
|
425
|
-
A
|
|
504
|
+
A pipe can stop normally by returning a `Response`, or use Spine's explicit control symbols:
|
|
426
505
|
|
|
427
506
|
```ts
|
|
428
507
|
import { Break_Pipe, Break_Pipeline } from "@bepalo/spine";
|
|
@@ -430,7 +509,7 @@ import { Break_Pipe, Break_Pipeline } from "@bepalo/spine";
|
|
|
430
509
|
spine.filterGet("/**", cors({ maxTokens: 60 }));
|
|
431
510
|
spine.filterGet("/api/**", [cors({ maxTokens: 200 }), () => Break_Pipeline]);. /* '/**' cors wont be called */
|
|
432
511
|
|
|
433
|
-
// Break_Pipeline breaks from the overall handlers
|
|
512
|
+
// Break_Pipeline breaks from the overall handlers pipe while
|
|
434
513
|
// Break_Pipe breaks from the current handler pipe without returning a Response.
|
|
435
514
|
|
|
436
515
|
```
|
|
@@ -32,7 +32,7 @@ export type ParseAuthFn<ExtendAuth extends Record<string, unknown> = Record<stri
|
|
|
32
32
|
* @param {Object} [options] - Configuration options.
|
|
33
33
|
* @param {ParseAuthFn}[options.parseAuth] - Function to extract authentication info from the request.
|
|
34
34
|
* Should return an `Auth` object if valid, `Error` if invalid, or `null/undefined` if missing.
|
|
35
|
-
* @param {boolean} [options.breakPipeline=false] - If true, stops only
|
|
35
|
+
* @param {boolean} [options.breakPipeline=false] - If true, stops only pipe flow per handler type after success.
|
|
36
36
|
* @param {boolean} [options.checkOnly=false] - If true, only checks authentication without returning a response.
|
|
37
37
|
*
|
|
38
38
|
* @returns {Handler<CTAuth<ExtendAuth> & ExtendContext>} A handler that sets `ctx.auth` if authentication succeeds,
|
|
@@ -54,7 +54,7 @@ export declare const authenticate: <ExtendAuth extends Record<string, unknown> =
|
|
|
54
54
|
* @param {string[]}[options.permissions] - List of permissions required for access.
|
|
55
55
|
* @param {(permission: string,role: string) => boolean|null|undefined}[options.hasPermission] - Function to check if a role has a given permission.
|
|
56
56
|
* Required if `permissions` is provided.
|
|
57
|
-
* @param {boolean} [options.breakPipeline=false] - If true, stops only
|
|
57
|
+
* @param {boolean} [options.breakPipeline=false] - If true, stops only pipe flow per handler type after success.
|
|
58
58
|
*
|
|
59
59
|
* @returns {Handler<CTAuth & ExtendContext>} A handler that checks `ctx.auth` and enforces role/permission rules.
|
|
60
60
|
* Returns `401 Unauthorized` if no auth is present, or `403 Forbidden` if checks fail.
|
package/dist/auth-middlewares.js
CHANGED
|
@@ -20,7 +20,7 @@ const types_ts_1 = require("./types.js");
|
|
|
20
20
|
* @param {Object} [options] - Configuration options.
|
|
21
21
|
* @param {ParseAuthFn}[options.parseAuth] - Function to extract authentication info from the request.
|
|
22
22
|
* Should return an `Auth` object if valid, `Error` if invalid, or `null/undefined` if missing.
|
|
23
|
-
* @param {boolean} [options.breakPipeline=false] - If true, stops only
|
|
23
|
+
* @param {boolean} [options.breakPipeline=false] - If true, stops only pipe flow per handler type after success.
|
|
24
24
|
* @param {boolean} [options.checkOnly=false] - If true, only checks authentication without returning a response.
|
|
25
25
|
*
|
|
26
26
|
* @returns {Handler<CTAuth<ExtendAuth> & ExtendContext>} A handler that sets `ctx.auth` if authentication succeeds,
|
|
@@ -61,7 +61,7 @@ exports.authenticate = authenticate;
|
|
|
61
61
|
* @param {string[]}[options.permissions] - List of permissions required for access.
|
|
62
62
|
* @param {(permission: string,role: string) => boolean|null|undefined}[options.hasPermission] - Function to check if a role has a given permission.
|
|
63
63
|
* Required if `permissions` is provided.
|
|
64
|
-
* @param {boolean} [options.breakPipeline=false] - If true, stops only
|
|
64
|
+
* @param {boolean} [options.breakPipeline=false] - If true, stops only pipe flow per handler type after success.
|
|
65
65
|
*
|
|
66
66
|
* @returns {Handler<CTAuth & ExtendContext>} A handler that checks `ctx.auth` and enforces role/permission rules.
|
|
67
67
|
* Returns `401 Unauthorized` if no auth is present, or `403 Forbidden` if checks fail.
|
|
@@ -32,7 +32,7 @@ export type ParseAuthFn<ExtendAuth extends Record<string, unknown> = Record<stri
|
|
|
32
32
|
* @param {Object} [options] - Configuration options.
|
|
33
33
|
* @param {ParseAuthFn}[options.parseAuth] - Function to extract authentication info from the request.
|
|
34
34
|
* Should return an `Auth` object if valid, `Error` if invalid, or `null/undefined` if missing.
|
|
35
|
-
* @param {boolean} [options.breakPipeline=false] - If true, stops only
|
|
35
|
+
* @param {boolean} [options.breakPipeline=false] - If true, stops only pipe flow per handler type after success.
|
|
36
36
|
* @param {boolean} [options.checkOnly=false] - If true, only checks authentication without returning a response.
|
|
37
37
|
*
|
|
38
38
|
* @returns {Handler<CTAuth<ExtendAuth> & ExtendContext>} A handler that sets `ctx.auth` if authentication succeeds,
|
|
@@ -54,7 +54,7 @@ export declare const authenticate: <ExtendAuth extends Record<string, unknown> =
|
|
|
54
54
|
* @param {string[]}[options.permissions] - List of permissions required for access.
|
|
55
55
|
* @param {(permission: string,role: string) => boolean|null|undefined}[options.hasPermission] - Function to check if a role has a given permission.
|
|
56
56
|
* Required if `permissions` is provided.
|
|
57
|
-
* @param {boolean} [options.breakPipeline=false] - If true, stops only
|
|
57
|
+
* @param {boolean} [options.breakPipeline=false] - If true, stops only pipe flow per handler type after success.
|
|
58
58
|
*
|
|
59
59
|
* @returns {Handler<CTAuth & ExtendContext>} A handler that checks `ctx.auth` and enforces role/permission rules.
|
|
60
60
|
* Returns `401 Unauthorized` if no auth is present, or `403 Forbidden` if checks fail.
|
|
@@ -20,7 +20,7 @@ const types_ts_1 = require("./types.js");
|
|
|
20
20
|
* @param {Object} [options] - Configuration options.
|
|
21
21
|
* @param {ParseAuthFn}[options.parseAuth] - Function to extract authentication info from the request.
|
|
22
22
|
* Should return an `Auth` object if valid, `Error` if invalid, or `null/undefined` if missing.
|
|
23
|
-
* @param {boolean} [options.breakPipeline=false] - If true, stops only
|
|
23
|
+
* @param {boolean} [options.breakPipeline=false] - If true, stops only pipe flow per handler type after success.
|
|
24
24
|
* @param {boolean} [options.checkOnly=false] - If true, only checks authentication without returning a response.
|
|
25
25
|
*
|
|
26
26
|
* @returns {Handler<CTAuth<ExtendAuth> & ExtendContext>} A handler that sets `ctx.auth` if authentication succeeds,
|
|
@@ -61,7 +61,7 @@ exports.authenticate = authenticate;
|
|
|
61
61
|
* @param {string[]}[options.permissions] - List of permissions required for access.
|
|
62
62
|
* @param {(permission: string,role: string) => boolean|null|undefined}[options.hasPermission] - Function to check if a role has a given permission.
|
|
63
63
|
* Required if `permissions` is provided.
|
|
64
|
-
* @param {boolean} [options.breakPipeline=false] - If true, stops only
|
|
64
|
+
* @param {boolean} [options.breakPipeline=false] - If true, stops only pipe flow per handler type after success.
|
|
65
65
|
*
|
|
66
66
|
* @returns {Handler<CTAuth & ExtendContext>} A handler that checks `ctx.auth` and enforces role/permission rules.
|
|
67
67
|
* Returns `401 Unauthorized` if no auth is present, or `403 Forbidden` if checks fail.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../../src/generator.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,MAAM,EAAqC,MAAM,aAAa,CAAC;AAkFxE,eAAO,MAAM,4BAA4B,GACvC,QAAQ,MAAM,EACd,YAAY,MAAM,EAClB,aAAY,MAAa,oBAuE1B,CAAC;AAEF,eAAe,4BAA4B,CAAC"}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
+
var t = {};
|
|
13
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
+
t[p] = s[p];
|
|
15
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
+
t[p[i]] = s[p[i]];
|
|
19
|
+
}
|
|
20
|
+
return t;
|
|
21
|
+
};
|
|
22
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
23
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
24
|
+
var m = o[Symbol.asyncIterator], i;
|
|
25
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
26
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
27
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
28
|
+
};
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
exports.generateStaticRoutesImporter = void 0;
|
|
31
|
+
const types_ts_1 = require("./types.js");
|
|
32
|
+
const router_ts_1 = require("./router.js");
|
|
33
|
+
const utils_node_ts_1 = require("./utils.node.js");
|
|
34
|
+
function getRouteLoaders(routerId, moduleId, pathname, module, importPath) {
|
|
35
|
+
let setters = "";
|
|
36
|
+
for (const [methodHandler, def] of Object.entries(module)) {
|
|
37
|
+
// get method and handler type
|
|
38
|
+
const [method_, handlerType_] = methodHandler.split("_", 2);
|
|
39
|
+
let method;
|
|
40
|
+
let handlerType = "handler";
|
|
41
|
+
if (router_ts_1.HTTP_METHODS_UPPER.has(method_.toUpperCase())) {
|
|
42
|
+
method = (method_.charAt(0).toUpperCase() +
|
|
43
|
+
method_.substring(1).toLowerCase());
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (handlerType_ &&
|
|
49
|
+
router_ts_1.HANDLER_TYPES.has(handlerType_.toLowerCase())) {
|
|
50
|
+
handlerType = handlerType_.toLowerCase();
|
|
51
|
+
}
|
|
52
|
+
if (!(Array.isArray(def) ||
|
|
53
|
+
typeof def === "function" ||
|
|
54
|
+
typeof def === "object")) {
|
|
55
|
+
// throw new RouterError(`Invalid route definition type \`${typeof def}\` '${methodHandler}' in ${importPath}`);
|
|
56
|
+
console.error(new types_ts_1.RouterError(`Invalid route definition type \`${typeof def}\` '${methodHandler}' in ${importPath}`));
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
const defIsObject = !Array.isArray(def) && typeof def === "object";
|
|
60
|
+
// get pipe and any other options
|
|
61
|
+
const _a = defIsObject
|
|
62
|
+
? def
|
|
63
|
+
: { pipe: def }, { pipe } = _a, options = __rest(_a, ["pipe"]);
|
|
64
|
+
if (!(Array.isArray(pipe) || typeof pipe === "function")) {
|
|
65
|
+
// throw new RouterError(`Invalid route 'pipe' type \`${typeof pipe}\` in ${importPath}`);
|
|
66
|
+
console.error(new types_ts_1.RouterError(`Invalid route 'pipe' type \`${typeof pipe}\` in ${importPath}`));
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const entryId = `${moduleId}.${methodHandler}`;
|
|
70
|
+
const optionsStr = Object.entries(options).length > 0 ? `, getOptions(${entryId})` : "";
|
|
71
|
+
const pipeStr = defIsObject ? `${entryId}.pipe` : entryId;
|
|
72
|
+
switch (handlerType) {
|
|
73
|
+
case "filter":
|
|
74
|
+
setters += `\n ${routerId}.filter${method}("${pathname}", ${pipeStr}${optionsStr});`;
|
|
75
|
+
break;
|
|
76
|
+
case "handler":
|
|
77
|
+
setters += `\n ${routerId}.handle${method}("${pathname}", ${pipeStr}${optionsStr});`;
|
|
78
|
+
break;
|
|
79
|
+
case "fallback":
|
|
80
|
+
setters += `\n ${routerId}.fallback${method}("${pathname}", ${pipeStr}${optionsStr});`;
|
|
81
|
+
break;
|
|
82
|
+
case "after":
|
|
83
|
+
setters += `\n ${routerId}.after${method}("${pathname}", ${pipeStr}${optionsStr});`;
|
|
84
|
+
break;
|
|
85
|
+
case "catcher":
|
|
86
|
+
setters += `\n ${routerId}.catch${method}("${pathname}", ${pipeStr}${optionsStr});`;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return setters;
|
|
91
|
+
}
|
|
92
|
+
const generateStaticRoutesImporter = (router_1, routesPath_1, ...args_1) => __awaiter(void 0, [router_1, routesPath_1, ...args_1], void 0, function* (router, routesPath, importRoot = "./") {
|
|
93
|
+
var _a, e_1, _b, _c;
|
|
94
|
+
let imports = "";
|
|
95
|
+
let loads = "";
|
|
96
|
+
const prefix = "route";
|
|
97
|
+
const map = new Map();
|
|
98
|
+
try {
|
|
99
|
+
for (var _d = true, _e = __asyncValues((0, utils_node_ts_1.walk)(routesPath)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
|
|
100
|
+
_c = _f.value;
|
|
101
|
+
_d = false;
|
|
102
|
+
const node = _c;
|
|
103
|
+
const { name, type, relativePath, fullPath, parent, path: _path } = node;
|
|
104
|
+
if (type === "file") {
|
|
105
|
+
const pureRelativePath = relativePath.substring(0, relativePath.lastIndexOf("."));
|
|
106
|
+
let pathname = router.translateRouteFilePath("/" + pureRelativePath);
|
|
107
|
+
pathname = pureRelativePath.endsWith("/index")
|
|
108
|
+
? pathname.substring(0, pathname.length - 1)
|
|
109
|
+
: pathname;
|
|
110
|
+
let path = pathname
|
|
111
|
+
.replace(/\//g, "_")
|
|
112
|
+
.replace(/(\*{1,2})!?/g, (_m, m1) => "$".repeat(m1.length))
|
|
113
|
+
.replace(/(_.+?(?:\|.+?)*)?\:{1,2}(.*)!?[\/$]?/g, (_m, m1, m2) => (m1 ? m1.replace(/\|/g, "$") + "$$" : "") + "$" + m2);
|
|
114
|
+
const importName = prefix + path;
|
|
115
|
+
const importPath = importRoot + pureRelativePath;
|
|
116
|
+
const module = (yield (0, utils_node_ts_1.dynamicImport)(fullPath));
|
|
117
|
+
const routeDef = " " +
|
|
118
|
+
(yield getRouteLoaders("router", importName, pathname, module, _path));
|
|
119
|
+
const importStr = `import * as ${importName} from "${importPath}";\n`;
|
|
120
|
+
const loadStr = routeDef;
|
|
121
|
+
let entry = map.get("/" + parent);
|
|
122
|
+
if (!entry) {
|
|
123
|
+
entry = [];
|
|
124
|
+
map.set("/" + parent, entry);
|
|
125
|
+
}
|
|
126
|
+
entry.push({ name, pathname, importStr, loadStr });
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
131
|
+
finally {
|
|
132
|
+
try {
|
|
133
|
+
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
|
|
134
|
+
}
|
|
135
|
+
finally { if (e_1) throw e_1.error; }
|
|
136
|
+
}
|
|
137
|
+
// append in a sorted order
|
|
138
|
+
map.forEach((e) => e.sort((a, b) => a.pathname.localeCompare(b.pathname)));
|
|
139
|
+
const sortedEntries = Array.from(map.entries()).sort((a, b) => a[0].localeCompare(b[0]));
|
|
140
|
+
for (const [p, es] of sortedEntries) {
|
|
141
|
+
loads += `\n // ${p}`;
|
|
142
|
+
for (const e of es) {
|
|
143
|
+
const { importStr, loadStr } = e;
|
|
144
|
+
imports += importStr;
|
|
145
|
+
loads += loadStr;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return `// This is auto-generated for static importing of @bepalo/spine file-based routes
|
|
149
|
+
import { Router, Handler, HandlerRegisterPiplineOptions, Pipeline } from '@bepalo/spine';
|
|
150
|
+
${imports}
|
|
151
|
+
const getOptions = <ExtendContext extends Record<string, unknown>>(
|
|
152
|
+
def: { pipe: Handler<ExtendContext> | Pipeline<ExtendContext> } & HandlerRegisterPiplineOptions,
|
|
153
|
+
) => {
|
|
154
|
+
const { pipe, ...options } = def;
|
|
155
|
+
return options;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
export const setRoutes = async <ExtendContext extends Record<string, unknown>>(router: Router<ExtendContext>) => {${loads}
|
|
159
|
+
};\n
|
|
160
|
+
// Use this to set the routes in your router
|
|
161
|
+
export default setRoutes;
|
|
162
|
+
`;
|
|
163
|
+
});
|
|
164
|
+
exports.generateStaticRoutesImporter = generateStaticRoutesImporter;
|
|
165
|
+
exports.default = exports.generateStaticRoutesImporter;
|
|
166
|
+
//# sourceMappingURL=generator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generator.js","sourceRoot":"","sources":["../../src/generator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;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CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IAC3E,MAAM,aAAa,GAAG,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAC5D,CAAC,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CACzB,CAAC;IACF,KAAK,MAAM,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,aAAa,EAAE,CAAC;QACpC,KAAK,IAAI,cAAc,CAAC,EAAE,CAAC;QAC3B,KAAK,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC;YACnB,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,GAAG,CAAC,CAAC;YACjC,OAAO,IAAI,SAAS,CAAC;YACrB,KAAK,IAAI,OAAO,CAAC;QACnB,CAAC;IACH,CAAC;IACD,OAAO;;EAEP,OAAO;;;;;;;;oHAQ2G,KAAK;;;;CAIxH,CAAC;AACF,CAAC,CAAA,CAAC;AA1EW,QAAA,4BAA4B,gCA0EvC;AAEF,kBAAe,oCAA4B,CAAC"}
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export * from "./parsers.ts";
|
|
|
7
7
|
export * from "./auth-middlewares.ts";
|
|
8
8
|
export * from "./middlewares.ts";
|
|
9
9
|
export * from "./router.ts";
|
|
10
|
+
export * from "./generator.ts";
|
|
10
11
|
import Router from "./router.ts";
|
|
11
12
|
export default Router;
|
|
12
13
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/cjs/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC;AAC7B,cAAc,uBAAuB,CAAC;AACtC,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC;AAC7B,cAAc,uBAAuB,CAAC;AACtC,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAE/B,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,eAAe,MAAM,CAAC"}
|
package/dist/cjs/index.js
CHANGED
|
@@ -26,6 +26,7 @@ __exportStar(require("./parsers.js"), exports);
|
|
|
26
26
|
__exportStar(require("./auth-middlewares.js"), exports);
|
|
27
27
|
__exportStar(require("./middlewares.js"), exports);
|
|
28
28
|
__exportStar(require("./router.js"), exports);
|
|
29
|
+
__exportStar(require("./generator.js"), exports);
|
|
29
30
|
const router_ts_1 = __importDefault(require("./router.js"));
|
|
30
31
|
exports.default = router_ts_1.default;
|
|
31
32
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AAAA,6CAA2B;AAC3B,6CAA2B;AAC3B,kDAAgC;AAChC,8CAA4B;AAC5B,+CAA6B;AAC7B,+CAA6B;AAC7B,wDAAsC;AACtC,mDAAiC;AACjC,8CAA4B;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AAAA,6CAA2B;AAC3B,6CAA2B;AAC3B,kDAAgC;AAChC,8CAA4B;AAC5B,+CAA6B;AAC7B,+CAA6B;AAC7B,wDAAsC;AACtC,mDAAiC;AACjC,8CAA4B;AAC5B,iDAA+B;AAE/B,4DAAiC;AACjC,kBAAe,mBAAM,CAAC"}
|
|
@@ -57,7 +57,7 @@ export declare const limitRate: <ExtendContext extends Record<string, unknown> =
|
|
|
57
57
|
* @param {boolean} [config.credentials=false] - Allow credentials (cookies, authorization headers)
|
|
58
58
|
* @param {number} [config.maxAge=86400] - Maximum age for preflight cache in seconds
|
|
59
59
|
* @param {boolean} [config.varyOrigin=false] - Add Vary: Origin header for caching
|
|
60
|
-
* @param {boolean} [options.breakPipeline=false] - If true, stops only
|
|
60
|
+
* @param {boolean} [options.breakPipeline=false] - If true, stops only pipe flow per handler type after success.
|
|
61
61
|
* @returns {Function} Middleware function that handles CORS headers
|
|
62
62
|
*
|
|
63
63
|
* @example
|
package/dist/cjs/middlewares.js
CHANGED
|
@@ -174,7 +174,7 @@ exports.limitRate = limitRate;
|
|
|
174
174
|
* @param {boolean} [config.credentials=false] - Allow credentials (cookies, authorization headers)
|
|
175
175
|
* @param {number} [config.maxAge=86400] - Maximum age for preflight cache in seconds
|
|
176
176
|
* @param {boolean} [config.varyOrigin=false] - Add Vary: Origin header for caching
|
|
177
|
-
* @param {boolean} [options.breakPipeline=false] - If true, stops only
|
|
177
|
+
* @param {boolean} [options.breakPipeline=false] - If true, stops only pipe flow per handler type after success.
|
|
178
178
|
* @returns {Function} Middleware function that handles CORS headers
|
|
179
179
|
*
|
|
180
180
|
* @example
|