rouzer 1.2.1 → 1.2.3
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 +33 -23
- 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,17 @@ 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 (!
|
|
60
|
-
if (config.debug) {
|
|
61
|
-
throw new Error(`Handler not found for route: ${keys[i]} ${method}`);
|
|
62
|
-
}
|
|
71
|
+
const match = route.path.match(url);
|
|
72
|
+
if (!match) {
|
|
63
73
|
continue;
|
|
64
74
|
}
|
|
65
75
|
if (isPreflight) {
|
|
@@ -74,8 +84,8 @@ class RouterObject extends MiddlewareChain {
|
|
|
74
84
|
if (origin) {
|
|
75
85
|
context.setHeader('Access-Control-Allow-Origin', origin);
|
|
76
86
|
}
|
|
77
|
-
if (
|
|
78
|
-
const error = parsePathParams(context, enableStringParsing(
|
|
87
|
+
if (schema.path) {
|
|
88
|
+
const error = parsePathParams(context, enableStringParsing(schema.path), match.params);
|
|
79
89
|
if (error) {
|
|
80
90
|
return httpClientError(error, 'Invalid path parameter', config);
|
|
81
91
|
}
|
|
@@ -83,25 +93,25 @@ class RouterObject extends MiddlewareChain {
|
|
|
83
93
|
else {
|
|
84
94
|
context.path = match.params;
|
|
85
95
|
}
|
|
86
|
-
if (
|
|
87
|
-
const error = parseHeaders(context, enableStringParsing(
|
|
96
|
+
if (schema.headers) {
|
|
97
|
+
const error = parseHeaders(context, enableStringParsing(schema.headers));
|
|
88
98
|
if (error) {
|
|
89
99
|
return httpClientError(error, 'Invalid request headers', config);
|
|
90
100
|
}
|
|
91
101
|
}
|
|
92
|
-
if (
|
|
93
|
-
const error = parseQueryString(context, enableStringParsing(
|
|
102
|
+
if (schema.query) {
|
|
103
|
+
const error = parseQueryString(context, enableStringParsing(schema.query));
|
|
94
104
|
if (error) {
|
|
95
105
|
return httpClientError(error, 'Invalid query string', config);
|
|
96
106
|
}
|
|
97
107
|
}
|
|
98
|
-
if (
|
|
99
|
-
const error = await parseRequestBody(context,
|
|
108
|
+
if (schema.body) {
|
|
109
|
+
const error = await parseRequestBody(context, schema.body);
|
|
100
110
|
if (error) {
|
|
101
111
|
return httpClientError(error, 'Invalid request body', config);
|
|
102
112
|
}
|
|
103
113
|
}
|
|
104
|
-
const result = await
|
|
114
|
+
const result = await handler(context);
|
|
105
115
|
if (result instanceof Response) {
|
|
106
116
|
return result;
|
|
107
117
|
}
|