0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-30 01:56:18 +01:00
hono/deno_dist/hono.ts

265 lines
7.9 KiB
TypeScript
Raw Normal View History

2022-07-02 08:09:45 +02:00
import { compose } from './compose.ts'
import { Context } from './context.ts'
2022-07-02 08:09:45 +02:00
import { extendRequestPrototype } from './request.ts'
import type { Router } from './router.ts'
import { METHOD_NAME_ALL, METHOD_NAME_ALL_LOWERCASE, METHODS } from './router.ts'
import { RegExpRouter } from './router/reg-exp-router/index.ts'
import { SmartRouter } from './router/smart-router/index.ts'
import { StaticRouter } from './router/static-router/index.ts'
import { TrieRouter } from './router/trie-router/index.ts'
import type { Handler, Environment, ParamKeys, ErrorHandler, NotFoundHandler } from './types.ts'
2022-07-02 08:09:45 +02:00
import { getPathFromURL, mergePath } from './utils/url.ts'
import type { Schema } from './validator/schema.ts'
2022-07-02 08:09:45 +02:00
interface HandlerInterface<
P extends string,
E extends Partial<Environment>,
S extends Partial<Schema>,
U = Hono<E, P, S>
> {
2022-08-31 11:01:33 +02:00
// app.get(handler...)
<Path extends string, Data extends Schema>(
...handlers: Handler<ParamKeys<Path> extends never ? string : ParamKeys<Path>, E, Data>[]
2022-07-02 08:09:45 +02:00
): U
(...handlers: Handler<string, E, S>[]): U
2022-08-31 11:01:33 +02:00
// app.get('/', handler, handler...)
<Path extends string, Data extends Partial<Schema> = Schema>(
2022-08-31 11:01:33 +02:00
path: Path,
...handlers: Handler<ParamKeys<Path> extends never ? string : ParamKeys<Path>, E, Data>[]
2022-07-02 08:09:45 +02:00
): U
<Path extends string, Data extends Schema>(path: Path, ...handlers: Handler<string, E, Data>[]): U
(path: string, ...handlers: Handler<string, E, S>[]): U
2022-07-02 08:09:45 +02:00
}
type Methods = typeof METHODS[number] | typeof METHOD_NAME_ALL_LOWERCASE
2022-07-02 08:09:45 +02:00
function defineDynamicClass(): {
new <
E extends Partial<Environment> = Environment,
P extends string = string,
S extends Partial<Schema> = Schema,
U = Hono<E, P, S>
>(): {
[K in Methods]: HandlerInterface<P, E, S, U>
2022-07-02 08:09:45 +02:00
}
} {
return class {} as never
2022-07-02 08:09:45 +02:00
}
interface Route<
P extends string = string,
E extends Partial<Environment> = Environment,
S extends Partial<Schema> = Schema
> {
2022-07-02 08:09:45 +02:00
path: string
method: string
handler: Handler<P, E, S>
2022-07-02 08:09:45 +02:00
}
export class Hono<
2022-08-28 11:16:51 +02:00
E extends Partial<Environment> = Environment,
P extends string = '/',
S extends Partial<Schema> = Schema
> extends defineDynamicClass()<E, P, S, Hono<E, P, S>> {
readonly router: Router<Handler<P, E, S>> = new SmartRouter({
routers: [new StaticRouter(), new RegExpRouter(), new TrieRouter()],
})
2022-07-02 08:09:45 +02:00
readonly strict: boolean = true // strict routing - default is true
private _tempPath: string = ''
private path: string = '/'
routes: Route<P, E, S>[] = []
2022-07-02 08:09:45 +02:00
constructor(init: Partial<Pick<Hono, 'router' | 'strict'>> = {}) {
super()
extendRequestPrototype()
const allMethods = [...METHODS, METHOD_NAME_ALL_LOWERCASE]
2022-07-02 08:09:45 +02:00
allMethods.map((method) => {
this[method] = <Path extends string, Env extends Environment, Data extends Schema>(
args1: Path | Handler<ParamKeys<Path>, Env, Data>,
...args: [Handler<ParamKeys<Path>, Env, Data>]
): this => {
2022-07-02 08:09:45 +02:00
if (typeof args1 === 'string') {
this.path = args1
} else {
this.addRoute(method, this.path, args1 as unknown as Handler<P, E, S>)
2022-07-02 08:09:45 +02:00
}
args.map((handler) => {
if (typeof handler !== 'string') {
this.addRoute(method, this.path, handler as unknown as Handler<P, E, S>)
2022-07-02 08:09:45 +02:00
}
})
return this
}
})
Object.assign(this, init)
}
private notFoundHandler: NotFoundHandler<E> = (c: Context<string, E>) => {
return c.text('404 Not Found', 404)
2022-07-02 08:09:45 +02:00
}
private errorHandler: ErrorHandler<E> = (err: Error, c: Context<string, E>) => {
2022-09-27 14:43:55 +02:00
console.trace(err.message)
2022-07-02 08:09:45 +02:00
const message = 'Internal Server Error'
return c.text(message, 500)
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
route(path: string, app?: Hono<any>) {
2022-07-02 08:09:45 +02:00
this._tempPath = path
if (app) {
app.routes.map((r) => {
this.addRoute(r.method, r.path, r.handler as unknown as Handler<P, E, S>)
2022-07-02 08:09:45 +02:00
})
this._tempPath = ''
}
return this
}
use<Path extends string = string, Data extends Partial<Schema> = Schema>(
...middleware: Handler<Path, E, Data>[]
): Hono<E, P, S>
use<Path extends string = string, Data extends Partial<Schema> = Schema>(
arg1: string,
...middleware: Handler<Path, E, Data>[]
): Hono<E, P, S>
use(arg1: string | Handler<P, E, S>, ...handlers: Handler<P, E, S>[]) {
2022-07-02 08:09:45 +02:00
if (typeof arg1 === 'string') {
this.path = arg1
} else {
handlers.unshift(arg1)
}
handlers.map((handler) => {
this.addRoute(METHOD_NAME_ALL, this.path, handler)
})
return this
}
on(method: string, path: string, ...handlers: Handler<P, E, S>[]) {
if (!method) return this
this.path = path
handlers.map((handler) => {
this.addRoute(method.toUpperCase(), this.path, handler)
})
return this
}
onError(handler: ErrorHandler<E>) {
this.errorHandler = handler
2022-07-02 08:09:45 +02:00
return this
}
notFound(handler: NotFoundHandler<E>) {
this.notFoundHandler = handler
2022-07-02 08:09:45 +02:00
return this
}
showRoutes() {
const length = 8
this.routes.map((route) => {
console.log(
`\x1b[32m${route.method}\x1b[0m ${' '.repeat(length - route.method.length)} ${route.path}`
)
})
}
private addRoute(method: string, path: string, handler: Handler<P, E, S>) {
2022-07-02 08:09:45 +02:00
method = method.toUpperCase()
if (this._tempPath) {
path = mergePath(this._tempPath, path)
}
this.router.add(method, path, handler)
const r: Route<P, E, S> = { path: path, method: method, handler: handler }
2022-07-02 08:09:45 +02:00
this.routes.push(r)
}
private matchRoute(method: string, path: string) {
return this.router.match(method, path)
}
private handleError(err: unknown, c: Context<string, E>) {
if (err instanceof Error) {
return this.errorHandler(err, c)
}
throw err
}
private dispatch(
2022-07-02 08:09:45 +02:00
request: Request,
eventOrExecutionCtx?: ExecutionContext | FetchEvent,
env?: E['Bindings']
) {
2022-07-02 08:09:45 +02:00
const path = getPathFromURL(request.url, this.strict)
const method = request.method
const result = this.matchRoute(method, path)
request.paramData = result?.params
const c = new Context<string, E, S>(request, env, eventOrExecutionCtx, this.notFoundHandler)
2022-07-02 08:09:45 +02:00
// Do not `compose` if it has only one handler
if (result && result.handlers.length === 1) {
const handler = result.handlers[0]
let res: ReturnType<Handler<P>>
try {
res = handler(c, async () => {})
if (!res) return this.notFoundHandler(c as Context)
} catch (err) {
return this.handleError(err, c as Context)
}
if (res instanceof Response) return res
return (async () => {
let awaited: Response | undefined | void
try {
awaited = await res
if (!awaited) {
return this.notFoundHandler(c as Context)
}
} catch (err) {
return this.handleError(err, c as Context)
}
return awaited
})()
}
const handlers = result ? result.handlers : [this.notFoundHandler]
const composed = compose<Context<P, E, S>, E>(handlers, this.notFoundHandler, this.errorHandler)
2022-07-02 08:09:45 +02:00
return (async () => {
try {
const tmp = composed(c)
const context = tmp instanceof Promise ? await tmp : tmp
if (!context.finalized) {
throw new Error(
'Context is not finalized. You may forget returning Response object or `await next()`'
)
}
return context.res
} catch (err) {
return this.handleError(err, c as Context)
}
})()
2022-07-02 08:09:45 +02:00
}
handleEvent = (event: FetchEvent) => {
return this.dispatch(event.request, event)
2022-07-02 08:09:45 +02:00
}
fetch = (request: Request, Environment?: E['Bindings'], executionCtx?: ExecutionContext) => {
return this.dispatch(request, executionCtx, Environment)
2022-07-02 08:09:45 +02:00
}
request = async (input: Request | string, requestInit?: RequestInit) => {
2022-07-02 08:09:45 +02:00
const req = input instanceof Request ? input : new Request(input, requestInit)
return await this.fetch(req)
2022-07-02 08:09:45 +02:00
}
}