@aura-stack/router 0.4.0 → 0.6.0-rc.1

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/assert.cjs CHANGED
@@ -20,6 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/assert.ts
21
21
  var assert_exports = {};
22
22
  __export(assert_exports, {
23
+ isInvalidZodSchemaError: () => isInvalidZodSchemaError,
24
+ isObject: () => isObject,
23
25
  isRouterError: () => isRouterError,
24
26
  isSupportedBodyMethod: () => isSupportedBodyMethod,
25
27
  isSupportedMethod: () => isSupportedMethod,
@@ -63,6 +65,24 @@ var statusText = Object.keys(statusCode).reduce(
63
65
  {}
64
66
  );
65
67
  var AuraStackRouterError = class extends Error {
68
+ /**
69
+ * The HTTP status code associated with the error.
70
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status
71
+ * @example
72
+ * NOT_FOUND: 404
73
+ * METHOD_NOT_ALLOWED: 405
74
+ * INTERNAL_SERVER_ERROR: 500
75
+ */
76
+ status;
77
+ /**
78
+ * The HTTP status text associated with the status code of the error.
79
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status
80
+ * @example
81
+ * NOT_FOUND: NOT_FOUND
82
+ * METHOD_NOT_ALLOWED: METHOD_NOT_ALLOWED
83
+ * INTERNAL_SERVER_ERROR: INTERNAL_SERVER_ERROR
84
+ */
85
+ statusText;
66
86
  constructor(type, message, name) {
67
87
  super(message);
68
88
  this.name = name ?? "RouterError";
@@ -76,6 +96,16 @@ var RouterError = class extends AuraStackRouterError {
76
96
  this.name = name ?? "RouterError";
77
97
  }
78
98
  };
99
+ var InvalidZodSchemaError = class {
100
+ status;
101
+ statusText;
102
+ errors;
103
+ constructor(type, errors) {
104
+ this.status = statusCode[type];
105
+ this.statusText = statusText[type];
106
+ this.errors = errors;
107
+ }
108
+ };
79
109
 
80
110
  // src/assert.ts
81
111
  var supportedMethods = /* @__PURE__ */ new Set(["GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS", "HEAD", "TRACE", "CONNECT"]);
@@ -97,8 +127,16 @@ var isValidHandler = (handler) => {
97
127
  var isRouterError = (error) => {
98
128
  return error instanceof RouterError;
99
129
  };
130
+ var isObject = (value) => {
131
+ return typeof value === "object" && value !== null && value !== void 0 && !Array.isArray(value);
132
+ };
133
+ var isInvalidZodSchemaError = (error) => {
134
+ return error instanceof InvalidZodSchemaError;
135
+ };
100
136
  // Annotate the CommonJS export names for ESM import in node:
101
137
  0 && (module.exports = {
138
+ isInvalidZodSchemaError,
139
+ isObject,
102
140
  isRouterError,
103
141
  isSupportedBodyMethod,
104
142
  isSupportedMethod,
package/dist/assert.d.ts CHANGED
@@ -1,6 +1,9 @@
1
- import { RouterError } from './error.js';
1
+ import { RouterError, InvalidZodSchemaError } from './error.js';
2
2
  import { HTTPMethod, RoutePattern, RouteHandler } from './types.js';
3
3
  import 'zod';
4
+ import './headers.js';
5
+ import 'cookie';
6
+ import 'http';
4
7
 
5
8
  declare const supportedProtocols: Set<string>;
6
9
  /**
@@ -48,5 +51,13 @@ declare const isValidHandler: (handler: unknown) => handler is RouteHandler<any,
48
51
  * }
49
52
  */
50
53
  declare const isRouterError: (error: unknown) => error is RouterError;
54
+ declare const isObject: (value: unknown) => value is Record<string, unknown>;
55
+ /**
56
+ * Checks if the provided error is an instance of InvalidZodSchemaError.
57
+ *
58
+ * @param error the error to check
59
+ * @returns true if the error is an instance of InvalidZodSchemaError, false otherwise.
60
+ */
61
+ declare const isInvalidZodSchemaError: (error: unknown) => error is InvalidZodSchemaError;
51
62
 
52
- export { isRouterError, isSupportedBodyMethod, isSupportedMethod, isValidHandler, isValidRoute, supportedProtocols };
63
+ export { isInvalidZodSchemaError, isObject, isRouterError, isSupportedBodyMethod, isSupportedMethod, isValidHandler, isValidRoute, supportedProtocols };
package/dist/assert.js CHANGED
@@ -1,13 +1,17 @@
1
1
  import {
2
+ isInvalidZodSchemaError,
3
+ isObject,
2
4
  isRouterError,
3
5
  isSupportedBodyMethod,
4
6
  isSupportedMethod,
5
7
  isValidHandler,
6
8
  isValidRoute,
7
9
  supportedProtocols
8
- } from "./chunk-JNMXLKDG.js";
9
- import "./chunk-GJC3ODME.js";
10
+ } from "./chunk-3X2BFSRT.js";
11
+ import "./chunk-FJYSN2I6.js";
10
12
  export {
13
+ isInvalidZodSchemaError,
14
+ isObject,
11
15
  isRouterError,
12
16
  isSupportedBodyMethod,
13
17
  isSupportedMethod,
@@ -1,6 +1,7 @@
1
1
  import {
2
+ InvalidZodSchemaError,
2
3
  RouterError
3
- } from "./chunk-GJC3ODME.js";
4
+ } from "./chunk-FJYSN2I6.js";
4
5
 
5
6
  // src/assert.ts
6
7
  var supportedMethods = /* @__PURE__ */ new Set(["GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS", "HEAD", "TRACE", "CONNECT"]);
@@ -22,6 +23,12 @@ var isValidHandler = (handler) => {
22
23
  var isRouterError = (error) => {
23
24
  return error instanceof RouterError;
24
25
  };
26
+ var isObject = (value) => {
27
+ return typeof value === "object" && value !== null && value !== void 0 && !Array.isArray(value);
28
+ };
29
+ var isInvalidZodSchemaError = (error) => {
30
+ return error instanceof InvalidZodSchemaError;
31
+ };
25
32
 
26
33
  export {
27
34
  supportedProtocols,
@@ -29,5 +36,7 @@ export {
29
36
  isSupportedBodyMethod,
30
37
  isValidRoute,
31
38
  isValidHandler,
32
- isRouterError
39
+ isRouterError,
40
+ isObject,
41
+ isInvalidZodSchemaError
33
42
  };
@@ -2,10 +2,10 @@ import {
2
2
  isSupportedMethod,
3
3
  isValidHandler,
4
4
  isValidRoute
5
- } from "./chunk-JNMXLKDG.js";
5
+ } from "./chunk-3X2BFSRT.js";
6
6
  import {
7
7
  RouterError
8
- } from "./chunk-GJC3ODME.js";
8
+ } from "./chunk-FJYSN2I6.js";
9
9
 
10
10
  // src/endpoint.ts
11
11
  var createEndpoint = (method, route, handler, config = {}) => {
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  RouterError
3
- } from "./chunk-GJC3ODME.js";
3
+ } from "./chunk-FJYSN2I6.js";
4
4
 
5
5
  // src/middlewares.ts
6
6
  var executeGlobalMiddlewares = async (context, middlewares) => {
@@ -0,0 +1,38 @@
1
+ // src/headers.ts
2
+ import { serialize, parse, parseSetCookie } from "cookie";
3
+ var HeadersBuilder = class {
4
+ headers;
5
+ constructor(initialHeaders) {
6
+ this.headers = new Headers(initialHeaders);
7
+ }
8
+ setHeader(name, value) {
9
+ this.headers.set(name, value);
10
+ return this;
11
+ }
12
+ setCookie(name, value, options) {
13
+ this.headers.append("Set-Cookie", serialize(name, value, options));
14
+ return this;
15
+ }
16
+ getHeader(name) {
17
+ return this.headers.get(name);
18
+ }
19
+ getCookie(name) {
20
+ const cookies = parse(this.headers.get("cookie") ?? "");
21
+ return cookies[name];
22
+ }
23
+ getSetCookie(name) {
24
+ const cookies = this.headers.getSetCookie();
25
+ const cookie = cookies.find((cookie2) => cookie2.startsWith(name + "="));
26
+ return cookie ? parseSetCookie(cookie).value : void 0;
27
+ }
28
+ toHeaders() {
29
+ return new Headers(this.headers);
30
+ }
31
+ toCookies() {
32
+ return parse(this.headers.get("cookie") ?? "");
33
+ }
34
+ };
35
+
36
+ export {
37
+ HeadersBuilder
38
+ };
@@ -32,6 +32,24 @@ var statusText = Object.keys(statusCode).reduce(
32
32
  {}
33
33
  );
34
34
  var AuraStackRouterError = class extends Error {
35
+ /**
36
+ * The HTTP status code associated with the error.
37
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status
38
+ * @example
39
+ * NOT_FOUND: 404
40
+ * METHOD_NOT_ALLOWED: 405
41
+ * INTERNAL_SERVER_ERROR: 500
42
+ */
43
+ status;
44
+ /**
45
+ * The HTTP status text associated with the status code of the error.
46
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status
47
+ * @example
48
+ * NOT_FOUND: NOT_FOUND
49
+ * METHOD_NOT_ALLOWED: METHOD_NOT_ALLOWED
50
+ * INTERNAL_SERVER_ERROR: INTERNAL_SERVER_ERROR
51
+ */
52
+ statusText;
35
53
  constructor(type, message, name) {
36
54
  super(message);
37
55
  this.name = name ?? "RouterError";
@@ -45,10 +63,21 @@ var RouterError = class extends AuraStackRouterError {
45
63
  this.name = name ?? "RouterError";
46
64
  }
47
65
  };
66
+ var InvalidZodSchemaError = class {
67
+ status;
68
+ statusText;
69
+ errors;
70
+ constructor(type, errors) {
71
+ this.status = statusCode[type];
72
+ this.statusText = statusText[type];
73
+ this.errors = errors;
74
+ }
75
+ };
48
76
 
49
77
  export {
50
78
  statusCode,
51
79
  statusText,
52
80
  AuraStackRouterError,
53
- RouterError
81
+ RouterError,
82
+ InvalidZodSchemaError
54
83
  };
@@ -0,0 +1,38 @@
1
+ // src/client.ts
2
+ function createClient(options) {
3
+ const { baseURL, headers: defaultHeaders } = options;
4
+ return new Proxy(
5
+ {},
6
+ {
7
+ get(_, prop) {
8
+ const method = prop.toString().toUpperCase();
9
+ return async (path, ctx) => {
10
+ const searchParams = new URLSearchParams(ctx?.searchParams);
11
+ let interpolatedPath = path;
12
+ for (const [key, value] of Object.entries(ctx?.params ?? {})) {
13
+ interpolatedPath = interpolatedPath.replace(`:${key}`, String(value));
14
+ }
15
+ const url = new URL(interpolatedPath, baseURL);
16
+ if (searchParams.size > 0) {
17
+ url.search = searchParams.toString();
18
+ }
19
+ const { params: _p, searchParams: _s, ...requestInit } = ctx ?? {};
20
+ const response = await fetch(url.toString(), {
21
+ ...requestInit,
22
+ method,
23
+ headers: {
24
+ ...defaultHeaders,
25
+ ...ctx?.headers
26
+ },
27
+ body: ctx?.body ? ctx.body instanceof FormData ? ctx.body : JSON.stringify(ctx.body) : void 0
28
+ });
29
+ return response;
30
+ };
31
+ }
32
+ }
33
+ );
34
+ }
35
+
36
+ export {
37
+ createClient
38
+ };
@@ -1,16 +1,32 @@
1
1
  import {
2
2
  isSupportedBodyMethod
3
- } from "./chunk-JNMXLKDG.js";
3
+ } from "./chunk-3X2BFSRT.js";
4
4
  import {
5
+ InvalidZodSchemaError,
5
6
  RouterError
6
- } from "./chunk-GJC3ODME.js";
7
+ } from "./chunk-FJYSN2I6.js";
7
8
 
8
9
  // src/context.ts
10
+ var formatZodError = (error) => {
11
+ if (!error.issues || error.issues.length === 0) {
12
+ return {};
13
+ }
14
+ return error.issues.reduce((previous, issue) => {
15
+ const key = issue.path.join(".");
16
+ return {
17
+ ...previous,
18
+ [key]: {
19
+ code: issue.code,
20
+ message: issue.message
21
+ }
22
+ };
23
+ }, {});
24
+ };
9
25
  var getRouteParams = (params, config) => {
10
26
  if (config.schemas?.params) {
11
27
  const parsed = config.schemas.params.safeParse(params);
12
28
  if (!parsed.success) {
13
- throw new RouterError("UNPROCESSABLE_ENTITY", "Invalid route parameters");
29
+ throw new InvalidZodSchemaError("UNPROCESSABLE_ENTITY", formatZodError(parsed.error));
14
30
  }
15
31
  return parsed.data;
16
32
  }
@@ -21,15 +37,12 @@ var getSearchParams = (url, config) => {
21
37
  if (config.schemas?.searchParams) {
22
38
  const parsed = config.schemas.searchParams.safeParse(Object.fromEntries(route.searchParams.entries()));
23
39
  if (!parsed.success) {
24
- throw new RouterError("UNPROCESSABLE_ENTITY", "Invalid search parameters");
40
+ throw new InvalidZodSchemaError("UNPROCESSABLE_ENTITY", formatZodError(parsed.error));
25
41
  }
26
42
  return parsed.data;
27
43
  }
28
44
  return new URLSearchParams(route.searchParams.toString());
29
45
  };
30
- var getHeaders = (request) => {
31
- return new Headers(request.headers);
32
- };
33
46
  var getBody = async (request, config) => {
34
47
  if (!isSupportedBodyMethod(request.method)) {
35
48
  return null;
@@ -41,7 +54,7 @@ var getBody = async (request, config) => {
41
54
  if (config.schemas?.body) {
42
55
  const parsed = config.schemas.body.safeParse(json);
43
56
  if (!parsed.success) {
44
- throw new RouterError("UNPROCESSABLE_ENTITY", "Invalid request body");
57
+ throw new InvalidZodSchemaError("UNPROCESSABLE_ENTITY", formatZodError(parsed.error));
45
58
  }
46
59
  return parsed.data;
47
60
  }
@@ -71,8 +84,8 @@ var createContentTypeRegex = (contentTypes, contenType) => {
71
84
  };
72
85
 
73
86
  export {
87
+ formatZodError,
74
88
  getRouteParams,
75
89
  getSearchParams,
76
- getHeaders,
77
90
  getBody
78
91
  };
@@ -1,21 +1,24 @@
1
+ import {
2
+ executeGlobalMiddlewares,
3
+ executeMiddlewares
4
+ } from "./chunk-6CIHAUKJ.js";
1
5
  import {
2
6
  getBody,
3
- getHeaders,
4
7
  getRouteParams,
5
8
  getSearchParams
6
- } from "./chunk-PT4GU6PH.js";
9
+ } from "./chunk-SY4MM2AG.js";
7
10
  import {
11
+ isInvalidZodSchemaError,
8
12
  isRouterError,
9
13
  isSupportedMethod
10
- } from "./chunk-JNMXLKDG.js";
11
- import {
12
- executeGlobalMiddlewares,
13
- executeMiddlewares
14
- } from "./chunk-CFAIW6YL.js";
14
+ } from "./chunk-3X2BFSRT.js";
15
15
  import {
16
16
  RouterError,
17
17
  statusText
18
- } from "./chunk-GJC3ODME.js";
18
+ } from "./chunk-FJYSN2I6.js";
19
+ import {
20
+ HeadersBuilder
21
+ } from "./chunk-6JNMFP4L.js";
19
22
 
20
23
  // src/router.ts
21
24
  var createNode = () => ({
@@ -81,6 +84,17 @@ var handleError = async (error, request, config) => {
81
84
  );
82
85
  }
83
86
  }
87
+ if (isInvalidZodSchemaError(error)) {
88
+ const { errors, status, statusText: statusText2 } = error;
89
+ return Response.json(
90
+ {
91
+ message: "Invalid request data",
92
+ error: "validation_error",
93
+ details: errors
94
+ },
95
+ { status, statusText: statusText2 }
96
+ );
97
+ }
84
98
  if (isRouterError(error)) {
85
99
  const { message, status, statusText: statusText2 } = error;
86
100
  return Response.json({ message }, { status, statusText: statusText2 });
@@ -107,7 +121,7 @@ var handleRequest = async (method, request, config, root) => {
107
121
  const dynamicParams = getRouteParams(params, endpoint.config);
108
122
  const body = await getBody(globalRequestContext.request, endpoint.config);
109
123
  const searchParams = getSearchParams(globalRequestContext.request.url, endpoint.config);
110
- const headers = getHeaders(globalRequestContext.request);
124
+ const headers = new HeadersBuilder(globalRequestContext.request.headers);
111
125
  let context = {
112
126
  params: dynamicParams,
113
127
  searchParams,
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/client.ts
21
+ var client_exports = {};
22
+ __export(client_exports, {
23
+ createClient: () => createClient
24
+ });
25
+ module.exports = __toCommonJS(client_exports);
26
+ function createClient(options) {
27
+ const { baseURL, headers: defaultHeaders } = options;
28
+ return new Proxy(
29
+ {},
30
+ {
31
+ get(_, prop) {
32
+ const method = prop.toString().toUpperCase();
33
+ return async (path, ctx) => {
34
+ const searchParams = new URLSearchParams(ctx?.searchParams);
35
+ let interpolatedPath = path;
36
+ for (const [key, value] of Object.entries(ctx?.params ?? {})) {
37
+ interpolatedPath = interpolatedPath.replace(`:${key}`, String(value));
38
+ }
39
+ const url = new URL(interpolatedPath, baseURL);
40
+ if (searchParams.size > 0) {
41
+ url.search = searchParams.toString();
42
+ }
43
+ const { params: _p, searchParams: _s, ...requestInit } = ctx ?? {};
44
+ const response = await fetch(url.toString(), {
45
+ ...requestInit,
46
+ method,
47
+ headers: {
48
+ ...defaultHeaders,
49
+ ...ctx?.headers
50
+ },
51
+ body: ctx?.body ? ctx.body instanceof FormData ? ctx.body : JSON.stringify(ctx.body) : void 0
52
+ });
53
+ return response;
54
+ };
55
+ }
56
+ }
57
+ );
58
+ }
59
+ // Annotate the CommonJS export names for ESM import in node:
60
+ 0 && (module.exports = {
61
+ createClient
62
+ });
@@ -0,0 +1,26 @@
1
+ import { Router, ClientOptions, Client, InferEndpoints } from './types.js';
2
+ import 'zod';
3
+ import './error.js';
4
+ import './headers.js';
5
+ import 'cookie';
6
+ import 'http';
7
+
8
+ /**
9
+ * Creates a client API for making requests to the specified router. It provides type-safe methods
10
+ * based on the router's endpoints.
11
+ *
12
+ * @param options - Configuration options for the client, including baseURL and default headers.
13
+ * @returns A client object with methods corresponding to HTTP methods (GET, POST, etc.).
14
+ * @example
15
+ * import { createClient } from "aura-stack/router/client";
16
+ * import { appRouter } from "./server";
17
+ *
18
+ * const client = createClient<typeof appRouter>({
19
+ * baseURL: "http://localhost:3000/api",
20
+ * })
21
+ *
22
+ * client.get("/users")
23
+ */
24
+ declare function createClient<InferRouter extends Router<any>>(options: ClientOptions): Client<InferEndpoints<InferRouter>>;
25
+
26
+ export { createClient };
package/dist/client.js ADDED
@@ -0,0 +1,6 @@
1
+ import {
2
+ createClient
3
+ } from "./chunk-MP4L2ICK.js";
4
+ export {
5
+ createClient
6
+ };
package/dist/context.cjs CHANGED
@@ -20,8 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/context.ts
21
21
  var context_exports = {};
22
22
  __export(context_exports, {
23
+ formatZodError: () => formatZodError,
23
24
  getBody: () => getBody,
24
- getHeaders: () => getHeaders,
25
25
  getRouteParams: () => getRouteParams,
26
26
  getSearchParams: () => getSearchParams
27
27
  });
@@ -61,6 +61,24 @@ var statusText = Object.keys(statusCode).reduce(
61
61
  {}
62
62
  );
63
63
  var AuraStackRouterError = class extends Error {
64
+ /**
65
+ * The HTTP status code associated with the error.
66
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status
67
+ * @example
68
+ * NOT_FOUND: 404
69
+ * METHOD_NOT_ALLOWED: 405
70
+ * INTERNAL_SERVER_ERROR: 500
71
+ */
72
+ status;
73
+ /**
74
+ * The HTTP status text associated with the status code of the error.
75
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status
76
+ * @example
77
+ * NOT_FOUND: NOT_FOUND
78
+ * METHOD_NOT_ALLOWED: METHOD_NOT_ALLOWED
79
+ * INTERNAL_SERVER_ERROR: INTERNAL_SERVER_ERROR
80
+ */
81
+ statusText;
64
82
  constructor(type, message, name) {
65
83
  super(message);
66
84
  this.name = name ?? "RouterError";
@@ -74,6 +92,16 @@ var RouterError = class extends AuraStackRouterError {
74
92
  this.name = name ?? "RouterError";
75
93
  }
76
94
  };
95
+ var InvalidZodSchemaError = class {
96
+ status;
97
+ statusText;
98
+ errors;
99
+ constructor(type, errors) {
100
+ this.status = statusCode[type];
101
+ this.statusText = statusText[type];
102
+ this.errors = errors;
103
+ }
104
+ };
77
105
 
78
106
  // src/assert.ts
79
107
  var supportedBodyMethods = /* @__PURE__ */ new Set(["POST", "PUT", "PATCH"]);
@@ -82,11 +110,26 @@ var isSupportedBodyMethod = (method) => {
82
110
  };
83
111
 
84
112
  // src/context.ts
113
+ var formatZodError = (error) => {
114
+ if (!error.issues || error.issues.length === 0) {
115
+ return {};
116
+ }
117
+ return error.issues.reduce((previous, issue) => {
118
+ const key = issue.path.join(".");
119
+ return {
120
+ ...previous,
121
+ [key]: {
122
+ code: issue.code,
123
+ message: issue.message
124
+ }
125
+ };
126
+ }, {});
127
+ };
85
128
  var getRouteParams = (params, config) => {
86
129
  if (config.schemas?.params) {
87
130
  const parsed = config.schemas.params.safeParse(params);
88
131
  if (!parsed.success) {
89
- throw new RouterError("UNPROCESSABLE_ENTITY", "Invalid route parameters");
132
+ throw new InvalidZodSchemaError("UNPROCESSABLE_ENTITY", formatZodError(parsed.error));
90
133
  }
91
134
  return parsed.data;
92
135
  }
@@ -97,15 +140,12 @@ var getSearchParams = (url, config) => {
97
140
  if (config.schemas?.searchParams) {
98
141
  const parsed = config.schemas.searchParams.safeParse(Object.fromEntries(route.searchParams.entries()));
99
142
  if (!parsed.success) {
100
- throw new RouterError("UNPROCESSABLE_ENTITY", "Invalid search parameters");
143
+ throw new InvalidZodSchemaError("UNPROCESSABLE_ENTITY", formatZodError(parsed.error));
101
144
  }
102
145
  return parsed.data;
103
146
  }
104
147
  return new URLSearchParams(route.searchParams.toString());
105
148
  };
106
- var getHeaders = (request) => {
107
- return new Headers(request.headers);
108
- };
109
149
  var getBody = async (request, config) => {
110
150
  if (!isSupportedBodyMethod(request.method)) {
111
151
  return null;
@@ -117,7 +157,7 @@ var getBody = async (request, config) => {
117
157
  if (config.schemas?.body) {
118
158
  const parsed = config.schemas.body.safeParse(json);
119
159
  if (!parsed.success) {
120
- throw new RouterError("UNPROCESSABLE_ENTITY", "Invalid request body");
160
+ throw new InvalidZodSchemaError("UNPROCESSABLE_ENTITY", formatZodError(parsed.error));
121
161
  }
122
162
  return parsed.data;
123
163
  }
@@ -147,8 +187,8 @@ var createContentTypeRegex = (contentTypes, contenType) => {
147
187
  };
148
188
  // Annotate the CommonJS export names for ESM import in node:
149
189
  0 && (module.exports = {
190
+ formatZodError,
150
191
  getBody,
151
- getHeaders,
152
192
  getRouteParams,
153
193
  getSearchParams
154
194
  });
package/dist/context.d.ts CHANGED
@@ -1,7 +1,15 @@
1
+ import { ZodError } from 'zod';
1
2
  import { EndpointConfig, ContextSearchParams } from './types.js';
2
- import 'zod';
3
3
  import './error.js';
4
+ import './headers.js';
5
+ import 'cookie';
6
+ import 'http';
4
7
 
8
+ /**
9
+ * @experimental
10
+ * @param error ZodError instance
11
+ */
12
+ declare const formatZodError: (error: ZodError<Record<string, unknown>>) => {};
5
13
  /**
6
14
  * Extracts route parameters from a given path using the specified route pattern.
7
15
  *
@@ -53,21 +61,6 @@ declare const getRouteParams: (params: Record<string, string>, config: EndpointC
53
61
  * const searchParams2 = getSearchParams(url2, {} as EndpointConfig);
54
62
  */
55
63
  declare const getSearchParams: <Config extends EndpointConfig>(url: string, config: Config) => ContextSearchParams<Config["schemas"]>["searchParams"];
56
- /**
57
- * Extracts headers from the given Request object and returns them as a Headers instance.
58
- *
59
- * @param request - The Request object from which to extract headers.
60
- * @returns A Headers instance containing all headers from the request.
61
- * @example
62
- * const request = new Request("https://example.com/api", {
63
- * headers: {
64
- * "Content-Type": "application/json",
65
- * "Authorization": "Bearer token",
66
- * },
67
- * });
68
- * const headers = getHeaders(request);
69
- */
70
- declare const getHeaders: (request: Request) => Headers;
71
64
  /**
72
65
  * Extracts and parses the body of a Request object based on its Content-Type header.
73
66
  *
@@ -82,4 +75,4 @@ declare const getHeaders: (request: Request) => Headers;
82
75
  */
83
76
  declare const getBody: <Config extends EndpointConfig>(request: Request, config: Config) => Promise<any>;
84
77
 
85
- export { getBody, getHeaders, getRouteParams, getSearchParams };
78
+ export { formatZodError, getBody, getRouteParams, getSearchParams };