@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 +38 -0
- package/dist/assert.d.ts +13 -2
- package/dist/assert.js +6 -2
- package/dist/{chunk-JNMXLKDG.js → chunk-3X2BFSRT.js} +11 -2
- package/dist/{chunk-6PZEXNTS.js → chunk-5F6FUKTB.js} +2 -2
- package/dist/{chunk-CFAIW6YL.js → chunk-6CIHAUKJ.js} +1 -1
- package/dist/chunk-6JNMFP4L.js +38 -0
- package/dist/{chunk-GJC3ODME.js → chunk-FJYSN2I6.js} +30 -1
- package/dist/chunk-MP4L2ICK.js +38 -0
- package/dist/{chunk-PT4GU6PH.js → chunk-SY4MM2AG.js} +22 -9
- package/dist/{chunk-CL5D7UJU.js → chunk-WJXMLTGP.js} +23 -9
- package/dist/client.cjs +62 -0
- package/dist/client.d.ts +26 -0
- package/dist/client.js +6 -0
- package/dist/context.cjs +48 -8
- package/dist/context.d.ts +10 -17
- package/dist/context.js +5 -5
- package/dist/cookie.cjs +24 -0
- package/dist/cookie.d.ts +1 -0
- package/dist/cookie.js +2 -0
- package/dist/endpoint.cjs +18 -0
- package/dist/endpoint.d.ts +6 -1
- package/dist/endpoint.js +3 -3
- package/dist/error.cjs +30 -0
- package/dist/error.d.ts +7 -1
- package/dist/error.js +3 -1
- package/dist/headers.cjs +62 -0
- package/dist/headers.d.ts +20 -0
- package/dist/headers.js +6 -0
- package/dist/index.cjs +165 -35
- package/dist/index.d.ts +6 -2
- package/dist/index.js +18 -8
- package/dist/middlewares.cjs +18 -0
- package/dist/middlewares.d.ts +4 -1
- package/dist/middlewares.js +2 -2
- package/dist/router.cjs +127 -38
- package/dist/router.d.ts +5 -2
- package/dist/router.js +6 -5
- package/dist/types.d.ts +50 -2
- package/package.json +22 -3
package/dist/context.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import {
|
|
2
|
+
formatZodError,
|
|
2
3
|
getBody,
|
|
3
|
-
getHeaders,
|
|
4
4
|
getRouteParams,
|
|
5
5
|
getSearchParams
|
|
6
|
-
} from "./chunk-
|
|
7
|
-
import "./chunk-
|
|
8
|
-
import "./chunk-
|
|
6
|
+
} from "./chunk-SY4MM2AG.js";
|
|
7
|
+
import "./chunk-3X2BFSRT.js";
|
|
8
|
+
import "./chunk-FJYSN2I6.js";
|
|
9
9
|
export {
|
|
10
|
+
formatZodError,
|
|
10
11
|
getBody,
|
|
11
|
-
getHeaders,
|
|
12
12
|
getRouteParams,
|
|
13
13
|
getSearchParams
|
|
14
14
|
};
|
package/dist/cookie.cjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
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 __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
+
|
|
17
|
+
// src/cookie.ts
|
|
18
|
+
var cookie_exports = {};
|
|
19
|
+
module.exports = __toCommonJS(cookie_exports);
|
|
20
|
+
__reExport(cookie_exports, require("cookie"), module.exports);
|
|
21
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
22
|
+
0 && (module.exports = {
|
|
23
|
+
...require("cookie")
|
|
24
|
+
});
|
package/dist/cookie.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from 'cookie';
|
package/dist/cookie.js
ADDED
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";
|
package/dist/endpoint.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { HTTPMethod, RoutePattern, EndpointSchemas, RouteHandler, EndpointConfig, RouteEndpoint } from './types.js';
|
|
2
2
|
import 'zod';
|
|
3
3
|
import './error.js';
|
|
4
|
+
import './headers.js';
|
|
5
|
+
import 'cookie';
|
|
6
|
+
import 'http';
|
|
4
7
|
|
|
5
8
|
/**
|
|
6
9
|
* Defines an API endpoint for the router by specifying the HTTP method, route pattern,
|
|
@@ -20,7 +23,9 @@ import './error.js';
|
|
|
20
23
|
*/
|
|
21
24
|
declare const createEndpoint: <const Method extends Uppercase<HTTPMethod>, const Route extends RoutePattern, const Schemas extends EndpointSchemas>(method: Method, route: Route, handler: RouteHandler<Route, {
|
|
22
25
|
schemas: Schemas;
|
|
23
|
-
}>, config?: EndpointConfig<Route, Schemas>) => RouteEndpoint<Method, Route, {
|
|
26
|
+
}>, config?: EndpointConfig<Route, Schemas>) => RouteEndpoint<Method, Route, {
|
|
27
|
+
schemas?: Schemas;
|
|
28
|
+
}>;
|
|
24
29
|
/**
|
|
25
30
|
* Create an endpoint configuration to be passed to the `createEndpoint` function.
|
|
26
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-
|
|
5
|
-
import "./chunk-
|
|
6
|
-
import "./chunk-
|
|
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
|
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var error_exports = {};
|
|
22
22
|
__export(error_exports, {
|
|
23
23
|
AuraStackRouterError: () => AuraStackRouterError,
|
|
24
|
+
InvalidZodSchemaError: () => InvalidZodSchemaError,
|
|
24
25
|
RouterError: () => RouterError,
|
|
25
26
|
statusCode: () => statusCode,
|
|
26
27
|
statusText: () => statusText
|
|
@@ -59,6 +60,24 @@ var statusText = Object.keys(statusCode).reduce(
|
|
|
59
60
|
{}
|
|
60
61
|
);
|
|
61
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;
|
|
62
81
|
constructor(type, message, name) {
|
|
63
82
|
super(message);
|
|
64
83
|
this.name = name ?? "RouterError";
|
|
@@ -72,9 +91,20 @@ var RouterError = class extends AuraStackRouterError {
|
|
|
72
91
|
this.name = name ?? "RouterError";
|
|
73
92
|
}
|
|
74
93
|
};
|
|
94
|
+
var InvalidZodSchemaError = class {
|
|
95
|
+
status;
|
|
96
|
+
statusText;
|
|
97
|
+
errors;
|
|
98
|
+
constructor(type, errors) {
|
|
99
|
+
this.status = statusCode[type];
|
|
100
|
+
this.statusText = statusText[type];
|
|
101
|
+
this.errors = errors;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
75
104
|
// Annotate the CommonJS export names for ESM import in node:
|
|
76
105
|
0 && (module.exports = {
|
|
77
106
|
AuraStackRouterError,
|
|
107
|
+
InvalidZodSchemaError,
|
|
78
108
|
RouterError,
|
|
79
109
|
statusCode,
|
|
80
110
|
statusText
|
package/dist/error.d.ts
CHANGED
|
@@ -61,5 +61,11 @@ declare class AuraStackRouterError extends Error {
|
|
|
61
61
|
declare class RouterError extends AuraStackRouterError {
|
|
62
62
|
constructor(type: StatusCode, message: string, name?: string);
|
|
63
63
|
}
|
|
64
|
+
declare class InvalidZodSchemaError {
|
|
65
|
+
readonly status: number;
|
|
66
|
+
readonly statusText: StatusCode;
|
|
67
|
+
readonly errors: Record<string, string>;
|
|
68
|
+
constructor(type: StatusCode, errors: Record<string, string>);
|
|
69
|
+
}
|
|
64
70
|
|
|
65
|
-
export { AuraStackRouterError, RouterError, statusCode, statusText };
|
|
71
|
+
export { AuraStackRouterError, InvalidZodSchemaError, RouterError, statusCode, statusText };
|
package/dist/error.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AuraStackRouterError,
|
|
3
|
+
InvalidZodSchemaError,
|
|
3
4
|
RouterError,
|
|
4
5
|
statusCode,
|
|
5
6
|
statusText
|
|
6
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-FJYSN2I6.js";
|
|
7
8
|
export {
|
|
8
9
|
AuraStackRouterError,
|
|
10
|
+
InvalidZodSchemaError,
|
|
9
11
|
RouterError,
|
|
10
12
|
statusCode,
|
|
11
13
|
statusText
|
package/dist/headers.cjs
ADDED
|
@@ -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/headers.ts
|
|
21
|
+
var headers_exports = {};
|
|
22
|
+
__export(headers_exports, {
|
|
23
|
+
HeadersBuilder: () => HeadersBuilder
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(headers_exports);
|
|
26
|
+
var import_cookie = require("cookie");
|
|
27
|
+
var HeadersBuilder = class {
|
|
28
|
+
headers;
|
|
29
|
+
constructor(initialHeaders) {
|
|
30
|
+
this.headers = new Headers(initialHeaders);
|
|
31
|
+
}
|
|
32
|
+
setHeader(name, value) {
|
|
33
|
+
this.headers.set(name, value);
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
setCookie(name, value, options) {
|
|
37
|
+
this.headers.append("Set-Cookie", (0, import_cookie.serialize)(name, value, options));
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
getHeader(name) {
|
|
41
|
+
return this.headers.get(name);
|
|
42
|
+
}
|
|
43
|
+
getCookie(name) {
|
|
44
|
+
const cookies = (0, import_cookie.parse)(this.headers.get("cookie") ?? "");
|
|
45
|
+
return cookies[name];
|
|
46
|
+
}
|
|
47
|
+
getSetCookie(name) {
|
|
48
|
+
const cookies = this.headers.getSetCookie();
|
|
49
|
+
const cookie = cookies.find((cookie2) => cookie2.startsWith(name + "="));
|
|
50
|
+
return cookie ? (0, import_cookie.parseSetCookie)(cookie).value : void 0;
|
|
51
|
+
}
|
|
52
|
+
toHeaders() {
|
|
53
|
+
return new Headers(this.headers);
|
|
54
|
+
}
|
|
55
|
+
toCookies() {
|
|
56
|
+
return (0, import_cookie.parse)(this.headers.get("cookie") ?? "");
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
60
|
+
0 && (module.exports = {
|
|
61
|
+
HeadersBuilder
|
|
62
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { SerializeOptions, Cookies } from 'cookie';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A builder class for constructing and manipulating HTTP headers.
|
|
5
|
+
* It includes methods to set, delete, and retrieve headers, as well as
|
|
6
|
+
* manage cookies within the headers.
|
|
7
|
+
*/
|
|
8
|
+
declare class HeadersBuilder {
|
|
9
|
+
private headers;
|
|
10
|
+
constructor(initialHeaders?: HeadersInit);
|
|
11
|
+
setHeader(name: string, value: string): HeadersBuilder;
|
|
12
|
+
setCookie(name: string, value: string, options?: SerializeOptions): HeadersBuilder;
|
|
13
|
+
getHeader(name: string): string | null;
|
|
14
|
+
getCookie(name: string): string | undefined;
|
|
15
|
+
getSetCookie(name: string): string | undefined;
|
|
16
|
+
toHeaders(): Headers;
|
|
17
|
+
toCookies(): Cookies;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { HeadersBuilder };
|
package/dist/headers.js
ADDED
package/dist/index.cjs
CHANGED
|
@@ -20,10 +20,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
HeadersBuilder: () => HeadersBuilder,
|
|
23
24
|
RouterError: () => RouterError,
|
|
25
|
+
createClient: () => createClient,
|
|
24
26
|
createEndpoint: () => createEndpoint,
|
|
25
27
|
createEndpointConfig: () => createEndpointConfig,
|
|
26
28
|
createRouter: () => createRouter,
|
|
29
|
+
isInvalidZodSchemaError: () => isInvalidZodSchemaError,
|
|
27
30
|
isRouterError: () => isRouterError,
|
|
28
31
|
statusCode: () => statusCode,
|
|
29
32
|
statusText: () => statusText
|
|
@@ -64,6 +67,24 @@ var statusText = Object.keys(statusCode).reduce(
|
|
|
64
67
|
{}
|
|
65
68
|
);
|
|
66
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;
|
|
67
88
|
constructor(type, message, name) {
|
|
68
89
|
super(message);
|
|
69
90
|
this.name = name ?? "RouterError";
|
|
@@ -77,6 +98,16 @@ var RouterError = class extends AuraStackRouterError {
|
|
|
77
98
|
this.name = name ?? "RouterError";
|
|
78
99
|
}
|
|
79
100
|
};
|
|
101
|
+
var InvalidZodSchemaError = class {
|
|
102
|
+
status;
|
|
103
|
+
statusText;
|
|
104
|
+
errors;
|
|
105
|
+
constructor(type, errors) {
|
|
106
|
+
this.status = statusCode[type];
|
|
107
|
+
this.statusText = statusText[type];
|
|
108
|
+
this.errors = errors;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
80
111
|
|
|
81
112
|
// src/assert.ts
|
|
82
113
|
var supportedMethods = /* @__PURE__ */ new Set(["GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS", "HEAD", "TRACE", "CONNECT"]);
|
|
@@ -97,6 +128,9 @@ var isValidHandler = (handler) => {
|
|
|
97
128
|
var isRouterError = (error) => {
|
|
98
129
|
return error instanceof RouterError;
|
|
99
130
|
};
|
|
131
|
+
var isInvalidZodSchemaError = (error) => {
|
|
132
|
+
return error instanceof InvalidZodSchemaError;
|
|
133
|
+
};
|
|
100
134
|
|
|
101
135
|
// src/endpoint.ts
|
|
102
136
|
var createEndpoint = (method, route, handler, config = {}) => {
|
|
@@ -116,45 +150,62 @@ function createEndpointConfig(...args) {
|
|
|
116
150
|
return args[0];
|
|
117
151
|
}
|
|
118
152
|
|
|
119
|
-
// src/
|
|
120
|
-
var
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
const executed = await middleware(context);
|
|
127
|
-
if (executed instanceof Response) {
|
|
128
|
-
return executed;
|
|
129
|
-
}
|
|
130
|
-
context = executed;
|
|
153
|
+
// src/headers.ts
|
|
154
|
+
var import_cookie = require("cookie");
|
|
155
|
+
var HeadersBuilder = class {
|
|
156
|
+
headers;
|
|
157
|
+
constructor(initialHeaders) {
|
|
158
|
+
this.headers = new Headers(initialHeaders);
|
|
131
159
|
}
|
|
132
|
-
|
|
133
|
-
|
|
160
|
+
setHeader(name, value) {
|
|
161
|
+
this.headers.set(name, value);
|
|
162
|
+
return this;
|
|
134
163
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
164
|
+
setCookie(name, value, options) {
|
|
165
|
+
this.headers.append("Set-Cookie", (0, import_cookie.serialize)(name, value, options));
|
|
166
|
+
return this;
|
|
167
|
+
}
|
|
168
|
+
getHeader(name) {
|
|
169
|
+
return this.headers.get(name);
|
|
170
|
+
}
|
|
171
|
+
getCookie(name) {
|
|
172
|
+
const cookies = (0, import_cookie.parse)(this.headers.get("cookie") ?? "");
|
|
173
|
+
return cookies[name];
|
|
174
|
+
}
|
|
175
|
+
getSetCookie(name) {
|
|
176
|
+
const cookies = this.headers.getSetCookie();
|
|
177
|
+
const cookie = cookies.find((cookie2) => cookie2.startsWith(name + "="));
|
|
178
|
+
return cookie ? (0, import_cookie.parseSetCookie)(cookie).value : void 0;
|
|
179
|
+
}
|
|
180
|
+
toHeaders() {
|
|
181
|
+
return new Headers(this.headers);
|
|
182
|
+
}
|
|
183
|
+
toCookies() {
|
|
184
|
+
return (0, import_cookie.parse)(this.headers.get("cookie") ?? "");
|
|
149
185
|
}
|
|
150
186
|
};
|
|
151
187
|
|
|
152
188
|
// src/context.ts
|
|
189
|
+
var formatZodError = (error) => {
|
|
190
|
+
if (!error.issues || error.issues.length === 0) {
|
|
191
|
+
return {};
|
|
192
|
+
}
|
|
193
|
+
return error.issues.reduce((previous, issue) => {
|
|
194
|
+
const key = issue.path.join(".");
|
|
195
|
+
return {
|
|
196
|
+
...previous,
|
|
197
|
+
[key]: {
|
|
198
|
+
code: issue.code,
|
|
199
|
+
message: issue.message
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
}, {});
|
|
203
|
+
};
|
|
153
204
|
var getRouteParams = (params, config) => {
|
|
154
205
|
if (config.schemas?.params) {
|
|
155
206
|
const parsed = config.schemas.params.safeParse(params);
|
|
156
207
|
if (!parsed.success) {
|
|
157
|
-
throw new
|
|
208
|
+
throw new InvalidZodSchemaError("UNPROCESSABLE_ENTITY", formatZodError(parsed.error));
|
|
158
209
|
}
|
|
159
210
|
return parsed.data;
|
|
160
211
|
}
|
|
@@ -165,15 +216,12 @@ var getSearchParams = (url, config) => {
|
|
|
165
216
|
if (config.schemas?.searchParams) {
|
|
166
217
|
const parsed = config.schemas.searchParams.safeParse(Object.fromEntries(route.searchParams.entries()));
|
|
167
218
|
if (!parsed.success) {
|
|
168
|
-
throw new
|
|
219
|
+
throw new InvalidZodSchemaError("UNPROCESSABLE_ENTITY", formatZodError(parsed.error));
|
|
169
220
|
}
|
|
170
221
|
return parsed.data;
|
|
171
222
|
}
|
|
172
223
|
return new URLSearchParams(route.searchParams.toString());
|
|
173
224
|
};
|
|
174
|
-
var getHeaders = (request) => {
|
|
175
|
-
return new Headers(request.headers);
|
|
176
|
-
};
|
|
177
225
|
var getBody = async (request, config) => {
|
|
178
226
|
if (!isSupportedBodyMethod(request.method)) {
|
|
179
227
|
return null;
|
|
@@ -185,7 +233,7 @@ var getBody = async (request, config) => {
|
|
|
185
233
|
if (config.schemas?.body) {
|
|
186
234
|
const parsed = config.schemas.body.safeParse(json);
|
|
187
235
|
if (!parsed.success) {
|
|
188
|
-
throw new
|
|
236
|
+
throw new InvalidZodSchemaError("UNPROCESSABLE_ENTITY", formatZodError(parsed.error));
|
|
189
237
|
}
|
|
190
238
|
return parsed.data;
|
|
191
239
|
}
|
|
@@ -214,6 +262,39 @@ var createContentTypeRegex = (contentTypes, contenType) => {
|
|
|
214
262
|
return regex.test(contenType);
|
|
215
263
|
};
|
|
216
264
|
|
|
265
|
+
// src/middlewares.ts
|
|
266
|
+
var executeGlobalMiddlewares = async (context, middlewares) => {
|
|
267
|
+
if (!middlewares) return context;
|
|
268
|
+
for (const middleware of middlewares) {
|
|
269
|
+
if (typeof middleware !== "function") {
|
|
270
|
+
throw new RouterError("BAD_REQUEST", "Global middlewares must be functions");
|
|
271
|
+
}
|
|
272
|
+
const executed = await middleware(context);
|
|
273
|
+
if (executed instanceof Response) {
|
|
274
|
+
return executed;
|
|
275
|
+
}
|
|
276
|
+
context = executed;
|
|
277
|
+
}
|
|
278
|
+
if (!context || !(context.request instanceof Request)) {
|
|
279
|
+
throw new RouterError("BAD_REQUEST", "Global middleware must return a Request or Response object");
|
|
280
|
+
}
|
|
281
|
+
return context;
|
|
282
|
+
};
|
|
283
|
+
var executeMiddlewares = async (context, middlewares = []) => {
|
|
284
|
+
try {
|
|
285
|
+
let ctx = context;
|
|
286
|
+
for (const middleware of middlewares) {
|
|
287
|
+
if (typeof middleware !== "function") {
|
|
288
|
+
throw new RouterError("BAD_REQUEST", "Middleware must be a function");
|
|
289
|
+
}
|
|
290
|
+
ctx = await middleware(ctx);
|
|
291
|
+
}
|
|
292
|
+
return ctx;
|
|
293
|
+
} catch {
|
|
294
|
+
throw new RouterError("BAD_REQUEST", "Handler threw an error");
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
|
|
217
298
|
// src/router.ts
|
|
218
299
|
var createNode = () => ({
|
|
219
300
|
statics: /* @__PURE__ */ new Map(),
|
|
@@ -278,6 +359,17 @@ var handleError = async (error, request, config) => {
|
|
|
278
359
|
);
|
|
279
360
|
}
|
|
280
361
|
}
|
|
362
|
+
if (isInvalidZodSchemaError(error)) {
|
|
363
|
+
const { errors, status, statusText: statusText2 } = error;
|
|
364
|
+
return Response.json(
|
|
365
|
+
{
|
|
366
|
+
message: "Invalid request data",
|
|
367
|
+
error: "validation_error",
|
|
368
|
+
details: errors
|
|
369
|
+
},
|
|
370
|
+
{ status, statusText: statusText2 }
|
|
371
|
+
);
|
|
372
|
+
}
|
|
281
373
|
if (isRouterError(error)) {
|
|
282
374
|
const { message, status, statusText: statusText2 } = error;
|
|
283
375
|
return Response.json({ message }, { status, statusText: statusText2 });
|
|
@@ -304,7 +396,7 @@ var handleRequest = async (method, request, config, root) => {
|
|
|
304
396
|
const dynamicParams = getRouteParams(params, endpoint.config);
|
|
305
397
|
const body = await getBody(globalRequestContext.request, endpoint.config);
|
|
306
398
|
const searchParams = getSearchParams(globalRequestContext.request.url, endpoint.config);
|
|
307
|
-
const headers =
|
|
399
|
+
const headers = new HeadersBuilder(globalRequestContext.request.headers);
|
|
308
400
|
let context = {
|
|
309
401
|
params: dynamicParams,
|
|
310
402
|
searchParams,
|
|
@@ -337,12 +429,50 @@ var createRouter = (endpoints, config = {}) => {
|
|
|
337
429
|
}
|
|
338
430
|
return server;
|
|
339
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
|
+
}
|
|
340
467
|
// Annotate the CommonJS export names for ESM import in node:
|
|
341
468
|
0 && (module.exports = {
|
|
469
|
+
HeadersBuilder,
|
|
342
470
|
RouterError,
|
|
471
|
+
createClient,
|
|
343
472
|
createEndpoint,
|
|
344
473
|
createEndpointConfig,
|
|
345
474
|
createRouter,
|
|
475
|
+
isInvalidZodSchemaError,
|
|
346
476
|
isRouterError,
|
|
347
477
|
statusCode,
|
|
348
478
|
statusText
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
export { createEndpoint, createEndpointConfig } from './endpoint.js';
|
|
2
2
|
export { createRouter } from './router.js';
|
|
3
|
-
export {
|
|
3
|
+
export { createClient } from './client.js';
|
|
4
|
+
export { isInvalidZodSchemaError, isRouterError } from './assert.js';
|
|
4
5
|
export { RouterError, statusCode, statusText } from './error.js';
|
|
5
|
-
export {
|
|
6
|
+
export { HeadersBuilder } from './headers.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';
|
|
8
|
+
import 'cookie';
|
|
6
9
|
import 'zod';
|
|
10
|
+
import 'http';
|
package/dist/index.js
CHANGED
|
@@ -1,25 +1,35 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createRouter
|
|
3
|
+
} from "./chunk-WJXMLTGP.js";
|
|
4
|
+
import "./chunk-6CIHAUKJ.js";
|
|
5
|
+
import {
|
|
6
|
+
createClient
|
|
7
|
+
} from "./chunk-MP4L2ICK.js";
|
|
8
|
+
import "./chunk-SY4MM2AG.js";
|
|
1
9
|
import {
|
|
2
10
|
createEndpoint,
|
|
3
11
|
createEndpointConfig
|
|
4
|
-
} from "./chunk-
|
|
5
|
-
import {
|
|
6
|
-
createRouter
|
|
7
|
-
} from "./chunk-CL5D7UJU.js";
|
|
8
|
-
import "./chunk-PT4GU6PH.js";
|
|
12
|
+
} from "./chunk-5F6FUKTB.js";
|
|
9
13
|
import {
|
|
14
|
+
isInvalidZodSchemaError,
|
|
10
15
|
isRouterError
|
|
11
|
-
} from "./chunk-
|
|
12
|
-
import "./chunk-CFAIW6YL.js";
|
|
16
|
+
} from "./chunk-3X2BFSRT.js";
|
|
13
17
|
import {
|
|
14
18
|
RouterError,
|
|
15
19
|
statusCode,
|
|
16
20
|
statusText
|
|
17
|
-
} from "./chunk-
|
|
21
|
+
} from "./chunk-FJYSN2I6.js";
|
|
22
|
+
import {
|
|
23
|
+
HeadersBuilder
|
|
24
|
+
} from "./chunk-6JNMFP4L.js";
|
|
18
25
|
export {
|
|
26
|
+
HeadersBuilder,
|
|
19
27
|
RouterError,
|
|
28
|
+
createClient,
|
|
20
29
|
createEndpoint,
|
|
21
30
|
createEndpointConfig,
|
|
22
31
|
createRouter,
|
|
32
|
+
isInvalidZodSchemaError,
|
|
23
33
|
isRouterError,
|
|
24
34
|
statusCode,
|
|
25
35
|
statusText
|
package/dist/middlewares.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";
|