Skip to content

Commit 40bddff

Browse files
pi0Copilot
andcommitted
refactor: split plugin definition
allows better dist bundling Co-authored-by: Copilot <copilot@github.com>
1 parent a94b7fb commit 40bddff

6 files changed

Lines changed: 21 additions & 16 deletions

File tree

src/h3.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { callMiddleware, normalizeMiddleware } from "./middleware.ts";
55
import { requestWithBaseURL } from "./utils/request.ts";
66

77
import type { ServerRequest } from "srvx";
8-
import type { H3Config, H3CoreConfig, H3Plugin, MatchedRoute, RouterContext } from "./types/h3.ts";
8+
import type { H3Config, H3CoreConfig, MatchedRoute, RouterContext } from "./types/h3.ts";
9+
import type { H3Plugin } from "./plugin.ts";
910
import type { H3EventContext } from "./types/context.ts";
1011
import type {
1112
EventHandler,

src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
export type {
44
H3Config,
55
H3CoreConfig,
6-
H3Plugin,
76
H3Route,
87
H3RouteMeta,
98
HTTPMethod,
@@ -14,10 +13,13 @@ export type {
1413
MatchedRoute,
1514
} from "./types/h3.ts";
1615

17-
export { definePlugin } from "./types/h3.ts";
18-
1916
export { H3Core, H3 } from "./h3.ts";
2017

18+
// Plugins
19+
20+
export type { H3Plugin } from "./plugin.ts";
21+
export { definePlugin } from "./plugin.ts";
22+
2123
// Event
2224

2325
export type { H3EventContext } from "./types/context.ts";

src/plugin.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { H3 } from "./h3.ts";
2+
3+
export type H3Plugin = (h3: H3) => void;
4+
5+
export function definePlugin<T = unknown>(
6+
def: (h3: H3, options: T) => void,
7+
): undefined extends T ? (options?: T) => H3Plugin : (options: T) => H3Plugin {
8+
return ((opts?: any) => (h3: H3) => def(h3, opts)) as any;
9+
}

src/tracing.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { H3Event } from "./event.ts";
22
import type { H3, H3Core } from "./h3.ts";
3-
import { type H3Plugin, type H3Route, type MiddlewareOptions } from "./types/h3.ts";
3+
import type { H3Plugin } from "./plugin.ts";
4+
import type { H3Route, MiddlewareOptions } from "./types/h3.ts";
45
import type { EventHandler, Middleware } from "./types/handler.ts";
56

67
/**

src/types/h3.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { MaybePromise } from "./_utils.ts";
55
import type { FetchHandler, ServerRequest } from "srvx";
66
// import type { MatchedRoute, RouterContext } from "rou3";
77
import type { H3Event } from "../event.ts";
8+
import type { H3Plugin } from "../plugin.ts";
89

910
// Inlined from rou3 for type portability
1011
export interface RouterContext {
@@ -57,16 +58,6 @@ export interface H3Route {
5758
handler: EventHandler;
5859
}
5960

60-
// --- H3 Plugins ---
61-
62-
export type H3Plugin = (h3: H3) => void;
63-
64-
export function definePlugin<T = unknown>(
65-
def: (h3: H3, options: T) => void,
66-
): undefined extends T ? (options?: T) => H3Plugin : (options: T) => H3Plugin {
67-
return ((opts?: any) => (h3: H3) => def(h3, opts)) as any;
68-
}
69-
7061
// --- H3 App ---
7162

7263
export type RouteOptions = {

src/utils/route.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { H3RouteMeta, HTTPMethod } from "../types/h3.ts";
22
import type { EventHandler, Middleware } from "../types/handler.ts";
3-
import type { H3Plugin, H3 } from "../types/h3.ts";
3+
import type { H3 } from "../types/h3.ts";
4+
import type { H3Plugin } from "../plugin.ts";
45
import type { StandardSchemaV1 } from "./internal/standard-schema.ts";
56
import { removeRoute as _removeRoute } from "rou3";
67
import { defineValidatedHandler } from "../handler.ts";

0 commit comments

Comments
 (0)