Comparing version 2.7.0 to 3.0.0-0
@@ -60,3 +60,9 @@ "use strict"; | ||
} | ||
if (res instanceof Response && "response" in res) { | ||
res = res["response"]; | ||
} | ||
if (!(res instanceof Promise)) { | ||
if (res !== void 0 && "response" in res) { | ||
res = res["response"]; | ||
} | ||
if (res && (context.finalized === false || isError)) { | ||
@@ -63,0 +69,0 @@ context.res = res; |
@@ -24,5 +24,8 @@ "use strict"; | ||
module.exports = __toCommonJS(context_exports); | ||
var import_request = require("./request"); | ||
var import_cookie = require("./utils/cookie"); | ||
class Context { | ||
constructor(req, env = {}, executionCtx = void 0, notFoundHandler = () => new Response()) { | ||
constructor(req, options) { | ||
this.env = {}; | ||
this.finalized = false; | ||
this.error = void 0; | ||
@@ -32,8 +35,21 @@ this._status = 200; | ||
this._prettySpace = 2; | ||
this._executionCtx = executionCtx; | ||
this.req = req; | ||
this.env = env; | ||
this.notFoundHandler = notFoundHandler; | ||
this.finalized = false; | ||
this.notFoundHandler = () => new Response(); | ||
this.rawRequest = req; | ||
if (options) { | ||
this._executionCtx = options.executionCtx; | ||
this._paramData = options.paramData; | ||
this.env = options.env; | ||
if (options.notFoundHandler) { | ||
this.notFoundHandler = options.notFoundHandler; | ||
} | ||
} | ||
} | ||
get req() { | ||
if (this._req) { | ||
return this._req; | ||
} else { | ||
this._req = new import_request.HonoRequest(this.rawRequest, this._paramData); | ||
return this._req; | ||
} | ||
} | ||
get event() { | ||
@@ -150,2 +166,9 @@ if (this._executionCtx instanceof FetchEvent) { | ||
} | ||
jsonT(object, status = this._status, headers = {}) { | ||
return { | ||
response: this.json(object, status, headers), | ||
data: object, | ||
format: "json" | ||
}; | ||
} | ||
html(html, status = this._status, headers = {}) { | ||
@@ -152,0 +175,0 @@ headers["content-type"] = "text/html; charset=UTF-8"; |
@@ -26,3 +26,2 @@ "use strict"; | ||
var import_context = require("./context"); | ||
var import_request = require("./request"); | ||
var import_router = require("./router"); | ||
@@ -56,2 +55,6 @@ var import_reg_exp_router = require("./router/reg-exp-router"); | ||
}; | ||
this.build = () => { | ||
const app = {}; | ||
return {}; | ||
}; | ||
this.handleEvent = (event) => { | ||
@@ -67,3 +70,2 @@ return this.dispatch(event.request, event); | ||
}; | ||
(0, import_request.extendRequestPrototype)(); | ||
const allMethods = [...import_router.METHODS, import_router.METHOD_NAME_ALL_LOWERCASE]; | ||
@@ -155,4 +157,9 @@ allMethods.map((method) => { | ||
const result = this.matchRoute(method, path); | ||
request.paramData = result?.params; | ||
const c = new import_context.Context(request, env, eventOrExecutionCtx, this.notFoundHandler); | ||
const paramData = result?.params; | ||
const c = new import_context.Context(request, { | ||
env, | ||
executionCtx: eventOrExecutionCtx, | ||
notFoundHandler: this.notFoundHandler, | ||
paramData | ||
}); | ||
if (result && result.handlers.length === 1) { | ||
@@ -164,9 +171,14 @@ const handler = result.handlers[0]; | ||
}); | ||
if (!res) | ||
if (!res) { | ||
return this.notFoundHandler(c); | ||
} | ||
} catch (err) { | ||
return this.handleError(err, c); | ||
} | ||
if (res instanceof Response) | ||
if ("response" in res) { | ||
res = res.response; | ||
} | ||
if (res instanceof Response) { | ||
return res; | ||
} | ||
return (async () => { | ||
@@ -176,2 +188,5 @@ let awaited; | ||
awaited = await res; | ||
if (awaited !== void 0 && "response" in awaited) { | ||
awaited = awaited["response"]; | ||
} | ||
if (!awaited) { | ||
@@ -178,0 +193,0 @@ return this.notFoundHandler(c); |
@@ -33,3 +33,3 @@ "use strict"; | ||
return async (c, next) => { | ||
const key = c.req; | ||
const key = c.req.raw; | ||
const cache2 = await caches.open(options.cacheName); | ||
@@ -36,0 +36,0 @@ const response = await cache2.match(key); |
@@ -21,6 +21,33 @@ "use strict"; | ||
__export(validator_exports, { | ||
validator: () => import_middleware.validatorMiddleware | ||
validator: () => validator | ||
}); | ||
module.exports = __toCommonJS(validator_exports); | ||
var import_middleware = require("./middleware"); | ||
var import_object = require("../../utils/object"); | ||
const validator = (type, validationFunc) => { | ||
return async (c, next) => { | ||
let value = {}; | ||
switch (type) { | ||
case "json": | ||
value = await c.req.json(); | ||
break; | ||
case "form": | ||
value = await c.req.parseBody(); | ||
break; | ||
case "query": | ||
value = c.req.query(); | ||
break; | ||
case "queries": | ||
value = c.req.queries(); | ||
break; | ||
} | ||
const res = validationFunc(value, c); | ||
if (res instanceof Response || res instanceof Promise) { | ||
return res; | ||
} | ||
const target = c.req.valid(); | ||
const newObject = (0, import_object.mergeObjects)(target, res); | ||
c.req.valid(newObject); | ||
await next(); | ||
}; | ||
}; | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -27,0 +54,0 @@ 0 && (module.exports = { |
@@ -21,3 +21,3 @@ "use strict"; | ||
__export(request_exports, { | ||
extendRequestPrototype: () => extendRequestPrototype | ||
HonoRequest: () => HonoRequest | ||
}); | ||
@@ -28,7 +28,9 @@ module.exports = __toCommonJS(request_exports); | ||
var import_url = require("./utils/url"); | ||
function extendRequestPrototype() { | ||
if (!!Request.prototype.param) { | ||
return; | ||
class HonoRequest { | ||
constructor(request, paramData) { | ||
this.raw = request; | ||
this.paramData = paramData; | ||
this.data = {}; | ||
} | ||
Request.prototype.param = function(key) { | ||
param(key) { | ||
if (this.paramData) { | ||
@@ -41,3 +43,3 @@ if (key) { | ||
for (const [key2, value] of Object.entries(this.paramData)) { | ||
if (value) { | ||
if (value && typeof value === "string") { | ||
decoded[key2] = decodeURIComponent(value); | ||
@@ -50,17 +52,4 @@ } | ||
return null; | ||
}; | ||
Request.prototype.header = function(name) { | ||
if (!this.headerData) { | ||
this.headerData = {}; | ||
this.headers.forEach((value, key) => { | ||
this.headerData[key] = value; | ||
}); | ||
} | ||
if (name) { | ||
return this.headerData[name.toLowerCase()]; | ||
} else { | ||
return this.headerData; | ||
} | ||
}; | ||
Request.prototype.query = function(key) { | ||
} | ||
query(key) { | ||
const queryString = (0, import_url.getQueryStringFromURL)(this.url); | ||
@@ -79,4 +68,4 @@ const searchParams = new URLSearchParams(queryString); | ||
} | ||
}; | ||
Request.prototype.queries = function(key) { | ||
} | ||
queries(key) { | ||
const queryString = (0, import_url.getQueryStringFromURL)(this.url); | ||
@@ -93,5 +82,20 @@ const searchParams = new URLSearchParams(queryString); | ||
} | ||
}; | ||
Request.prototype.cookie = function(key) { | ||
const cookie = this.headers.get("Cookie") || ""; | ||
} | ||
header(name) { | ||
if (!this.headerData) { | ||
this.headerData = {}; | ||
this.raw.headers.forEach((value, key) => { | ||
this.headerData[key] = value; | ||
}); | ||
} | ||
if (name) { | ||
return this.headerData[name.toLowerCase()]; | ||
} else { | ||
return this.headerData; | ||
} | ||
} | ||
cookie(key) { | ||
const cookie = this.raw.headers.get("Cookie"); | ||
if (!cookie) | ||
return; | ||
const obj = (0, import_cookie.parse)(cookie); | ||
@@ -104,7 +108,7 @@ if (key) { | ||
} | ||
}; | ||
Request.prototype.parseBody = async function() { | ||
} | ||
async parseBody() { | ||
let body; | ||
if (!this.bodyData) { | ||
body = await (0, import_body.parseBody)(this); | ||
body = await (0, import_body.parseBody)(this.raw); | ||
this.bodyData = body; | ||
@@ -115,7 +119,7 @@ } else { | ||
return body; | ||
}; | ||
Request.prototype.json = async function() { | ||
} | ||
async json() { | ||
let jsonData; | ||
if (!this.jsonData) { | ||
jsonData = JSON.parse(await this.text()); | ||
jsonData = this.raw.json(); | ||
this.jsonData = jsonData; | ||
@@ -126,4 +130,16 @@ } else { | ||
return jsonData; | ||
}; | ||
Request.prototype.valid = function(data) { | ||
} | ||
async text() { | ||
return this.raw.text(); | ||
} | ||
async arrayBuffer() { | ||
return this.raw.arrayBuffer(); | ||
} | ||
async blob() { | ||
return this.raw.blob(); | ||
} | ||
async formData() { | ||
return this.raw.formData(); | ||
} | ||
valid(data) { | ||
if (!this.data) { | ||
@@ -136,7 +152,49 @@ this.data = {}; | ||
return this.data; | ||
}; | ||
} | ||
get url() { | ||
return this.raw.url; | ||
} | ||
get method() { | ||
return this.raw.method; | ||
} | ||
get headers() { | ||
return this.raw.headers; | ||
} | ||
get redirect() { | ||
return this.raw.redirect; | ||
} | ||
get body() { | ||
return this.raw.body; | ||
} | ||
get bodyUsed() { | ||
return this.raw.bodyUsed; | ||
} | ||
get cache() { | ||
return this.raw.cache; | ||
} | ||
get credentials() { | ||
return this.raw.credentials; | ||
} | ||
get integrity() { | ||
return this.raw.integrity; | ||
} | ||
get keepalive() { | ||
return this.raw.keepalive; | ||
} | ||
get mode() { | ||
return this.raw.mode; | ||
} | ||
get referrer() { | ||
return this.raw.referrer; | ||
} | ||
get refererPolicy() { | ||
return this.raw.referrerPolicy; | ||
} | ||
get signal() { | ||
return this.raw.signal; | ||
} | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
extendRequestPrototype | ||
HonoRequest | ||
}); |
@@ -38,3 +38,9 @@ // src/compose.ts | ||
} | ||
if (res instanceof Response && "response" in res) { | ||
res = res["response"]; | ||
} | ||
if (!(res instanceof Promise)) { | ||
if (res !== void 0 && "response" in res) { | ||
res = res["response"]; | ||
} | ||
if (res && (context.finalized === false || isError)) { | ||
@@ -41,0 +47,0 @@ context.res = res; |
// src/context.ts | ||
import { HonoRequest } from "./request.js"; | ||
import { serialize } from "./utils/cookie.js"; | ||
var Context = class { | ||
constructor(req, env = {}, executionCtx = void 0, notFoundHandler = () => new Response()) { | ||
constructor(req, options) { | ||
this.env = {}; | ||
this.finalized = false; | ||
this.error = void 0; | ||
@@ -9,8 +12,21 @@ this._status = 200; | ||
this._prettySpace = 2; | ||
this._executionCtx = executionCtx; | ||
this.req = req; | ||
this.env = env; | ||
this.notFoundHandler = notFoundHandler; | ||
this.finalized = false; | ||
this.notFoundHandler = () => new Response(); | ||
this.rawRequest = req; | ||
if (options) { | ||
this._executionCtx = options.executionCtx; | ||
this._paramData = options.paramData; | ||
this.env = options.env; | ||
if (options.notFoundHandler) { | ||
this.notFoundHandler = options.notFoundHandler; | ||
} | ||
} | ||
} | ||
get req() { | ||
if (this._req) { | ||
return this._req; | ||
} else { | ||
this._req = new HonoRequest(this.rawRequest, this._paramData); | ||
return this._req; | ||
} | ||
} | ||
get event() { | ||
@@ -127,2 +143,9 @@ if (this._executionCtx instanceof FetchEvent) { | ||
} | ||
jsonT(object, status = this._status, headers = {}) { | ||
return { | ||
response: this.json(object, status, headers), | ||
data: object, | ||
format: "json" | ||
}; | ||
} | ||
html(html, status = this._status, headers = {}) { | ||
@@ -129,0 +152,0 @@ headers["content-type"] = "text/html; charset=UTF-8"; |
// src/hono.ts | ||
import { compose } from "./compose.js"; | ||
import { Context } from "./context.js"; | ||
import { extendRequestPrototype } from "./request.js"; | ||
import { METHOD_NAME_ALL, METHOD_NAME_ALL_LOWERCASE, METHODS } from "./router.js"; | ||
@@ -33,2 +32,6 @@ import { RegExpRouter } from "./router/reg-exp-router/index.js"; | ||
}; | ||
this.build = () => { | ||
const app = {}; | ||
return {}; | ||
}; | ||
this.handleEvent = (event) => { | ||
@@ -44,3 +47,2 @@ return this.dispatch(event.request, event); | ||
}; | ||
extendRequestPrototype(); | ||
const allMethods = [...METHODS, METHOD_NAME_ALL_LOWERCASE]; | ||
@@ -132,4 +134,9 @@ allMethods.map((method) => { | ||
const result = this.matchRoute(method, path); | ||
request.paramData = result?.params; | ||
const c = new Context(request, env, eventOrExecutionCtx, this.notFoundHandler); | ||
const paramData = result?.params; | ||
const c = new Context(request, { | ||
env, | ||
executionCtx: eventOrExecutionCtx, | ||
notFoundHandler: this.notFoundHandler, | ||
paramData | ||
}); | ||
if (result && result.handlers.length === 1) { | ||
@@ -141,9 +148,14 @@ const handler = result.handlers[0]; | ||
}); | ||
if (!res) | ||
if (!res) { | ||
return this.notFoundHandler(c); | ||
} | ||
} catch (err) { | ||
return this.handleError(err, c); | ||
} | ||
if (res instanceof Response) | ||
if ("response" in res) { | ||
res = res.response; | ||
} | ||
if (res instanceof Response) { | ||
return res; | ||
} | ||
return (async () => { | ||
@@ -153,2 +165,5 @@ let awaited; | ||
awaited = await res; | ||
if (awaited !== void 0 && "response" in awaited) { | ||
awaited = awaited["response"]; | ||
} | ||
if (!awaited) { | ||
@@ -155,0 +170,0 @@ return this.notFoundHandler(c); |
@@ -11,3 +11,3 @@ // src/middleware/cache/index.ts | ||
return async (c, next) => { | ||
const key = c.req; | ||
const key = c.req.raw; | ||
const cache2 = await caches.open(options.cacheName); | ||
@@ -14,0 +14,0 @@ const response = await cache2.match(key); |
// src/middleware/validator/index.ts | ||
import { validatorMiddleware } from "./middleware.js"; | ||
import { mergeObjects } from "../../utils/object.js"; | ||
var validator = (type, validationFunc) => { | ||
return async (c, next) => { | ||
let value = {}; | ||
switch (type) { | ||
case "json": | ||
value = await c.req.json(); | ||
break; | ||
case "form": | ||
value = await c.req.parseBody(); | ||
break; | ||
case "query": | ||
value = c.req.query(); | ||
break; | ||
case "queries": | ||
value = c.req.queries(); | ||
break; | ||
} | ||
const res = validationFunc(value, c); | ||
if (res instanceof Response || res instanceof Promise) { | ||
return res; | ||
} | ||
const target = c.req.valid(); | ||
const newObject = mergeObjects(target, res); | ||
c.req.valid(newObject); | ||
await next(); | ||
}; | ||
}; | ||
export { | ||
validatorMiddleware as validator | ||
validator | ||
}; |
@@ -5,7 +5,9 @@ // src/request.ts | ||
import { getQueryStringFromURL } from "./utils/url.js"; | ||
function extendRequestPrototype() { | ||
if (!!Request.prototype.param) { | ||
return; | ||
var HonoRequest = class { | ||
constructor(request, paramData) { | ||
this.raw = request; | ||
this.paramData = paramData; | ||
this.data = {}; | ||
} | ||
Request.prototype.param = function(key) { | ||
param(key) { | ||
if (this.paramData) { | ||
@@ -18,3 +20,3 @@ if (key) { | ||
for (const [key2, value] of Object.entries(this.paramData)) { | ||
if (value) { | ||
if (value && typeof value === "string") { | ||
decoded[key2] = decodeURIComponent(value); | ||
@@ -27,17 +29,4 @@ } | ||
return null; | ||
}; | ||
Request.prototype.header = function(name) { | ||
if (!this.headerData) { | ||
this.headerData = {}; | ||
this.headers.forEach((value, key) => { | ||
this.headerData[key] = value; | ||
}); | ||
} | ||
if (name) { | ||
return this.headerData[name.toLowerCase()]; | ||
} else { | ||
return this.headerData; | ||
} | ||
}; | ||
Request.prototype.query = function(key) { | ||
} | ||
query(key) { | ||
const queryString = getQueryStringFromURL(this.url); | ||
@@ -56,4 +45,4 @@ const searchParams = new URLSearchParams(queryString); | ||
} | ||
}; | ||
Request.prototype.queries = function(key) { | ||
} | ||
queries(key) { | ||
const queryString = getQueryStringFromURL(this.url); | ||
@@ -70,5 +59,20 @@ const searchParams = new URLSearchParams(queryString); | ||
} | ||
}; | ||
Request.prototype.cookie = function(key) { | ||
const cookie = this.headers.get("Cookie") || ""; | ||
} | ||
header(name) { | ||
if (!this.headerData) { | ||
this.headerData = {}; | ||
this.raw.headers.forEach((value, key) => { | ||
this.headerData[key] = value; | ||
}); | ||
} | ||
if (name) { | ||
return this.headerData[name.toLowerCase()]; | ||
} else { | ||
return this.headerData; | ||
} | ||
} | ||
cookie(key) { | ||
const cookie = this.raw.headers.get("Cookie"); | ||
if (!cookie) | ||
return; | ||
const obj = parse(cookie); | ||
@@ -81,7 +85,7 @@ if (key) { | ||
} | ||
}; | ||
Request.prototype.parseBody = async function() { | ||
} | ||
async parseBody() { | ||
let body; | ||
if (!this.bodyData) { | ||
body = await parseBody(this); | ||
body = await parseBody(this.raw); | ||
this.bodyData = body; | ||
@@ -92,7 +96,7 @@ } else { | ||
return body; | ||
}; | ||
Request.prototype.json = async function() { | ||
} | ||
async json() { | ||
let jsonData; | ||
if (!this.jsonData) { | ||
jsonData = JSON.parse(await this.text()); | ||
jsonData = this.raw.json(); | ||
this.jsonData = jsonData; | ||
@@ -103,4 +107,16 @@ } else { | ||
return jsonData; | ||
}; | ||
Request.prototype.valid = function(data) { | ||
} | ||
async text() { | ||
return this.raw.text(); | ||
} | ||
async arrayBuffer() { | ||
return this.raw.arrayBuffer(); | ||
} | ||
async blob() { | ||
return this.raw.blob(); | ||
} | ||
async formData() { | ||
return this.raw.formData(); | ||
} | ||
valid(data) { | ||
if (!this.data) { | ||
@@ -113,6 +129,48 @@ this.data = {}; | ||
return this.data; | ||
}; | ||
} | ||
} | ||
get url() { | ||
return this.raw.url; | ||
} | ||
get method() { | ||
return this.raw.method; | ||
} | ||
get headers() { | ||
return this.raw.headers; | ||
} | ||
get redirect() { | ||
return this.raw.redirect; | ||
} | ||
get body() { | ||
return this.raw.body; | ||
} | ||
get bodyUsed() { | ||
return this.raw.bodyUsed; | ||
} | ||
get cache() { | ||
return this.raw.cache; | ||
} | ||
get credentials() { | ||
return this.raw.credentials; | ||
} | ||
get integrity() { | ||
return this.raw.integrity; | ||
} | ||
get keepalive() { | ||
return this.raw.keepalive; | ||
} | ||
get mode() { | ||
return this.raw.mode; | ||
} | ||
get referrer() { | ||
return this.raw.referrer; | ||
} | ||
get refererPolicy() { | ||
return this.raw.referrerPolicy; | ||
} | ||
get signal() { | ||
return this.raw.signal; | ||
} | ||
}; | ||
export { | ||
extendRequestPrototype | ||
HonoRequest | ||
}; |
@@ -6,3 +6,3 @@ import type { Environment, NotFoundHandler, ErrorHandler } from './types'; | ||
} | ||
export declare const compose: <C extends ComposeContext, E extends Partial<Environment> = Environment>(middleware: Function[], onNotFound?: NotFoundHandler<E> | undefined, onError?: ErrorHandler<E> | undefined) => (context: C, next?: Function) => C | Promise<C>; | ||
export declare const compose: <C extends ComposeContext, E extends Partial<Environment> = Environment>(middleware: Function[], onNotFound?: NotFoundHandler<E, import("./types").Route> | undefined, onError?: ErrorHandler<E> | undefined) => (context: C, next?: Function) => C | Promise<C>; | ||
export {}; |
@@ -1,14 +0,26 @@ | ||
import type { ExecutionContext } from './types'; | ||
import type { Environment, NotFoundHandler, ContextVariableMap } from './types'; | ||
import { HonoRequest } from './request'; | ||
import type { TypeResponse, Route } from './types'; | ||
import type { Environment, NotFoundHandler } from './types'; | ||
import type { CookieOptions } from './utils/cookie'; | ||
import type { StatusCode } from './utils/http-status'; | ||
import type { Schema, SchemaToProp } from './validator/schema'; | ||
declare type Runtime = 'node' | 'deno' | 'bun' | 'cloudflare' | 'fastly' | 'vercel' | 'lagon' | 'other'; | ||
declare type Headers = Record<string, string | string[]>; | ||
declare type Runtime = 'node' | 'deno' | 'bun' | 'cloudflare' | 'fastly' | 'vercel' | 'lagon' | 'other'; | ||
export declare type Data = string | ArrayBuffer | ReadableStream; | ||
export declare class Context<P extends string = string, E extends Partial<Environment> = Environment, S = any> { | ||
req: Request<unknown, P, S extends Schema ? SchemaToProp<S> : S>; | ||
declare type Data = string | ArrayBuffer | ReadableStream; | ||
export interface ExecutionContext { | ||
waitUntil(promise: Promise<void>): void; | ||
passThroughOnException(): void; | ||
} | ||
export interface ContextVariableMap { | ||
} | ||
declare type ContextOptions<E extends Partial<Environment>, R extends Route> = { | ||
env?: E['Bindings']; | ||
executionCtx?: FetchEvent | ExecutionContext | undefined; | ||
notFoundHandler?: NotFoundHandler<E, R>; | ||
paramData?: Record<string, string>; | ||
}; | ||
export declare class Context<E extends Partial<Environment> = Environment, R extends Route = Route, I = any> { | ||
env: E['Bindings']; | ||
finalized: boolean; | ||
error: Error | undefined; | ||
private _req?; | ||
private _status; | ||
@@ -21,4 +33,7 @@ private _executionCtx; | ||
private _res; | ||
private _paramData; | ||
private rawRequest; | ||
private notFoundHandler; | ||
constructor(req: Request<unknown, P>, env?: E['Bindings'], executionCtx?: FetchEvent | ExecutionContext | undefined, notFoundHandler?: NotFoundHandler<E>); | ||
constructor(req: Request, options?: ContextOptions<E, R>); | ||
get req(): HonoRequest<R, I>; | ||
get event(): FetchEvent; | ||
@@ -35,4 +50,4 @@ get executionCtx(): ExecutionContext; | ||
set(key: string, value: unknown): void; | ||
get<Key extends keyof E['Variables']>(key: Key): E['Variables'][Key]; | ||
get<Key extends keyof ContextVariableMap>(key: Key): ContextVariableMap[Key]; | ||
get<Key extends keyof E['Variables']>(key: Key): E['Variables'][Key]; | ||
get<T>(key: string): T; | ||
@@ -45,2 +60,3 @@ pretty(prettyJSON: boolean, space?: number): void; | ||
json<T>(object: T, status?: StatusCode, headers?: Headers): Response; | ||
jsonT<T>(object: T, status?: StatusCode, headers?: Headers): TypeResponse<T>; | ||
html(html: string, status?: StatusCode, headers?: Headers): Response; | ||
@@ -47,0 +63,0 @@ redirect(location: string, status?: StatusCode): Response; |
@@ -0,32 +1,26 @@ | ||
import type { ExecutionContext } from './context'; | ||
import type { Router } from './router'; | ||
import type { ExecutionContext } from './types'; | ||
import type { Handler, Environment, ParamKeys, ErrorHandler, NotFoundHandler } from './types'; | ||
interface HandlerInterface<P extends string, E extends Partial<Environment> = Environment, S = unknown, U = Hono<E, P, S>> { | ||
<Path extends string, Data = S>(...handlers: Handler<ParamKeys<Path> extends never ? string : ParamKeys<Path>, E, Data>[]): Hono<E, Path, Data & S>; | ||
(...handlers: Handler<P, E, S>[]): U; | ||
<Path extends string, Data = S>(path: Path, ...handlers: Handler<ParamKeys<Path> extends never ? string : ParamKeys<Path>, E, Data>[]): Hono<E, Path, Data & S>; | ||
<Path extends string, Data = S>(path: Path, ...handlers: Handler<string, E, Data>[]): Hono<E, string, Data & S>; | ||
(path: string, ...handlers: Handler<P, E, S>[]): U; | ||
} | ||
interface Route<P extends string = string, E extends Partial<Environment> = Environment, S = unknown> { | ||
import type { HandlerInterface, ToAppType, Handler, ErrorHandler, NotFoundHandler, Environment, Route } from './types'; | ||
interface RouterRoute { | ||
path: string; | ||
method: string; | ||
handler: Handler<P, E, S>; | ||
handler: Handler; | ||
} | ||
declare const Hono_base: new <E_1 extends Partial<Environment> = Environment, P_1 extends string = string, S_1 = unknown, U = Hono<E_1, P_1, S_1>>() => { | ||
all: HandlerInterface<P_1, E_1, S_1, U>; | ||
get: HandlerInterface<P_1, E_1, S_1, U>; | ||
post: HandlerInterface<P_1, E_1, S_1, U>; | ||
put: HandlerInterface<P_1, E_1, S_1, U>; | ||
delete: HandlerInterface<P_1, E_1, S_1, U>; | ||
head: HandlerInterface<P_1, E_1, S_1, U>; | ||
options: HandlerInterface<P_1, E_1, S_1, U>; | ||
patch: HandlerInterface<P_1, E_1, S_1, U>; | ||
declare const Hono_base: new <E_1 extends Partial<Environment> = Partial<Environment>, _M extends string = string, P extends string = string, I_1 = unknown, O_1 = unknown>() => { | ||
all: HandlerInterface<E_1, "all", P, I_1, O_1>; | ||
get: HandlerInterface<E_1, "get", P, I_1, O_1>; | ||
post: HandlerInterface<E_1, "post", P, I_1, O_1>; | ||
put: HandlerInterface<E_1, "put", P, I_1, O_1>; | ||
delete: HandlerInterface<E_1, "delete", P, I_1, O_1>; | ||
head: HandlerInterface<E_1, "head", P, I_1, O_1>; | ||
options: HandlerInterface<E_1, "options", P, I_1, O_1>; | ||
patch: HandlerInterface<E_1, "patch", P, I_1, O_1>; | ||
}; | ||
export declare class Hono<E extends Partial<Environment> = Environment, P extends string = string, S = unknown> extends Hono_base<E, P, S, Hono<E, P, S>> { | ||
readonly router: Router<Handler<P, E, S>>; | ||
export declare class Hono<E extends Partial<Environment> = Partial<Environment>, R extends Route = Route, I = any, // should be any | ||
O = unknown> extends Hono_base<E, R['method'], R['path'], I, O> { | ||
readonly router: Router<Handler>; | ||
readonly strict: boolean; | ||
private _tempPath; | ||
private path; | ||
routes: Route<P, E, S>[]; | ||
routes: RouterRoute[]; | ||
constructor(init?: Partial<Pick<Hono, 'router' | 'strict'>>); | ||
@@ -36,5 +30,18 @@ private notFoundHandler; | ||
route(path: string, app?: Hono<any>): this; | ||
use<Path extends string = string, Data = unknown>(...middleware: Handler<Path, E, Data>[]): Hono<E, P, S>; | ||
use<Path extends string = string, Data = unknown>(arg1: string, ...middleware: Handler<Path, E, Data>[]): Hono<E, P, S>; | ||
on(method: string, path: string, ...handlers: Handler<P, E, S>[]): this; | ||
use(...middleware: Handler<E>[]): Hono<E, { | ||
method: 'all'; | ||
path: string; | ||
}, I, O>; | ||
use<Path extends string, E2 extends Partial<Environment> = E>(arg1: Path, ...middleware: Handler<E2>[]): Hono<E, { | ||
method: 'all'; | ||
path: Path; | ||
}, I, O>; | ||
on<Method extends string, Path extends string>(method: Method, path: Path, ...handlers: Handler<E, { | ||
method: Method; | ||
path: Path; | ||
}>[]): Hono<E, { | ||
method: Method; | ||
path: Path; | ||
}, I, O>; | ||
build: () => ToAppType<typeof this>; | ||
onError(handler: ErrorHandler<E>): this; | ||
@@ -41,0 +48,0 @@ notFound(handler: NotFoundHandler<E>): this; |
@@ -1,6 +0,4 @@ | ||
/// <reference path="request.d.ts" /> | ||
import { Hono } from './hono'; | ||
export type { Next, ContextVariableMap, MiddlewareHandler, ErrorHandler, NotFoundHandler, } from './types'; | ||
export type { Context } from './context'; | ||
export type { Validator } from './validator/validator'; | ||
export type { Next, MiddlewareHandler, ErrorHandler, NotFoundHandler } from './types'; | ||
export type { Context, ContextVariableMap } from './context'; | ||
import type { CustomHandler } from './types'; | ||
@@ -7,0 +5,0 @@ export type { CustomHandler as Handler }; |
import type { ServeStaticOptions } from './serve-static'; | ||
declare const module: (options?: ServeStaticOptions) => import("../..").MiddlewareHandler<string, import("../../types").Environment, any>; | ||
declare const module: (options?: ServeStaticOptions) => import("../..").MiddlewareHandler<Partial<import("../../types").Environment>, import("../../types").Route, unknown>; | ||
export { module as serveStatic }; |
@@ -1,2 +0,8 @@ | ||
import { validatorMiddleware } from './middleware'; | ||
export { validatorMiddleware as validator }; | ||
import type { Context } from '../../context'; | ||
import type { Environment, Next, Route, ValidationTypes } from '../../types'; | ||
declare type ValidatorHandler<E extends Partial<Environment>, R extends Route = Route, I = unknown> = (c: Context<E, R, I>, next: Next) => Promise<Response | undefined | void> | Response; | ||
export declare const validator: <T, U extends ValidationTypes, V extends { | ||
type: U; | ||
data: T; | ||
}, V2 = {}, E extends Partial<Environment> = Environment>(type: U, validationFunc: (value: unknown, c: Context<E, Route, any>) => Response | Promise<Response> | T) => ValidatorHandler<E, Route, V | V2>; | ||
export {}; |
@@ -0,39 +1,44 @@ | ||
import type { GetParamKeys, InputToData, Route } from './types'; | ||
import type { BodyData } from './utils/body'; | ||
import type { Cookie } from './utils/cookie'; | ||
declare global { | ||
interface Request<CfHostMetadata = unknown, ParamKeyType extends string = string, Data = any> { | ||
paramData?: Record<ParamKeyType, string>; | ||
param: { | ||
(key: ParamKeyType): string; | ||
(): Record<ParamKeyType, string>; | ||
}; | ||
queryData?: Record<string, string>; | ||
query: { | ||
(key: string): string; | ||
(): Record<string, string>; | ||
}; | ||
queries: { | ||
(key: string): string[]; | ||
(): Record<string, string[]>; | ||
}; | ||
headerData?: Record<string, string>; | ||
header: { | ||
(name: string): string; | ||
(): Record<string, string>; | ||
}; | ||
cookie: { | ||
(name: string): string | undefined; | ||
(): Cookie; | ||
}; | ||
bodyData?: BodyData; | ||
parseBody<BodyType extends BodyData>(): Promise<BodyType>; | ||
jsonData?: any; | ||
json<T>(): Promise<T>; | ||
data: Data; | ||
valid: { | ||
(data: Data): Data; | ||
(): Data; | ||
}; | ||
} | ||
export declare class HonoRequest<R extends Route = Route, I = any> { | ||
raw: Request; | ||
private paramData; | ||
private headerData; | ||
private queryData; | ||
private bodyData; | ||
private jsonData; | ||
private data; | ||
constructor(request: Request, paramData?: Record<string, string> | undefined); | ||
param(key: GetParamKeys<R['path']>): string; | ||
param(): Record<GetParamKeys<R['path']>, string>; | ||
query(key: string): string; | ||
query(): Record<string, string>; | ||
queries(key: string): string[]; | ||
queries(): Record<string, string[]>; | ||
header(name: string): string; | ||
header(): Record<string, string>; | ||
cookie(key: string): string | undefined; | ||
cookie(): Cookie; | ||
parseBody<BodyType extends BodyData>(): Promise<BodyType>; | ||
json<JSONData = unknown>(): Promise<Partial<JSONData>>; | ||
text(): Promise<string>; | ||
arrayBuffer(): Promise<ArrayBuffer>; | ||
blob(): Promise<Blob>; | ||
formData(): Promise<FormData>; | ||
valid(data?: unknown): InputToData<I>; | ||
get url(): string; | ||
get method(): string; | ||
get headers(): Headers; | ||
get redirect(): RequestRedirect; | ||
get body(): ReadableStream<Uint8Array> | null; | ||
get bodyUsed(): boolean; | ||
get cache(): RequestCache; | ||
get credentials(): RequestCredentials; | ||
get integrity(): string; | ||
get keepalive(): boolean; | ||
get mode(): RequestMode; | ||
get referrer(): string; | ||
get refererPolicy(): ReferrerPolicy; | ||
get signal(): AbortSignal; | ||
} | ||
export declare function extendRequestPrototype(): void; |
import type { Context } from './context'; | ||
export interface ContextVariableMap { | ||
} | ||
import type { Hono } from './hono'; | ||
import type { UnionToIntersection } from './utils/types'; | ||
export declare type Bindings = Record<string, any>; | ||
@@ -10,17 +10,90 @@ export declare type Variables = Record<string, any>; | ||
}; | ||
export declare type Handler<P extends string = string, E extends Partial<Environment> = Environment, S = any> = (c: Context<P, E, S>, next: Next) => Response | Promise<Response | undefined | void>; | ||
export declare type MiddlewareHandler<P extends string = string, E extends Partial<Environment> = Environment, S = any> = (c: Context<P, E, S>, next: Next) => Promise<Response | undefined | void>; | ||
export declare type NotFoundHandler<E extends Partial<Environment> = Environment> = (c: Context<string, E>) => Response | Promise<Response>; | ||
export declare type ErrorHandler<E extends Partial<Environment> = Environment> = (err: Error, c: Context<string, E>) => Response; | ||
export declare type Next = () => Promise<void>; | ||
declare type Env = Partial<Environment>; | ||
export declare type Route = { | ||
path: string; | ||
method: string; | ||
}; | ||
export declare type Handler<E extends Env = Environment, R extends Route = Route, I = any, // should be any | ||
O = unknown> = (c: Context<E, R, I>, next: Next) => Response | Promise<Response | undefined | void> | TypeResponse<O> | Promise<TypeResponse<O>>; | ||
export interface HandlerInterface<E extends Env = Env, M extends string = string, P extends string = string, _I = any, _O = unknown> { | ||
<I2, O2>(...handlers: Handler<E, { | ||
method: M; | ||
path: string; | ||
}, I2, O2>[]): Hono<E, { | ||
method: M; | ||
path: string; | ||
}, I2, O2>; | ||
(...handlers: Handler<any, any>[]): Hono; | ||
<I2, O2, Path extends string = P>(path: Path, ...handlers: Handler<E, { | ||
method: M; | ||
path: Path; | ||
}, I2, O2>[]): Hono<E, { | ||
method: M; | ||
path: Path; | ||
}, I2, O2>; | ||
<I2, O2, Path extends string>(path: Path, ...handlers: Handler<any, any, I2, O2>[]): Hono<E, { | ||
method: M; | ||
path: Path; | ||
}, I2, O2>; | ||
} | ||
export declare type ExtractType<T> = T extends TypeResponse<infer R> ? R : T extends Promise<TypeResponse<infer R>> ? R : never; | ||
declare type ParamKeyName<NameWithPattern> = NameWithPattern extends `${infer Name}{${infer _Pattern}` ? Name : NameWithPattern; | ||
declare type ParamKey<Component> = Component extends `:${infer NameWithPattern}` ? ParamKeyName<NameWithPattern> : never; | ||
export declare type ParamKeys<Path> = Path extends `${infer Component}/${infer Rest}` ? ParamKey<Component> | ParamKeys<Rest> : ParamKey<Path>; | ||
export interface CustomHandler<P = string, E = Partial<Environment>, S = any> { | ||
(c: Context<P extends string ? P : string, P extends Partial<Environment> ? P : E extends Partial<Environment> ? E : never, P extends string ? E extends Partial<Environment> ? S : P extends Partial<Environment> ? E : never : P extends Partial<Environment> ? E extends Partial<Environment> ? S : E : P>, next: Next): Response | Promise<Response | undefined | void>; | ||
declare type ParamKeys<Path> = Path extends `${infer Component}/${infer Rest}` ? ParamKey<Component> | ParamKeys<Rest> : ParamKey<Path>; | ||
export declare type GetParamKeys<Path> = ParamKeys<Path> extends never ? Path : ParamKeys<Path>; | ||
export declare type MiddlewareHandler<E extends Env = Env, R extends Route = Route, S = unknown> = (c: Context<E, R, S>, next: Next) => Promise<Response | undefined | void>; | ||
export declare type NotFoundHandler<E extends Env = Environment, R extends Route = Route> = (c: Context<E, R>) => Response | Promise<Response>; | ||
export declare type ErrorHandler<E extends Env = Environment> = (err: Error, c: Context<E>) => Response; | ||
export declare type Next = () => Promise<void>; | ||
export declare type TypeResponse<T = unknown> = { | ||
response: Response | Promise<Response>; | ||
data: T; | ||
format: 'json'; | ||
}; | ||
export interface CustomHandler<E = Env, R = Route, I = any> { | ||
(c: Context<E extends Env ? E : Env, E extends Route ? E : R extends Route ? R : R extends string ? { | ||
path: R; | ||
method: string; | ||
} : never, E extends Env ? R extends Route | string ? I : E extends Env ? E : never : E extends Route | string ? R extends Env ? E : R : E>, next: Next): Response | Promise<Response | undefined | void>; | ||
} | ||
export interface ExecutionContext { | ||
waitUntil(promise: Promise<void>): void; | ||
passThroughOnException(): void; | ||
} | ||
export declare type ValidationTypes = 'json' | 'form' | 'query' | 'queries'; | ||
export declare type ToAppType<T> = T extends Hono<infer _, infer R, infer I, infer O> ? ToAppTypeInner<R, I, O> : never; | ||
declare type RemoveBlank<T> = { | ||
[K in keyof T]: T extends { | ||
type: ValidationTypes; | ||
} ? T : never; | ||
}; | ||
declare type ToAppTypeInner<R extends Route, I, O> = RemoveBlank<I> extends { | ||
[K in string]: { | ||
type: ValidationTypes; | ||
data: unknown; | ||
}; | ||
} ? { | ||
[K in R['method']]: { | ||
[K2 in R['path']]: { | ||
input: I extends { | ||
type: ValidationTypes; | ||
data: unknown; | ||
} ? I extends { | ||
type: infer R; | ||
} ? R extends string ? { | ||
[K in R]: I['data']; | ||
} : never : never : never; | ||
output: { | ||
json: O; | ||
}; | ||
}; | ||
}; | ||
} : { | ||
output: { | ||
json: O; | ||
}; | ||
}; | ||
export declare type InputToData<T> = ExtractData<T> extends never ? any : UnionToIntersection<ExtractData<T>>; | ||
declare type ExtractData<T> = T extends { | ||
type: ValidationTypes; | ||
} ? T extends { | ||
type: ValidationTypes; | ||
data?: infer R; | ||
} ? R : any : T; | ||
export {}; |
export declare type Expect<T extends true> = T; | ||
export declare type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false; | ||
export declare type NotEqual<X, Y> = true extends Equal<X, Y> ? false : true; | ||
export declare type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never; |
{ | ||
"name": "hono", | ||
"version": "2.7.0", | ||
"version": "3.0.0-0", | ||
"description": "Ultrafast web framework for Cloudflare Workers, Deno, and Bun.", | ||
@@ -16,3 +16,3 @@ "main": "dist/cjs/index.js", | ||
"test:bun": "bun wiptest --jsx-import-source ../src/middleware/jsx/jsx-dev-runtime bun_test/index.test.tsx", | ||
"test:lagon": "npx start-server-and-test \"lagon dev lagon_test/index.ts\" http://127.0.0.1:1234 \"yarn jest lagon_test/index.test.ts --testMatch '**/*.test.ts'\"", | ||
"test:lagon": "start-server-and-test \"lagon dev lagon_test/index.ts\" http://127.0.0.1:1234 \"yarn jest lagon_test/index.test.ts --testMatch '**/*.test.ts'\"", | ||
"test:all": "yarn test && yarn test:deno && yarn test:bun && yarn test:lagon", | ||
@@ -280,5 +280,7 @@ "lint": "eslint --ext js,ts src .eslintrc.cjs", | ||
"rimraf": "^3.0.2", | ||
"start-server-and-test": "^1.15.2", | ||
"ts-jest": "^29.0.1", | ||
"tsx": "^3.11.0", | ||
"typescript": "^4.8.3" | ||
"typescript": "^4.8.3", | ||
"zod": "^3.20.2" | ||
}, | ||
@@ -285,0 +287,0 @@ "engines": { |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
253664
30
167
7542
1