Comparing version 0.3.7 to 0.3.8
@@ -8,4 +8,4 @@ /// <reference types="@cloudflare/workers-types" /> | ||
} | ||
export declare class Context { | ||
req: Request; | ||
export declare class Context<RequestParamKeyType = string> { | ||
req: Request<RequestParamKeyType>; | ||
res: Response; | ||
@@ -18,3 +18,3 @@ env: Env; | ||
render: (template: string, params?: object, options?: object) => Promise<Response>; | ||
constructor(req: Request, opts?: { | ||
constructor(req: Request<RequestParamKeyType>, opts?: { | ||
res: Response; | ||
@@ -21,0 +21,0 @@ env: Env; |
@@ -31,3 +31,3 @@ "use strict"; | ||
if (this.res) { | ||
console.warn('c.res.status is already setted.'); | ||
console.warn('c.res.status is already set.'); | ||
return; | ||
@@ -34,0 +34,0 @@ } |
@@ -7,4 +7,4 @@ /// <reference types="@cloudflare/workers-types" /> | ||
declare global { | ||
interface Request { | ||
param: (key: string) => string; | ||
interface Request<ParamKeyType = string> { | ||
param: (key: ParamKeyType) => string; | ||
query: (key: string) => string; | ||
@@ -15,4 +15,7 @@ header: (name: string) => string; | ||
} | ||
export declare type Handler = (c: Context, next?: Function) => Response | Promise<Response>; | ||
export declare type Handler<RequestParamKeyType = string> = (c: Context<RequestParamKeyType>, next?: Function) => Response | Promise<Response>; | ||
export declare type MiddlewareHandler = (c: Context, next: Function) => Promise<void>; | ||
declare type ParamKeyName<NameWithPattern> = NameWithPattern extends `${infer Name}{${infer _Pattern}` ? Name : NameWithPattern; | ||
declare type ParamKey<Component> = Component extends `:${infer NameWithPattern}` ? ParamKeyName<NameWithPattern> : never; | ||
declare type ParamKeys<Path> = Path extends `${infer Component}/${infer Rest}` ? ParamKey<Component> | ParamKeys<Rest> : ParamKey<Path>; | ||
export declare class Router<T> { | ||
@@ -29,10 +32,18 @@ node: Node<T>; | ||
constructor(); | ||
get(arg: string | Handler, ...args: Handler[]): Hono; | ||
post(arg: string | Handler, ...args: Handler[]): Hono; | ||
put(arg: string | Handler, ...args: Handler[]): Hono; | ||
head(arg: string | Handler, ...args: Handler[]): Hono; | ||
delete(arg: string | Handler, ...args: Handler[]): Hono; | ||
options(arg: string | Handler, ...args: Handler[]): Hono; | ||
patch(arg: string | Handler, ...args: Handler[]): Hono; | ||
all(arg: string | Handler, ...args: Handler[]): Hono; | ||
get<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono; | ||
get(arg: Handler<never>, ...args: Handler<never>[]): Hono; | ||
post<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono; | ||
post(arg: Handler, ...args: Handler[]): Hono; | ||
put<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono; | ||
put(arg: Handler, ...args: Handler[]): Hono; | ||
head<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono; | ||
head(arg: Handler, ...args: Handler[]): Hono; | ||
delete<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono; | ||
delete(arg: Handler, ...args: Handler[]): Hono; | ||
options<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono; | ||
options(arg: Handler, ...args: Handler[]): Hono; | ||
patch<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono; | ||
patch(arg: Handler, ...args: Handler[]): Hono; | ||
all<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono; | ||
all(arg: Handler<never>, ...args: Handler<never>[]): Hono; | ||
route(path: string): Hono; | ||
@@ -49,1 +60,2 @@ use(path: string, middleware: MiddlewareHandler): void; | ||
} | ||
export {}; |
@@ -27,3 +27,2 @@ "use strict"; | ||
} | ||
/* HTTP METHODS */ | ||
get(arg, ...args) { | ||
@@ -50,18 +49,2 @@ return this.addRoute('get', arg, ...args); | ||
} | ||
/* | ||
We may implement these HTTP methods: | ||
trace | ||
copy | ||
lock | ||
purge | ||
unlock | ||
report | ||
checkout | ||
merge | ||
notify | ||
subscribe | ||
unsubscribe | ||
search | ||
connect | ||
*/ | ||
all(arg, ...args) { | ||
@@ -163,3 +146,3 @@ return this.addRoute('all', arg, ...args); | ||
const message = 'Not Found'; | ||
return new Response('Not Found', { | ||
return new Response(message, { | ||
status: 404, | ||
@@ -166,0 +149,0 @@ headers: { |
{ | ||
"name": "hono", | ||
"version": "0.3.7", | ||
"version": "0.3.8", | ||
"description": "[炎] Ultrafast web framework for Cloudflare Workers.", | ||
@@ -20,3 +20,4 @@ "main": "dist/index.js", | ||
"./serve-static": "./dist/middleware/serve-static/serve-static.js", | ||
"./utils/buffer": "./dist/utils/buffer.js" | ||
"./utils/buffer": "./dist/utils/buffer.js", | ||
"./package.json": "./package.json" | ||
}, | ||
@@ -23,0 +24,0 @@ "typesVersions": { |
@@ -16,3 +16,3 @@ # Hono | ||
- **Ultra fast** - the router is implemented with Trie-Tree structure. | ||
- **Ultra fast** - the router is implemented with Trie-Tree structure. Not use loops. | ||
- **Zero dependencies** - using only Web standard API. | ||
@@ -178,3 +178,3 @@ - **Middleware** - builtin middleware, and you can make your own middleware. | ||
await next() | ||
await c.res.headers.add('x-message', 'This is middleware!') | ||
await c.header('x-message', 'This is middleware!') | ||
}) | ||
@@ -198,26 +198,5 @@ | ||
### Complex Pattern | ||
You can also do this: | ||
```js | ||
// Output response time | ||
app.use('*', async (c, next) => { | ||
await next() | ||
const responseTime = await c.res.headers.get('X-Response-Time') | ||
console.log(`X-Response-Time: ${responseTime}`) | ||
}) | ||
// Add X-Response-Time header | ||
app.use('*', async (c, next) => { | ||
const start = Date.now() | ||
await next() | ||
const ms = Date.now() - start | ||
await c.res.headers.append('X-Response-Time', `${ms}ms`) | ||
}) | ||
``` | ||
## Context | ||
To handle Request and Reponse easily, you can use Context object: | ||
To handle Request and Reponse, you can use Context object: | ||
@@ -436,3 +415,3 @@ ### c.req | ||
### Publish | ||
### 7. Publish | ||
@@ -439,0 +418,0 @@ Deploy to Cloudflare. That's all! |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
54435
39
1229
450