rouzer 1.0.0-beta.3 → 1.0.0-beta.5
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.d.ts +3 -2
- package/dist/client/index.js +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +3 -3
- package/dist/route.d.ts +2 -2
- package/dist/route.js +1 -1
- package/dist/server/router.d.ts +10 -6
- package/dist/server/router.js +28 -10
- package/package.json +1 -1
- package/readme.md +1 -1
package/dist/client/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Promisable, RouteRequest } from '../types.js';
|
|
1
2
|
export declare function createClient(config: {
|
|
2
3
|
/**
|
|
3
4
|
* Base URL to use for all requests.
|
|
@@ -11,7 +12,7 @@ export declare function createClient(config: {
|
|
|
11
12
|
* Custom handler for non-200 response to a `.json()` request. By default, the
|
|
12
13
|
* response is always parsed as JSON, regardless of the HTTP status code.
|
|
13
14
|
*/
|
|
14
|
-
onJsonError?: (response: Response) => Response
|
|
15
|
+
onJsonError?: (response: Response) => Promisable<Response>;
|
|
15
16
|
}): {
|
|
16
17
|
config: {
|
|
17
18
|
/**
|
|
@@ -26,7 +27,7 @@ export declare function createClient(config: {
|
|
|
26
27
|
* Custom handler for non-200 response to a `.json()` request. By default, the
|
|
27
28
|
* response is always parsed as JSON, regardless of the HTTP status code.
|
|
28
29
|
*/
|
|
29
|
-
onJsonError?: (response: Response) => Response
|
|
30
|
+
onJsonError?: (response: Response) => Promisable<Response>;
|
|
30
31
|
};
|
|
31
32
|
request<T extends RouteRequest>({ pathPattern, method, args: { path, query, body, headers }, route, }: T): Promise<Response & {
|
|
32
33
|
json(): Promise<T["$result"]>;
|
package/dist/client/index.js
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from './route';
|
|
2
|
-
export * from './server/router';
|
|
3
|
-
export * from './client/index';
|
|
4
|
-
export type * from './types';
|
|
1
|
+
export * from './route.js';
|
|
2
|
+
export * from './server/router.js';
|
|
3
|
+
export * from './client/index.js';
|
|
4
|
+
export type * from './types.js';
|
|
5
5
|
export * from 'alien-middleware';
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export * from './route';
|
|
2
|
-
export * from './server/router';
|
|
3
|
-
export * from './client/index';
|
|
1
|
+
export * from './route.js';
|
|
2
|
+
export * from './server/router.js';
|
|
3
|
+
export * from './client/index.js';
|
|
4
4
|
export * from 'alien-middleware';
|
package/dist/route.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { RoutePattern } from '@remix-run/route-pattern';
|
|
2
|
-
import type { Routes } from './types';
|
|
2
|
+
import type { MutationRoute, QueryRoute, RouteFunction, Routes, Unchecked } from './types.js';
|
|
3
3
|
export declare function $type<T>(): Unchecked<T>;
|
|
4
4
|
export declare function route<P extends string, T extends Routes>(path: P, routes: T): {
|
|
5
5
|
path: P;
|
|
6
6
|
pathPattern: RoutePattern<string>;
|
|
7
7
|
routes: T;
|
|
8
|
-
} & { [K in keyof T]: RouteFunction<
|
|
8
|
+
} & { [K in keyof T]: RouteFunction<Extract<T[K], MutationRoute | QueryRoute>>; };
|
package/dist/route.js
CHANGED
package/dist/server/router.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { AdapterRequestContext } from '@hattip/core';
|
|
|
2
2
|
import { type Params } from '@remix-run/route-pattern';
|
|
3
3
|
import { chain, MiddlewareChain, type MiddlewareContext } from 'alien-middleware';
|
|
4
4
|
import * as z from 'zod/mini';
|
|
5
|
-
import type { Routes } from '../types';
|
|
5
|
+
import type { InferRouteResponse, MutationRoute, Promisable, QueryRoute, Routes } from '../types.js';
|
|
6
6
|
export { chain };
|
|
7
7
|
type EmptyMiddlewareChain<TPlatform = unknown> = MiddlewareChain<{
|
|
8
8
|
initial: {
|
|
@@ -31,14 +31,18 @@ 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
|
-
}) => Promisable<
|
|
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
|
-
}) => Promisable<
|
|
45
|
+
}) => Promisable<Response | InferRouteResponse<T>> : never : never : never; }; }) => import("alien-middleware").ApplyMiddleware<TMiddleware, (context: AdapterRequestContext<TMiddleware extends MiddlewareChain<infer T extends {
|
|
42
46
|
initial: {
|
|
43
47
|
env: object;
|
|
44
48
|
properties: object;
|
|
@@ -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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { RoutePattern } from '@remix-run/route-pattern';
|
|
2
2
|
import { chain, } from 'alien-middleware';
|
|
3
|
-
import { mapValues } from '../common';
|
|
3
|
+
import { mapValues } from '../common.js';
|
|
4
4
|
import * as z from 'zod/mini';
|
|
5
5
|
export { chain };
|
|
6
6
|
export function createRouter(config) {
|
|
@@ -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
|
},
|