rouzer 1.3.0 → 1.4.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 +13 -1
- package/dist/server/types.d.ts +20 -18
- package/dist/types.d.ts +0 -1
- package/package.json +1 -1
package/dist/server/router.js
CHANGED
|
@@ -47,6 +47,11 @@ class RouterObject extends MiddlewareChain {
|
|
|
47
47
|
};
|
|
48
48
|
}),
|
|
49
49
|
}));
|
|
50
|
+
const addDebugHeaders = config.debug
|
|
51
|
+
? (context, route) => {
|
|
52
|
+
context.setHeader('X-Route-Name', route.name);
|
|
53
|
+
}
|
|
54
|
+
: null;
|
|
50
55
|
return super.use(async function (context) {
|
|
51
56
|
const request = context.request;
|
|
52
57
|
const origin = request.headers.get('Origin');
|
|
@@ -60,7 +65,9 @@ class RouterObject extends MiddlewareChain {
|
|
|
60
65
|
'GET';
|
|
61
66
|
}
|
|
62
67
|
for (const route of routes) {
|
|
63
|
-
const props = route.methods
|
|
68
|
+
const props = route.methods.hasOwnProperty(method)
|
|
69
|
+
? route.methods[method]
|
|
70
|
+
: route.methods.ALL;
|
|
64
71
|
if (!props) {
|
|
65
72
|
continue;
|
|
66
73
|
}
|
|
@@ -94,6 +101,7 @@ class RouterObject extends MiddlewareChain {
|
|
|
94
101
|
if (schema.path) {
|
|
95
102
|
const error = parsePathParams(context, enableStringParsing(schema.path), match.params);
|
|
96
103
|
if (error) {
|
|
104
|
+
addDebugHeaders?.(context, route);
|
|
97
105
|
return httpClientError(error, 'Invalid path parameter', config);
|
|
98
106
|
}
|
|
99
107
|
}
|
|
@@ -103,22 +111,26 @@ class RouterObject extends MiddlewareChain {
|
|
|
103
111
|
if (schema.headers) {
|
|
104
112
|
const error = parseHeaders(context, enableStringParsing(schema.headers));
|
|
105
113
|
if (error) {
|
|
114
|
+
addDebugHeaders?.(context, route);
|
|
106
115
|
return httpClientError(error, 'Invalid request headers', config);
|
|
107
116
|
}
|
|
108
117
|
}
|
|
109
118
|
if (schema.query) {
|
|
110
119
|
const error = parseQueryString(context, enableStringParsing(schema.query));
|
|
111
120
|
if (error) {
|
|
121
|
+
addDebugHeaders?.(context, route);
|
|
112
122
|
return httpClientError(error, 'Invalid query string', config);
|
|
113
123
|
}
|
|
114
124
|
}
|
|
115
125
|
if (schema.body) {
|
|
116
126
|
const error = await parseRequestBody(context, schema.body);
|
|
117
127
|
if (error) {
|
|
128
|
+
addDebugHeaders?.(context, route);
|
|
118
129
|
return httpClientError(error, 'Invalid request body', config);
|
|
119
130
|
}
|
|
120
131
|
}
|
|
121
132
|
const result = await handler(context);
|
|
133
|
+
addDebugHeaders?.(context, route);
|
|
122
134
|
if (result instanceof Response) {
|
|
123
135
|
return result;
|
|
124
136
|
}
|
package/dist/server/types.d.ts
CHANGED
|
@@ -1,35 +1,37 @@
|
|
|
1
1
|
import type { Params } from '@remix-run/route-pattern';
|
|
2
2
|
import type { AnyMiddlewareChain, MiddlewareChain, MiddlewareContext } from 'alien-middleware';
|
|
3
3
|
import type * as z from 'zod/mini';
|
|
4
|
-
import type { InferRouteResponse,
|
|
4
|
+
import type { InferRouteResponse, Promisable, Routes, RouteSchema } from '../types.js';
|
|
5
5
|
type RequestContext<TMiddleware extends AnyMiddlewareChain> = MiddlewareContext<TMiddleware>;
|
|
6
6
|
type RouteRequestHandler<TMiddleware extends AnyMiddlewareChain, TArgs extends object, TResult> = (context: RequestContext<TMiddleware> & TArgs) => Promisable<TResult | Response>;
|
|
7
|
-
type InferRouteRequestHandler<TMiddleware extends AnyMiddlewareChain,
|
|
8
|
-
path:
|
|
7
|
+
type InferRouteRequestHandler<TMiddleware extends AnyMiddlewareChain, TSchema extends RouteSchema, TMethod extends string, TPath extends string> = TMethod extends 'GET' ? RouteRequestHandler<TMiddleware, {
|
|
8
|
+
path: TSchema extends {
|
|
9
9
|
path: any;
|
|
10
|
-
} ? z.infer<
|
|
11
|
-
query:
|
|
10
|
+
} ? z.infer<TSchema['path']> : Params<TPath>;
|
|
11
|
+
query: TSchema extends {
|
|
12
12
|
query: any;
|
|
13
|
-
} ? z.infer<
|
|
14
|
-
headers:
|
|
13
|
+
} ? z.infer<TSchema['query']> : undefined;
|
|
14
|
+
headers: TSchema extends {
|
|
15
15
|
headers: any;
|
|
16
|
-
} ? z.infer<
|
|
17
|
-
}, InferRouteResponse<
|
|
18
|
-
path:
|
|
16
|
+
} ? z.infer<TSchema['headers']> : undefined;
|
|
17
|
+
}, InferRouteResponse<TSchema>> : RouteRequestHandler<TMiddleware, {
|
|
18
|
+
path: TSchema extends {
|
|
19
19
|
path: any;
|
|
20
|
-
} ? z.infer<
|
|
21
|
-
body:
|
|
20
|
+
} ? z.infer<TSchema['path']> : Params<TPath>;
|
|
21
|
+
body: TSchema extends {
|
|
22
22
|
body: any;
|
|
23
|
-
} ? z.infer<
|
|
24
|
-
headers:
|
|
23
|
+
} ? z.infer<TSchema['body']> : undefined;
|
|
24
|
+
headers: TSchema extends {
|
|
25
25
|
headers: any;
|
|
26
|
-
} ? z.infer<
|
|
27
|
-
}, InferRouteResponse<
|
|
26
|
+
} ? z.infer<TSchema['headers']> : undefined;
|
|
27
|
+
}, InferRouteResponse<TSchema>>;
|
|
28
28
|
export type RouteRequestHandlerMap<TRoutes extends Routes = Routes, TMiddleware extends AnyMiddlewareChain = MiddlewareChain> = {
|
|
29
29
|
[K in keyof TRoutes]: {
|
|
30
|
-
[
|
|
30
|
+
[TMethod in keyof TRoutes[K]['methods']]: InferRouteRequestHandler<TMiddleware, Extract<TRoutes[K]['methods'][TMethod], RouteSchema>, Extract<TMethod, string>, TRoutes[K]['path']['source']>;
|
|
31
31
|
} & {
|
|
32
|
-
OPTIONS?: RouteRequestHandler<TMiddleware, {
|
|
32
|
+
OPTIONS?: RouteRequestHandler<TMiddleware, {
|
|
33
|
+
path: Params<TRoutes[K]['path']['source']>;
|
|
34
|
+
}, void>;
|
|
33
35
|
};
|
|
34
36
|
};
|
|
35
37
|
export {};
|
package/dist/types.d.ts
CHANGED