rouzer 1.0.0-beta.4 → 1.0.0-beta.6
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/client/index.js +3 -1
- package/dist/server/router.d.ts +7 -3
- package/dist/server/router.js +27 -9
- package/package.json +1 -1
- package/readme.md +1 -1
package/dist/client/index.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { shake } from '../common.js';
|
|
2
2
|
export function createClient(config) {
|
|
3
|
+
const baseURL = config.baseURL.replace(/\/$/, '');
|
|
3
4
|
return {
|
|
4
5
|
config,
|
|
5
6
|
request({ pathPattern, method, args: { path, query, body, headers }, route, }) {
|
|
6
7
|
if (route.path) {
|
|
7
8
|
path = route.path.parse(path);
|
|
8
9
|
}
|
|
9
|
-
const url = new URL(
|
|
10
|
+
const url = new URL(baseURL);
|
|
11
|
+
url.pathname += pathPattern.href(path);
|
|
10
12
|
if (route.query) {
|
|
11
13
|
query = route.query.parse(query ?? {});
|
|
12
14
|
url.search = new URLSearchParams(query).toString();
|
package/dist/server/router.d.ts
CHANGED
|
@@ -31,12 +31,16 @@ export declare function createRouter<TRoutes extends Record<string, {
|
|
|
31
31
|
middlewares?: TMiddleware;
|
|
32
32
|
debug?: boolean;
|
|
33
33
|
}): (handlers: { [K in keyof TRoutes]: { [M in keyof TRoutes[K]["routes"]]: TRoutes[K]["routes"][M] extends infer T ? T extends TRoutes[K]["routes"][M] ? T extends QueryRoute ? (context: MiddlewareContext<TMiddleware> & {
|
|
34
|
+
path: T extends {
|
|
35
|
+
path: any;
|
|
36
|
+
} ? z.infer<T["path"]> : Params<TRoutes[K]["path"]>;
|
|
34
37
|
query: z.infer<T["query"]>;
|
|
35
|
-
params: Params<TRoutes[K]["path"]>;
|
|
36
38
|
headers: z.infer<T["headers"]>;
|
|
37
39
|
}) => Promisable<Response | InferRouteResponse<T>> : T extends MutationRoute ? (context: MiddlewareContext<TMiddleware> & {
|
|
40
|
+
path: T extends {
|
|
41
|
+
path: any;
|
|
42
|
+
} ? z.infer<T["path"]> : Params<TRoutes[K]["path"]>;
|
|
38
43
|
body: z.infer<T["body"]>;
|
|
39
|
-
params: Params<TRoutes[K]["path"]>;
|
|
40
44
|
headers: z.infer<T["headers"]>;
|
|
41
45
|
}) => Promisable<Response | InferRouteResponse<T>> : never : never : never; }; }) => import("alien-middleware").ApplyMiddleware<TMiddleware, (context: AdapterRequestContext<TMiddleware extends MiddlewareChain<infer T extends {
|
|
42
46
|
initial: {
|
|
@@ -50,5 +54,5 @@ export declare function createRouter<TRoutes extends Record<string, {
|
|
|
50
54
|
platform: unknown;
|
|
51
55
|
}> ? T["platform"] : never> & {
|
|
52
56
|
url?: URL;
|
|
53
|
-
|
|
57
|
+
path?: {};
|
|
54
58
|
}) => Promise<Response>>;
|
package/dist/server/router.js
CHANGED
|
@@ -12,15 +12,30 @@ export function createRouter(config) {
|
|
|
12
12
|
const method = request.method.toUpperCase();
|
|
13
13
|
const url = (context.url ??= new URL(request.url));
|
|
14
14
|
for (let i = 0; i < keys.length; i++) {
|
|
15
|
-
const
|
|
16
|
-
|
|
15
|
+
const route = config.routes[keys[i]].routes[method];
|
|
16
|
+
if (!route) {
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
const match = patterns[keys[i]].match(url);
|
|
17
20
|
if (!match) {
|
|
18
21
|
continue;
|
|
19
22
|
}
|
|
20
|
-
const
|
|
21
|
-
if (!
|
|
23
|
+
const handler = handlers[keys[i]][method];
|
|
24
|
+
if (!handler) {
|
|
25
|
+
if (config.debug) {
|
|
26
|
+
throw new Error(`Handler not found for route: ${keys[i]} ${method}`);
|
|
27
|
+
}
|
|
22
28
|
continue;
|
|
23
29
|
}
|
|
30
|
+
if (route.path) {
|
|
31
|
+
const error = parsePathParams(context, enableStringParsing(route.path), match.params);
|
|
32
|
+
if (error) {
|
|
33
|
+
return httpClientError(error, 'Invalid path parameter', config);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
context.path = match.params;
|
|
38
|
+
}
|
|
24
39
|
if (route.headers) {
|
|
25
40
|
const error = parseHeaders(context, enableStringParsing(route.headers));
|
|
26
41
|
if (error) {
|
|
@@ -39,11 +54,6 @@ export function createRouter(config) {
|
|
|
39
54
|
return httpClientError(error, 'Invalid request body', config);
|
|
40
55
|
}
|
|
41
56
|
}
|
|
42
|
-
const handler = handlers[keys[i]][method];
|
|
43
|
-
if (!handler) {
|
|
44
|
-
continue;
|
|
45
|
-
}
|
|
46
|
-
context.params = match.params;
|
|
47
57
|
const result = await handler(context);
|
|
48
58
|
if (result instanceof Response) {
|
|
49
59
|
return result;
|
|
@@ -58,6 +68,14 @@ function httpClientError(error, message, config) {
|
|
|
58
68
|
message: config.debug ? `${message}: ${error.message}` : message,
|
|
59
69
|
}, { status: 400 });
|
|
60
70
|
}
|
|
71
|
+
function parsePathParams(context, schema, params) {
|
|
72
|
+
const result = schema.safeParse(params);
|
|
73
|
+
if (!result.success) {
|
|
74
|
+
return result.error;
|
|
75
|
+
}
|
|
76
|
+
context.path = result.data;
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
61
79
|
function parseHeaders(context, schema) {
|
|
62
80
|
const headers = Object.fromEntries(context.request.headers);
|
|
63
81
|
const result = schema.safeParse(headers);
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -59,7 +59,7 @@ export const handler = createRouter({
|
|
|
59
59
|
})({
|
|
60
60
|
helloRoute: {
|
|
61
61
|
GET(ctx) {
|
|
62
|
-
const message = `Hello, ${ctx.
|
|
62
|
+
const message = `Hello, ${ctx.path.name}${ctx.query.excited ? '!' : '.'}`
|
|
63
63
|
return { message }
|
|
64
64
|
},
|
|
65
65
|
},
|