Skip to content

Commit 886935e

Browse files
committed
refactor: Update middleware module imports and remove unused files
1 parent cb9b00f commit 886935e

File tree

8 files changed

+141
-139
lines changed

8 files changed

+141
-139
lines changed

packages/vitnode/src/api/modules/middleware/middleware.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createModuleApi } from '@/api/lib/module';
2-
import { test } from '@/test/module';
2+
import { test } from '@/test/test';
33
import { OpenAPIHono } from '@hono/zod-openapi';
44

55
import { middlewareRoute } from './route';

packages/vitnode/src/lib/fetcher-new.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

packages/vitnode/src/lib/test.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

packages/vitnode/src/lib/test.type.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Route } from './route';
2+
import { Test } from './test';
3+
4+
type FetcherParams<
5+
T extends { plugin: string; routes: Route },
6+
R extends T['routes'][number],
7+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
8+
> = R extends any
9+
? Pick<R['route'], 'method' | 'path'> & { plugin: T['plugin'] }
10+
: never;
11+
12+
function fetcher<T extends { plugin: string; routes: Route }>(
13+
params: FetcherParams<T, T['routes'][number]>,
14+
) {
15+
const { path, method, plugin } = params;
16+
}
17+
18+
export const testFetcher = () => {
19+
fetcher<Test>({ path: '/test34', method: 'get', plugin: 'test_plugin' });
20+
fetcher<Test>({ path: '/test2', method: 'post', plugin: 'test_plugin' });
21+
};
Lines changed: 2 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
1-
import {
2-
createRoute,
3-
OpenAPIHono,
4-
RouteConfig,
5-
RouteHandler,
6-
z,
7-
} from '@hono/zod-openapi';
1+
import { OpenAPIHono } from '@hono/zod-openapi';
82

9-
export const withHandler = <R extends RouteConfig, H extends RouteHandler<R>>({
10-
route,
11-
handler,
12-
}: {
13-
handler: H;
14-
route: R;
15-
}) => ({ route, handler });
16-
17-
type Route<
18-
R extends RouteConfig = RouteConfig,
19-
H extends RouteHandler<R> = RouteHandler<R>,
20-
> = readonly { handler: H; route: R }[];
3+
import { Route } from './route';
214

225
export interface BuildModuleType<T extends Route, Plugin extends string> {
236
plugin: Plugin;
@@ -36,70 +19,3 @@ export function buildModule<
3619

3720
return { routes, plugin, hono };
3821
}
39-
40-
export const test = buildModule({
41-
plugin: 'test_plugin',
42-
routes: [
43-
withHandler({
44-
route: createRoute({
45-
path: '/test34',
46-
method: 'get',
47-
responses: {
48-
200: {
49-
description: 'Success',
50-
content: {
51-
'application/json': {
52-
schema: z.object({ message: z.string() }),
53-
},
54-
},
55-
},
56-
},
57-
}),
58-
handler: c => c.json({ message: 'Success' }),
59-
}),
60-
withHandler({
61-
route: createRoute({
62-
path: '/test2',
63-
method: 'post',
64-
responses: {
65-
200: {
66-
description: 'Success 2',
67-
content: {
68-
'application/json': {
69-
schema: z.object({
70-
message: z.string(),
71-
}),
72-
},
73-
},
74-
},
75-
},
76-
}),
77-
handler: c => {
78-
return c.json({
79-
message: `Hello from ${c.req.path}`,
80-
});
81-
},
82-
}),
83-
],
84-
});
85-
86-
type Test = typeof test;
87-
88-
type FetcherParams<
89-
T extends { plugin: string; routes: Route },
90-
R extends T['routes'][number],
91-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
92-
> = R extends any
93-
? Pick<R['route'], 'method' | 'path'> & { plugin: T['plugin'] }
94-
: never;
95-
96-
function fetcher<T extends { plugin: string; routes: Route }>(
97-
params: FetcherParams<T, T['routes'][number]>,
98-
) {
99-
const { path, method, plugin } = params;
100-
}
101-
102-
export const testFetcher = () => {
103-
fetcher<Test>({ path: '/test34', method: 'get', plugin: 'test_plugin' });
104-
fetcher<Test>({ path: '/test2', method: 'post', plugin: 'test_plugin' });
105-
};

packages/vitnode/src/test/route.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { RouteConfig, RouteHandler } from '@hono/zod-openapi';
2+
3+
type RoutingPath<P extends string> =
4+
P extends `${infer Head}/{${infer Param}}${infer Tail}`
5+
? `${Head}/:${Param}${RoutingPath<Tail>}`
6+
: P;
7+
8+
type ValidHandler<R extends RouteConfig> = (
9+
c: Parameters<RouteHandler<R>>[0],
10+
) => ReturnType<RouteHandler<R>>;
11+
12+
export const buildRoute = <
13+
P extends string,
14+
R extends Omit<RouteConfig, 'path'> & {
15+
path: P;
16+
},
17+
H extends ValidHandler<R & { path: P }>,
18+
>({
19+
route,
20+
handler,
21+
}: {
22+
handler: H;
23+
route: R;
24+
}): {
25+
handler: H;
26+
route: R & {
27+
getRoutingPath: () => RoutingPath<R['path']>;
28+
};
29+
} => ({
30+
route: route as R & {
31+
getRoutingPath: () => RoutingPath<R['path']>;
32+
},
33+
handler,
34+
});
35+
36+
export type Route<
37+
R extends RouteConfig = RouteConfig,
38+
H extends RouteHandler<R> = RouteHandler<R>,
39+
> = readonly { handler: H; route: R }[];

packages/vitnode/src/test/test.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { z } from '@hono/zod-openapi';
2+
3+
import { buildModule } from './module';
4+
import { buildRoute } from './route';
5+
6+
export const test = buildModule({
7+
plugin: 'test_plugin',
8+
routes: [
9+
buildRoute({
10+
route: {
11+
path: '/test34',
12+
method: 'get',
13+
responses: {
14+
200: {
15+
description: 'Success',
16+
content: {
17+
'application/json': {
18+
schema: z.object({ message: z.string() }),
19+
},
20+
},
21+
},
22+
},
23+
},
24+
handler: c => {
25+
return c.json({
26+
message: `Hello from ${c.req.path}`,
27+
});
28+
},
29+
}),
30+
buildRoute({
31+
route: {
32+
path: '/test2',
33+
method: 'post',
34+
request: {
35+
body: {
36+
required: true,
37+
content: {
38+
'application/json': {
39+
schema: z.object({
40+
email: z.string().email().toLowerCase().openapi({
41+
example: 'test@test.com',
42+
}),
43+
password: z.string().openapi({
44+
example: 'Test123!',
45+
}),
46+
isAdmin: z.boolean().optional().openapi({
47+
example: false,
48+
}),
49+
}),
50+
},
51+
},
52+
},
53+
},
54+
responses: {
55+
200: {
56+
description: 'Success 2',
57+
content: {
58+
'application/json': {
59+
schema: z.object({
60+
message: z.string(),
61+
}),
62+
},
63+
},
64+
},
65+
},
66+
},
67+
handler: c => {
68+
const { isAdmin, email } = c.req.valid('json');
69+
70+
return c.json({
71+
message: `Got request from ${email} with admin status: ${isAdmin ?? false}`,
72+
});
73+
},
74+
}),
75+
],
76+
});
77+
78+
export type Test = typeof test;

0 commit comments

Comments
 (0)