@stacksjs/router 0.58.50 → 0.58.52
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +3 -4
- package/package.json +1 -1
- package/src/router.ts +11 -11
- package/src/server.ts +4 -4
package/dist/index.js
CHANGED
|
@@ -159,9 +159,8 @@ var request = new Request;
|
|
|
159
159
|
// src/server.ts
|
|
160
160
|
import {extname as extname2} from "path";
|
|
161
161
|
import {URL} from "url";
|
|
162
|
-
import {localUrl} from "@stacksjs/config";
|
|
163
162
|
async function serve(options = {}) {
|
|
164
|
-
const hostname = options.host ||
|
|
163
|
+
const hostname = options.host || "localhost";
|
|
165
164
|
const port = options.port || 3000;
|
|
166
165
|
Bun.serve({
|
|
167
166
|
hostname,
|
|
@@ -173,7 +172,7 @@ async function serve(options = {}) {
|
|
|
173
172
|
});
|
|
174
173
|
}
|
|
175
174
|
async function serverResponse(req) {
|
|
176
|
-
console.log("serverResponse", req);
|
|
175
|
+
console.log("serverResponse", JSON.stringify(req));
|
|
177
176
|
const routesList = await route.getRoutes();
|
|
178
177
|
const url = new URL(req.url);
|
|
179
178
|
const foundRoute = routesList.find((route2) => {
|
|
@@ -181,7 +180,7 @@ async function serverResponse(req) {
|
|
|
181
180
|
return pattern.test(url.pathname);
|
|
182
181
|
});
|
|
183
182
|
if (!foundRoute)
|
|
184
|
-
return new Response("
|
|
183
|
+
return new Response("Pretty 404 page coming soon", { status: 404 });
|
|
185
184
|
addRouteParamsAndQuery(url, foundRoute);
|
|
186
185
|
executeMiddleware(foundRoute);
|
|
187
186
|
return execute(foundRoute, req, { statusCode: foundRoute?.statusCode });
|
package/package.json
CHANGED
package/src/router.ts
CHANGED
|
@@ -2,17 +2,17 @@ import type { RedirectCode, Route, RouteGroupOptions, StatusCode } from '@stacks
|
|
|
2
2
|
import { projectPath } from '@stacksjs/path'
|
|
3
3
|
|
|
4
4
|
export interface RouterInterface {
|
|
5
|
-
get(url: Route['url'], callback: Route['callback'])
|
|
6
|
-
post(url: Route['url'], callback: Route['callback'])
|
|
7
|
-
view(url: Route['url'], callback: Route['callback'])
|
|
8
|
-
redirect(url: Route['url'], callback: Route['callback'], status?: RedirectCode)
|
|
9
|
-
delete(url: Route['url'], callback: Route['callback'])
|
|
10
|
-
patch(url: Route['url'], callback: Route['callback'])
|
|
11
|
-
put(url: Route['url'], callback: Route['callback'])
|
|
12
|
-
group(options: RouteGroupOptions, callback: () => void)
|
|
13
|
-
name(name: string)
|
|
14
|
-
middleware(middleware: Route['middleware'])
|
|
15
|
-
getRoutes()
|
|
5
|
+
get: (url: Route['url'], callback: Route['callback']) => this
|
|
6
|
+
post: (url: Route['url'], callback: Route['callback']) => this
|
|
7
|
+
view: (url: Route['url'], callback: Route['callback']) => this
|
|
8
|
+
redirect: (url: Route['url'], callback: Route['callback'], status?: RedirectCode) => this
|
|
9
|
+
delete: (url: Route['url'], callback: Route['callback']) => this
|
|
10
|
+
patch: (url: Route['url'], callback: Route['callback']) => this
|
|
11
|
+
put: (url: Route['url'], callback: Route['callback']) => this
|
|
12
|
+
group: (options: RouteGroupOptions, callback: () => void) => this
|
|
13
|
+
name: (name: string) => this
|
|
14
|
+
middleware: (middleware: Route['middleware']) => this
|
|
15
|
+
getRoutes: () => Promise<Route[]>
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
export class Router implements RouterInterface {
|
package/src/server.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { extname } from 'node:path'
|
|
2
2
|
import { URL } from 'node:url'
|
|
3
3
|
import type { MiddlewareType, Route, StatusCode } from '@stacksjs/types'
|
|
4
|
-
import { localUrl } from '@stacksjs/config'
|
|
5
4
|
import { middlewares } from './middleware'
|
|
6
5
|
import { request } from './request'
|
|
7
6
|
import { route } from '.'
|
|
@@ -13,7 +12,8 @@ interface ServeOptions {
|
|
|
13
12
|
}
|
|
14
13
|
|
|
15
14
|
export async function serve(options: ServeOptions = {}) {
|
|
16
|
-
|
|
15
|
+
// maybe make use of localUrl({ type: 'backend' }) here & options.tunnel
|
|
16
|
+
const hostname = options.host || 'localhost'
|
|
17
17
|
const port = options.port || 3000
|
|
18
18
|
|
|
19
19
|
Bun.serve({
|
|
@@ -30,7 +30,7 @@ export async function serve(options: ServeOptions = {}) {
|
|
|
30
30
|
|
|
31
31
|
export async function serverResponse(req: Request) {
|
|
32
32
|
// eslint-disable-next-line no-console
|
|
33
|
-
console.log('serverResponse', req)
|
|
33
|
+
console.log('serverResponse', JSON.stringify(req))
|
|
34
34
|
const routesList: Route[] = await route.getRoutes()
|
|
35
35
|
const url = new URL(req.url)
|
|
36
36
|
|
|
@@ -44,7 +44,7 @@ export async function serverResponse(req: Request) {
|
|
|
44
44
|
// return new Response('')
|
|
45
45
|
|
|
46
46
|
if (!foundRoute)
|
|
47
|
-
return new Response('
|
|
47
|
+
return new Response('Pretty 404 page coming soon', { status: 404 }) // TODO: create a pretty 404 page
|
|
48
48
|
|
|
49
49
|
addRouteParamsAndQuery(url, foundRoute)
|
|
50
50
|
executeMiddleware(foundRoute)
|