Skip to content

Commit a542b3e

Browse files
committed
refactor: Api plugin system
1 parent 750e406 commit a542b3e

File tree

29 files changed

+432
-133
lines changed

29 files changed

+432
-133
lines changed

apps/docs/content/docs/dev/meta.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
"structure",
1010
"debugging",
1111
"swagger",
12+
"---Plugins---",
13+
"modules",
1214
"---Framework---",
1315
"auth",
1416
"fetcher",
15-
"modules",
1617
"routes",
1718
"---API---",
1819
"sso",

apps/docs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"fumadocs-mdx": "^11.6.0",
1515
"fumadocs-ui": "^15.2.8",
1616
"lucide-react": "^0.488.0",
17+
"motion": "^12.7.4",
1718
"next": "^15.3.1",
1819
"react": "^19.1.0",
1920
"react-dom": "^19.1.0",

apps/web/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"react-hook-form": "^7.55.0",
3434
"sonner": "^2.0.3",
3535
"vitnode": "workspace:*",
36-
"zod": "^3.24.3"
36+
"zod": "^3.24.3",
37+
"vitnode-blog": "workspace:*"
3738
},
3839
"devDependencies": {
3940
"@tailwindcss/postcss": "^4.1.4",

packages/vitnode/src/api/lib/module.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,15 @@ export interface ModuleApi<
1212
plugin: P;
1313
}
1414

15-
export function createModuleApi<
16-
E extends Env,
17-
S extends Schema = Schema,
18-
N extends string = string,
19-
P extends string = string,
20-
>({
15+
export function createModuleApi<E extends Env, S extends Schema = Schema>({
2116
name,
2217
plugin,
2318
routes,
2419
}: {
25-
name: N;
26-
plugin: P;
20+
name: string;
21+
plugin: string;
2722
routes: OpenAPIHono<E, S, string>;
28-
}): ModuleApi<E, S, N, P> {
23+
}) {
2924
const current = routes;
3025

3126
return {

packages/vitnode/src/api/lib/route.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { PluginConfig } from '@/plugin.config';
12
import { createRoute as createRouteHono, RouteConfig } from '@hono/zod-openapi';
23
import { MiddlewareHandler } from 'hono';
34

@@ -15,11 +16,11 @@ export function createApiRoute<
1516
},
1617
>({
1718
isAuth,
18-
plugin,
19+
pluginConfig,
1920
...routeConfig
2021
}: R & {
2122
isAuth?: boolean;
22-
plugin: string;
23+
pluginConfig: PluginConfig;
2324
}): R & {
2425
getRoutingPath: () => RoutingPath<R['path']>;
2526
} {
@@ -28,10 +29,7 @@ export function createApiRoute<
2829
? routeConfig.middleware
2930
: [routeConfig.middleware]
3031
: [];
31-
const tags: string[] = [
32-
plugin.charAt(0).toUpperCase() + plugin.slice(1),
33-
...(routeConfig.tags ?? []),
34-
];
32+
const tags: string[] = [pluginConfig.name, ...(routeConfig.tags ?? [])];
3533

3634
return createRouteHono({
3735
middleware: isAuth ? [sessionMiddleware(), ...middlewareFromConfig] : [],

packages/vitnode/src/api/modules/admin/routes/session.route.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ const route = createApiRoute({
77
method: 'get',
88
description: 'Verify admin session',
99
plugin: 'core',
10+
pluginConfig: {
11+
id: 'core',
12+
name: 'Core',
13+
},
1014
path: '/',
1115
responses: {
1216
200: {
Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,12 @@
11
import { createModuleApi } from '@/api/lib/module';
2-
import { createApiRoute } from '@/api/lib/route';
3-
import { EmailModel } from '@/api/models/email';
4-
import { OpenAPIHono, z } from '@hono/zod-openapi';
2+
import { OpenAPIHono } from '@hono/zod-openapi';
53

64
import { middlewareRoute } from './route';
75

86
export const middlewareModule = createModuleApi({
97
name: 'middleware',
108
plugin: 'core',
11-
routes: new OpenAPIHono().route('/', middlewareRoute).openapi(
12-
createApiRoute({
13-
method: 'post',
14-
plugin: 'core',
15-
path: '/test',
16-
responses: {
17-
200: {
18-
content: {
19-
'application/json': {
20-
schema: z.string(),
21-
},
22-
},
23-
description: 'test',
24-
},
25-
},
26-
}),
27-
async c => {
28-
const email = new EmailModel(c);
29-
await email.send({
30-
to: 'ithereplay@gmail.com',
31-
subject: 'test',
32-
html: 'test',
33-
});
34-
35-
return c.json('Hello, world!');
36-
},
37-
),
9+
routes: new OpenAPIHono().route('/', middlewareRoute),
3810
});
3911

4012
export type MiddlewareTypes = typeof middlewareModule;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import { z } from 'zod';
55

66
const route = createApiRoute({
77
method: 'get',
8-
plugin: 'core',
8+
pluginConfig: {
9+
id: 'core',
10+
name: 'Core',
11+
},
912
description: 'Middleware route with user authentication',
1013
path: '/',
1114
responses: {

packages/vitnode/src/api/modules/users/routes/session.route.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import { z } from 'zod';
77
const route = createApiRoute({
88
method: 'get',
99
description: 'Verify session',
10-
plugin: 'core',
10+
pluginConfig: {
11+
id: 'core',
12+
name: 'Core',
13+
},
1114
path: '/',
1215
responses: {
1316
200: {

packages/vitnode/src/api/modules/users/routes/sign-in.route.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import { z } from 'zod';
88
const route = createApiRoute({
99
method: 'post',
1010
description: 'Sign in with email and password',
11-
plugin: 'core',
11+
pluginConfig: {
12+
id: 'core',
13+
name: 'Core',
14+
},
1215
path: '/',
1316
request: {
1417
body: {

0 commit comments

Comments
 (0)