hono 2.7.0 → 3.0.0-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/dist/cjs/compose.js +6 -0
  2. package/dist/cjs/context.js +29 -6
  3. package/dist/cjs/hono.js +21 -6
  4. package/dist/cjs/middleware/cache/index.js +1 -1
  5. package/dist/cjs/middleware/validator/index.js +29 -2
  6. package/dist/cjs/request.js +94 -36
  7. package/dist/compose.js +6 -0
  8. package/dist/context.js +29 -6
  9. package/dist/hono.js +21 -6
  10. package/dist/middleware/cache/index.js +1 -1
  11. package/dist/middleware/validator/index.js +29 -2
  12. package/dist/request.js +94 -36
  13. package/dist/types/compose.d.ts +1 -1
  14. package/dist/types/context.d.ts +25 -9
  15. package/dist/types/hono.d.ts +33 -26
  16. package/dist/types/index.d.ts +2 -4
  17. package/dist/types/middleware/serve-static/module.d.ts +1 -1
  18. package/dist/types/middleware/validator/index.d.ts +8 -2
  19. package/dist/types/request.d.ts +41 -36
  20. package/dist/types/types.d.ts +87 -14
  21. package/dist/types/utils/types.d.ts +1 -0
  22. package/package.json +5 -3
  23. package/dist/cjs/middleware/validator/middleware.js +0 -137
  24. package/dist/cjs/validator/rule.js +0 -94
  25. package/dist/cjs/validator/sanitizer.js +0 -30
  26. package/dist/cjs/validator/schema.js +0 -16
  27. package/dist/cjs/validator/validator.js +0 -441
  28. package/dist/middleware/validator/middleware.js +0 -114
  29. package/dist/types/middleware/validator/middleware.d.ts +0 -15
  30. package/dist/types/validator/rule.d.ts +0 -21
  31. package/dist/types/validator/sanitizer.d.ts +0 -3
  32. package/dist/types/validator/schema.d.ts +0 -10
  33. package/dist/types/validator/validator.d.ts +0 -134
  34. package/dist/validator/rule.js +0 -71
  35. package/dist/validator/sanitizer.js +0 -7
  36. package/dist/validator/schema.js +0 -0
  37. package/dist/validator/validator.js +0 -408
package/dist/request.js CHANGED
@@ -2,11 +2,13 @@
2
2
  import { parseBody } from "./utils/body.js";
3
3
  import { parse } from "./utils/cookie.js";
4
4
  import { getQueryStringFromURL } from "./utils/url.js";
