@aura-stack/router 0.5.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
@@ -65,6 +65,24 @@ var statusText = Object.keys(statusCode).reduce(
65
65
  {}
66
66
  );
67
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;
68
86
  constructor(type, message, name) {
69
87
  super(message);
70
88
  this.name = name ?? "RouterError";
@@ -79,6 +97,9 @@ var RouterError = class extends AuraStackRouterError {
79
97
  }
80
98
  };
81
99
  var InvalidZodSchemaError = class {
100
+ status;
101
+ statusText;
102
+ errors;
82
103
  constructor(type, errors) {
83
104
  this.status = statusCode[type];
84
105
  this.statusText = statusText[type];
package/dist/assert.d.ts CHANGED
@@ -3,6 +3,7 @@ import { HTTPMethod, RoutePattern, RouteHandler } from './types.js';
3
3
  import 'zod';
4
4
  import './headers.js';
5
5
  import 'cookie';
6
+ import 'http';
6
7
 
7
8
  declare const supportedProtocols: Set<string>;
8
9
  /**
package/dist/assert.js CHANGED
@@ -7,8 +7,8 @@ import {
7
7
  isValidHandler,
8
8
  isValidRoute,
9
9
  supportedProtocols
10
- } from "./chunk-FU35BPU7.js";
11
- import "./chunk-B6PMGVSL.js";
10
+ } from "./chunk-3X2BFSRT.js";
11
+ import "./chunk-FJYSN2I6.js";
12
12
  export {
13
13
  isInvalidZodSchemaError,
14
14
  isObject,
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  InvalidZodSchemaError,
3
3
  RouterError
4
- } from "./chunk-B6PMGVSL.js";
4
+ } from "./chunk-FJYSN2I6.js";
5
5
 
6
6
  // src/assert.ts
7
7
  var supportedMethods = /* @__PURE__ */ new Set(["GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS", "HEAD", "TRACE", "CONNECT"]);
@@ -2,10 +2,10 @@ import {
2
2
  isSupportedMethod,
3
3
  isValidHandler,
4
4
  isValidRoute
5
- } from "./chunk-FU35BPU7.js";
5
+ } from "./chunk-3X2BFSRT.js";
6
6
  import {
7
7
  RouterError
8
- } from "./chunk-B6PMGVSL.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-B6PMGVSL.js";
3
+ } from "./chunk-FJYSN2I6.js";
4
4
 
5
5
  // src/middlewares.ts
6
6
  var executeGlobalMiddlewares = async (context, middlewares) => {
@@ -1,6 +1,7 @@
1
1
  // src/headers.ts
2
2
  import { serialize, parse, parseSetCookie } from "cookie";
3
3
  var HeadersBuilder = class {
4
+ headers;
4
5
  constructor(initialHeaders) {
5
6
  this.headers = new Headers(initialHeaders);
6
7
  }
@@ -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";
@@ -46,6 +64,9 @@ var RouterError = class extends AuraStackRouterError {
46
64
  }
47
65
  };
48
66
  var InvalidZodSchemaError = class {
67
+ status;
68
+ statusText;
69
+ errors;
49
70
  constructor(type, errors) {
50
71
  this.status = statusCode[type];
51
72
  this.statusText = statusText[type];
@@ -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,10 +1,10 @@
1
1
  import {
2
2
  isSupportedBodyMethod
3
- } from "./chunk-FU35BPU7.js";
3
+ } from "./chunk-3X2BFSRT.js";
4
4
  import {
5
5
  InvalidZodSchemaError,
6
6
  RouterError
7
- } from "./chunk-B6PMGVSL.js";
7
+ } from "./chunk-FJYSN2I6.js";
8
8
 
9
9
  // src/context.ts
10
10
  var formatZodError = (error) => {
@@ -1,24 +1,24 @@
1
+ import {
2
+ executeGlobalMiddlewares,
3
+ executeMiddlewares
4
+ } from "./chunk-6CIHAUKJ.js";
1
5
  import {
2
6
  getBody,
3
7
  getRouteParams,
4
8
  getSearchParams
5
- } from "./chunk-XB5P5CQZ.js";
9
+ } from "./chunk-SY4MM2AG.js";
6
10
  import {
7
11
  isInvalidZodSchemaError,
8
12
  isRouterError,
9
13
  isSupportedMethod
10
- } from "./chunk-FU35BPU7.js";
11
- import {
12
- HeadersBuilder
13
- } from "./chunk-Z6JJAIWN.js";
14
- import {
15
- executeGlobalMiddlewares,
16
- executeMiddlewares
17
- } from "./chunk-BWIKAYZV.js";
14
+ } from "./chunk-3X2BFSRT.js";
18
15
  import {
19
16
  RouterError,
20
17
  statusText
21
- } from "./chunk-B6PMGVSL.js";
18
+ } from "./chunk-FJYSN2I6.js";
19
+ import {
20
+ HeadersBuilder
21
+ } from "./chunk-6JNMFP4L.js";
22
22
 
