@hyperspan/framework 0.1.4 → 0.1.5
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/server.d.ts +14 -24
- package/dist/server.js +4 -5
- package/package.json +1 -1
- package/src/server.ts +20 -17
package/dist/server.d.ts
CHANGED
|
@@ -20,41 +20,31 @@ export declare const IS_PROD: boolean;
|
|
|
20
20
|
*/
|
|
21
21
|
export type THSResponseTypes = TmplHtml | Response | string | null;
|
|
22
22
|
export type THSRouteHandler = (context: Context) => THSResponseTypes | Promise<THSResponseTypes>;
|
|
23
|
+
export type THSRoute = {
|
|
24
|
+
_kind: "hsRoute";
|
|
25
|
+
get: (handler: THSRouteHandler) => THSRoute;
|
|
26
|
+
post: (handler: THSRouteHandler) => THSRoute;
|
|
27
|
+
put: (handler: THSRouteHandler) => THSRoute;
|
|
28
|
+
delete: (handler: THSRouteHandler) => THSRoute;
|
|
29
|
+
patch: (handler: THSRouteHandler) => THSRoute;
|
|
30
|
+
run: (method: string, context: Context) => Promise<Response>;
|
|
31
|
+
};
|
|
23
32
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* Route handlers should return a Response or TmplHtml object
|
|
33
|
+
* Define a route that can handle a direct HTTP request.
|
|
34
|
+
* Route handlers should return a TmplHtml or Response object
|
|
27
35
|
*/
|
|
28
|
-
export declare function createRoute(handler?: THSRouteHandler):
|
|
29
|
-
_kind: string;
|
|
30
|
-
get(handler: THSRouteHandler): any;
|
|
31
|
-
post(handler: THSRouteHandler): any;
|
|
32
|
-
put(handler: THSRouteHandler): any;
|
|
33
|
-
delete(handler: THSRouteHandler): any;
|
|
34
|
-
patch(handler: THSRouteHandler): any;
|
|
35
|
-
run(method: string, context: Context): Promise<Response>;
|
|
36
|
-
};
|
|
37
|
-
export type THSRoute = ReturnType<typeof createRoute>;
|
|
36
|
+
export declare function createRoute(handler?: THSRouteHandler): THSRoute;
|
|
38
37
|
/**
|
|
39
38
|
* Create new API Route
|
|
40
39
|
* API Route handlers should return a JSON object or a Response
|
|
41
40
|
*/
|
|
42
|
-
export declare function createAPIRoute(handler?: THSRouteHandler):
|
|
43
|
-
_kind: string;
|
|
44
|
-
get(handler: THSRouteHandler): any;
|
|
45
|
-
post(handler: THSRouteHandler): any;
|
|
46
|
-
put(handler: THSRouteHandler): any;
|
|
47
|
-
delete(handler: THSRouteHandler): any;
|
|
48
|
-
patch(handler: THSRouteHandler): any;
|
|
49
|
-
run(method: string, context: Context): Promise<Response>;
|
|
50
|
-
};
|
|
51
|
-
export type THSAPIRoute = ReturnType<typeof createAPIRoute>;
|
|
41
|
+
export declare function createAPIRoute(handler?: THSRouteHandler): THSRoute;
|
|
52
42
|
/**
|
|
53
43
|
* Get a Hyperspan runnable route from a module import
|
|
54
44
|
* @throws Error if no runnable route found
|
|
55
45
|
*/
|
|
56
46
|
export declare function getRunnableRoute(route: unknown): THSRoute;
|
|
57
|
-
export declare function
|
|
47
|
+
export declare function isRunnableRoute(route: unknown): boolean;
|
|
58
48
|
export type THSServerConfig = {
|
|
59
49
|
appDir: string;
|
|
60
50
|
staticFileRoot: string;
|
package/dist/server.js
CHANGED
|
@@ -1931,7 +1931,7 @@ function createAPIRoute(handler) {
|
|
|
1931
1931
|
return api;
|
|
1932
1932
|
}
|
|
1933
1933
|
function getRunnableRoute(route) {
|
|
1934
|
-
if (
|
|
1934
|
+
if (isRunnableRoute(route)) {
|
|
1935
1935
|
return route;
|
|
1936
1936
|
}
|
|
1937
1937
|
const kind = typeof route;
|
|
@@ -1943,7 +1943,7 @@ function getRunnableRoute(route) {
|
|
|
1943
1943
|
}
|
|
1944
1944
|
throw new Error('Route not runnable. Use "export default createRoute()" to create a Hyperspan route.');
|
|
1945
1945
|
}
|
|
1946
|
-
function
|
|
1946
|
+
function isRunnableRoute(route) {
|
|
1947
1947
|
return typeof route === "object" && "run" in route;
|
|
1948
1948
|
}
|
|
1949
1949
|
async function showErrorReponse(context, err) {
|
|
@@ -2024,8 +2024,7 @@ async function createServer(config) {
|
|
|
2024
2024
|
const fullRouteFile = join(CWD, route.file);
|
|
2025
2025
|
const routePattern = normalizePath(route.route);
|
|
2026
2026
|
routeMap.push({ route: routePattern, file: route.file });
|
|
2027
|
-
|
|
2028
|
-
app.all(routePattern, createRouteFromModule(routeModule));
|
|
2027
|
+
app.all(routePattern, createRouteFromModule(await import(fullRouteFile)));
|
|
2029
2028
|
}
|
|
2030
2029
|
if (routeMap.length === 0) {
|
|
2031
2030
|
app.get("/", (context) => {
|
|
@@ -2085,7 +2084,7 @@ function normalizePath(urlPath) {
|
|
|
2085
2084
|
}
|
|
2086
2085
|
export {
|
|
2087
2086
|
normalizePath,
|
|
2088
|
-
|
|
2087
|
+
isRunnableRoute,
|
|
2089
2088
|
getRunnableRoute,
|
|
2090
2089
|
createServer,
|
|
2091
2090
|
createRouteFromModule,
|
package/package.json
CHANGED
package/src/server.ts
CHANGED
|
@@ -3,11 +3,9 @@ import { basename, extname, join } from 'node:path';
|
|
|
3
3
|
import { TmplHtml, html, renderStream, renderAsync, render } from '@hyperspan/html';
|
|
4
4
|
import { isbot } from 'isbot';
|
|
5
5
|
import { buildClientJS, buildClientCSS } from './assets';
|
|
6
|
-
import { Hono } from 'hono';
|
|
6
|
+
import { Hono, type Context } from 'hono';
|
|
7
7
|
import { serveStatic } from 'hono/bun';
|
|
8
|
-
import type { Context, Handler } from 'hono';
|
|
9
8
|
import { HTTPException } from 'hono/http-exception';
|
|
10
|
-
import { create } from 'node:domain';
|
|
11
9
|
|
|
12
10
|
export const IS_PROD = process.env.NODE_ENV === 'production';
|
|
13
11
|
const CWD = process.cwd();
|
|
@@ -18,19 +16,28 @@ const CWD = process.cwd();
|
|
|
18
16
|
export type THSResponseTypes = TmplHtml | Response | string | null;
|
|
19
17
|
export type THSRouteHandler = (context: Context) => THSResponseTypes | Promise<THSResponseTypes>;
|
|
20
18
|
|
|
19
|
+
export type THSRoute = {
|
|
20
|
+
_kind: 'hsRoute';
|
|
21
|
+
get: (handler: THSRouteHandler) => THSRoute;
|
|
22
|
+
post: (handler: THSRouteHandler) => THSRoute;
|
|
23
|
+
put: (handler: THSRouteHandler) => THSRoute;
|
|
24
|
+
delete: (handler: THSRouteHandler) => THSRoute;
|
|
25
|
+
patch: (handler: THSRouteHandler) => THSRoute;
|
|
26
|
+
run: (method: string, context: Context) => Promise<Response>;
|
|
27
|
+
};
|
|
28
|
+
|
|
21
29
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* Route handlers should return a Response or TmplHtml object
|
|
30
|
+
* Define a route that can handle a direct HTTP request.
|
|
31
|
+
* Route handlers should return a TmplHtml or Response object
|
|
25
32
|
*/
|
|
26
|
-
export function createRoute(handler?: THSRouteHandler) {
|
|
33
|
+
export function createRoute(handler?: THSRouteHandler): THSRoute {
|
|
27
34
|
let _handlers: Record<string, THSRouteHandler> = {};
|
|
28
35
|
|
|
29
36
|
if (handler) {
|
|
30
37
|
_handlers['GET'] = handler;
|
|
31
38
|
}
|
|
32
39
|
|
|
33
|
-
const api = {
|
|
40
|
+
const api: THSRoute = {
|
|
34
41
|
_kind: 'hsRoute',
|
|
35
42
|
get(handler: THSRouteHandler) {
|
|
36
43
|
_handlers['GET'] = handler;
|
|
@@ -95,20 +102,19 @@ export function createRoute(handler?: THSRouteHandler) {
|
|
|
95
102
|
|
|
96
103
|
return api;
|
|
97
104
|
}
|
|
98
|
-
export type THSRoute = ReturnType<typeof createRoute>;
|
|
99
105
|
|
|
100
106
|
/**
|
|
101
107
|
* Create new API Route
|
|
102
108
|
* API Route handlers should return a JSON object or a Response
|
|
103
109
|
*/
|
|
104
|
-
export function createAPIRoute(handler?: THSRouteHandler) {
|
|
110
|
+
export function createAPIRoute(handler?: THSRouteHandler): THSRoute {
|
|
105
111
|
let _handlers: Record<string, THSRouteHandler> = {};
|
|
106
112
|
|
|
107
113
|
if (handler) {
|
|
108
114
|
_handlers['GET'] = handler;
|
|
109
115
|
}
|
|
110
116
|
|
|
111
|
-
const api = {
|
|
117
|
+
const api: THSRoute = {
|
|
112
118
|
_kind: 'hsRoute',
|
|
113
119
|
get(handler: THSRouteHandler) {
|
|
114
120
|
_handlers['GET'] = handler;
|
|
@@ -168,7 +174,6 @@ export function createAPIRoute(handler?: THSRouteHandler) {
|
|
|
168
174
|
|
|
169
175
|
return api;
|
|
170
176
|
}
|
|
171
|
-
export type THSAPIRoute = ReturnType<typeof createAPIRoute>;
|
|
172
177
|
|
|
173
178
|
/**
|
|
174
179
|
* Get a Hyperspan runnable route from a module import
|
|
@@ -176,7 +181,7 @@ export type THSAPIRoute = ReturnType<typeof createAPIRoute>;
|
|
|
176
181
|
*/
|
|
177
182
|
export function getRunnableRoute(route: unknown): THSRoute {
|
|
178
183
|
// Runnable already? Just return it
|
|
179
|
-
if (
|
|
184
|
+
if (isRunnableRoute(route)) {
|
|
180
185
|
return route as THSRoute;
|
|
181
186
|
}
|
|
182
187
|
|
|
@@ -199,7 +204,7 @@ export function getRunnableRoute(route: unknown): THSRoute {
|
|
|
199
204
|
);
|
|
200
205
|
}
|
|
201
206
|
|
|
202
|
-
export function
|
|
207
|
+
export function isRunnableRoute(route: unknown): boolean {
|
|
203
208
|
// @ts-ignore
|
|
204
209
|
return typeof route === 'object' && 'run' in route;
|
|
205
210
|
}
|
|
@@ -337,9 +342,7 @@ export async function createServer(config: THSServerConfig): Promise<Hono> {
|
|
|
337
342
|
routeMap.push({ route: routePattern, file: route.file });
|
|
338
343
|
|
|
339
344
|
// Import route
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
app.all(routePattern, createRouteFromModule(routeModule));
|
|
345
|
+
app.all(routePattern, createRouteFromModule(await import(fullRouteFile)));
|
|
343
346
|
}
|
|
344
347
|
|
|
345
348
|
// Help route if no routes found
|