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.
@@ -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[method] ?? route.methods.ALL;
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
  }
@@ -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, MutationRouteSchema, Promisable, QueryRouteSchema, Routes } from '../types.js';
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, T, P extends string> = T extends QueryRouteSchema ? RouteRequestHandler<TMiddleware, {
8
- path: T extends {
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<T['path']> : Params<P>;
11
- query: T extends {
10
+ } ? z.infer<TSchema['path']> : Params<TPath>;
11
+ query: TSchema extends {
12
12
  query: any;
13
- } ? z.infer<T['query']> : undefined;
14
- headers: T extends {
13
+ } ? z.infer<TSchema['query']> : undefined;
14
+ headers: TSchema extends {
15
15
  headers: any;
16
- } ? z.infer<T['headers']> : undefined;
17
- }, InferRouteResponse<T>> : T extends MutationRouteSchema ? RouteRequestHandler<TMiddleware, {
18
- path: T extends {
16
+ } ? z.infer<TSchema['headers']> : undefined;
17
+ }, InferRouteResponse<TSchema>> : RouteRequestHandler<TMiddleware, {
18
+ path: TSchema extends {
19
19
  path: any;
20
- } ? z.infer<T['path']> : Params<P>;
21
- body: T extends {
20
+ } ? z.infer<TSchema['path']> : Params<TPath>;
21
+ body: TSchema extends {
22
22
  body: any;
23
- } ? z.infer<T['body']> : undefined;
24
- headers: T extends {
23
+ } ? z.infer<TSchema['body']> : undefined;
24
+ headers: TSchema extends {
25
25
  headers: any;
26
- } ? z.infer<T['headers']> : undefined;
27
- }, InferRouteResponse<T>> : never;
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
- [M in keyof TRoutes[K]['methods']]: InferRouteRequestHandler<TMiddleware, TRoutes[K]['methods'][M], TRoutes[K]['path']['source']>;
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, {}, void>;
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
@@ -32,7 +32,6 @@ export type RouteSchemaMap = {
32
32
  response?: never;
33
33
  };
34
34
  };
35
- export type Method = string & keyof RouteSchemaMap;
36
35
  export type RouteSchema = QueryRouteSchema | MutationRouteSchema;
37
36
  export type Routes = {
38
37
  [key: string]: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rouzer",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {