@aura-stack/router 0.2.0 → 0.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.
package/dist/context.d.ts CHANGED
@@ -1,5 +1,6 @@
1
- import { RoutePattern, Params, EndpointConfig, ContextSearchParams } from './types.js';
1
+ import { EndpointConfig, ContextSearchParams } from './types.js';
2
2
  import 'zod';
3
+ import './error.js';
3
4
 
4
5
  /**
5
6
  * Extracts route parameters from a given path using the specified route pattern.
@@ -8,9 +9,9 @@ import 'zod';
8
9
  * (e.g., "/users/:userId/posts/:postId") and returns an object mapping parameter
9
10
  * names to their decoded values.
10
11
  *
11
- * @param route - The route pattern, typically defined in the endpoint configuration.
12
- * @param path - The actual request path to extract parameters from.
13
- * @returns An object containing the extracted route parameters as key-value pairs.
12
+ * @param params - The extracted route parameters as key-value pairs.
13
+ * @param config - The endpoint configuration which may include schemas for validation.
14
+ * @returns An object containing the extracted and validated route parameters.
14
15
  *
15
16
  * @example
16
17
  * const route = "/users/:userId/posts/:postId";
@@ -19,7 +20,7 @@ import 'zod';
19
20
  * // Expected: { userId: "123", postId: "456" }
20
21
  * const params = getRouteParams(route, path);
21
22
  */
22
- declare const getRouteParams: <Route extends RoutePattern>(route: Route, path: string) => Params<Route>;
23
+ declare const getRouteParams: (params: Record<string, string>, config: EndpointConfig) => Record<string, unknown>;
23
24
  /**
24
25
  * Extracts and validates search parameters from a given URL from the request.
25
26
  *
package/dist/context.js CHANGED
@@ -3,10 +3,9 @@ import {
3
3
  getHeaders,
4
4
  getRouteParams,
5
5
  getSearchParams
6
- } from "./chunk-OXDCFAMF.js";
7
- import "./chunk-YUX3YHXF.js";
8
- import "./chunk-JRJKKBSH.js";
9
- import "./chunk-RFYOPPMW.js";
6
+ } from "./chunk-PT4GU6PH.js";
7
+ import "./chunk-JNMXLKDG.js";
8
+ import "./chunk-GJC3ODME.js";
10
9
  export {
11
10
  getBody,
12
11
  getHeaders,
package/dist/endpoint.cjs CHANGED
@@ -21,24 +21,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var endpoint_exports = {};
22
22
  __export(endpoint_exports, {
23
23
  createEndpoint: () => createEndpoint,
24
- createEndpointConfig: () => createEndpointConfig,
25
- createRoutePattern: () => createRoutePattern
24
+ createEndpointConfig: () => createEndpointConfig
26
25
  });
27
26
  module.exports = __toCommonJS(endpoint_exports);
28
27
 
29
- // src/assert.ts
30
- var supportedMethods = /* @__PURE__ */ new Set(["GET", "POST", "DELETE", "PUT", "PATCH"]);
31
- var isSupportedMethod = (method) => {
32
- return supportedMethods.has(method);
33
- };
34
- var isValidRoute = (route) => {
35
- const routePattern = /^\/[a-zA-Z0-9/_:-]*$/;
36
- return routePattern.test(route);
37
- };
38
- var isValidHandler = (handler) => {
39
- return typeof handler === "function";
40
- };
41
-
42
28
  // src/error.ts
43
29
  var statusCode = {
44
30
  OK: 200,
@@ -66,35 +52,50 @@ var statusCode = {
66
52
  SERVICE_UNAVAILABLE: 503,
67
53
  HTTP_VERSION_NOT_SUPPORTED: 505
68
54
  };
69
- var statusText = Object.entries(statusCode).reduce(
70
- (previous, [status, code]) => {
71
- return { ...previous, [code]: status };
55
+ var statusText = Object.keys(statusCode).reduce(
56
+ (previous, status) => {
57
+ return { ...previous, [status]: status };
72
58
  },
73
59
  {}
74
60
  );
75
61
  var AuraStackRouterError = class extends Error {
76
62
  constructor(type, message, name) {
77
63
  super(message);
78
- this.name = name ?? "AuraStackRouterError";
64
+ this.name = name ?? "RouterError";
79
65
  this.status = statusCode[type];
80
- this.statusText = statusText[this.status];
66
+ this.statusText = statusText[type];
67
+ }
68
+ };
69
+ var RouterError = class extends AuraStackRouterError {
70
+ constructor(type, message, name) {
71
+ super(type, message, name);
72
+ this.name = name ?? "RouterError";
81
73
  }
82
74
  };
83
75
 
84
- // src/endpoint.ts
85
- var createRoutePattern = (route) => {
86
- const pattern = route.replace(/:[^/]+/g, "([^/]+)").replace(/\//g, "\\/");
87
- return new RegExp(`^${pattern}$`);
76
+ // src/assert.ts
77
+ var supportedMethods = /* @__PURE__ */ new Set(["GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS", "HEAD", "TRACE", "CONNECT"]);
78
+ var isSupportedMethod = (method) => {
79
+ return supportedMethods.has(method);
80
+ };
81
+ var isValidRoute = (route) => {
82
+ const routePattern = /^\/[a-zA-Z0-9/_:-]*$/;
83
+ return routePattern.test(route);
88
84
  };
85
+ var isValidHandler = (handler) => {
86
+ return typeof handler === "function";
87
+ };
88
+
89
+ // src/endpoint.ts
89
90
  var createEndpoint = (method, route, handler, config = {}) => {
90
91
  if (!isSupportedMethod(method)) {
91
- throw new AuraStackRouterError("METHOD_NOT_ALLOWED", `Unsupported HTTP method: ${method}`);
92
+ throw new RouterError("METHOD_NOT_ALLOWED", `Unsupported HTTP method: ${method}`);
92
93
  }
93
94
  if (!isValidRoute(route)) {
94
- throw new AuraStackRouterError("BAD_REQUEST", `Invalid route format: ${route}`);
95
+ throw new RouterError("BAD_REQUEST", `Invalid route format: ${route}`);
95
96
  }
96
97
  if (!isValidHandler(handler)) {
97
- throw new AuraStackRouterError("BAD_REQUEST", "Handler must be a function");
98
+ throw new RouterError("BAD_REQUEST", "Handler must be a function");
98
99
  }
99
100
  return { method, route, handler, config };
100
101
  };
@@ -105,6 +106,5 @@ function createEndpointConfig(...args) {
105
106
  // Annotate the CommonJS export names for ESM import in node:
106
107
  0 && (module.exports = {
107
108
  createEndpoint,
108
- createEndpointConfig,
109
- createRoutePattern
109
+ createEndpointConfig
110
110
  });
@@ -1,18 +1,7 @@
1
- import { RoutePattern, HTTPMethod, EndpointSchemas, RouteHandler, EndpointConfig, RouteEndpoint } from './types.js';
1
+ import { HTTPMethod, RoutePattern, EndpointSchemas, RouteHandler, EndpointConfig, RouteEndpoint } from './types.js';
2
2
  import 'zod';
3
+ import './error.js';
3
4
 
4
- /**
5
- * Create a RegExp pattern from a route string. This function allows segment the
6
- * dynamic params in the route. For example, the route `/users/:id` will be
7
- * converted to a regex pattern that captures the `id` parameter.
8
- *
9
- * @param route - The route pattern string
10
- * @returns A RegExp object that matches the route pattern
11
- * @example
12
- * // Expected: /^\/users\/([^/]+)$/
13
- * const pattern = createRoutePattern("/users/:id");
14
- */
15
- declare const createRoutePattern: (route: RoutePattern) => RegExp;
16
5
  /**
17
6
  * Defines an API endpoint for the router by specifying the HTTP method, route pattern,
18
7
  * handler function, and optional configuration such as validation schemas or middlewares.
@@ -36,10 +25,13 @@ declare const createEndpoint: <const Method extends Uppercase<HTTPMethod>, const
36
25
  * Create an endpoint configuration to be passed to the `createEndpoint` function.
37
26
  * This function is primarily for type inference and does not perform any runtime checks.
38
27
  *
39
- * @experimental
28
+ * This overload is recommended when the route pattern does not need to be specified explicitly,
29
+ * otherwise use the overload that accepts the route pattern as the first argument.
30
+ *
40
31
  * @param config - The endpoint configuration object
41
32
  * @returns The same configuration object, typed as EndpointConfig
42
33
  * @example
34
+ * // Without route pattern
43
35
  * const config = createEndpointConfig({
44
36
  * middlewares: [myMiddleware],
45
37
  * schemas: {
@@ -52,8 +44,17 @@ declare const createEndpoint: <const Method extends Uppercase<HTTPMethod>, const
52
44
  * const search = createEndpoint("GET", "/search", async (request, ctx) => {
53
45
  * return new Response("Search results");
54
46
  * }, config);
47
+ *
48
+ * // Overload with route pattern
49
+ * const config = createEndpointConfig("/users/:userId", {
50
+ * middlewares: [myMiddleware],
51
+ * })
52
+ *
53
+ * const getUser = createEndpoint("GET", "/users/:userId", async (request, ctx) => {
54
+ * return new Response("User details");
55
+ * }, config);
55
56
  */
56
57
  declare function createEndpointConfig<Schemas extends EndpointSchemas>(config: EndpointConfig<RoutePattern, Schemas>): EndpointConfig<RoutePattern, Schemas>;
57
- declare function createEndpointConfig<Route extends RoutePattern, S extends EndpointSchemas>(route: Route, config: EndpointConfig<Route, S>): EndpointConfig<Route, S>;
58
+ declare function createEndpointConfig<Route extends RoutePattern, Schemas extends EndpointSchemas>(route: Route, config: EndpointConfig<Route, Schemas>): EndpointConfig<Route, Schemas>;
58
59
 
59
- export { createEndpoint, createEndpointConfig, createRoutePattern };
60
+ export { createEndpoint, createEndpointConfig };
package/dist/endpoint.js CHANGED
@@ -1,12 +1,10 @@
1
1
  import {
2
2
  createEndpoint,
3
- createEndpointConfig,
4
- createRoutePattern
5
- } from "./chunk-YUX3YHXF.js";
6
- import "./chunk-JRJKKBSH.js";
7
- import "./chunk-RFYOPPMW.js";
3
+ createEndpointConfig
4
+ } from "./chunk-6PZEXNTS.js";
5
+ import "./chunk-JNMXLKDG.js";
6
+ import "./chunk-GJC3ODME.js";
8
7
  export {
9
8
  createEndpoint,
10
- createEndpointConfig,
11
- createRoutePattern
9
+ createEndpointConfig
12
10
  };
package/dist/error.cjs CHANGED
@@ -20,7 +20,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/error.ts
21
21
  var error_exports = {};
22
22
  __export(error_exports, {
23
- AuraStackRouterError: () => AuraStackRouterError
23
+ AuraStackRouterError: () => AuraStackRouterError,
24
+ RouterError: () => RouterError,
25
+ statusCode: () => statusCode,
26
+ statusText: () => statusText
24
27
  });
25
28
  module.exports = __toCommonJS(error_exports);
26
29
  var statusCode = {
@@ -49,21 +52,30 @@ var statusCode = {
49
52
  SERVICE_UNAVAILABLE: 503,
50
53
  HTTP_VERSION_NOT_SUPPORTED: 505
51
54
  };
52
- var statusText = Object.entries(statusCode).reduce(
53
- (previous, [status, code]) => {
54
- return { ...previous, [code]: status };
55
+ var statusText = Object.keys(statusCode).reduce(
56
+ (previous, status) => {
57
+ return { ...previous, [status]: status };
55
58
  },
56
59
  {}
57
60
  );
58
61
  var AuraStackRouterError = class extends Error {
59
62
  constructor(type, message, name) {
60
63
  super(message);
61
- this.name = name ?? "AuraStackRouterError";
64
+ this.name = name ?? "RouterError";
62
65
  this.status = statusCode[type];
63
- this.statusText = statusText[this.status];
66
+ this.statusText = statusText[type];
67
+ }
68
+ };
69
+ var RouterError = class extends AuraStackRouterError {
70
+ constructor(type, message, name) {
71
+ super(type, message, name);
72
+ this.name = name ?? "RouterError";
64
73
  }
65
74
  };
66
75
  // Annotate the CommonJS export names for ESM import in node:
67
76
  0 && (module.exports = {
68
- AuraStackRouterError
77
+ AuraStackRouterError,
78
+ RouterError,
79
+ statusCode,
80
+ statusText
69
81
  });
package/dist/error.d.ts CHANGED
@@ -27,14 +27,39 @@ declare const statusCode: {
27
27
  SERVICE_UNAVAILABLE: number;
28
28
  HTTP_VERSION_NOT_SUPPORTED: number;
29
29
  };
30
+ type StatusCode = keyof typeof statusCode;
31
+ /**
32
+ * Reverse mapping of status codes to their corresponding status text.
33
+ */
34
+ declare const statusText: Record<"OK" | "CREATED" | "ACCEPTED" | "NO_CONTENT" | "MULTIPLE_CHOICES" | "MOVED_PERMANENTLY" | "FOUND" | "SEE_OTHER" | "NOT_MODIFIED" | "TEMPORARY_REDIRECT" | "BAD_REQUEST" | "UNAUTHORIZED" | "PAYMENT_REQUIRED" | "FORBIDDEN" | "NOT_FOUND" | "METHOD_NOT_ALLOWED" | "NOT_ACCEPTABLE" | "PROXY_AUTHENTICATION_REQUIRED" | "UNPROCESSABLE_ENTITY" | "INTERNAL_SERVER_ERROR" | "NOT_IMPLEMENTED" | "BAD_GATEWAY" | "SERVICE_UNAVAILABLE" | "HTTP_VERSION_NOT_SUPPORTED", "OK" | "CREATED" | "ACCEPTED" | "NO_CONTENT" | "MULTIPLE_CHOICES" | "MOVED_PERMANENTLY" | "FOUND" | "SEE_OTHER" | "NOT_MODIFIED" | "TEMPORARY_REDIRECT" | "BAD_REQUEST" | "UNAUTHORIZED" | "PAYMENT_REQUIRED" | "FORBIDDEN" | "NOT_FOUND" | "METHOD_NOT_ALLOWED" | "NOT_ACCEPTABLE" | "PROXY_AUTHENTICATION_REQUIRED" | "UNPROCESSABLE_ENTITY" | "INTERNAL_SERVER_ERROR" | "NOT_IMPLEMENTED" | "BAD_GATEWAY" | "SERVICE_UNAVAILABLE" | "HTTP_VERSION_NOT_SUPPORTED">;
30
35
  /**
31
36
  * Defines the errors used in AuraStack Router. Includes HTTP status code and
32
37
  * status text.
38
+ * @deprecated Use RouterError instead
33
39
  */
34
40
  declare class AuraStackRouterError extends Error {
41
+ /**
42
+ * The HTTP status code associated with the error.
43
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status
44
+ * @example
45
+ * NOT_FOUND: 404
46
+ * METHOD_NOT_ALLOWED: 405
47
+ * INTERNAL_SERVER_ERROR: 500
48
+ */
35
49
  readonly status: number;
36
- readonly statusText: keyof typeof statusCode;
37
- constructor(type: keyof typeof statusCode, message: string, name?: string);
50
+ /**
51
+ * The HTTP status text associated with the status code of the error.
52
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status
53
+ * @example
54
+ * NOT_FOUND: NOT_FOUND
55
+ * METHOD_NOT_ALLOWED: METHOD_NOT_ALLOWED
56
+ * INTERNAL_SERVER_ERROR: INTERNAL_SERVER_ERROR
57
+ */
58
+ readonly statusText: StatusCode;
59
+ constructor(type: StatusCode, message: string, name?: string);
60
+ }
61
+ declare class RouterError extends AuraStackRouterError {
62
+ constructor(type: StatusCode, message: string, name?: string);
38
63
  }
39
64
 
40
- export { AuraStackRouterError };
65
+ export { AuraStackRouterError, RouterError, statusCode, statusText };
package/dist/error.js CHANGED
@@ -1,6 +1,12 @@
1
1
  import {
2
- AuraStackRouterError
3
- } from "./chunk-RFYOPPMW.js";
2
+ AuraStackRouterError,
3
+ RouterError,
4
+ statusCode,
5
+ statusText
6
+ } from "./chunk-GJC3ODME.js";
4
7
  export {
5
- AuraStackRouterError
8
+ AuraStackRouterError,
9
+ RouterError,
10
+ statusCode,
11
+ statusText
6
12
  };