5
- function extendRequestPrototype() {
6
- if (!!Request.prototype.param) {
7
- return;
5
+ var HonoRequest = class {
6
+ constructor(request, paramData) {
7
+ this.raw = request;
8
+ this.paramData = paramData;
9
+ this.data = {};
8
10
  }
9
- Request.prototype.param = function(key) {
11
+ param(key) {
10
12
  if (this.paramData) {
11
13
  if (key) {
12
14
  const param = this.paramData[key];
@@ -14,7 +16,7 @@ function extendRequestPrototype() {
14
16
  } else {
15
17
  const decoded = {};
16
18
  for (const [key2, value] of Object.entries(this.paramData)) {
17
- if (value) {
19
+ if (value && typeof value === "string") {
18
20
  decoded[key2] = decodeURIComponent(value);
19
21
  }
20
22
  }
@@ -22,21 +24,8 @@ function extendRequestPrototype() {
22
24
  }
23
25
  }
24
26
  return null;
25
- };
26
- Request.prototype.header = function(name) {
27
- if (!this.headerData) {
28
- this.headerData = {};
29
- this.headers.forEach((value, key) => {
30
- this.headerData[key] = value;
31
- });
32
- }
33
- if (name) {
34
- return this.headerData[name.toLowerCase()];
35
- } else {
36
- return this.headerData;
37
- }
38
- };
39
- Request.prototype.query = function(key) {
27
+ }
28
+ query(key) {
40
29
  const queryString = getQueryStringFromURL(this.url);
41
30
  const searchParams = new URLSearchParams(queryString);
42
31
  if (!this.queryData) {
@@ -50,8 +39,8 @@ function extendRequestPrototype() {
50
39
  } else {
51
40
  return this.queryData;
52
41
  }
53
- };
54
- Request.prototype.queries = function(key) {
42
+ }
43
+ queries(key) {
55
44
  const queryString = getQueryStringFromURL(this.url);
56
45
  const searchParams = new URLSearchParams(queryString);
57
46
  if (key) {
@@ -63,9 +52,24 @@ function extendRequestPrototype() {
63
52
  }
64
53
  return result;
65
54
  }
66
- };
67
- Request.prototype.cookie = function(key) {
68
- const cookie = this.headers.get("Cookie") || "";
55
+ }
56
+ header(name) {
57
+ if (!this.headerData) {
58
+ this.headerData = {};
59
+ this.raw.headers.forEach((value, key) => {
60
+ this.headerData[key] = value;
61
+ });
62
+ }
63
+ if (name) {
64
+ return this.headerData[name.toLowerCase()];
65
+ } else {
66
+ return this.headerData;
67
+ }
68
+ }
69
+ cookie(key) {
70
+ const cookie = this.raw.headers.get("Cookie");
71
+ if (!cookie)
72
+ return;
69
73
  const obj = parse(cookie);
70
74
  if (key) {
71
75
  const value = obj[key];
@@ -73,28 +77,40 @@ function extendRequestPrototype() {
73
77
  } else {
74
78
  return obj;
75
79
  }
76
- };
77
- Request.prototype.parseBody = async function() {
80
+ }
81
+ async parseBody() {
78
82
  let body;
79
83
  if (!this.bodyData) {
80
- body = await parseBody(this);
84
+ body = await parseBody(this.raw);
81
85
  this.bodyData = body;
82
86
  } else {
83
87
  body = this.bodyData;
84
88
  }
85
89
  return body;
86
- };
87
- Request.prototype.json = async function() {
90
+ }
91
+ async json() {
88
92
  let jsonData;
89
93
  if (!this.jsonData) {
90
- jsonData = JSON.parse(await this.text());
94
+ jsonData = this.raw.json();
91
95
  this.jsonData = jsonData;
92
96
  } else {
93
97
  jsonData = this.jsonData;
94
98
  }
95
99
  return jsonData;
96
- };
97
- Request.prototype.valid = function(data) {
100
+ }
101
+ async text() {
102
+ return this.raw.text();
103
+ }
104
+ async arrayBuffer() {
105
+ return this.raw.arrayBuffer();
106
+ }
107
+ async blob() {
108
+ return this.raw.blob();
109
+ }
110
+ async formData() {
111
+ return this.raw.formData();
112
+ }
113
+ valid(data) {
98
114
  if (!this.data) {
99
115
  this.data = {};
100
116
  }
@@ -102,8 +118,50 @@ function extendRequestPrototype() {
102
118
  this.data = data;
103
119
  }
104
120
  return this.data;
105
- };
106
- }
121
+ }
122
+ get url() {
123
+ return this.raw.url;
124
+ }
125
+ get method() {
126
+ return this.raw.method;
127
+ }
128
+ get headers() {
129
+ return this.raw.headers;
130
+ }
131
+ get redirect() {
132
+ return this.raw.redirect;
133
+ }
134
+ get body() {
135
+ return this.raw.body;
136
+ }
137
+ get bodyUsed() {
138
+ return this.raw.bodyUsed;
139
+ }
140
+ get cache() {
141
+ return this.raw.cache;
142
+ }
143
+ get credentials() {
144
+ return this.raw.credentials;
145
+ }
146
+ get integrity() {
147
+ return this.raw.integrity;
148
+ }
149
+ get keepalive() {
150
+ return this.raw.keepalive;
151
+ }
152
+ get mode() {
153
+ return this.raw.mode;
154
+ }
155
+ get referrer() {
156
+ return this.raw.referrer;
157
+ }
158
+ get refererPolicy() {
159
+ return this.raw.referrerPolicy;
160
+ }
161
+ get signal() {
162
+ return this.raw.signal;
163
+ }
164
+ };
107
165
  export {
108
- extendRequestPrototype
166
+ HonoRequest
109
167
  };
@@ -3,5 +3,5 @@ interface ComposeContext {
3
3
  finalized: boolean;
4
4
  res: unknown;
5
5
  }
6
- 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>;
6
+ 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>;
7
7
  export {};
@@ -1,16 +1,28 @@
1
- import type { ExecutionContext } from './types';
2
- import type { Environment, NotFoundHandler, ContextVariableMap } from './types';
1
+ import { HonoRequest } from './request';
2
+ import type { TypeResponse, Route } from './types';
3
+ import type { Environment, NotFoundHandler } from './types';
3
4
  import type { CookieOptions } from './utils/cookie';
4
5
  import type { StatusCode } from './utils/http-status';
5
- import type { Schema, SchemaToProp } from './validator/schema';
6
- declare type Headers = Record<string, string | string[]>;
7
6
  declare type Runtime = 'node' | 'deno' | 'bun' | 'cloudflare' | 'fastly' | 'vercel' | 'lagon' | 'other';
8
- export declare type Data = string | ArrayBuffer | ReadableStream;
9
- export declare class Context<P extends string = string, E extends Partial<Environment> = Environment, S = any> {
10
- req: Request<unknown, P, S extends Schema ? SchemaToProp<S> : S>;
7
+ declare type Headers = Record<string, string | string[]>;
8
+ declare type Data = string | ArrayBuffer | ReadableStream;
9
+ export interface ExecutionContext {
10
+ waitUntil(promise: Promise<void>): void;
11
+ passThroughOnException(): void;
12
+ }
13
+ export interface ContextVariableMap {
14
+ }
15
+ declare type ContextOptions<E extends Partial<Environment>, R extends Route> = {
16
+ env?: E['Bindings'];
17
+ executionCtx?: FetchEvent | ExecutionContext | undefined;
18
+ notFoundHandler?: NotFoundHandler<E, R>;
19
+ paramData?: Record<string, string>;
20
+ };
21
+ export declare class Context<E extends Partial<Environment> = Environment, R extends Route = Route, I = any> {
11
22
  env: E['Bindings'];
12
23
  finalized: boolean;
13
24
  error: Error | undefined;
25
+ private _req?;
14
26
  private _status;
15
27
  private _executionCtx;
16
28
  private _pretty;
@@ -18,8 +30,11 @@ export declare class Context<P extends string = string, E extends Partial<Enviro
18
30
  private _map;
19
31
  private _headers;
20
32
  private _res;
33
+ private _paramData;
34
+ private rawRequest;
21
35
  private notFoundHandler;
22
- constructor(req: Request<unknown, P>, env?: E['Bindings'], executionCtx?: FetchEvent | ExecutionContext | undefined, notFoundHandler?: NotFoundHandler<E>);
36
+ constructor(req: Request, options?: ContextOptions<E, R>);
37
+ get req(): HonoRequest<R, I>;
23
38
  get event(): FetchEvent;
24
39
  get executionCtx(): ExecutionContext;
25
40
  get res(): Response;
@@ -31,8 +46,8 @@ export declare class Context<P extends string = string, E extends Partial<Enviro
31
46
  set<Key extends keyof ContextVariableMap>(key: Key, value: ContextVariableMap[Key]): void;
32
47
  set<Key extends keyof E['Variables']>(key: Key, value: E['Variables'][Key]): void;
33
48
  set(key: string, value: unknown): void;
34
- get<Key extends keyof ContextVariableMap>(key: Key): ContextVariableMap[Key];
35
49
  get<Key extends keyof E['Variables']>(key: Key): E['Variables'][Key];
50
+ get<Key extends keyof ContextVariableMap>(key: Key): ContextVariableMap[Key];
36
51
  get<T>(key: string): T;
37
52
  pretty(prettyJSON: boolean, space?: number): void;
38
53
  newResponse(data: Data | null, status: StatusCode, headers?: Headers): Response;
@@ -40,6 +55,7 @@ export declare class Context<P extends string = string, E extends Partial<Enviro
40
55
  body(data: Data | null, status?: StatusCode, headers?: Headers): Response;
41
56
  text(text: string, status?: StatusCode, headers?: Headers): Response;
42
57
  json<T>(object: T, status?: StatusCode, headers?: Headers): Response;
58
+ jsonT<T>(object: T, status?: StatusCode, headers?: Headers): TypeResponse<T>;
43
59
  html(html: string, status?: StatusCode, headers?: Headers): Response;
44
60
  redirect(location: string, status?: StatusCode): Response;
45
61
  cookie(name: string, value: string, opt?: CookieOptions): void;
@@ -1,41 +1,48 @@
1
+ import type { ExecutionContext } from './context';
1
2
  import type { Router } from './router';
2
- import type { ExecutionContext } from './types';
3
- import type { Handler, Environment, ParamKeys, ErrorHandler, NotFoundHandler } from './types';
4
- interface HandlerInterface<P extends string, E extends Partial<Environment> = Environment, S = unknown, U = Hono<E, P, S>> {
5
- <Path extends string, Data = S>(...handlers: Handler<ParamKeys<Path> extends never ? string : ParamKeys<Path>, E, Data>[]): Hono<E, Path, Data & S>;
6
- (...handlers: Handler<P, E, S>[]): U;
7
- <Path extends string, Data = S>(path: Path, ...handlers: Handler<ParamKeys<Path> extends never ? string : ParamKeys<Path>, E, Data>[]): Hono<E, Path, Data & S>;
8
- <Path extends string, Data = S>(path: Path, ...handlers: Handler<string, E, Data>[]): Hono<E, string, Data & S>;
9
- (path: string, ...handlers: Handler<P, E, S>[]): U;
10
- }
11
- interface Route<P extends string = string, E extends Partial<Environment> = Environment, S = unknown> {
3
+ import type { HandlerInterface, ToAppType, Handler, ErrorHandler, NotFoundHandler, Environment, Route } from './types';
4
+ interface RouterRoute {
12
5
  path: string;
13
6
  method: string;
14
- handler: Handler<P, E, S>;
7
+ handler: Handler;
15
8
  }
16
- 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>>() => {
17
- all: HandlerInterface<P_1, E_1, S_1, U>;
18
- get: HandlerInterface<P_1, E_1, S_1, U>;
19
- post: HandlerInterface<P_1, E_1, S_1, U>;
20
- put: HandlerInterface<P_1, E_1, S_1, U>;
21
- delete: HandlerInterface<P_1, E_1, S_1, U>;
22
- head: HandlerInterface<P_1, E_1, S_1, U>;
23
- options: HandlerInterface<P_1, E_1, S_1, U>;
24
- patch: HandlerInterface<P_1, E_1, S_1, U>;
9
+ 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>() => {
10
+ all: HandlerInterface<E_1, "all", P, I_1, O_1>;
11
+ get: HandlerInterface<E_1, "get", P, I_1, O_1>;
12
+ post: HandlerInterface<E_1, "post", P, I_1, O_1>;
13
+ put: HandlerInterface<E_1, "put", P, I_1, O_1>;
14
+ delete: HandlerInterface<E_1, "delete", P, I_1, O_1>;
15
+ head: HandlerInterface<E_1, "head", P, I_1, O_1>;
16
+ options: HandlerInterface<E_1, "options", P, I_1, O_1>;
17
+ patch: HandlerInterface<E_1, "patch", P, I_1, O_1>;
25
18
  };
26
- 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>> {
27
- readonly router: Router<Handler<P, E, S>>;
19
+ export declare class Hono<E extends Partial<Environment> = Partial<Environment>, R extends Route = Route, I = any, // should be any
20
+ O = unknown> extends Hono_base<E, R['method'], R['path'], I, O> {
21
+ readonly router: Router<Handler>;
28
22
  readonly strict: boolean;
29
23
  private _tempPath;
30
24
  private path;
31
- routes: Route<P, E, S>[];
25
+ routes: RouterRoute[];
32
26
  constructor(init?: Partial<Pick<Hono, 'router' | 'strict'>>);
33
27
  private notFoundHandler;
34
28
  private errorHandler;
35
29
  route(path: string, app?: Hono<any>): this;
36
- use<Path extends string = string, Data = unknown>(...middleware: Handler<Path, E, Data>[]): Hono<E, P, S>;
37
- use<Path extends string = string, Data = unknown>(arg1: string, ...middleware: Handler<Path, E, Data>[]): Hono<E, P, S>;
38
- on(method: string, path: string, ...handlers: Handler<P, E, S>[]): this;
30
+ use(...middleware: Handler<E>[]): Hono<E, {
31
+ method: 'all';
32
+ path: string;
33
+ }, I, O>;
34
+ use<Path extends string, E2 extends Partial<Environment> = E>(arg1: Path, ...middleware: Handler<E2>[]): Hono<E, {
35
+ method: 'all';
36
+ path: Path;
37
+ }, I, O>;
38
+ on<Method extends string, Path extends string>(method: Method, path: Path, ...handlers: Handler<E, {
39
+ method: Method;
40
+ path: Path;
41
+ }>[]): Hono<E, {
42
+ method: Method;
43
+ path: Path;
44
+ }, I, O>;
45
+ build: () => ToAppType<typeof this>;
39
46
  onError(handler: ErrorHandler<E>): this;
40
47
  notFound(handler: NotFoundHandler<E>): this;
41
48
  showRoutes(): void;
@@ -1,8 +1,6 @@
1
- /// <reference path="request.d.ts" />
2
1
  import { Hono } from './hono';
3
- export type { Next, ContextVariableMap, MiddlewareHandler, ErrorHandler, NotFoundHandler, } from './types';
4
- export type { Context } from './context';
5
- export type { Validator } from './validator/validator';
2
+ export type { Next, MiddlewareHandler, ErrorHandler, NotFoundHandler } from './types';
3
+ export type { Context, ContextVariableMap } from './context';
6
4
  import type { CustomHandler } from './types';
7
5
  export type { CustomHandler as Handler };
8
6
  declare module './hono' {
@@ -1,3 +1,3 @@
1
1
  import type { ServeStaticOptions } from './serve-static';
2
- declare const module: (options?: ServeStaticOptions) => import("../..").MiddlewareHandler<string, import("../../types").Environment, any>;
2
+ declare const module: (options?: ServeStaticOptions) => import("../..").MiddlewareHandler<Partial<import("../../types").Environment>, import("../../types").Route, unknown>;
3
3
  export { module as serveStatic };
@@ -1,2 +1,8 @@
1
- import { validatorMiddleware } from './middleware';
2
- export { validatorMiddleware as validator };
1
+ import type { Context } from '../../context';
2
+ import type { Environment, Next, Route, ValidationTypes } from '../../types';
3
+ 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;
4
+ export declare const validator: <T, U extends ValidationTypes, V extends {
5
+ type: U;
6
+ data: T;
7
+ }, 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>;
8
+ export {};
@@ -1,39 +1,44 @@
1
+ import type { GetParamKeys, InputToData, Route } from './types';
1
2
  import type { BodyData } from './utils/body';
2
3
  import type { Cookie } from './utils/cookie';
3
- declare global {
4
- interface Request<CfHostMetadata = unknown, ParamKeyType extends string = string, Data = any> {
5
- paramData?: Record<ParamKeyType, string>;
6
- param: {
7
- (key: ParamKeyType): string;
8
- (): Record<ParamKeyType, string>;
9
- };
10
- queryData?: Record<string, string>;
11
- query: {
12
- (key: string): string;
13
- (): Record<string, string>;
14
- };
15
- queries: {
16
- (key: string): string[];
17
- (): Record<string, string[]>;
18
- };
19
- headerData?: Record<string, string>;
20
- header: {
21
- (name: string): string;
22
- (): Record<string, string>;
23
- };
24
- cookie: {
25
- (name: string): string | undefined;
26
- (): Cookie;
27
- };
28
- bodyData?: BodyData;
29
- parseBody<BodyType extends BodyData>(): Promise<BodyType>;
30
- jsonData?: any;
31
- json<T>(): Promise<T>;
32
- data: Data;
33
- valid: {
34
- (data: Data): Data;
35
- (): Data;
36
- };
37
- }
4
+ export declare class HonoRequest<R extends Route = Route, I = any> {
5
+ raw: Request;
6
+ private paramData;
7
+ private headerData;
8
+ private queryData;
9
+ private bodyData;
10
+ private jsonData;
11
+ private data;
12
+ constructor(request: Request, paramData?: Record<string, string> | undefined);
13
+ param(key: GetParamKeys<R['path']>): string;
14
+ param(): Record<GetParamKeys<R['path']>, string>;
15
+ query(key: string): string;
16
+ query(): Record<string, string>;
17
+ queries(key: string): string[];
18
+ queries(): Record<string, string[]>;
19
+ header(name: string): string;
20
+ header(): Record<string, string>;
21
+ cookie(key: string): string | undefined;
22
+ cookie(): Cookie;
23
+ parseBody<BodyType extends BodyData>(): Promise<BodyType>;
24
+ json<JSONData = unknown>(): Promise<Partial<JSONData>>;
25
+ text(): Promise<string>;
26
+ arrayBuffer(): Promise<ArrayBuffer>;
27
+ blob(): Promise<Blob>;
28
+ formData(): Promise<FormData>;
29
+ valid(data?: unknown): InputToData<I>;
30
+ get url(): string;
31
+ get method(): string;
32
+ get headers(): Headers;
33
+ get redirect(): RequestRedirect;
34
+ get body(): ReadableStream<Uint8Array> | null;
35
+ get bodyUsed(): boolean;
36
+ get cache(): RequestCache;
37
+ get credentials(): RequestCredentials;
38
+ get integrity(): string;
39
+ get keepalive(): boolean;
40
+ get mode(): RequestMode;
41
+ get referrer(): string;
42
+ get refererPolicy(): ReferrerPolicy;
43
+ get signal(): AbortSignal;
38
44
  }
39
- export declare function extendRequestPrototype(): void;
@@ -1,25 +1,98 @@
1
1
  import type { Context } from './context';
2
- export interface ContextVariableMap {
3
- }
2
+ import type { Hono } from './hono';
3
+ import type { UnionToIntersection } from './utils/types';
4
4
  export declare type Bindings = Record<string, any>;
5
5
  export declare type Variables = Record<string, any>;
6
6
  export declare type Environment = {
7
7
  Bindings: Bindings;
8
8
  Variables: Variables;
9
9
  };
10
- 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>;
11
- 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>;
12
- export declare type NotFoundHandler<E extends Partial<Environment> = Environment> = (c: Context<string, E>) => Response | Promise<Response>;
13
- export declare type ErrorHandler<E extends Partial<Environment> = Environment> = (err: Error, c: Context<string, E>) => Response;
14
- export declare type Next = () => Promise<void>;
10
+ declare type Env = Partial<Environment>;
11
+ export declare type Route = {
12
+ path: string;
13
+ method: string;
14
+ };
15
+ export declare type Handler<E extends Env = Environment, R extends Route = Route, I = any, // should be any
16
+ O = unknown> = (c: Context<E, R, I>, next: Next) => Response | Promise<Response | undefined | void> | TypeResponse<O> | Promise<TypeResponse<O>>;
17
+ export interface HandlerInterface<E extends Env = Env, M extends string = string, P extends string = string, _I = any, _O = unknown> {
18
+ <I2, O2>(...handlers: Handler<E, {
19
+ method: M;
20
+ path: string;
21
+ }, I2, O2>[]): Hono<E, {
22
+ method: M;
23
+ path: string;
24
+ }, I2, O2>;
25
+ (...handlers: Handler<any, any>[]): Hono;
26
+ <I2, O2, Path extends string = P>(path: Path, ...handlers: Handler<E, {
27
+ method: M;
28
+ path: Path;
29
+ }, I2, O2>[]): Hono<E, {
30
+ method: M;
31
+ path: Path;
32
+ }, I2, O2>;
33
+ <I2, O2, Path extends string>(path: Path, ...handlers: Handler<any, any, I2, O2>[]): Hono<E, {
34
+ method: M;
35
+ path: Path;
36
+ }, I2, O2>;
37
+ }
38
+ export declare type ExtractType<T> = T extends TypeResponse<infer R> ? R : T extends Promise<TypeResponse<infer R>> ? R : never;
15
39
  declare type ParamKeyName<NameWithPattern> = NameWithPattern extends `${infer Name}{${infer _Pattern}` ? Name : NameWithPattern;
16
40
  declare type ParamKey<Component> = Component extends `:${infer NameWithPattern}` ? ParamKeyName<NameWithPattern> : never;
17
- export declare type ParamKeys<Path> = Path extends `${infer Component}/${infer Rest}` ? ParamKey<Component> | ParamKeys<Rest> : ParamKey<Path>;
18
- export interface CustomHandler<P = string, E = Partial<Environment>, S = any> {
19
- (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>;
20
- }
21
- export interface ExecutionContext {
22
- waitUntil(promise: Promise<void>): void;
23
- passThroughOnException(): void;
41
+ declare type ParamKeys<Path> = Path extends `${infer Component}/${infer Rest}` ? ParamKey<Component> | ParamKeys<Rest> : ParamKey<Path>;
42
+ export declare type GetParamKeys<Path> = ParamKeys<Path> extends never ? Path : ParamKeys<Path>;
43
+ 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>;
44
+ export declare type NotFoundHandler<E extends Env = Environment, R extends Route = Route> = (c: Context<E, R>) => Response | Promise<Response>;
45
+ export declare type ErrorHandler<E extends Env = Environment> = (err: Error, c: Context<E>) => Response;
46
+ export declare type Next = () => Promise<void>;
47
+ export declare type TypeResponse<T = unknown> = {
48
+ response: Response | Promise<Response>;
49
+ data: T;
50
+ format: 'json';
51
+ };
52
+ export interface CustomHandler<E = Env, R = Route, I = any> {
53
+ (c: Context<E extends Env ? E : Env, E extends Route ? E : R extends Route ? R : R extends string ? {
54
+ path: R;
55
+ method: string;
56
+ } : 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>;
24
57
  }
58
+ export declare type ValidationTypes = 'json' | 'form' | 'query' | 'queries';
59
+ export declare type ToAppType<T> = T extends Hono<infer _, infer R, infer I, infer O> ? ToAppTypeInner<R, I, O> : never;
60
+ declare type RemoveBlank<T> = {
61
+ [K in keyof T]: T extends {
62
+ type: ValidationTypes;
63
+ } ? T : never;
64
+ };
65
+ declare type ToAppTypeInner<R extends Route, I, O> = RemoveBlank<I> extends {
66
+ [K in string]: {
67
+ type: ValidationTypes;
68
+ data: unknown;
69
+ };
70
+ } ? {
71
+ [K in R['method']]: {
72
+ [K2 in R['path']]: {
73
+ input: I extends {
74
+ type: ValidationTypes;
75
+ data: unknown;
76
+ } ? I extends {
77
+ type: infer R;
78
+ } ? R extends string ? {
79
+ [K in R]: I['data'];
80
+ } : never : never : never;
81
+ output: {
82
+ json: O;
83
+ };
84
+ };
85
+ };
86
+ } : {
87
+ output: {
88
+ json: O;
89
+ };
90
+ };
91
+ export declare type InputToData<T> = ExtractData<T> extends never ? any : UnionToIntersection<ExtractData<T>>;
92
+ declare type ExtractData<T> = T extends {
93
+ type: ValidationTypes;
94
+ } ? T extends {
95
+ type: ValidationTypes;
96
+ data?: infer R;
97
+ } ? R : any : T;
25
98
  export {};
@@ -1,3 +1,4 @@
1
1
  export declare type Expect<T extends true> = T;
2
2
  export declare type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
3
3
  export declare type NotEqual<X, Y> = true extends Equal<X, Y> ? false : true;
4
+ export declare type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "2.7.0",
3
+ "version": "3.0.0-0",
4
4
  "description": "Ultrafast web framework for Cloudflare Workers, Deno, and Bun.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "type": "module",
@@ -13,7 +13,7 @@
13
13
  "test": "jest",
14
14
  "test:deno": "deno test --allow-read deno_test",
15
15
  "test:bun": "bun wiptest --jsx-import-source ../src/middleware/jsx/jsx-dev-runtime bun_test/index.test.tsx",
16
- "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'\"",
16
+ "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'\"",
17
17
  "test:all": "yarn test && yarn test:deno && yarn test:bun && yarn test:lagon",
18
18
  "lint": "eslint --ext js,ts src .eslintrc.cjs",
19
19
  "lint:fix": "eslint --ext js,ts src .eslintrc.cjs --fix",
@@ -276,9 +276,11 @@
276
276
  "np": "^7.6.2",
277
277
  "prettier": "^2.6.2",
278
278
  "rimraf": "^3.0.2",
279
+ "start-server-and-test": "^1.15.2",
279
280
  "ts-jest": "^29.0.1",
280
281
  "tsx": "^3.11.0",
281
- "typescript": "^4.8.3"
282
+ "typescript": "^4.8.3",
283
+ "zod": "^3.20.2"
282
284
  },
283
285
  "engines": {
284
286
  "node": ">=16.0.0"