23
23
  // src/router.ts
24
24
  var createNode = () => ({
@@ -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
@@ -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";
@@ -75,6 +93,9 @@ var RouterError = class extends AuraStackRouterError {
75
93
  }
76
94
  };
77
95
  var InvalidZodSchemaError = class {
96
+ status;
97
+ statusText;
98
+ errors;
78
99
  constructor(type, errors) {
79
100
  this.status = statusCode[type];
80
101
  this.statusText = statusText[type];
package/dist/context.d.ts CHANGED
@@ -3,6 +3,7 @@ import { EndpointConfig, ContextSearchParams } from './types.js';
3
3
  import './error.js';
4
4
  import './headers.js';
5
5
  import 'cookie';
6
+ import 'http';
6
7
 
7
8
  /**
8
9
  * @experimental
package/dist/context.js CHANGED
@@ -3,9 +3,9 @@ import {
3
3
  getBody,
4
4
  getRouteParams,
5
5
  getSearchParams
6
- } from "./chunk-XB5P5CQZ.js";
7
- import "./chunk-FU35BPU7.js";
8
- import "./chunk-B6PMGVSL.js";
6
+ } from "./chunk-SY4MM2AG.js";
7
+ import "./chunk-3X2BFSRT.js";
8
+ import "./chunk-FJYSN2I6.js";
9
9
  export {
10
10
  formatZodError,
11
11
  getBody,
package/dist/endpoint.cjs CHANGED
@@ -59,6 +59,24 @@ var statusText = Object.keys(statusCode).reduce(
59
59
  {}
60
60
  );
61
61
  var AuraStackRouterError = class extends Error {
62
+ /**
63
+ * The HTTP status code associated with the error.
64
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status
65
+ * @example
66
+ * NOT_FOUND: 404
67
+ * METHOD_NOT_ALLOWED: 405
68
+ * INTERNAL_SERVER_ERROR: 500
69
+ */
70
+ status;
71
+ /**
72
+ * The HTTP status text associated with the status code of the error.
73
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status
74
+ * @example
75
+ * NOT_FOUND: NOT_FOUND
76
+ * METHOD_NOT_ALLOWED: METHOD_NOT_ALLOWED
77
+ * INTERNAL_SERVER_ERROR: INTERNAL_SERVER_ERROR
78
+ */
79
+ statusText;
62
80
  constructor(type, message, name) {
63
81
  super(message);
64
82
  this.name = name ?? "RouterError";
@@ -3,6 +3,7 @@ import 'zod';
3
3
  import './error.js';
4
4
  import './headers.js';
5
5
  import 'cookie';
6
+ import 'http';
6
7
 
7
8
  /**
8
9
  * Defines an API endpoint for the router by specifying the HTTP method, route pattern,
@@ -22,7 +23,9 @@ import 'cookie';
22
23
  */
23
24
  declare const createEndpoint: <const Method extends Uppercase<HTTPMethod>, const Route extends RoutePattern, const Schemas extends EndpointSchemas>(method: Method, route: Route, handler: RouteHandler<Route, {
24
25
  schemas: Schemas;
25
- }>, config?: EndpointConfig<Route, Schemas>) => RouteEndpoint<Method, Route, {}>;
26
+ }>, config?: EndpointConfig<Route, Schemas>) => RouteEndpoint<Method, Route, {
27
+ schemas?: Schemas;
28
+ }>;
26
29
  /**
27
30
  * Create an endpoint configuration to be passed to the `createEndpoint` function.
28
31
  * This function is primarily for type inference and does not perform any runtime checks.
package/dist/endpoint.js CHANGED
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  createEndpoint,
3
3
  createEndpointConfig
4
- } from "./chunk-4UYDR5IO.js";
5
- import "./chunk-FU35BPU7.js";
6
- import "./chunk-B6PMGVSL.js";
4
+ } from "./chunk-5F6FUKTB.js";
5
+ import "./chunk-3X2BFSRT.js";
6
+ import "./chunk-FJYSN2I6.js";
7
7
  export {
8
8
  createEndpoint,
9
9
  createEndpointConfig
package/dist/error.cjs CHANGED
@@ -60,6 +60,24 @@ var statusText = Object.keys(statusCode).reduce(
60
60
  {}
61
61
  );
62
62
  var AuraStackRouterError = class extends Error {
63
+ /**
64
+ * The HTTP status code associated with the error.
65
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status
66
+ * @example
67
+ * NOT_FOUND: 404
68
+ * METHOD_NOT_ALLOWED: 405
69
+ * INTERNAL_SERVER_ERROR: 500
70
+ */
71
+ status;
72
+ /**
73
+ * The HTTP status text associated with the status code of the error.
74
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status
75
+ * @example
76
+ * NOT_FOUND: NOT_FOUND
77
+ * METHOD_NOT_ALLOWED: METHOD_NOT_ALLOWED
78
+ * INTERNAL_SERVER_ERROR: INTERNAL_SERVER_ERROR
79
+ */
80
+ statusText;
63
81
  constructor(type, message, name) {
64
82
  super(message);
65
83
  this.name = name ?? "RouterError";
@@ -74,6 +92,9 @@ var RouterError = class extends AuraStackRouterError {
74
92
  }
75
93
  };
76
94
  var InvalidZodSchemaError = class {
95
+ status;
96
+ statusText;
97
+ errors;
77
98
  constructor(type, errors) {
78
99
  this.status = statusCode[type];
79
100
  this.statusText = statusText[type];
package/dist/error.js CHANGED
@@ -4,7 +4,7 @@ import {
4
4
  RouterError,
5
5
  statusCode,
6
6
  statusText
7
- } from "./chunk-B6PMGVSL.js";
7
+ } from "./chunk-FJYSN2I6.js";
8
8
  export {
9
9
  AuraStackRouterError,
10
10
  InvalidZodSchemaError,
package/dist/headers.cjs CHANGED
@@ -25,6 +25,7 @@ __export(headers_exports, {
25
25
  module.exports = __toCommonJS(headers_exports);
26
26
  var import_cookie = require("cookie");
27
27
  var HeadersBuilder = class {
28
+ headers;
28
29
  constructor(initialHeaders) {
29
30
  this.headers = new Headers(initialHeaders);
30
31
  }
package/dist/headers.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  HeadersBuilder
3
- } from "./chunk-Z6JJAIWN.js";
3
+ } from "./chunk-6JNMFP4L.js";
4
4
  export {
5
5
  HeadersBuilder
6
6
  };
package/dist/index.cjs CHANGED
@@ -22,6 +22,7 @@ var index_exports = {};
22
22
  __export(index_exports, {
23
23
  HeadersBuilder: () => HeadersBuilder,
24
24
  RouterError: () => RouterError,
25
+ createClient: () => createClient,
25
26
  createEndpoint: () => createEndpoint,
26
27
  createEndpointConfig: () => createEndpointConfig,
27
28
  createRouter: () => createRouter,
@@ -66,6 +67,24 @@ var statusText = Object.keys(statusCode).reduce(
66
67
  {}
67
68
  );
68
69
  var AuraStackRouterError = class extends Error {
70
+ /**
71
+ * The HTTP status code associated with the error.
72
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status
73
+ * @example
74
+ * NOT_FOUND: 404
75
+ * METHOD_NOT_ALLOWED: 405
76
+ * INTERNAL_SERVER_ERROR: 500
77
+ */
78
+ status;
79
+ /**
80
+ * The HTTP status text associated with the status code of the error.
81
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status
82
+ * @example
83
+ * NOT_FOUND: NOT_FOUND
84
+ * METHOD_NOT_ALLOWED: METHOD_NOT_ALLOWED
85
+ * INTERNAL_SERVER_ERROR: INTERNAL_SERVER_ERROR
86
+ */
87
+ statusText;
69
88
  constructor(type, message, name) {
70
89
  super(message);
71
90
  this.name = name ?? "RouterError";
@@ -80,6 +99,9 @@ var RouterError = class extends AuraStackRouterError {
80
99
  }
81
100
  };
82
101
  var InvalidZodSchemaError = class {
102
+ status;
103
+ statusText;
104
+ errors;
83
105
  constructor(type, errors) {
84
106
  this.status = statusCode[type];
85
107
  this.statusText = statusText[type];
@@ -131,6 +153,7 @@ function createEndpointConfig(...args) {
131
153
  // src/headers.ts
132
154
  var import_cookie = require("cookie");
133
155
  var HeadersBuilder = class {
156
+ headers;
134
157
  constructor(initialHeaders) {
135
158
  this.headers = new Headers(initialHeaders);
136
159
  }
@@ -406,10 +429,46 @@ var createRouter = (endpoints, config = {}) => {
406
429
  }
407
430
  return server;
408
431
  };
432
+
433
+ // src/client.ts
434
+ function createClient(options) {
435
+ const { baseURL, headers: defaultHeaders } = options;
436
+ return new Proxy(
437
+ {},
438
+ {
439
+ get(_, prop) {
440
+ const method = prop.toString().toUpperCase();
441
+ return async (path, ctx) => {
442
+ const searchParams = new URLSearchParams(ctx?.searchParams);
443
+ let interpolatedPath = path;
444
+ for (const [key, value] of Object.entries(ctx?.params ?? {})) {
445
+ interpolatedPath = interpolatedPath.replace(`:${key}`, String(value));
446
+ }
447
+ const url = new URL(interpolatedPath, baseURL);
448
+ if (searchParams.size > 0) {
449
+ url.search = searchParams.toString();
450
+ }
451
+ const { params: _p, searchParams: _s, ...requestInit } = ctx ?? {};
452
+ const response = await fetch(url.toString(), {
453
+ ...requestInit,
454
+ method,
455
+ headers: {
456
+ ...defaultHeaders,
457
+ ...ctx?.headers
458
+ },
459
+ body: ctx?.body ? ctx.body instanceof FormData ? ctx.body : JSON.stringify(ctx.body) : void 0
460
+ });
461
+ return response;
462
+ };
463
+ }
464
+ }
465
+ );
466
+ }
409
467
  // Annotate the CommonJS export names for ESM import in node:
410
468
  0 && (module.exports = {
411
469
  HeadersBuilder,
412
470
  RouterError,
471
+ createClient,
413
472
  createEndpoint,
414
473
  createEndpointConfig,
415
474
  createRouter,
package/dist/index.d.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  export { createEndpoint, createEndpointConfig } from './endpoint.js';
2
2
  export { createRouter } from './router.js';
3
+ export { createClient } from './client.js';
3
4
  export { isInvalidZodSchemaError, isRouterError } from './assert.js';
4
5
  export { RouterError, statusCode, statusText } from './error.js';
5
6
  export { HeadersBuilder } from './headers.js';
6
- export { ContentType, ContextBody, ContextParams, ContextSearchParams, EndpointConfig, EndpointSchemas, GetHttpHandlers, GetRouteParams, GlobalContext, GlobalCtx, GlobalMiddleware, GlobalMiddlewareContext, HTTPMethod, InferMethod, MiddlewareFunction, Prettify, RequestContext, RouteEndpoint, RouteHandler, RoutePattern, RouterConfig } from './types.js';
7
+ export { Client, ClientOptions, ContentType, ContextBody, ContextParams, ContextSearchParams, EndpointConfig, EndpointSchemas, ExtractEndpoint, ExtractRoutesByMethod, Find, GetHttpHandlers, GetRouteParams, GlobalContext, GlobalCtx, GlobalMiddleware, GlobalMiddlewareContext, HTTPMethod, InferEndpoints, InferMethod, InferZod, MiddlewareFunction, Prettify, RemoveUndefined, RequestContext, RouteEndpoint, RouteHandler, RoutePattern, Router, RouterConfig, RoutesByMethod, ToInferZod } from './types.js';
7
8
  import 'cookie';
8
9
  import 'zod';
10
+ import 'http';
package/dist/index.js CHANGED
@@ -1,27 +1,31 @@
1
1
  import {
2
2
  createRouter
3
- } from "./chunk-TEDN6QMU.js";
4
- import "./chunk-XB5P5CQZ.js";
3
+ } from "./chunk-WJXMLTGP.js";
4
+ import "./chunk-6CIHAUKJ.js";
5
+ import {
6
+ createClient
7
+ } from "./chunk-MP4L2ICK.js";
8
+ import "./chunk-SY4MM2AG.js";
5
9
  import {
6
10
  createEndpoint,
7
11
  createEndpointConfig
8
- } from "./chunk-4UYDR5IO.js";
12
+ } from "./chunk-5F6FUKTB.js";
9
13
  import {
10
14
  isInvalidZodSchemaError,
11
15
  isRouterError
12
- } from "./chunk-FU35BPU7.js";
13
- import {
14
- HeadersBuilder
15
- } from "./chunk-Z6JJAIWN.js";
16
- import "./chunk-BWIKAYZV.js";
16
+ } from "./chunk-3X2BFSRT.js";
17
17
  import {
18
18
  RouterError,
19
19
  statusCode,
20
20
  statusText
21
- } from "./chunk-B6PMGVSL.js";
21
+ } from "./chunk-FJYSN2I6.js";
22
+ import {
23
+ HeadersBuilder
24
+ } from "./chunk-6JNMFP4L.js";
22
25
  export {
23
26
  HeadersBuilder,
24
27
  RouterError,
28
+ createClient,
25
29
  createEndpoint,
26
30
  createEndpointConfig,
27
31
  createRouter,
@@ -59,6 +59,24 @@ var statusText = Object.keys(statusCode).reduce(
59
59
  {}
60
60
  );
61
61
  var AuraStackRouterError = class extends Error {
62
+ /**
63
+ * The HTTP status code associated with the error.
64
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status
65
+ * @example
66
+ * NOT_FOUND: 404
67
+ * METHOD_NOT_ALLOWED: 405
68
+ * INTERNAL_SERVER_ERROR: 500
69
+ */
70
+ status;
71
+ /**
72
+ * The HTTP status text associated with the status code of the error.
73
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status
74
+ * @example
75
+ * NOT_FOUND: NOT_FOUND
76
+ * METHOD_NOT_ALLOWED: METHOD_NOT_ALLOWED
77
+ * INTERNAL_SERVER_ERROR: INTERNAL_SERVER_ERROR
78
+ */
79
+ statusText;
62
80
  constructor(type, message, name) {
63
81
  super(message);
64
82
  this.name = name ?? "RouterError";
@@ -3,6 +3,7 @@ import 'zod';
3
3
  import './error.js';
4
4
  import './headers.js';
5
5
  import 'cookie';
6
+ import 'http';
6
7
 
7
8
  /**
8
9
  * Executes the middlewares in sequence, passing the request to each middleware.
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  executeGlobalMiddlewares,
3
3
  executeMiddlewares
4
- } from "./chunk-BWIKAYZV.js";
5
- import "./chunk-B6PMGVSL.js";
4
+ } from "./chunk-6CIHAUKJ.js";
5
+ import "./chunk-FJYSN2I6.js";
6
6
  export {
7
7
  executeGlobalMiddlewares,
8
8
  executeMiddlewares
package/dist/router.cjs CHANGED
@@ -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";
@@ -75,6 +93,9 @@ var RouterError = class extends AuraStackRouterError {
75
93
  }
76
94
  };
77
95
  var InvalidZodSchemaError = class {
96
+ status;
97
+ statusText;
98
+ errors;
78
99
  constructor(type, errors) {
79
100
  this.status = statusCode[type];
80
101
  this.statusText = statusText[type];
@@ -85,6 +106,7 @@ var InvalidZodSchemaError = class {
85
106
  // src/headers.ts
86
107
  var import_cookie = require("cookie");
87
108
  var HeadersBuilder = class {
109
+ headers;
88
110
  constructor(initialHeaders) {
89
111
  this.headers = new Headers(initialHeaders);
90
112
  }
package/dist/router.d.ts CHANGED
@@ -1,8 +1,9 @@
1
- import { RouteEndpoint, RouterConfig, GetHttpHandlers, HTTPMethod, RoutePattern, EndpointSchemas, MiddlewareFunction } from './types.js';
1
+ import { RouteEndpoint, RouterConfig, Router, HTTPMethod, RoutePattern, EndpointSchemas, MiddlewareFunction } from './types.js';
2
2
  import 'zod';
3
3
  import './error.js';
4
4
  import './headers.js';
5
5
  import 'cookie';
6
+ import 'http';
6
7
 
7
8
  interface TrieNode {
8
9
  statics: Map<string, TrieNode>;
@@ -34,6 +35,6 @@ declare const search: (method: HTTPMethod, root: TrieNode, pathname: string) =>
34
35
  * @param config - Optional configuration object for the router
35
36
  * @returns An object with methods corresponding to HTTP methods, each handling requests for that method
36
37
  */
37
- declare const createRouter: <const Endpoints extends RouteEndpoint[]>(endpoints: Endpoints, config?: RouterConfig) => GetHttpHandlers<Endpoints>;
38
+ declare const createRouter: <const Endpoints extends RouteEndpoint[]>(endpoints: Endpoints, config?: RouterConfig) => Router<Endpoints>;
38
39
 
39
40
  export { createNode, createRouter, insert, search };
package/dist/router.js CHANGED
@@ -3,12 +3,12 @@ import {
3
3
  createRouter,
4
4
  insert,
5
5
  search
6
- } from "./chunk-TEDN6QMU.js";
7
- import "./chunk-XB5P5CQZ.js";
8
- import "./chunk-FU35BPU7.js";
9
- import "./chunk-Z6JJAIWN.js";
10
- import "./chunk-BWIKAYZV.js";
11
- import "./chunk-B6PMGVSL.js";
6
+ } from "./chunk-WJXMLTGP.js";
7
+ import "./chunk-6CIHAUKJ.js";
8
+ import "./chunk-SY4MM2AG.js";
9
+ import "./chunk-3X2BFSRT.js";
10
+ import "./chunk-FJYSN2I6.js";
11
+ import "./chunk-6JNMFP4L.js";
12
12
  export {
13
13
  createNode,
14
14
  createRouter,
package/dist/types.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { ZodObject, z } from 'zod';
2
2
  import { RouterError } from './error.js';
3
3
  import { HeadersBuilder } from './headers.js';
4
+ import { IncomingHttpHeaders } from 'http';
4
5
  import 'cookie';
5
6
 
6
7
  /**
@@ -210,5 +211,50 @@ interface RouterConfig extends GlobalCtx {
210
211
  */
211
212
  onError?: (error: Error | RouterError, request: Request) => Response | Promise<Response>;
212
213
  }
214
+ /**
215
+ * @experimental
216
+ */
217
+ type ExtractEndpoint<T> = T extends RouteEndpoint<infer M, infer P, infer C> ? {
218
+ method: M;
219
+ path: P;
220
+ config: C;
221
+ } : never;
222
+ /**
223
+ * @experimental
224
+ */
225
+ type RoutesByMethod<Defs extends readonly RouteEndpoint[], Met extends HTTPMethod> = ExtractEndpoint<Defs[number]> extends infer E ? (E extends {
226
+ method: Met;
227
+ path: infer P;
228
+ } ? P : never) : never;
229
+ type ExtractRoutesByMethod<Defs extends RouteEndpoint[], Met extends HTTPMethod> = Defs extends unknown[] ? Defs extends [infer First, ...infer Rest] ? First extends RouteEndpoint<infer M, infer R> ? M extends Met ? R | ExtractRoutesByMethod<Rest extends RouteEndpoint[] ? Rest : [], Met> : ExtractRoutesByMethod<Rest extends RouteEndpoint[] ? Rest : [], Met> : ExtractRoutesByMethod<Rest extends RouteEndpoint[] ? Rest : [], Met> : never : false;
230
+ type InferZod<T> = T extends z.ZodTypeAny ? z.infer<T> : T;
231
+ type ToInferZod<T> = {
232
+ [K in keyof T]: InferZod<T[K]>;
233
+ };
234
+ type RemoveUndefined<T> = {
235
+ [K in keyof T as undefined extends T[K] ? never : K]: T[K];
236
+ };
237
+ type Find<Defs extends RouteEndpoint[], Met extends HTTPMethod, Path extends string> = Defs extends unknown[] ? Defs extends [infer First, ...infer Rest] ? First extends RouteEndpoint<infer M, infer R, infer C> ? M extends Met ? R extends Path ? RemoveUndefined<ToInferZod<NonNullable<C["schemas"]>>> : Find<Rest extends RouteEndpoint[] ? Rest : [], Met, Path> : Find<Rest extends RouteEndpoint[] ? Rest : [], Met, Path> : Find<Rest extends RouteEndpoint[] ? Rest : [], Met, Path> : never : never;
238
+ type Client<Defs extends RouteEndpoint[]> = {
239
+ [M in InferMethod<Defs> as Lowercase<M>]: <T extends ExtractRoutesByMethod<Defs, M>, Config extends Find<Defs, M, T>>(...args: Config extends EndpointSchemas ? [path: T, ctx?: RequestInit] : [path: T, ctx: Prettify<Omit<RequestInit, "body"> & Config>]) => Promise<Response>;
240
+ };
241
+ declare const endpointsSymbol: unique symbol;
242
+ type Router<Endpoints extends RouteEndpoint[]> = GetHttpHandlers<Endpoints> & {
243
+ readonly [endpointsSymbol]?: Endpoints;
244
+ };
245
+ type InferEndpoints<T> = T extends Router<infer E> ? E : never;
246
+ interface ClientOptions {
247
+ /**
248
+ * Base URL for the router client to make requests to the server.
249
+ * This is useful when the server is hosted on a different origin.
250
+ *
251
+ * baseURL: "https://api.example.com"
252
+ */
253
+ baseURL: string;
254
+ /**
255
+ * Default headers to include in every request made by the client.
256
+ */
257
+ headers?: IncomingHttpHeaders;
258
+ }
213
259
 
214
- export type { ContentType, ContextBody, ContextParams, ContextSearchParams, EndpointConfig, EndpointSchemas, GetHttpHandlers, GetRouteParams, GlobalContext, GlobalCtx, GlobalMiddleware, GlobalMiddlewareContext, HTTPMethod, InferMethod, MiddlewareFunction, Prettify, RequestContext, RouteEndpoint, RouteHandler, RoutePattern, RouterConfig };
260
+ export type { Client, ClientOptions, ContentType, ContextBody, ContextParams, ContextSearchParams, EndpointConfig, EndpointSchemas, ExtractEndpoint, ExtractRoutesByMethod, Find, GetHttpHandlers, GetRouteParams, GlobalContext, GlobalCtx, GlobalMiddleware, GlobalMiddlewareContext, HTTPMethod, InferEndpoints, InferMethod, InferZod, MiddlewareFunction, Prettify, RemoveUndefined, RequestContext, RouteEndpoint, RouteHandler, RoutePattern, Router, RouterConfig, RoutesByMethod, ToInferZod };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aura-stack/router",
3
- "version": "0.5.0",
3
+ "version": "0.6.0-rc.1",
4
4
  "type": "module",
5
5
  "description": "A lightweight TypeScript library for building, managing, and validating API routes and endpoints in Node.js applications.",
6
6
  "repository": {
@@ -60,13 +60,19 @@
60
60
  "import": "./dist/cookie.js",
61
61
  "require": "./dist/cookie.cjs"
62
62
  },
63
+ "./client": {
64
+ "types": "./dist/client.d.ts",
65
+ "import": "./dist/client.js",
66
+ "require": "./dist/client.cjs"
67
+ },
63
68
  "./types": "./dist/types.d.ts"
64
69
  },
65
70
  "devDependencies": {
71
+ "@types/node": "24.6.2",
66
72
  "prettier": "^3.6.2",
67
- "tsup": "^8.5.0",
73
+ "tsup": "^8.5.1",
68
74
  "typescript": "^5.9.3",
69
- "vitest": "^3.2.4",
75
+ "vitest": "^4.0.18",
70
76
  "zod": "^4.1.11"
71
77
  },
72
78
  "dependencies": {