rouzer 1.0.0-beta.8 → 1.0.0-beta.9

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.
@@ -22,12 +22,12 @@ export declare function createClient(config: {
22
22
  /**
23
23
  * Default headers to send with every request.
24
24
  */
25
- headers?: Record<string, string>;
25
+ headers?: Record<string, string> | undefined;
26
26
  /**
27
27
  * Custom handler for non-200 response to a `.json()` request. By default, the
28
28
  * response is always parsed as JSON, regardless of the HTTP status code.
29
29
  */
30
- onJsonError?: (response: Response) => Promisable<Response>;
30
+ onJsonError?: ((response: Response) => Promisable<Response>) | undefined;
31
31
  };
32
32
  request<T extends RouteRequest>({ path: pathBuilder, method, args: { path, query, body, headers }, route, }: T): Promise<Response & {
33
33
  json(): Promise<T["$result"]>;
@@ -41,7 +41,7 @@ export function createClient(config) {
41
41
  return fetch(url, {
42
42
  method,
43
43
  body: body !== undefined ? JSON.stringify(body) : undefined,
44
- headers,
44
+ headers: headers,
45
45
  });
46
46
  },
47
47
  async json(request) {
@@ -61,6 +61,15 @@ export type RouterConfig = {
61
61
  * ```
62
62
  */
63
63
  debug?: boolean;
64
+ /**
65
+ * CORS configuration.
66
+ */
67
+ cors?: {
68
+ /**
69
+ * If defined, requests must have an `Origin` header that is in this list.
70
+ */
71
+ allowOrigins?: string[];
72
+ };
64
73
  };
65
74
  interface CreateRouterConfig<TRoutes extends Routes, TMiddleware extends MiddlewareChain> extends RouterConfig {
66
75
  routes: TRoutes;
@@ -89,6 +98,6 @@ export declare function createRouter<TRoutes extends Routes, TMiddleware extends
89
98
  };
90
99
  platform: unknown;
91
100
  }> ? T["platform"] : never> & {
92
- url?: URL;
93
- path?: {};
94
- }) => Promise<Response>>;
101
+ url?: URL | undefined;
102
+ path?: {} | undefined;
103
+ }) => Promise<Response | undefined>>;
@@ -10,8 +10,13 @@ export function createRouter(config) {
10
10
  const patterns = mapValues(config.routes, ({ path }) => basePath ? new RoutePattern(path.source.replace(/^\/?/, basePath)) : path);
11
11
  return (handlers) => middlewares.use(async function (context) {
12
12
  const request = context.request;
13
- const method = request.method.toUpperCase();
14
13
  const url = (context.url ??= new URL(request.url));
14
+ let method = request.method.toUpperCase();
15
+ let isPreflight = false;
16
+ if (method === 'OPTIONS') {
17
+ method = request.headers.get('Access-Control-Request-Method') ?? 'GET';
18
+ isPreflight = true;
19
+ }
15
20
  for (let i = 0; i < keys.length; i++) {
16
21
  const route = config.routes[keys[i]].methods[method];
17
22
  if (!route) {
@@ -28,6 +33,20 @@ export function createRouter(config) {
28
33
  }
29
34
  continue;
30
35
  }
36
+ if (isPreflight) {
37
+ const origin = request.headers.get('Origin');
38
+ const allowed = config.cors?.allowOrigins;
39
+ if (allowed && !(origin && allowed.includes(origin))) {
40
+ return new Response(null, { status: 403 });
41
+ }
42
+ return new Response(null, {
43
+ headers: {
44
+ 'Access-Control-Allow-Origin': origin ?? '*',
45
+ 'Access-Control-Allow-Methods': method,
46
+ 'Access-Control-Allow-Headers': request.headers.get('Access-Control-Request-Headers') ?? '',
47
+ },
48
+ });
49
+ }
31
50
  if (route.path) {
32
51
  const error = parsePathParams(context, enableStringParsing(route.path), match.params);
33
52
  if (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rouzer",
3
- "version": "1.0.0-beta.8",
3
+ "version": "1.0.0-beta.9",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -15,6 +15,8 @@
15
15
  "@alloc/prettier-config": "^1.0.0",
16
16
  "@typescript/native-preview": "7.0.0-dev.20251208.1",
17
17
  "prettier": "^3.7.4",
18
+ "tsc-lint": "^0.1.9",
19
+ "typescript": "^5.9.3",
18
20
  "zod": "^4.1.13"
19
21
  },
20
22
  "dependencies": {