rouzer 1.2.3 → 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
  }
@@ -73,6 +80,13 @@ class RouterObject extends MiddlewareChain {
73
80
  continue;
74
81
  }
75
82
  if (isPreflight) {
83
+ const optionsHandler = handlers[route.name].OPTIONS;
84
+ if (optionsHandler) {
85
+ const response = await optionsHandler(context);
86
+ if (response) {
87
+ return response;
88
+ }
89
+ }
76
90
  return new Response(null, {
77
91
  headers: {
78
92
  'Access-Control-Allow-Origin': origin ?? '',
@@ -87,6 +101,7 @@ class RouterObject extends MiddlewareChain {
87
101
  if (schema.path) {
88
102
  const error = parsePathParams(context, enableStringParsing(schema.path), match.params);
89
103
  if (error) {
104
+ addDebugHeaders?.(context, route);
90
105
  return httpClientError(error, 'Invalid path parameter', config);
91
106
  }
92
107
  }
@@ -96,22 +111,26 @@ class RouterObject extends MiddlewareChain {
96
111
  if (schema.headers) {
97
112
  const error = parseHeaders(context, enableStringParsing(schema.headers));
98
113
  if (error) {
114
+ addDebugHeaders?.(context, route);
99
115
  return httpClientError(error, 'Invalid request headers', config);
100
116
  }
101
117
  }
102
118
  if (schema.query) {
103
119
  const error = parseQueryString(context, enableStringParsing(schema.query));
104
120
  if (error) {
121
+ addDebugHeaders?.(context, route);
105
122
  return httpClientError(error, 'Invalid query string', config);
106
123
  }
107
124
  }
108
125
  if (schema.body) {
109
126
  const error = await parseRequestBody(context, schema.body);
110
127
  if (error) {
128
+ addDebugHeaders?.(context, route);
111
129
  return httpClientError(error, 'Invalid request body', config);
112
130
  }
113
131
  }
114
132
  const result = await handler(context);
133
+ addDebugHeaders?.(context, route);
115
134
  if (result instanceof Response) {
116
135
  return result;
117
136
  }
@@ -1,33 +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
+ } & {
32
+ OPTIONS?: RouteRequestHandler<TMiddleware, {
33
+ path: Params<TRoutes[K]['path']['source']>;
34
+ }, void>;
31
35
  };
32
36
  };
33
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.2.3",
3
+ "version": "1.4.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {