Skip to content

Commit 812fbdf

Browse files
committed
feat: infer plugin id for modules and routes
1 parent 55ae065 commit 812fbdf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+47
-136
lines changed

apps/docs/content/docs/dev/api/modules.mdx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ Think of modules as containers for related API endpoints. They help organize you
1010
```ts title="plugins/blog/src/api/modules/categories/categories.module.ts"
1111
import { buildModule } from "@vitnode/core/api/lib/module";
1212

13-
import { CONFIG_PLUGIN } from "@/config";
1413

1514
export const categoriesModule = buildModule({
16-
pluginId: CONFIG_PLUGIN.id,
1715
name: "categories",
1816
routes: [] // We'll populate this soon!
1917
});
@@ -26,12 +24,10 @@ Want to create a module hierarchy? VitNode's got your back! Nested modules are p
2624
```ts title="plugins/blog/src/api/modules/categories/categories.module.ts"
2725
import { buildModule } from "@vitnode/core/api/lib/module";
2826

29-
import { CONFIG_PLUGIN } from "@/config";
3027

3128
import { postsModule } from "./posts/posts.module"; // [!code ++]
3229

3330
export const categoriesModule = buildModule({
34-
pluginId: CONFIG_PLUGIN.id,
3531
name: "categories",
3632
routes: [],
3733
modules: [postsModule] // [!code ++]
@@ -45,14 +41,12 @@ This creates a structure: `/api/{plugin_id}/categories/posts/*`
4541
```ts title="plugins/blog/src/config.api.ts"
4642
import { buildApiPlugin } from "@vitnode/core/api/lib/plugin";
4743

48-
import { CONFIG_PLUGIN } from "@/config";
4944

5045
import { categoriesModule } from "./api/modules/categories/categories.module"; // [!code ++]
5146

5247
export const blogApiPlugin = () => {
5348
return buildApiPlugin({
54-
pluginId: CONFIG_PLUGIN.pluginId,
55-
modules: [categoriesModule] // [!code ++]
49+
modules: [categoriesModule] // [!code ++]
5650
});
5751
};
58-
```
52+
```

apps/docs/content/docs/dev/api/routes.mdx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ Now for the fun part - creating actual endpoints! Each route is a small but migh
1111
import { z } from "@hono/zod-openapi";
1212
import { buildRoute } from "@vitnode/core/api/lib/route";
1313

14-
import { CONFIG_PLUGIN } from "@/config";
1514

1615
export const getCategoriesRoute = buildRoute({
17-
pluginId: CONFIG_PLUGIN.pluginId,
1816
route: {
1917
method: "get",
2018
path: "/",
@@ -54,11 +52,9 @@ export const getCategoriesRoute = buildRoute({
5452
```ts title="plugins/blog/src/api/modules/categories/categories.module.ts"
5553
import { buildModule } from "@vitnode/core/api/lib/module";
5654

57-
import { CONFIG_PLUGIN } from "@/config";
5855
import { getCategoriesRoute } from "./routes/get.route"; // [!code ++]
5956

6057
export const categoriesModule = buildModule({
61-
pluginId: CONFIG_PLUGIN.id,
6258
name: "categories",
6359
routes: [getCategoriesRoute] // [!code ++]
6460
});
@@ -75,10 +71,8 @@ import { z } from "@hono/zod-openapi";
7571
import { buildRoute } from "@vitnode/core/api/lib/route";
7672
import { HTTPException } from "hono/http-exception";
7773

78-
import { CONFIG_PLUGIN } from "@/config";
7974

8075
export const getCategoryByIdRoute = buildRoute({
81-
pluginId: CONFIG_PLUGIN.pluginId,
8276
route: {
8377
method: "get",
8478
// [!code highlight]
@@ -136,10 +130,8 @@ Query parameters are your best friends for filtering, searching, and pagination.
136130
import { z } from "@hono/zod-openapi";
137131
import { buildRoute } from "@vitnode/core/api/lib/route";
138132

139-
import { CONFIG_PLUGIN } from "@/config";
140133

141134
export const searchCategoriesRoute = buildRoute({
142-
pluginId: CONFIG_PLUGIN.pluginId,
143135
route: {
144136
method: "get",
145137
path: "/search",
@@ -203,7 +195,6 @@ When you need to send complex data (creating, updating), request bodies are your
203195
import { z } from "@hono/zod-openapi";
204196
import { buildRoute } from "@vitnode/core/api/lib/route";
205197

206-
import { CONFIG_PLUGIN } from "@/config";
207198

208199
const createCategorySchema = z.object({
209200
name: z.string().min(1).max(100).openapi({
@@ -225,7 +216,6 @@ const createCategorySchema = z.object({
225216
});
226217

227218
export const createCategoryRoute = buildRoute({
228-
pluginId: CONFIG_PLUGIN.pluginId,
229219
route: {
230220
method: "post",
231221
path: "/",
@@ -274,4 +264,4 @@ export const createCategoryRoute = buildRoute({
274264
return c.json(newCategory, 201);
275265
}
276266
});
277-
```
267+
```

apps/docs/content/docs/dev/captcha/custom-adapter.mdx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ If you want to use captcha in your custom form or somewhere else, follow these s
1616
import { buildRoute } from "@vitnode/core/api/lib/route";
1717

1818
export const exampleRoute = buildRoute({
19-
pluginId: CONFIG_PLUGIN.pluginId,
2019
route: {
2120
method: "post",
2221
description: "Create a new user",

apps/docs/content/docs/dev/captcha/index.mdx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ Add `withCaptcha` to your route config to enable captcha validation for this rou
4343
import { buildRoute } from "@vitnode/core/api/lib/route";
4444

4545
export const exampleRoute = buildRoute({
46-
pluginId: CONFIG_PLUGIN.pluginId,
4746
route: {
4847
method: "post",
4948
description: "Create a new user",

apps/docs/content/docs/dev/cron/index.mdx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,11 @@ export const cleanCron = buildCron({
4646

4747
```ts title="modules/clean/clean.module.ts"
4848
import { buildModule } from "@vitnode/core/api/lib/module";
49-
import { CONFIG_PLUGIN } from "@/config";
5049

5150
// [!code ++]
5251
import { cleanCron } from "./cron/clean.cron";
5352

5453
export const cronModule = buildModule({
55-
pluginId: CONFIG_PLUGIN.pluginId,
5654
name: "clean",
5755
routes: [],
5856
// [!code ++]
@@ -69,4 +67,4 @@ When your CRON job will run first time, you should see your job in AdminCP under
6967

7068
</Step>
7169

72-
</Steps>
70+
</Steps>

apps/docs/content/docs/dev/database/index.mdx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ Access the database in your plugin handlers using `c.get('database')` from the H
2727

2828
```ts title="plugins/{plugin_name}/src/routes/posts.ts"
2929
export const postsRoute = buildRoute({
30-
pluginId: CONFIG_PLUGIN.pluginId,
3130
route: {},
3231
handler: async (c) => {
3332
// [!code ++:7]

apps/docs/content/docs/dev/database/pagination.mdx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ import {
1919
zodPaginationPageInfo,
2020
zodPaginationQuery
2121
} from "@/api/lib/with-pagination";
22-
import { CONFIG_PLUGIN } from "@/config";
2322
import { core_cron } from "@/database/cron";
2423

2524
export const getCronsRoute = buildRoute({
26-
pluginId: CONFIG_PLUGIN.pluginId,
2725
route: {
2826
method: "get",
2927
description: "Get Admin Cron Logs",
@@ -275,4 +273,4 @@ export default function UsersPage() {
275273
</React.Suspense>
276274
);
277275
}
278-
```
276+
```

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,45 @@ import { OpenAPIHono } from "@hono/zod-openapi";
33
import type { BuildCronReturn } from "./cron";
44
import type { Route } from "./route";
55

6-
export interface BuildModuleType<T extends Route, Plugin extends string> {
7-
plugin: Plugin;
8-
routes: T;
9-
}
6+
import { getCurrentPluginId } from "./plugin-context";
107

118
export interface BaseBuildModuleReturn<
12-
P extends string = string,
139
M extends string = string,
14-
Routes extends Route<P>[] = Route<P>[],
10+
Routes extends Route[] = Route[],
1511
> {
1612
cronJobs: BuildCronReturn[];
1713
hono: OpenAPIHono;
18-
modules?: BaseBuildModuleReturn<P>[];
14+
modules?: BaseBuildModuleReturn[];
1915
name: M;
20-
pluginId: P;
16+
pluginId: string;
2117
routes: Routes;
2218
}
2319

2420
export interface BuildModuleReturn<
25-
P extends string,
2621
M extends string,
27-
Routes extends Route<P>[] = Route<P>[],
28-
Modules extends BaseBuildModuleReturn<P>[] = BaseBuildModuleReturn<P>[],
29-
> extends BaseBuildModuleReturn<P, M, Routes> {
22+
Routes extends Route[] = Route[],
23+
Modules extends BaseBuildModuleReturn[] = BaseBuildModuleReturn[],
24+
> extends BaseBuildModuleReturn<M, Routes> {
3025
modules?: Modules;
3126
}
3227

3328
export function buildModule<
34-
const P extends string,
3529
const M extends string,
36-
const Routes extends Route<P>[],
37-
Modules extends BaseBuildModuleReturn<P>[],
30+
const Routes extends Route[],
31+
Modules extends BaseBuildModuleReturn[],
3832
>({
3933
routes,
40-
pluginId,
4134
name,
4235
modules,
4336
cronJobs = [],
4437
}: {
4538
cronJobs?: BuildCronReturn[];
4639
modules?: Modules;
4740
name: M;
48-
pluginId: P;
4941
routes: Routes;
50-
}): BuildModuleReturn<P, M, Routes, Modules> {
42+
}): BuildModuleReturn<M, Routes, Modules> {
43+
const pluginId = getCurrentPluginId();
44+
5145
const hono = new OpenAPIHono();
5246

5347
if (routes) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
let currentPluginId: string | undefined;
2+
3+
export const setCurrentPluginId = (pluginId: string) => {
4+
currentPluginId = pluginId;
5+
};
6+
7+
export const getCurrentPluginId = () => {
8+
if (!currentPluginId) {
9+
throw new Error(
10+
"Plugin ID is not defined. Ensure that your plugin config sets it before building modules or routes.",
11+
);
12+
}
13+
14+
return currentPluginId;
15+
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function buildApiPlugin<P extends string>({
1515
pluginId,
1616
modules = [],
1717
}: {
18-
modules?: BuildModuleReturn<P, string>[];
18+
modules?: BuildModuleReturn<string>[];
1919
pluginId: P;
2020
}): BuildPluginApiReturn {
2121
// Run for checking if the plugin is valid

0 commit comments

Comments
 (0)