rouzer 1.2.2 → 1.3.0
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/router.js +40 -20
- package/dist/server/types.d.ts +2 -0
- package/package.json +1 -1
package/dist/server/router.js
CHANGED
|
@@ -29,10 +29,24 @@ class RouterObject extends MiddlewareChain {
|
|
|
29
29
|
return handler;
|
|
30
30
|
}
|
|
31
31
|
/** @internal */
|
|
32
|
-
useRoutes(
|
|
32
|
+
useRoutes(routeSchemas, handlers) {
|
|
33
33
|
const { config, basePath } = this;
|
|
34
|
-
const
|
|
35
|
-
|
|
34
|
+
const routes = Object.entries(routeSchemas).map(([name, route]) => ({
|
|
35
|
+
name,
|
|
36
|
+
path: basePath
|
|
37
|
+
? new RoutePattern(route.path.source.replace(/^\/?/, basePath))
|
|
38
|
+
: route.path,
|
|
39
|
+
methods: mapValues(route.methods, (schema, method) => {
|
|
40
|
+
const handler = handlers[name][method];
|
|
41
|
+
if (!handler && config.debug) {
|
|
42
|
+
console.error(`Handler missing for route: ${method} ${name}`);
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
schema,
|
|
46
|
+
handler,
|
|
47
|
+
};
|
|
48
|
+
}),
|
|
49
|
+
}));
|
|
36
50
|
return super.use(async function (context) {
|
|
37
51
|
const request = context.request;
|
|
38
52
|
const origin = request.headers.get('Origin');
|
|
@@ -45,21 +59,27 @@ class RouterObject extends MiddlewareChain {
|
|
|
45
59
|
request.headers.get('Access-Control-Request-Method')?.toUpperCase() ??
|
|
46
60
|
'GET';
|
|
47
61
|
}
|
|
48
|
-
for (
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
if (!route) {
|
|
62
|
+
for (const route of routes) {
|
|
63
|
+
const props = route.methods[method] ?? route.methods.ALL;
|
|
64
|
+
if (!props) {
|
|
52
65
|
continue;
|
|
53
66
|
}
|
|
54
|
-
const
|
|
55
|
-
if (!
|
|
67
|
+
const { schema, handler } = props;
|
|
68
|
+
if (!handler) {
|
|
56
69
|
continue;
|
|
57
70
|
}
|
|
58
|
-
const
|
|
59
|
-
if (!
|
|
71
|
+
const match = route.path.match(url);
|
|
72
|
+
if (!match) {
|
|
60
73
|
continue;
|
|
61
74
|
}
|
|
62
75
|
if (isPreflight) {
|
|
76
|
+
const optionsHandler = handlers[route.name].OPTIONS;
|
|
77
|
+
if (optionsHandler) {
|
|
78
|
+
const response = await optionsHandler(context);
|
|
79
|
+
if (response) {
|
|
80
|
+
return response;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
63
83
|
return new Response(null, {
|
|
64
84
|
headers: {
|
|
65
85
|
'Access-Control-Allow-Origin': origin ?? '',
|
|
@@ -71,8 +91,8 @@ class RouterObject extends MiddlewareChain {
|
|
|
71
91
|
if (origin) {
|
|
72
92
|
context.setHeader('Access-Control-Allow-Origin', origin);
|
|
73
93
|
}
|
|
74
|
-
if (
|
|
75
|
-
const error = parsePathParams(context, enableStringParsing(
|
|
94
|
+
if (schema.path) {
|
|
95
|
+
const error = parsePathParams(context, enableStringParsing(schema.path), match.params);
|
|
76
96
|
if (error) {
|
|
77
97
|
return httpClientError(error, 'Invalid path parameter', config);
|
|
78
98
|
}
|
|
@@ -80,25 +100,25 @@ class RouterObject extends MiddlewareChain {
|
|
|
80
100
|
else {
|
|
81
101
|
context.path = match.params;
|
|
82
102
|
}
|
|
83
|
-
if (
|
|
84
|
-
const error = parseHeaders(context, enableStringParsing(
|
|
103
|
+
if (schema.headers) {
|
|
104
|
+
const error = parseHeaders(context, enableStringParsing(schema.headers));
|
|
85
105
|
if (error) {
|
|
86
106
|
return httpClientError(error, 'Invalid request headers', config);
|
|
87
107
|
}
|
|
88
108
|
}
|
|
89
|
-
if (
|
|
90
|
-
const error = parseQueryString(context, enableStringParsing(
|
|
109
|
+
if (schema.query) {
|
|
110
|
+
const error = parseQueryString(context, enableStringParsing(schema.query));
|
|
91
111
|
if (error) {
|
|
92
112
|
return httpClientError(error, 'Invalid query string', config);
|
|
93
113
|
}
|
|
94
114
|
}
|
|
95
|
-
if (
|
|
96
|
-
const error = await parseRequestBody(context,
|
|
115
|
+
if (schema.body) {
|
|
116
|
+
const error = await parseRequestBody(context, schema.body);
|
|
97
117
|
if (error) {
|
|
98
118
|
return httpClientError(error, 'Invalid request body', config);
|
|
99
119
|
}
|
|
100
120
|
}
|
|
101
|
-
const result = await
|
|
121
|
+
const result = await handler(context);
|
|
102
122
|
if (result instanceof Response) {
|
|
103
123
|
return result;
|
|
104
124
|
}
|
package/dist/server/types.d.ts
CHANGED
|
@@ -28,6 +28,8 @@ type InferRouteRequestHandler<TMiddleware extends AnyMiddlewareChain, T, P exten
|
|
|
28
28
|
export type RouteRequestHandlerMap<TRoutes extends Routes = Routes, TMiddleware extends AnyMiddlewareChain = MiddlewareChain> = {
|
|
29
29
|
[K in keyof TRoutes]: {
|
|
30
30
|
[M in keyof TRoutes[K]['methods']]: InferRouteRequestHandler<TMiddleware, TRoutes[K]['methods'][M], TRoutes[K]['path']['source']>;
|
|
31
|
+
} & {
|
|
32
|
+
OPTIONS?: RouteRequestHandler<TMiddleware, {}, void>;
|
|
31
33
|
};
|
|
32
34
|
};
|
|
33
35
|
export {};
|