feat(http): move Next and Middleware to @webnet/http/router
Both types are only meaningful in a routing context. Moving them to the ./router sub-export keeps them alongside Router and avoids consumers needing to mix imports from @webnet/http and @webnet/http/router just to annotate middleware. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -7,8 +7,6 @@ export {
|
||||
type Handler,
|
||||
type JsonBody,
|
||||
type ListenOptions,
|
||||
type Middleware,
|
||||
type Next,
|
||||
type RawListener,
|
||||
type RawTransport,
|
||||
type ReadBufferOptions,
|
||||
|
||||
@@ -6,8 +6,6 @@ export type {
|
||||
ServerRequest,
|
||||
ServerResponse,
|
||||
Context,
|
||||
Next,
|
||||
Middleware,
|
||||
Handler,
|
||||
} from "./types.js"
|
||||
export type {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { parse } from "regexparam"
|
||||
import type { Context, Handler, Middleware, Next, ServerRequest } from "./types.js"
|
||||
import type { Context, Handler, Middleware, ServerRequest } from "./types.js"
|
||||
export type { Middleware, Next } from "./types.js"
|
||||
|
||||
// Internal type-erased representation stored in layers
|
||||
type AnyFn = (ctx: unknown, next: (extra?: unknown) => Promise<void>) => Promise<void>
|
||||
@@ -52,7 +53,10 @@ export class Router<T extends object = {}, TGlobal extends object = {}> {
|
||||
this.#layers = []
|
||||
}
|
||||
|
||||
get(path: string, ...middlewaresAndHandler: [...Middleware<T & Match>[], Handler<T & Match>]): this {
|
||||
get(
|
||||
path: string,
|
||||
...middlewaresAndHandler: [...Middleware<T & Match>[], Handler<T & Match>]
|
||||
): this {
|
||||
this.#method("GET", path, middlewaresAndHandler)
|
||||
return this
|
||||
}
|
||||
@@ -63,7 +67,10 @@ export class Router<T extends object = {}, TGlobal extends object = {}> {
|
||||
this.#method("POST", path, middlewaresAndHandler)
|
||||
return this
|
||||
}
|
||||
put(path: string, ...middlewaresAndHandler: [...Middleware<T & Match>[], Handler<T & Match>]): this {
|
||||
put(
|
||||
path: string,
|
||||
...middlewaresAndHandler: [...Middleware<T & Match>[], Handler<T & Match>]
|
||||
): this {
|
||||
this.#method("PUT", path, middlewaresAndHandler)
|
||||
return this
|
||||
}
|
||||
@@ -240,9 +247,7 @@ export class Router<T extends object = {}, TGlobal extends object = {}> {
|
||||
const nextFn = chain
|
||||
const mw = middleware as unknown as AnyFn
|
||||
chain = (c: C): Promise<void> =>
|
||||
mw({ ...c, keys: layerKeys }, (extra?) =>
|
||||
nextFn(extra ? { ...c, ...(extra as C) } : c),
|
||||
)
|
||||
mw({ ...c, keys: layerKeys }, (extra?) => nextFn(extra ? { ...c, ...(extra as C) } : c))
|
||||
}
|
||||
|
||||
await chain({ ...(baseCtx as unknown as C), route: match.route, keys: match.keys })
|
||||
@@ -264,9 +269,7 @@ export class Router<T extends object = {}, TGlobal extends object = {}> {
|
||||
const nextFn = chain
|
||||
const mw = middleware as unknown as AnyFn
|
||||
chain = (c: C): Promise<void> =>
|
||||
mw({ ...c, keys: layerKeys }, (extra?) =>
|
||||
nextFn(extra ? { ...c, ...(extra as C) } : c),
|
||||
)
|
||||
mw({ ...c, keys: layerKeys }, (extra?) => nextFn(extra ? { ...c, ...(extra as C) } : c))
|
||||
}
|
||||
|
||||
await chain(baseCtx as unknown as C)
|
||||
|
||||
Reference in New Issue
Block a user