@stacksjs/router 0.57.4 โ 0.58.20
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 +4 -4
- package/dist/index.js +841 -0
- package/package.json +28 -18
- package/LICENSE.md +0 -21
- package/dist/index.d.ts +0 -18
- package/dist/index.mjs +0 -94
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ wip
|
|
|
15
15
|
wip
|
|
16
16
|
|
|
17
17
|
```bash
|
|
18
|
-
|
|
18
|
+
bun install -d @stacksjs/actions
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
Now, you can use it in your project:
|
|
@@ -31,7 +31,7 @@ Learn more in the docs.
|
|
|
31
31
|
## ๐งช Testing
|
|
32
32
|
|
|
33
33
|
```bash
|
|
34
|
-
|
|
34
|
+
bun test
|
|
35
35
|
```
|
|
36
36
|
|
|
37
37
|
## ๐ Changelog
|
|
@@ -50,10 +50,10 @@ For help, discussion about best practices, or any other conversation that would
|
|
|
50
50
|
|
|
51
51
|
For casual chit-chat with others using this package:
|
|
52
52
|
|
|
53
|
-
[Join the Stacks Discord Server](https://discord.
|
|
53
|
+
[Join the Stacks Discord Server](https://discord.gg/stacksjs)
|
|
54
54
|
|
|
55
55
|
## ๐ License
|
|
56
56
|
|
|
57
57
|
The MIT License (MIT). Please see [LICENSE](https://github.com/stacksjs/stacks/tree/main/LICENSE.md) for more information.
|
|
58
58
|
|
|
59
|
-
Made with
|
|
59
|
+
Made with ๐
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,841 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/middleware.ts
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import {promisify} from "util";
|
|
5
|
+
|
|
6
|
+
// /Users/chrisbreuer/Code/stacks/storage/framework/core/src/path/src/index.ts
|
|
7
|
+
import process2 from "process";
|
|
8
|
+
|
|
9
|
+
// /Users/chrisbreuer/Code/stacks/node_modules/pathe/dist/shared/pathe.ff20891b.mjs
|
|
10
|
+
var normalizeWindowsPath = function(input = "") {
|
|
11
|
+
if (!input) {
|
|
12
|
+
return input;
|
|
13
|
+
}
|
|
14
|
+
return input.replace(/\\/g, "/").replace(_DRIVE_LETTER_START_RE, (r) => r.toUpperCase());
|
|
15
|
+
};
|
|
16
|
+
var cwd = function() {
|
|
17
|
+
if (typeof process !== "undefined" && typeof process.cwd === "function") {
|
|
18
|
+
return process.cwd().replace(/\\/g, "/");
|
|
19
|
+
}
|
|
20
|
+
return "/";
|
|
21
|
+
};
|
|
22
|
+
var normalizeString = function(path, allowAboveRoot) {
|
|
23
|
+
let res = "";
|
|
24
|
+
let lastSegmentLength = 0;
|
|
25
|
+
let lastSlash = -1;
|
|
26
|
+
let dots = 0;
|
|
27
|
+
let char = null;
|
|
28
|
+
for (let index = 0;index <= path.length; ++index) {
|
|
29
|
+
if (index < path.length) {
|
|
30
|
+
char = path[index];
|
|
31
|
+
} else if (char === "/") {
|
|
32
|
+
break;
|
|
33
|
+
} else {
|
|
34
|
+
char = "/";
|
|
35
|
+
}
|
|
36
|
+
if (char === "/") {
|
|
37
|
+
if (lastSlash === index - 1 || dots === 1)
|
|
38
|
+
;
|
|
39
|
+
else if (dots === 2) {
|
|
40
|
+
if (res.length < 2 || lastSegmentLength !== 2 || res[res.length - 1] !== "." || res[res.length - 2] !== ".") {
|
|
41
|
+
if (res.length > 2) {
|
|
42
|
+
const lastSlashIndex = res.lastIndexOf("/");
|
|
43
|
+
if (lastSlashIndex === -1) {
|
|
44
|
+
res = "";
|
|
45
|
+
lastSegmentLength = 0;
|
|
46
|
+
} else {
|
|
47
|
+
res = res.slice(0, lastSlashIndex);
|
|
48
|
+
lastSegmentLength = res.length - 1 - res.lastIndexOf("/");
|
|
49
|
+
}
|
|
50
|
+
lastSlash = index;
|
|
51
|
+
dots = 0;
|
|
52
|
+
continue;
|
|
53
|
+
} else if (res.length > 0) {
|
|
54
|
+
res = "";
|
|
55
|
+
lastSegmentLength = 0;
|
|
56
|
+
lastSlash = index;
|
|
57
|
+
dots = 0;
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (allowAboveRoot) {
|
|
62
|
+
res += res.length > 0 ? "/.." : "..";
|
|
63
|
+
lastSegmentLength = 2;
|
|
64
|
+
}
|
|
65
|
+
} else {
|
|
66
|
+
if (res.length > 0) {
|
|
67
|
+
res += `/${path.slice(lastSlash + 1, index)}`;
|
|
68
|
+
} else {
|
|
69
|
+
res = path.slice(lastSlash + 1, index);
|
|
70
|
+
}
|
|
71
|
+
lastSegmentLength = index - lastSlash - 1;
|
|
72
|
+
}
|
|
73
|
+
lastSlash = index;
|
|
74
|
+
dots = 0;
|
|
75
|
+
} else if (char === "." && dots !== -1) {
|
|
76
|
+
++dots;
|
|
77
|
+
} else {
|
|
78
|
+
dots = -1;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return res;
|
|
82
|
+
};
|
|
83
|
+
var _DRIVE_LETTER_START_RE = /^[A-Za-z]:\//;
|
|
84
|
+
var _UNC_REGEX = /^[/\\]{2}/;
|
|
85
|
+
var _IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/;
|
|
86
|
+
var _DRIVE_LETTER_RE = /^[A-Za-z]:$/;
|
|
87
|
+
var _ROOT_FOLDER_RE = /^\/([A-Za-z]:)?$/;
|
|
88
|
+
var sep = "/";
|
|
89
|
+
var delimiter = ":";
|
|
90
|
+
var normalize = function(path) {
|
|
91
|
+
if (path.length === 0) {
|
|
92
|
+
return ".";
|
|
93
|
+
}
|
|
94
|
+
path = normalizeWindowsPath(path);
|
|
95
|
+
const isUNCPath = path.match(_UNC_REGEX);
|
|
96
|
+
const isPathAbsolute = isAbsolute(path);
|
|
97
|
+
const trailingSeparator = path[path.length - 1] === "/";
|
|
98
|
+
path = normalizeString(path, !isPathAbsolute);
|
|
99
|
+
if (path.length === 0) {
|
|
100
|
+
if (isPathAbsolute) {
|
|
101
|
+
return "/";
|
|
102
|
+
}
|
|
103
|
+
return trailingSeparator ? "./" : ".";
|
|
104
|
+
}
|
|
105
|
+
if (trailingSeparator) {
|
|
106
|
+
path += "/";
|
|
107
|
+
}
|
|
108
|
+
if (_DRIVE_LETTER_RE.test(path)) {
|
|
109
|
+
path += "/";
|
|
110
|
+
}
|
|
111
|
+
if (isUNCPath) {
|
|
112
|
+
if (!isPathAbsolute) {
|
|
113
|
+
return `//./${path}`;
|
|
114
|
+
}
|
|
115
|
+
return `//${path}`;
|
|
116
|
+
}
|
|
117
|
+
return isPathAbsolute && !isAbsolute(path) ? `/${path}` : path;
|
|
118
|
+
};
|
|
119
|
+
var join = function(...arguments_) {
|
|
120
|
+
if (arguments_.length === 0) {
|
|
121
|
+
return ".";
|
|
122
|
+
}
|
|
123
|
+
let joined;
|
|
124
|
+
for (const argument of arguments_) {
|
|
125
|
+
if (argument && argument.length > 0) {
|
|
126
|
+
if (joined === undefined) {
|
|
127
|
+
joined = argument;
|
|
128
|
+
} else {
|
|
129
|
+
joined += `/${argument}`;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (joined === undefined) {
|
|
134
|
+
return ".";
|
|
135
|
+
}
|
|
136
|
+
return normalize(joined.replace(/\/\/+/g, "/"));
|
|
137
|
+
};
|
|
138
|
+
var resolve = function(...arguments_) {
|
|
139
|
+
arguments_ = arguments_.map((argument) => normalizeWindowsPath(argument));
|
|
140
|
+
let resolvedPath = "";
|
|
141
|
+
let resolvedAbsolute = false;
|
|
142
|
+
for (let index = arguments_.length - 1;index >= -1 && !resolvedAbsolute; index--) {
|
|
143
|
+
const path = index >= 0 ? arguments_[index] : cwd();
|
|
144
|
+
if (!path || path.length === 0) {
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
resolvedPath = `${path}/${resolvedPath}`;
|
|
148
|
+
resolvedAbsolute = isAbsolute(path);
|
|
149
|
+
}
|
|
150
|
+
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute);
|
|
151
|
+
if (resolvedAbsolute && !isAbsolute(resolvedPath)) {
|
|
152
|
+
return `/${resolvedPath}`;
|
|
153
|
+
}
|
|
154
|
+
return resolvedPath.length > 0 ? resolvedPath : ".";
|
|
155
|
+
};
|
|
156
|
+
var isAbsolute = function(p) {
|
|
157
|
+
return _IS_ABSOLUTE_RE.test(p);
|
|
158
|
+
};
|
|
159
|
+
var toNamespacedPath = function(p) {
|
|
160
|
+
return normalizeWindowsPath(p);
|
|
161
|
+
};
|
|
162
|
+
var _EXTNAME_RE = /.(\.[^./]+)$/;
|
|
163
|
+
var extname = function(p) {
|
|
164
|
+
const match = _EXTNAME_RE.exec(normalizeWindowsPath(p));
|
|
165
|
+
return match && match[1] || "";
|
|
166
|
+
};
|
|
167
|
+
var relative = function(from, to) {
|
|
168
|
+
const _from = resolve(from).replace(_ROOT_FOLDER_RE, "$1").split("/");
|
|
169
|
+
const _to = resolve(to).replace(_ROOT_FOLDER_RE, "$1").split("/");
|
|
170
|
+
if (_to[0][1] === ":" && _from[0][1] === ":" && _from[0] !== _to[0]) {
|
|
171
|
+
return _to.join("/");
|
|
172
|
+
}
|
|
173
|
+
const _fromCopy = [..._from];
|
|
174
|
+
for (const segment of _fromCopy) {
|
|
175
|
+
if (_to[0] !== segment) {
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
_from.shift();
|
|
179
|
+
_to.shift();
|
|
180
|
+
}
|
|
181
|
+
return [..._from.map(() => ".."), ..._to].join("/");
|
|
182
|
+
};
|
|
183
|
+
var dirname = function(p) {
|
|
184
|
+
const segments = normalizeWindowsPath(p).replace(/\/$/, "").split("/").slice(0, -1);
|
|
185
|
+
if (segments.length === 1 && _DRIVE_LETTER_RE.test(segments[0])) {
|
|
186
|
+
segments[0] += "/";
|
|
187
|
+
}
|
|
188
|
+
return segments.join("/") || (isAbsolute(p) ? "/" : ".");
|
|
189
|
+
};
|
|
190
|
+
var format = function(p) {
|
|
191
|
+
const segments = [p.root, p.dir, p.base ?? p.name + p.ext].filter(Boolean);
|
|
192
|
+
return normalizeWindowsPath(p.root ? resolve(...segments) : segments.join("/"));
|
|
193
|
+
};
|
|
194
|
+
var basename = function(p, extension) {
|
|
195
|
+
const lastSegment = normalizeWindowsPath(p).split("/").pop();
|
|
196
|
+
return extension && lastSegment.endsWith(extension) ? lastSegment.slice(0, -extension.length) : lastSegment;
|
|
197
|
+
};
|
|
198
|
+
var parse = function(p) {
|
|
199
|
+
const root = normalizeWindowsPath(p).split("/").shift() || "/";
|
|
200
|
+
const base = basename(p);
|
|
201
|
+
const extension = extname(base);
|
|
202
|
+
return {
|
|
203
|
+
root,
|
|
204
|
+
dir: dirname(p),
|
|
205
|
+
base,
|
|
206
|
+
ext: extension,
|
|
207
|
+
name: base.slice(0, base.length - extension.length)
|
|
208
|
+
};
|
|
209
|
+
};
|
|
210
|
+
// /Users/chrisbreuer/Code/stacks/storage/framework/core/src/path/src/index.ts
|
|
211
|
+
function actionsPath(path2) {
|
|
212
|
+
return corePath(`actions/src/${path2 || ""}`);
|
|
213
|
+
}
|
|
214
|
+
function relativeActionsPath(path2) {
|
|
215
|
+
return relative(projectPath(), actionsPath(path2));
|
|
216
|
+
}
|
|
217
|
+
function aiPath(path2) {
|
|
218
|
+
return corePath(`ai/${path2 || ""}`);
|
|
219
|
+
}
|
|
220
|
+
function assetsPath(path2) {
|
|
221
|
+
return resourcesPath(`assets/${path2 || ""}`);
|
|
222
|
+
}
|
|
223
|
+
function aliasPath() {
|
|
224
|
+
return corePath("alias/src/index.ts");
|
|
225
|
+
}
|
|
226
|
+
function analyticsPath(path2) {
|
|
227
|
+
return corePath(`analytics/${path2 || ""}`);
|
|
228
|
+
}
|
|
229
|
+
function arraysPath(path2) {
|
|
230
|
+
return corePath(`arrays/${path2 || ""}`);
|
|
231
|
+
}
|
|
232
|
+
function appPath(path2) {
|
|
233
|
+
return projectPath(`app/${path2 || ""}`);
|
|
234
|
+
}
|
|
235
|
+
function authPath(path2) {
|
|
236
|
+
return corePath(`auth/${path2 || ""}`);
|
|
237
|
+
}
|
|
238
|
+
function buildPath(path2) {
|
|
239
|
+
return corePath(`build/${path2 || ""}`);
|
|
240
|
+
}
|
|
241
|
+
function buildEnginePath(path2) {
|
|
242
|
+
return buildPath(`${path2 || ""}`);
|
|
243
|
+
}
|
|
244
|
+
function libsPath(path2) {
|
|
245
|
+
return frameworkPath(`libs/${path2 || ""}`);
|
|
246
|
+
}
|
|
247
|
+
function libsEntriesPath(path2) {
|
|
248
|
+
return libsPath(`entries/${path2 || ""}`);
|
|
249
|
+
}
|
|
250
|
+
function cachePath(path2) {
|
|
251
|
+
return corePath(`cache/${path2 || ""}`);
|
|
252
|
+
}
|
|
253
|
+
function chatPath(path2) {
|
|
254
|
+
return corePath(`chat/${path2 || ""}`);
|
|
255
|
+
}
|
|
256
|
+
function cliPath(path2) {
|
|
257
|
+
return corePath(`cli/${path2 || ""}`);
|
|
258
|
+
}
|
|
259
|
+
function cloudPath(path2) {
|
|
260
|
+
return corePath(`cloud/${path2 || ""}`);
|
|
261
|
+
}
|
|
262
|
+
function collectionsPath(path2) {
|
|
263
|
+
return corePath(`collections/${path2 || ""}`);
|
|
264
|
+
}
|
|
265
|
+
function componentsPath(path2) {
|
|
266
|
+
return resourcesPath(`components/${path2 || ""}`);
|
|
267
|
+
}
|
|
268
|
+
function configPath(path2) {
|
|
269
|
+
return corePath(`config/${path2 || ""}`);
|
|
270
|
+
}
|
|
271
|
+
function corePath(path2) {
|
|
272
|
+
return frameworkPath(`core/src/${path2 || ""}`);
|
|
273
|
+
}
|
|
274
|
+
function customElementsDataPath() {
|
|
275
|
+
return corePath("custom-elements.json");
|
|
276
|
+
}
|
|
277
|
+
function databasePath(path2) {
|
|
278
|
+
return corePath(`database/${path2 || ""}`);
|
|
279
|
+
}
|
|
280
|
+
function datetimePath(path2) {
|
|
281
|
+
return corePath(`datetime/${path2 || ""}`);
|
|
282
|
+
}
|
|
283
|
+
function developmentPath(path2) {
|
|
284
|
+
return corePath(`development/${path2 || ""}`);
|
|
285
|
+
}
|
|
286
|
+
function desktopPath(path2) {
|
|
287
|
+
return corePath(`desktop/${path2 || ""}`);
|
|
288
|
+
}
|
|
289
|
+
function docsPath(path2) {
|
|
290
|
+
return corePath(`docs/${path2 || ""}`);
|
|
291
|
+
}
|
|
292
|
+
function dnsPath(path2) {
|
|
293
|
+
return corePath(`domains/${path2 || ""}`);
|
|
294
|
+
}
|
|
295
|
+
function emailPath(path2) {
|
|
296
|
+
return notificationsPath(`email/${path2 || ""}`);
|
|
297
|
+
}
|
|
298
|
+
function enumsPath(path2) {
|
|
299
|
+
return corePath(`enums/${path2 || ""}`);
|
|
300
|
+
}
|
|
301
|
+
function errorHandlingPath(path2) {
|
|
302
|
+
return corePath(`error-handling/${path2 || ""}`);
|
|
303
|
+
}
|
|
304
|
+
function eventsPath(path2) {
|
|
305
|
+
return corePath(`events/${path2 || ""}`);
|
|
306
|
+
}
|
|
307
|
+
function coreEnvPath(path2) {
|
|
308
|
+
return corePath(`env/${path2 || ""}`);
|
|
309
|
+
}
|
|
310
|
+
function examplesPath(type) {
|
|
311
|
+
return frameworkPath(`examples/${type || ""}`);
|
|
312
|
+
}
|
|
313
|
+
function fakerPath(path2) {
|
|
314
|
+
return corePath(`faker/${path2 || ""}`);
|
|
315
|
+
}
|
|
316
|
+
function frameworkPath(path2) {
|
|
317
|
+
return projectStoragePath(`framework/${path2 || ""}`);
|
|
318
|
+
}
|
|
319
|
+
function healthPath(path2) {
|
|
320
|
+
return corePath(`health/${path2 || ""}`);
|
|
321
|
+
}
|
|
322
|
+
function functionsPath(path2) {
|
|
323
|
+
return resourcesPath(`functions/${path2 || ""}`);
|
|
324
|
+
}
|
|
325
|
+
function gitPath(path2) {
|
|
326
|
+
return corePath(`git/${path2 || ""}`);
|
|
327
|
+
}
|
|
328
|
+
function langPath(path2) {
|
|
329
|
+
return projectPath(`lang/${path2 || ""}`);
|
|
330
|
+
}
|
|
331
|
+
function libraryEntryPath(type) {
|
|
332
|
+
return libsEntriesPath(`${type}.ts`);
|
|
333
|
+
}
|
|
334
|
+
function lintPath(path2) {
|
|
335
|
+
return corePath(`lint/${path2 || ""}`);
|
|
336
|
+
}
|
|
337
|
+
function loggingPath(path2) {
|
|
338
|
+
return corePath(`logging/${path2 || ""}`);
|
|
339
|
+
}
|
|
340
|
+
function modulesPath(path2) {
|
|
341
|
+
return resourcesPath(`modules/${path2 || ""}`);
|
|
342
|
+
}
|
|
343
|
+
function notificationsPath(path2) {
|
|
344
|
+
return corePath(`notifications/${path2 || ""}`);
|
|
345
|
+
}
|
|
346
|
+
function ormPath(path2) {
|
|
347
|
+
return corePath(`orm/${path2 || ""}`);
|
|
348
|
+
}
|
|
349
|
+
function objectsPath(path2) {
|
|
350
|
+
return corePath(`objects/${path2 || ""}`);
|
|
351
|
+
}
|
|
352
|
+
function onboardingPath(path2) {
|
|
353
|
+
return projectPath(`${path2 || "views/dashboard/onboarding"}`);
|
|
354
|
+
}
|
|
355
|
+
function packageJsonPath(type) {
|
|
356
|
+
if (type === "vue-components")
|
|
357
|
+
return frameworkPath("libs/components/vue/package.json");
|
|
358
|
+
if (type === "web-components")
|
|
359
|
+
return frameworkPath("libs/components/web/package.json");
|
|
360
|
+
return frameworkPath(`libs/${type}/package.json`);
|
|
361
|
+
}
|
|
362
|
+
function viewsPath(path2) {
|
|
363
|
+
return resourcesPath(`views/${path2 || ""}`);
|
|
364
|
+
}
|
|
365
|
+
function pathPath(path2) {
|
|
366
|
+
return corePath(`path/${path2 || ""}`);
|
|
367
|
+
}
|
|
368
|
+
function paymentsPath(path2) {
|
|
369
|
+
return corePath(`payments/${path2 || ""}`);
|
|
370
|
+
}
|
|
371
|
+
function projectPath(filePath = "") {
|
|
372
|
+
let path2 = process2.cwd();
|
|
373
|
+
while (path2.includes("storage"))
|
|
374
|
+
path2 = resolve(path2, "..");
|
|
375
|
+
return resolve(path2, filePath);
|
|
376
|
+
}
|
|
377
|
+
function projectConfigPath(path2) {
|
|
378
|
+
return projectPath(`config/${path2 || ""}`);
|
|
379
|
+
}
|
|
380
|
+
function projectStoragePath(path2) {
|
|
381
|
+
return projectPath(`storage/${path2 || ""}`);
|
|
382
|
+
}
|
|
383
|
+
function publicPath(path2) {
|
|
384
|
+
return projectPath(`public/${path2 || ""}`);
|
|
385
|
+
}
|
|
386
|
+
function pushPath(path2) {
|
|
387
|
+
return notificationsPath(`push/${path2 || ""}`);
|
|
388
|
+
}
|
|
389
|
+
function queryBuilderPath(path2) {
|
|
390
|
+
return corePath(`query-builder/${path2 || ""}`);
|
|
391
|
+
}
|
|
392
|
+
function queuePath(path2) {
|
|
393
|
+
return corePath(`queue/${path2 || ""}`);
|
|
394
|
+
}
|
|
395
|
+
function realtimePath(path2) {
|
|
396
|
+
return corePath(`realtime/${path2 || ""}`);
|
|
397
|
+
}
|
|
398
|
+
function resourcesPath(path2) {
|
|
399
|
+
return projectPath(`resources/${path2 || ""}`);
|
|
400
|
+
}
|
|
401
|
+
function replPath(path2) {
|
|
402
|
+
return corePath(`repl/${path2 || ""}`);
|
|
403
|
+
}
|
|
404
|
+
function routerPath(path2) {
|
|
405
|
+
return corePath(`router/${path2 || ""}`);
|
|
406
|
+
}
|
|
407
|
+
function routesPath(path2) {
|
|
408
|
+
return projectPath(`routes/${path2 || ""}`);
|
|
409
|
+
}
|
|
410
|
+
function searchEnginePath(path2) {
|
|
411
|
+
return corePath(`search-engine/${path2 || ""}`);
|
|
412
|
+
}
|
|
413
|
+
function settingsPath(path2) {
|
|
414
|
+
return projectPath(`${path2 || "views/dashboard/settings"}`);
|
|
415
|
+
}
|
|
416
|
+
function scriptsPath(path2) {
|
|
417
|
+
return frameworkPath(`core/scripts/${path2 || ""}`);
|
|
418
|
+
}
|
|
419
|
+
function schedulerPath(path2) {
|
|
420
|
+
return corePath(`scheduler/${path2 || ""}`);
|
|
421
|
+
}
|
|
422
|
+
function signalsPath(path2) {
|
|
423
|
+
return corePath(`signals/${path2 || ""}`);
|
|
424
|
+
}
|
|
425
|
+
function slugPath(path2) {
|
|
426
|
+
return corePath(`slug/${path2 || ""}`);
|
|
427
|
+
}
|
|
428
|
+
function smsPath(path2) {
|
|
429
|
+
return notificationsPath(`sms/${path2 || ""}`);
|
|
430
|
+
}
|
|
431
|
+
function storagePath(path2) {
|
|
432
|
+
return corePath(`storage/${path2 || ""}`);
|
|
433
|
+
}
|
|
434
|
+
function storesPath(path2) {
|
|
435
|
+
return resourcesPath(`stores/${path2 || ""}`);
|
|
436
|
+
}
|
|
437
|
+
function securityPath(path2) {
|
|
438
|
+
return corePath(`security/${path2 || ""}`);
|
|
439
|
+
}
|
|
440
|
+
function serverPath(path2) {
|
|
441
|
+
return corePath(`server/${path2 || ""}`);
|
|
442
|
+
}
|
|
443
|
+
function serverlessPath(path2) {
|
|
444
|
+
return corePath(`serverless/${path2 || ""}`);
|
|
445
|
+
}
|
|
446
|
+
function stacksPath(path2) {
|
|
447
|
+
return frameworkPath(`src/${path2 || ""}`);
|
|
448
|
+
}
|
|
449
|
+
function stringsPath(path2) {
|
|
450
|
+
return corePath(`strings/${path2 || ""}`);
|
|
451
|
+
}
|
|
452
|
+
function testingPath(path2) {
|
|
453
|
+
return corePath(`testing/${path2 || ""}`);
|
|
454
|
+
}
|
|
455
|
+
function tinkerPath(path2) {
|
|
456
|
+
return corePath(`tinker/${path2 || ""}`);
|
|
457
|
+
}
|
|
458
|
+
function testsPath(path2) {
|
|
459
|
+
return frameworkPath(`tests/${path2 || ""}`);
|
|
460
|
+
}
|
|
461
|
+
function typesPath(path2) {
|
|
462
|
+
return corePath(`types/${path2 || ""}`);
|
|
463
|
+
}
|
|
464
|
+
function uiPath(path2) {
|
|
465
|
+
return corePath(`ui/${path2 || ""}`);
|
|
466
|
+
}
|
|
467
|
+
function utilsPath(path2) {
|
|
468
|
+
return corePath(`utils/${path2 || ""}`);
|
|
469
|
+
}
|
|
470
|
+
function validationPath(path2) {
|
|
471
|
+
return corePath(`validation/${path2 || ""}`);
|
|
472
|
+
}
|
|
473
|
+
function vitePath(path2) {
|
|
474
|
+
return corePath(`vite/${path2 || ""}`);
|
|
475
|
+
}
|
|
476
|
+
function xRayPath(path2) {
|
|
477
|
+
return frameworkPath(`stacks/x-ray/${path2 || ""}`);
|
|
478
|
+
}
|
|
479
|
+
var path2 = {
|
|
480
|
+
actionsPath,
|
|
481
|
+
aiPath,
|
|
482
|
+
assetsPath,
|
|
483
|
+
relativeActionsPath,
|
|
484
|
+
aliasPath,
|
|
485
|
+
analyticsPath,
|
|
486
|
+
arraysPath,
|
|
487
|
+
appPath,
|
|
488
|
+
authPath,
|
|
489
|
+
buildEnginePath,
|
|
490
|
+
libsEntriesPath,
|
|
491
|
+
buildPath,
|
|
492
|
+
cachePath,
|
|
493
|
+
chatPath,
|
|
494
|
+
cliPath,
|
|
495
|
+
cloudPath,
|
|
496
|
+
collectionsPath,
|
|
497
|
+
componentsPath,
|
|
498
|
+
configPath,
|
|
499
|
+
projectConfigPath,
|
|
500
|
+
corePath,
|
|
501
|
+
customElementsDataPath,
|
|
502
|
+
databasePath,
|
|
503
|
+
datetimePath,
|
|
504
|
+
developmentPath,
|
|
505
|
+
desktopPath,
|
|
506
|
+
docsPath,
|
|
507
|
+
dnsPath,
|
|
508
|
+
emailPath,
|
|
509
|
+
enumsPath,
|
|
510
|
+
errorHandlingPath,
|
|
511
|
+
eventsPath,
|
|
512
|
+
coreEnvPath,
|
|
513
|
+
healthPath,
|
|
514
|
+
examplesPath,
|
|
515
|
+
fakerPath,
|
|
516
|
+
frameworkPath,
|
|
517
|
+
storagePath,
|
|
518
|
+
functionsPath,
|
|
519
|
+
gitPath,
|
|
520
|
+
langPath,
|
|
521
|
+
libsPath,
|
|
522
|
+
libraryEntryPath,
|
|
523
|
+
lintPath,
|
|
524
|
+
loggingPath,
|
|
525
|
+
modulesPath,
|
|
526
|
+
ormPath,
|
|
527
|
+
objectsPath,
|
|
528
|
+
onboardingPath,
|
|
529
|
+
notificationsPath,
|
|
530
|
+
packageJsonPath,
|
|
531
|
+
viewsPath,
|
|
532
|
+
pathPath,
|
|
533
|
+
paymentsPath,
|
|
534
|
+
projectPath,
|
|
535
|
+
projectStoragePath,
|
|
536
|
+
publicPath,
|
|
537
|
+
pushPath,
|
|
538
|
+
queryBuilderPath,
|
|
539
|
+
queuePath,
|
|
540
|
+
realtimePath,
|
|
541
|
+
resourcesPath,
|
|
542
|
+
replPath,
|
|
543
|
+
routerPath,
|
|
544
|
+
routesPath,
|
|
545
|
+
searchEnginePath,
|
|
546
|
+
schedulerPath,
|
|
547
|
+
settingsPath,
|
|
548
|
+
smsPath,
|
|
549
|
+
signalsPath,
|
|
550
|
+
slugPath,
|
|
551
|
+
scriptsPath,
|
|
552
|
+
securityPath,
|
|
553
|
+
serverPath,
|
|
554
|
+
serverlessPath,
|
|
555
|
+
stacksPath,
|
|
556
|
+
stringsPath,
|
|
557
|
+
storesPath,
|
|
558
|
+
testingPath,
|
|
559
|
+
testsPath,
|
|
560
|
+
tinkerPath,
|
|
561
|
+
typesPath,
|
|
562
|
+
uiPath,
|
|
563
|
+
utilsPath,
|
|
564
|
+
validationPath,
|
|
565
|
+
vitePath,
|
|
566
|
+
xRayPath,
|
|
567
|
+
basename,
|
|
568
|
+
delimiter,
|
|
569
|
+
dirname,
|
|
570
|
+
extname,
|
|
571
|
+
format,
|
|
572
|
+
isAbsolute,
|
|
573
|
+
join,
|
|
574
|
+
normalize,
|
|
575
|
+
normalizeString,
|
|
576
|
+
parse,
|
|
577
|
+
relative,
|
|
578
|
+
resolve,
|
|
579
|
+
sep,
|
|
580
|
+
toNamespacedPath
|
|
581
|
+
};
|
|
582
|
+
|
|
583
|
+
// src/middleware.ts
|
|
584
|
+
async function importMiddlewares(directory) {
|
|
585
|
+
const middlewares = [];
|
|
586
|
+
const files = await readdir(directory);
|
|
587
|
+
for (const file of files) {
|
|
588
|
+
const imported = await import(path2.join(directory, file));
|
|
589
|
+
middlewares.push(imported.default);
|
|
590
|
+
}
|
|
591
|
+
return middlewares;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
class Middleware {
|
|
595
|
+
name;
|
|
596
|
+
priority;
|
|
597
|
+
handle;
|
|
598
|
+
constructor(data) {
|
|
599
|
+
this.name = data.name;
|
|
600
|
+
this.priority = data.priority;
|
|
601
|
+
this.handle = data.handle;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
var readdir = promisify(fs.readdir);
|
|
605
|
+
var middlewares = await importMiddlewares(appPath("middleware"));
|
|
606
|
+
// src/request.ts
|
|
607
|
+
class Request {
|
|
608
|
+
query = {};
|
|
609
|
+
params = null;
|
|
610
|
+
addQuery(url) {
|
|
611
|
+
this.query = Object.fromEntries(url.searchParams);
|
|
612
|
+
}
|
|
613
|
+
get(element) {
|
|
614
|
+
return this.query[element];
|
|
615
|
+
}
|
|
616
|
+
all() {
|
|
617
|
+
return this.query;
|
|
618
|
+
}
|
|
619
|
+
has(element) {
|
|
620
|
+
return element in this.query;
|
|
621
|
+
}
|
|
622
|
+
isEmpty() {
|
|
623
|
+
return Object.keys(this.query).length === 0;
|
|
624
|
+
}
|
|
625
|
+
extractParamsFromRoute(routePattern, pathname) {
|
|
626
|
+
const pattern = new RegExp(`^${routePattern.replace(/:(\w+)/g, (match2, paramName) => `(?<${paramName}>\\w+)`)}\$`);
|
|
627
|
+
const match = pattern.exec(pathname);
|
|
628
|
+
if (match?.groups)
|
|
629
|
+
this.params = match?.groups;
|
|
630
|
+
}
|
|
631
|
+
getParams(key) {
|
|
632
|
+
return this.params ? this.params[key] || null : null;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
var request = new Request;
|
|
636
|
+
// src/server.ts
|
|
637
|
+
import {extname as extname2} from "path";
|
|
638
|
+
import {URL} from "url";
|
|
639
|
+
import {localUrl} from "@stacksjs/config";
|
|
640
|
+
async function serve(options = {}) {
|
|
641
|
+
const hostname = options.host || options.tunnel ? await localUrl({ type: "backend" }) : "127.0.0.1";
|
|
642
|
+
const port = options.port || 3000;
|
|
643
|
+
Bun.serve({
|
|
644
|
+
hostname,
|
|
645
|
+
port,
|
|
646
|
+
fetch(req) {
|
|
647
|
+
console.log(req);
|
|
648
|
+
return serverResponse(req);
|
|
649
|
+
}
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
async function serverResponse(req) {
|
|
653
|
+
console.log("serverResponse", req);
|
|
654
|
+
const routesList = await route.getRoutes();
|
|
655
|
+
const url = new URL(req.url);
|
|
656
|
+
const foundRoute = routesList.find((route2) => {
|
|
657
|
+
const pattern = new RegExp(`^${route2.uri.replace(/:\w+/g, "\\w+")}\$`);
|
|
658
|
+
return pattern.test(url.pathname);
|
|
659
|
+
});
|
|
660
|
+
if (!foundRoute)
|
|
661
|
+
return new Response("Not found", { status: 404 });
|
|
662
|
+
addRouteParamsAndQuery(url, foundRoute);
|
|
663
|
+
executeMiddleware(foundRoute);
|
|
664
|
+
return execute(foundRoute, req, { statusCode: foundRoute?.statusCode });
|
|
665
|
+
}
|
|
666
|
+
var addRouteParamsAndQuery = function(url, route2) {
|
|
667
|
+
if (!isObjectNotEmpty(url.searchParams))
|
|
668
|
+
request.addQuery(url);
|
|
669
|
+
request.extractParamsFromRoute(route2.uri, url.pathname);
|
|
670
|
+
};
|
|
671
|
+
var executeMiddleware = function(route2) {
|
|
672
|
+
const { middleware: middleware2 = null } = route2;
|
|
673
|
+
if (middleware2 && middlewares && isObjectNotEmpty(middlewares)) {
|
|
674
|
+
if (isString(middleware2)) {
|
|
675
|
+
const middlewareItem = middlewares.find((middlewareItem2) => {
|
|
676
|
+
return middlewareItem2.name === middleware2;
|
|
677
|
+
});
|
|
678
|
+
if (middlewareItem)
|
|
679
|
+
middlewareItem.handle();
|
|
680
|
+
} else {
|
|
681
|
+
middleware2.forEach((m) => {
|
|
682
|
+
const middlewareItem = middlewares.find((middlewareItem2) => {
|
|
683
|
+
return middlewareItem2.name === m;
|
|
684
|
+
});
|
|
685
|
+
if (middlewareItem)
|
|
686
|
+
middlewareItem.handle();
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
};
|
|
691
|
+
var execute = function(route2, request3, { statusCode }) {
|
|
692
|
+
if (!statusCode)
|
|
693
|
+
statusCode = 200;
|
|
694
|
+
if (route2?.method === "GET" && (statusCode === 301 || statusCode === 302)) {
|
|
695
|
+
const callback = String(route2.callback);
|
|
696
|
+
const response = Response.redirect(callback, statusCode);
|
|
697
|
+
return noCache(response);
|
|
698
|
+
}
|
|
699
|
+
if (route2?.method !== request3.method)
|
|
700
|
+
return new Response("Method not allowed", { status: 405 });
|
|
701
|
+
if (isString(route2.callback) && extname2(route2.callback) === ".html") {
|
|
702
|
+
try {
|
|
703
|
+
const fileContent = Bun.file(route2.callback);
|
|
704
|
+
return new Response(fileContent, { headers: { "Content-Type": "text/html" } });
|
|
705
|
+
} catch (error) {
|
|
706
|
+
return new Response("Error reading the HTML file", { status: 500 });
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
if (isString(route2.callback))
|
|
710
|
+
return new Response(route2.callback);
|
|
711
|
+
if (isFunction(route2.callback)) {
|
|
712
|
+
const result = route2.callback();
|
|
713
|
+
return new Response(JSON.stringify(result));
|
|
714
|
+
}
|
|
715
|
+
if (isObject(route2.callback))
|
|
716
|
+
return new Response(JSON.stringify(route2.callback));
|
|
717
|
+
return new Response("Unknown callback type.", { status: 500 });
|
|
718
|
+
};
|
|
719
|
+
var noCache = function(response) {
|
|
720
|
+
response.headers.set("Cache-Control", "no-store, no-cache, must-revalidate, proxy-revalidate");
|
|
721
|
+
response.headers.set("Pragma", "no-cache");
|
|
722
|
+
response.headers.set("Expires", "0");
|
|
723
|
+
return response;
|
|
724
|
+
};
|
|
725
|
+
var isString = function(val) {
|
|
726
|
+
return typeof val === "string";
|
|
727
|
+
};
|
|
728
|
+
var isObjectNotEmpty = function(obj) {
|
|
729
|
+
return Object.keys(obj).length > 0;
|
|
730
|
+
};
|
|
731
|
+
var isFunction = function(val) {
|
|
732
|
+
return typeof val === "function";
|
|
733
|
+
};
|
|
734
|
+
var isObject = function(val) {
|
|
735
|
+
return val !== null && typeof val === "object" && !Array.isArray(val);
|
|
736
|
+
};
|
|
737
|
+
// src/router.ts
|
|
738
|
+
class Router {
|
|
739
|
+
routes = [];
|
|
740
|
+
addRoute(method, uri, callback, statusCode) {
|
|
741
|
+
const name = uri.replace(/\//g, ".").replace(/:/g, "");
|
|
742
|
+
const pattern = new RegExp(`^${uri.replace(/:[a-zA-Z]+/g, (_match) => {
|
|
743
|
+
return "([a-zA-Z0-9-]+)";
|
|
744
|
+
})}\$`);
|
|
745
|
+
let routeCallback;
|
|
746
|
+
if (typeof callback === "string" || typeof callback === "object") {
|
|
747
|
+
routeCallback = () => callback;
|
|
748
|
+
} else {
|
|
749
|
+
routeCallback = callback;
|
|
750
|
+
}
|
|
751
|
+
this.routes.push({
|
|
752
|
+
name,
|
|
753
|
+
method,
|
|
754
|
+
url: uri,
|
|
755
|
+
uri,
|
|
756
|
+
callback: routeCallback,
|
|
757
|
+
pattern,
|
|
758
|
+
statusCode,
|
|
759
|
+
paramNames: []
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
get(path5, callback) {
|
|
763
|
+
this.addRoute("GET", path5, callback, 200);
|
|
764
|
+
return this;
|
|
765
|
+
}
|
|
766
|
+
post(path5, callback) {
|
|
767
|
+
this.addRoute("POST", path5, callback, 201);
|
|
768
|
+
return this;
|
|
769
|
+
}
|
|
770
|
+
view(path5, callback) {
|
|
771
|
+
this.addRoute("GET", path5, callback, 200);
|
|
772
|
+
return this;
|
|
773
|
+
}
|
|
774
|
+
redirect(path5, callback, _status) {
|
|
775
|
+
this.addRoute("GET", path5, callback, 302);
|
|
776
|
+
return this;
|
|
777
|
+
}
|
|
778
|
+
delete(path5, callback) {
|
|
779
|
+
this.addRoute("DELETE", path5, callback, 204);
|
|
780
|
+
return this;
|
|
781
|
+
}
|
|
782
|
+
patch(path5, callback) {
|
|
783
|
+
this.addRoute("PATCH", path5, callback, 202);
|
|
784
|
+
return this;
|
|
785
|
+
}
|
|
786
|
+
put(path5, callback) {
|
|
787
|
+
this.addRoute("PUT", path5, callback, 202);
|
|
788
|
+
return this;
|
|
789
|
+
}
|
|
790
|
+
group(options, callback) {
|
|
791
|
+
let cb;
|
|
792
|
+
if (typeof options === "function") {
|
|
793
|
+
cb = options;
|
|
794
|
+
options = {};
|
|
795
|
+
} else {
|
|
796
|
+
if (!callback)
|
|
797
|
+
throw new Error("Missing callback function for route group.");
|
|
798
|
+
cb = callback;
|
|
799
|
+
}
|
|
800
|
+
const { prefix = "", middleware: middleware2 = [] } = options;
|
|
801
|
+
const originalRoutes = this.routes;
|
|
802
|
+
this.routes = [];
|
|
803
|
+
cb();
|
|
804
|
+
this.routes.forEach((r) => {
|
|
805
|
+
r.uri = `${prefix}${r.uri}`;
|
|
806
|
+
if (middleware2.length)
|
|
807
|
+
r.middleware = middleware2;
|
|
808
|
+
originalRoutes.push(r);
|
|
809
|
+
return this;
|
|
810
|
+
});
|
|
811
|
+
this.routes = originalRoutes;
|
|
812
|
+
return this;
|
|
813
|
+
}
|
|
814
|
+
name(name) {
|
|
815
|
+
this.routes[this.routes.length - 1].name = name;
|
|
816
|
+
return this;
|
|
817
|
+
}
|
|
818
|
+
middleware(middleware2) {
|
|
819
|
+
this.routes[this.routes.length - 1].middleware = middleware2;
|
|
820
|
+
return this;
|
|
821
|
+
}
|
|
822
|
+
prefix(prefix) {
|
|
823
|
+
this.routes[this.routes.length - 1].prefix = prefix;
|
|
824
|
+
return this;
|
|
825
|
+
}
|
|
826
|
+
async getRoutes() {
|
|
827
|
+
await import(projectPath("routes/api.ts"));
|
|
828
|
+
return this.routes;
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
var route = new Router;
|
|
832
|
+
export {
|
|
833
|
+
serverResponse,
|
|
834
|
+
serve,
|
|
835
|
+
route,
|
|
836
|
+
request,
|
|
837
|
+
middlewares,
|
|
838
|
+
Router,
|
|
839
|
+
Request,
|
|
840
|
+
Middleware
|
|
841
|
+
};
|
package/package.json
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/router",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
5
|
-
"packageManager": "pnpm@8.6.6",
|
|
4
|
+
"version": "0.58.20",
|
|
6
5
|
"description": "The Stacks framework router.",
|
|
7
6
|
"author": "Chris Breuer",
|
|
8
7
|
"license": "MIT",
|
|
9
8
|
"funding": "https://github.com/sponsors/chrisbbreuer",
|
|
10
|
-
"homepage": "https://github.com/stacksjs/stacks/tree/main
|
|
9
|
+
"homepage": "https://github.com/stacksjs/stacks/tree/main/storage/framework/core/src/router#readme",
|
|
11
10
|
"repository": {
|
|
12
11
|
"type": "git",
|
|
13
12
|
"url": "git+https://github.com/stacksjs/stacks.git",
|
|
14
|
-
"directory": "
|
|
13
|
+
"directory": "./storage/framework/core/src/router"
|
|
15
14
|
},
|
|
16
15
|
"bugs": {
|
|
17
16
|
"url": "https://github.com/stacksjs/stacks/issues"
|
|
@@ -25,28 +24,39 @@
|
|
|
25
24
|
],
|
|
26
25
|
"exports": {
|
|
27
26
|
".": {
|
|
28
|
-
"
|
|
29
|
-
"import": "./dist/index.
|
|
27
|
+
"bun": "./src/index.ts",
|
|
28
|
+
"import": "./dist/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./*": {
|
|
31
|
+
"bun": "./src/*",
|
|
32
|
+
"import": "./dist/*"
|
|
30
33
|
}
|
|
31
34
|
},
|
|
32
|
-
"module": "dist/index.
|
|
35
|
+
"module": "dist/index.js",
|
|
33
36
|
"types": "dist/index.d.ts",
|
|
34
37
|
"contributors": [
|
|
35
|
-
"Chris Breuer <chris@
|
|
38
|
+
"Chris Breuer <chris@stacksjs.org>"
|
|
36
39
|
],
|
|
37
40
|
"files": [
|
|
38
|
-
"
|
|
39
|
-
"
|
|
41
|
+
"README.md",
|
|
42
|
+
"dist"
|
|
40
43
|
],
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "bun --bun build.ts",
|
|
46
|
+
"typecheck": "bun --bun tsc --noEmit",
|
|
47
|
+
"prepublishOnly": "bun --bun run build"
|
|
48
|
+
},
|
|
41
49
|
"peerDependencies": {
|
|
42
|
-
"
|
|
50
|
+
"@stacksjs/config": "workspace:*",
|
|
51
|
+
"unplugin-vue-router": "^0.7.0",
|
|
52
|
+
"vue-router": "^4.2.5"
|
|
43
53
|
},
|
|
44
|
-
"
|
|
45
|
-
"@stacksjs/
|
|
54
|
+
"dependencies": {
|
|
55
|
+
"@stacksjs/config": "workspace:*",
|
|
56
|
+
"unplugin-vue-router": "^0.7.0",
|
|
57
|
+
"vue-router": "^4.2.5"
|
|
46
58
|
},
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"dev": "unbuild --stub",
|
|
50
|
-
"typecheck": "tsc --noEmit"
|
|
59
|
+
"devDependencies": {
|
|
60
|
+
"@stacksjs/development": "workspace:*"
|
|
51
61
|
}
|
|
52
|
-
}
|
|
62
|
+
}
|
package/LICENSE.md
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
# MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022 Open Web Foundation
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
package/dist/index.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { HttpMethod, RouteCallback, RouteGroupOptions } from '@stacksjs/types';
|
|
2
|
-
export declare class Router {
|
|
3
|
-
private routes;
|
|
4
|
-
addRoute(method: HttpMethod, url: string, callback: RouteCallback): void;
|
|
5
|
-
group(options: RouteGroupOptions, callback: () => void): void;
|
|
6
|
-
before(callback: RouteCallback): void;
|
|
7
|
-
after(callback: RouteCallback): void;
|
|
8
|
-
handleRequest(method: HttpMethod, url: string): Promise<any>;
|
|
9
|
-
get(url: string, callback: RouteCallback): Promise<void>;
|
|
10
|
-
post(url: string, callback: RouteCallback): Promise<void>;
|
|
11
|
-
put(url: string, callback: RouteCallback): Promise<void>;
|
|
12
|
-
patch(url: string, callback: RouteCallback): Promise<void>;
|
|
13
|
-
delete(url: string, callback: RouteCallback): Promise<void>;
|
|
14
|
-
private matchRoute;
|
|
15
|
-
private extractParams;
|
|
16
|
-
private getMiddleware;
|
|
17
|
-
}
|
|
18
|
-
export declare const route: Router;
|
package/dist/index.mjs
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
export class Router {
|
|
2
|
-
constructor() {
|
|
3
|
-
this.routes = [];
|
|
4
|
-
}
|
|
5
|
-
addRoute(method, url, callback) {
|
|
6
|
-
const paramNames = [];
|
|
7
|
-
const pattern = new RegExp(`^${url.replace(/:[a-zA-Z]+/g, (match) => {
|
|
8
|
-
paramNames.push(match.slice(1));
|
|
9
|
-
return "([a-zA-Z0-9-]+)";
|
|
10
|
-
})}$`);
|
|
11
|
-
this.routes.push({ method, pattern, callback, paramNames });
|
|
12
|
-
}
|
|
13
|
-
group(options, callback) {
|
|
14
|
-
const { prefix = "", middleware = [] } = options;
|
|
15
|
-
const routes = this.routes.slice();
|
|
16
|
-
this.routes = [];
|
|
17
|
-
middleware.forEach((m) => {
|
|
18
|
-
if (m.before)
|
|
19
|
-
this.before(m.before);
|
|
20
|
-
if (m.after)
|
|
21
|
-
this.after(m.after);
|
|
22
|
-
});
|
|
23
|
-
callback();
|
|
24
|
-
this.routes = this.routes.map((r) => {
|
|
25
|
-
return {
|
|
26
|
-
...r,
|
|
27
|
-
pattern: new RegExp(`^${prefix}${r.pattern.source}$`)
|
|
28
|
-
};
|
|
29
|
-
}).concat(routes);
|
|
30
|
-
}
|
|
31
|
-
before(callback) {
|
|
32
|
-
this.routes.unshift({ method: "before", pattern: /^$/, callback, paramNames: [] });
|
|
33
|
-
}
|
|
34
|
-
after(callback) {
|
|
35
|
-
this.routes.push({ method: "after", pattern: /^$/, callback, paramNames: [] });
|
|
36
|
-
}
|
|
37
|
-
async handleRequest(method, url) {
|
|
38
|
-
const route2 = this.matchRoute(method, url);
|
|
39
|
-
if (!route2)
|
|
40
|
-
throw new Error("Route not found");
|
|
41
|
-
const params = this.extractParams(route2, url);
|
|
42
|
-
const middleware = this.getMiddleware(route2);
|
|
43
|
-
if (middleware.before)
|
|
44
|
-
await middleware.before(params);
|
|
45
|
-
const result = await route2.callback(params);
|
|
46
|
-
if (middleware.after)
|
|
47
|
-
await middleware.after(params);
|
|
48
|
-
return result;
|
|
49
|
-
}
|
|
50
|
-
async get(url, callback) {
|
|
51
|
-
this.addRoute("get", url, callback);
|
|
52
|
-
}
|
|
53
|
-
async post(url, callback) {
|
|
54
|
-
this.addRoute("post", url, callback);
|
|
55
|
-
}
|
|
56
|
-
async put(url, callback) {
|
|
57
|
-
this.addRoute("put", url, callback);
|
|
58
|
-
}
|
|
59
|
-
async patch(url, callback) {
|
|
60
|
-
this.addRoute("patch", url, callback);
|
|
61
|
-
}
|
|
62
|
-
async delete(url, callback) {
|
|
63
|
-
this.addRoute("delete", url, callback);
|
|
64
|
-
}
|
|
65
|
-
matchRoute(method, url) {
|
|
66
|
-
const route2 = this.routes.find((r) => r.method === method && r.pattern.test(url));
|
|
67
|
-
return route2 || null;
|
|
68
|
-
}
|
|
69
|
-
extractParams(route2, url) {
|
|
70
|
-
const matches = url.match(route2.pattern);
|
|
71
|
-
const params = {};
|
|
72
|
-
if (matches) {
|
|
73
|
-
route2.paramNames.forEach((name, i) => {
|
|
74
|
-
params[name] = matches[i + 1];
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
return params;
|
|
78
|
-
}
|
|
79
|
-
getMiddleware(route2) {
|
|
80
|
-
const before = this.routes.filter((r) => r.method === "before");
|
|
81
|
-
const after = this.routes.filter((r) => r.method === "after");
|
|
82
|
-
const middleware = {};
|
|
83
|
-
before.forEach((b) => {
|
|
84
|
-
if (b.pattern.test(route2.pattern.source))
|
|
85
|
-
middleware.before = b.callback;
|
|
86
|
-
});
|
|
87
|
-
after.forEach((a) => {
|
|
88
|
-
if (a.pattern.test(route2.pattern.source))
|
|
89
|
-
middleware.after = a.callback;
|
|
90
|
-
});
|
|
91
|
-
return middleware;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
export const route = new Router();
|