hono 0.1.0 → 0.2.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.
- package/README.md +61 -33
- package/dist/context.d.ts +8 -1
- package/dist/context.js +15 -1
- package/dist/hono.d.ts +2 -1
- package/dist/hono.js +1 -1
- package/dist/middleware/cookie/cookie.d.ts +23 -0
- package/dist/middleware/cookie/cookie.js +34 -0
- package/dist/middleware/default.js +3 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -35,7 +35,7 @@ Fastest is hono
|
|
|
35
35
|
|
|
36
36
|
Below is a demonstration to create an application of Cloudflare Workers with Hono.
|
|
37
37
|
|
|
38
|
-

|
|
39
39
|
|
|
40
40
|
## Install
|
|
41
41
|
|
|
@@ -90,7 +90,7 @@ app.all('/hello', (c) => c.text('Any Method /hello'))
|
|
|
90
90
|
|
|
91
91
|
```js
|
|
92
92
|
app.get('/user/:name', (c) => {
|
|
93
|
-
const name = c.req.
|
|
93
|
+
const name = c.req.param('name')
|
|
94
94
|
...
|
|
95
95
|
})
|
|
96
96
|
```
|
|
@@ -99,8 +99,8 @@ app.get('/user/:name', (c) => {
|
|
|
99
99
|
|
|
100
100
|
```js
|
|
101
101
|
app.get('/post/:date{[0-9]+}/:title{[a-z]+}', (c) => {
|
|
102
|
-
const date = c.req.
|
|
103
|
-
const title = c.req.
|
|
102
|
+
const date = c.req.param('date')
|
|
103
|
+
const title = c.req.param('title')
|
|
104
104
|
...
|
|
105
105
|
```
|
|
106
106
|
|
|
@@ -219,13 +219,18 @@ To handle Request and Reponse easily, you can use Context object:
|
|
|
219
219
|
### c.req
|
|
220
220
|
|
|
221
221
|
```js
|
|
222
|
-
|
|
223
222
|
// Get Request object
|
|
224
223
|
app.get('/hello', (c) => {
|
|
225
224
|
const userAgent = c.req.headers.get('User-Agent')
|
|
226
225
|
...
|
|
227
226
|
})
|
|
228
227
|
|
|
228
|
+
// Shortcut to get a header value
|
|
229
|
+
app.get('/shortcut', (c) => {
|
|
230
|
+
const userAgent = c.req.header('User-Agent')
|
|
231
|
+
...
|
|
232
|
+
})
|
|
233
|
+
|
|
229
234
|
// Query params
|
|
230
235
|
app.get('/search', (c) => {
|
|
231
236
|
const query = c.req.query('q')
|
|
@@ -234,40 +239,31 @@ app.get('/search', (c) => {
|
|
|
234
239
|
|
|
235
240
|
// Captured params
|
|
236
241
|
app.get('/entry/:id', (c) => {
|
|
237
|
-
const id = c.req.
|
|
242
|
+
const id = c.req.param('id')
|
|
238
243
|
...
|
|
239
244
|
})
|
|
240
245
|
```
|
|
241
246
|
|
|
242
|
-
###
|
|
243
|
-
|
|
244
|
-
```js
|
|
245
|
-
// Response object
|
|
246
|
-
app.use('/', (c, next) => {
|
|
247
|
-
next()
|
|
248
|
-
c.res.headers.append('X-Debug', 'Debug message')
|
|
249
|
-
})
|
|
250
|
-
```
|
|
251
|
-
|
|
252
|
-
### c.event
|
|
253
|
-
|
|
254
|
-
```js
|
|
255
|
-
// FetchEvent object
|
|
256
|
-
app.use('*', async (c, next) => {
|
|
257
|
-
c.event.waitUntil(
|
|
258
|
-
...
|
|
259
|
-
)
|
|
260
|
-
await next()
|
|
261
|
-
})
|
|
262
|
-
```
|
|
263
|
-
|
|
264
|
-
### c.env
|
|
247
|
+
### Shortcuts for Response
|
|
265
248
|
|
|
266
249
|
```js
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
250
|
+
app.get('/welcome', (c) => {
|
|
251
|
+
c.header('X-Message', 'Hello!')
|
|
252
|
+
c.header('Content-Type', 'text/plain')
|
|
253
|
+
c.status(201)
|
|
254
|
+
c.statusText('201 Content Created')
|
|
255
|
+
return c.body('Thank you for comming')
|
|
256
|
+
/*
|
|
257
|
+
Same as:
|
|
258
|
+
return new Response('Thank you for comming', {
|
|
259
|
+
status: 201,
|
|
260
|
+
statusText: '201 Content Created',
|
|
261
|
+
headers: {
|
|
262
|
+
'X-Message': 'Hello',
|
|
263
|
+
'Content-Type': 'text/plain'
|
|
264
|
+
}
|
|
265
|
+
})
|
|
266
|
+
*/
|
|
271
267
|
})
|
|
272
268
|
```
|
|
273
269
|
|
|
@@ -310,6 +306,38 @@ app.get('/redirect', (c) => c.redirect('/'))
|
|
|
310
306
|
app.get('/redirect-permanently', (c) => c.redirect('/', 301))
|
|
311
307
|
```
|
|
312
308
|
|
|
309
|
+
### c.res
|
|
310
|
+
|
|
311
|
+
```js
|
|
312
|
+
// Response object
|
|
313
|
+
app.use('/', (c, next) => {
|
|
314
|
+
next()
|
|
315
|
+
c.res.headers.append('X-Debug', 'Debug message')
|
|
316
|
+
})
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
### c.event
|
|
320
|
+
|
|
321
|
+
```js
|
|
322
|
+
// FetchEvent object
|
|
323
|
+
app.use('*', async (c, next) => {
|
|
324
|
+
c.event.waitUntil(
|
|
325
|
+
...
|
|
326
|
+
)
|
|
327
|
+
await next()
|
|
328
|
+
})
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
### c.env
|
|
332
|
+
|
|
333
|
+
```js
|
|
334
|
+
// Environment object for Cloudflare Workers
|
|
335
|
+
app.get('*', async c => {
|
|
336
|
+
const counter = c.env.COUNTER
|
|
337
|
+
...
|
|
338
|
+
})
|
|
339
|
+
```
|
|
340
|
+
|
|
313
341
|
## fire
|
|
314
342
|
|
|
315
343
|
`app.fire()` do:
|
package/dist/context.d.ts
CHANGED
|
@@ -9,12 +9,19 @@ export declare class Context {
|
|
|
9
9
|
res: Response;
|
|
10
10
|
env: Env;
|
|
11
11
|
event: FetchEvent;
|
|
12
|
+
private _headers;
|
|
13
|
+
private _status;
|
|
14
|
+
private _statusText;
|
|
15
|
+
body: (body: BodyInit, init?: ResponseInit) => Response;
|
|
12
16
|
constructor(req: Request, opts?: {
|
|
13
17
|
res: Response;
|
|
14
18
|
env: Env;
|
|
15
19
|
event: FetchEvent;
|
|
16
20
|
});
|
|
17
|
-
|
|
21
|
+
header(name: string, value: string): void;
|
|
22
|
+
status(number: number): void;
|
|
23
|
+
statusText(text: string): void;
|
|
24
|
+
newResponse(body: BodyInit, init?: ResponseInit): Response;
|
|
18
25
|
text(text: string, status?: number, headers?: Headers): Response;
|
|
19
26
|
json(object: object, status?: number, headers?: Headers): Response;
|
|
20
27
|
html(html: string, status?: number, headers?: Headers): Response;
|
package/dist/context.js
CHANGED
|
@@ -10,8 +10,22 @@ class Context {
|
|
|
10
10
|
this.env = opts.env;
|
|
11
11
|
this.event = opts.event;
|
|
12
12
|
}
|
|
13
|
+
this._headers = {};
|
|
14
|
+
this.body = this.newResponse;
|
|
13
15
|
}
|
|
14
|
-
|
|
16
|
+
header(name, value) {
|
|
17
|
+
this._headers[name] = value;
|
|
18
|
+
}
|
|
19
|
+
status(number) {
|
|
20
|
+
this._status = number;
|
|
21
|
+
}
|
|
22
|
+
statusText(text) {
|
|
23
|
+
this._statusText = text;
|
|
24
|
+
}
|
|
25
|
+
newResponse(body, init = {}) {
|
|
26
|
+
init.status = this._status || init.status;
|
|
27
|
+
init.statusText = this._statusText || init.statusText;
|
|
28
|
+
init.headers = Object.assign(Object.assign({}, init.headers), this._headers);
|
|
15
29
|
return new Response(body, init);
|
|
16
30
|
}
|
|
17
31
|
text(text, status = 200, headers = {}) {
|
package/dist/hono.d.ts
CHANGED
|
@@ -5,8 +5,9 @@ import { Context } from './context';
|
|
|
5
5
|
import type { Env } from './context';
|
|
6
6
|
declare global {
|
|
7
7
|
interface Request {
|
|
8
|
-
|
|
8
|
+
param: (key: string) => string;
|
|
9
9
|
query: (key: string) => string | null;
|
|
10
|
+
header: (name: string) => string;
|
|
10
11
|
parsedBody: any;
|
|
11
12
|
}
|
|
12
13
|
}
|
package/dist/hono.js
CHANGED
|
@@ -96,7 +96,7 @@ class Hono {
|
|
|
96
96
|
async dispatch(request, env, event) {
|
|
97
97
|
const [method, path] = [request.method, (0, url_1.getPathFromURL)(request.url)];
|
|
98
98
|
const result = await this.matchRoute(method, path);
|
|
99
|
-
request.
|
|
99
|
+
request.param = (key) => {
|
|
100
100
|
if (result) {
|
|
101
101
|
return result.params[key];
|
|
102
102
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Context } from '../../context';
|
|
2
|
+
declare global {
|
|
3
|
+
interface Request {
|
|
4
|
+
cookie: (name: string) => string;
|
|
5
|
+
}
|
|
6
|
+
interface Response {
|
|
7
|
+
cookie: (name: string, value: string, options?: CookieOptions) => void;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export declare type Cookie = {
|
|
11
|
+
[key: string]: string;
|
|
12
|
+
};
|
|
13
|
+
export declare type CookieOptions = {
|
|
14
|
+
domain?: string;
|
|
15
|
+
expires?: Date;
|
|
16
|
+
httpOnly?: boolean;
|
|
17
|
+
maxAge?: number;
|
|
18
|
+
path?: string;
|
|
19
|
+
secure?: boolean;
|
|
20
|
+
signed?: boolean;
|
|
21
|
+
sameSite?: 'Strict' | 'Lax' | 'None';
|
|
22
|
+
};
|
|
23
|
+
export declare const cookie: () => (c: Context, next: Function) => Promise<void>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.cookie = void 0;
|
|
4
|
+
const cookie = () => {
|
|
5
|
+
return async (c, next) => {
|
|
6
|
+
c.req.cookie = (name) => {
|
|
7
|
+
const cookie = c.req.headers.get('Cookie');
|
|
8
|
+
const obj = parse(cookie);
|
|
9
|
+
const value = obj[name];
|
|
10
|
+
return value;
|
|
11
|
+
};
|
|
12
|
+
await next();
|
|
13
|
+
c.res.cookie = (name, value, options) => {
|
|
14
|
+
console.log('c.res.cookie!');
|
|
15
|
+
const cookie = serialize(name, value);
|
|
16
|
+
c.res.headers.set('Set-Cookie', cookie);
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
exports.cookie = cookie;
|
|
21
|
+
const parse = (cookie) => {
|
|
22
|
+
const pairs = cookie.split(/;\s*/g);
|
|
23
|
+
const parsedCookie = {};
|
|
24
|
+
for (let i = 0, len = pairs.length; i < len; i++) {
|
|
25
|
+
const pair = pairs[i].split(/\s*=\s*([^\s]+)/);
|
|
26
|
+
parsedCookie[pair[0]] = decodeURIComponent(pair[1]);
|
|
27
|
+
}
|
|
28
|
+
return parsedCookie;
|
|
29
|
+
};
|
|
30
|
+
const serialize = (name, value, options = {}) => {
|
|
31
|
+
value = encodeURIComponent(value);
|
|
32
|
+
const cookie = `${name}=${value}`;
|
|
33
|
+
return cookie;
|
|
34
|
+
};
|
|
@@ -7,6 +7,9 @@ const defaultMiddleware = async (c, next) => {
|
|
|
7
7
|
const url = new URL(c.req.url);
|
|
8
8
|
return url.searchParams.get(key);
|
|
9
9
|
};
|
|
10
|
+
c.req.header = (name) => {
|
|
11
|
+
return c.req.headers.get(name);
|
|
12
|
+
};
|
|
10
13
|
await next();
|
|
11
14
|
if (c.res.body) {
|
|
12
15
|
// Do not clone Response, ex: c.res.clone().arrayBuffer()
|