hono 1.2.1 → 1.2.2

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 (73) hide show
  1. package/README.md +1 -1
  2. package/dist/compose.test.d.ts +1 -0
  3. package/dist/compose.test.js +511 -0
  4. package/dist/context.test.d.ts +1 -0
  5. package/dist/context.test.js +127 -0
  6. package/dist/hono.d.ts +1 -1
  7. package/dist/hono.test.d.ts +1 -0
  8. package/dist/hono.test.js +592 -0
  9. package/dist/middleware/basic-auth/index.test.d.ts +1 -0
  10. package/dist/middleware/basic-auth/index.test.js +119 -0
  11. package/dist/middleware/body-parse/index.test.d.ts +1 -0
  12. package/dist/middleware/body-parse/index.test.js +59 -0
  13. package/dist/middleware/cookie/index.d.ts +1 -1
  14. package/dist/middleware/cookie/index.test.d.ts +1 -0
  15. package/dist/middleware/cookie/index.test.js +54 -0
  16. package/dist/middleware/cors/index.test.d.ts +1 -0
  17. package/dist/middleware/cors/index.test.js +59 -0
  18. package/dist/middleware/etag/index.test.d.ts +1 -0
  19. package/dist/middleware/etag/index.test.js +45 -0
  20. package/dist/middleware/graphql-server/index.js +1 -1
  21. package/dist/middleware/graphql-server/index.test.d.ts +1 -0
  22. package/dist/middleware/graphql-server/index.test.js +480 -0
  23. package/dist/middleware/graphql-server/parse-body.test.d.ts +1 -0
  24. package/dist/middleware/graphql-server/parse-body.test.js +57 -0
  25. package/dist/middleware/jwt/index.test.d.ts +1 -0
  26. package/dist/middleware/jwt/index.test.js +51 -0
  27. package/dist/middleware/logger/index.test.d.ts +1 -0
  28. package/dist/middleware/logger/index.test.js +49 -0
  29. package/dist/middleware/mustache/index.test.d.ts +1 -0
  30. package/dist/middleware/mustache/index.test.js +49 -0
  31. package/dist/middleware/powered-by/index.test.d.ts +1 -0
  32. package/dist/middleware/powered-by/index.test.js +15 -0
  33. package/dist/middleware/pretty-json/index.test.d.ts +1 -0
  34. package/dist/middleware/pretty-json/index.test.js +28 -0
  35. package/dist/middleware/serve-static/index.test.d.ts +1 -0
  36. package/dist/middleware/serve-static/index.test.js +58 -0
  37. package/dist/router/reg-exp-router/index.d.ts +1 -1
  38. package/dist/router/reg-exp-router/index.js +1 -1
  39. package/dist/router/reg-exp-router/router.js +1 -1
  40. package/dist/router/reg-exp-router/router.test.d.ts +1 -0
  41. package/dist/router/reg-exp-router/router.test.js +212 -0
  42. package/dist/router/reg-exp-router/trie.d.ts +3 -3
  43. package/dist/router/reg-exp-router/trie.js +1 -1
  44. package/dist/router/trie-router/index.d.ts +1 -1
  45. package/dist/router/trie-router/index.js +1 -1
  46. package/dist/router/trie-router/node.test.d.ts +1 -0
  47. package/dist/router/trie-router/node.test.js +351 -0
  48. package/dist/router/trie-router/router.d.ts +1 -1
  49. package/dist/router/trie-router/router.js +1 -1
  50. package/dist/router/trie-router/router.test.d.ts +1 -0
  51. package/dist/router/trie-router/router.test.js +98 -0
  52. package/dist/utils/body.test.d.ts +1 -0
  53. package/dist/utils/body.test.js +45 -0
  54. package/dist/utils/buffer.js +1 -1
  55. package/dist/utils/buffer.test.d.ts +1 -0
  56. package/dist/utils/buffer.test.js +36 -0
  57. package/dist/utils/cloudflare.test.d.ts +1 -0
  58. package/dist/utils/cloudflare.test.js +42 -0
  59. package/dist/utils/crypto.test.d.ts +1 -0
  60. package/dist/utils/crypto.test.js +15 -0
  61. package/dist/utils/encode.test.d.ts +1 -0
  62. package/dist/utils/encode.test.js +54 -0
  63. package/dist/utils/http-status.test.d.ts +1 -0
  64. package/dist/utils/http-status.test.js +8 -0
  65. package/dist/utils/jwt/jwt.test.d.ts +1 -0
  66. package/dist/utils/jwt/jwt.test.js +171 -0
  67. package/dist/utils/jwt/types.test.d.ts +1 -0
  68. package/dist/utils/jwt/types.test.js +12 -0
  69. package/dist/utils/mime.test.d.ts +1 -0
  70. package/dist/utils/mime.test.js +13 -0
  71. package/dist/utils/url.test.d.ts +1 -0
  72. package/dist/utils/url.test.js +87 -0
  73. package/package.json +3 -4
package/README.md CHANGED
@@ -87,7 +87,7 @@ import { etag } from 'hono/etag'
87
87
  import { logger } from 'hono/logger'
88
88
 
89
89
  const app = new Hono()
90
- app.use('*', etag(), (logger())
90
+ app.use('*', etag(), logger())
91
91
  ```
92
92
 
93
93
  And, the routing of Hono is so flexible. It's easy to construct large web applications.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,511 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const compose_1 = require("./compose");
4
+ const context_1 = require("./context");
5
+ describe('compose', () => {
6
+ const middleware = [];
7
+ const a = async (c, next) => {
8
+ c.req['log'] = 'log';
9
+ await next();
10
+ };
11
+ const b = async (c, next) => {
12
+ await next();
13
+ c.res['headers'] = 'custom-header';
14
+ };
15
+ const c = async (c, next) => {
16
+ c.req['xxx'] = 'yyy';
17
+ await next();
18
+ c.res['zzz'] = c.req['xxx'];
19
+ };
20
+ const handler = async (c, next) => {
21
+ c.req['log'] = `${c.req.log} message`;
22
+ await next();
23
+ c.res = { message: 'new response' };
24
+ };
25
+ middleware.push(a);
26
+ middleware.push(b);
27
+ middleware.push(c);
28
+ middleware.push(handler);
29
+ it('Request', async () => {
30
+ const c = { req: {}, res: {} };
31
+ const composed = (0, compose_1.compose)(middleware);
32
+ const context = await composed(c);
33
+ expect(context.req['log']).not.toBeNull();
34
+ expect(context.req['log']).toBe('log message');
35
+ expect(context.req['xxx']).toBe('yyy');
36
+ });
37
+ it('Response', async () => {
38
+ const c = { req: {}, res: {} };
39
+ const composed = (0, compose_1.compose)(middleware);
40
+ const context = await composed(c);
41
+ expect(context.res['headers']).not.toBeNull();
42
+ expect(context.res['headers']).toBe('custom-header');
43
+ expect(context.res['message']).toBe('new response');
44
+ expect(context.res['zzz']).toBe('yyy');
45
+ });
46
+ });
47
+ describe('Handler and middlewares', () => {
48
+ const middleware = [];
49
+ const req = new Request('http://localhost/');
50
+ const c = new context_1.Context(req);
51
+ const onError = (error, c) => {
52
+ return c.text('onError', 500);
53
+ };
54
+ const mHandlerFoo = async (c, next) => {
55
+ c.req.headers.append('x-header-foo', 'foo');
56
+ await next();
57
+ };
58
+ const mHandlerBar = async (c, next) => {
59
+ await next();
60
+ c.header('x-header-bar', 'bar');
61
+ };
62
+ const handler = (c) => {
63
+ const foo = c.req.header('x-header-foo') || '';
64
+ return c.text(foo);
65
+ };
66
+ middleware.push(mHandlerFoo);
67
+ middleware.push(mHandlerBar);
68
+ middleware.push(handler);
69
+ it('Should return 200 Response', async () => {
70
+ const composed = (0, compose_1.compose)(middleware, onError);
71
+ const context = await composed(c);
72
+ const res = context.res;
73
+ expect(res).not.toBeNull();
74
+ expect(res.status).toBe(200);
75
+ expect(await res.text()).toBe('foo');
76
+ expect(res.headers.get('x-header-bar')).toBe('bar');
77
+ });
78
+ });
79
+ describe('compose with Context - 200 success', () => {
80
+ const middleware = [];
81
+ const req = new Request('http://localhost/');
82
+ const c = new context_1.Context(req);
83
+ const onError = (error, c) => {
84
+ return c.text('onError', 500);
85
+ };
86
+ const handler = (c) => {
87
+ return c.text('Hello');
88
+ };
89
+ const mHandler = async (c, next) => {
90
+ await next();
91
+ };
92
+ middleware.push(handler);
93
+ middleware.push(mHandler);
94
+ it('Should return 200 Response', async () => {
95
+ const composed = (0, compose_1.compose)(middleware, onError);
96
+ const context = await composed(c);
97
+ expect(context.res).not.toBeNull();
98
+ expect(context.res.status).toBe(200);
99
+ expect(await context.res.text()).toBe('Hello');
100
+ });
101
+ });
102
+ describe('compose with Context - 404 not found', () => {
103
+ const middleware = [];
104
+ const req = new Request('http://localhost/');
105
+ const c = new context_1.Context(req);
106
+ const onError = (error, c) => {
107
+ return c.text('onError', 500);
108
+ };
109
+ const onNotFound = (c) => {
110
+ return c.text('onNotFound', 404);
111
+ };
112
+ const mHandler = async (c, next) => {
113
+ await next();
114
+ };
115
+ middleware.push(mHandler);
116
+ it('Should return 404 Response', async () => {
117
+ const composed = (0, compose_1.compose)(middleware, onError, onNotFound);
118
+ const context = await composed(c);
119
+ expect(context.res).not.toBeNull();
120
+ expect(context.res.status).toBe(404);
121
+ expect(await context.res.text()).toBe('onNotFound');
122
+ });
123
+ });
124
+ describe('compose with Context - 401 not authorized', () => {
125
+ const middleware = [];
126
+ const req = new Request('http://localhost/');
127
+ const c = new context_1.Context(req);
128
+ const onError = (error, c) => {
129
+ return c.text('onError', 500);
130
+ };
131
+ const handler = (c, next) => {
132
+ return c.text('Hello');
133
+ };
134
+ const mHandler = async (c, next) => {
135
+ await next();
136
+ c.res = new Response('Not authorized', { status: 401 });
137
+ };
138
+ middleware.push(handler);
139
+ middleware.push(mHandler);
140
+ it('Should return 401 Response', async () => {
141
+ const composed = (0, compose_1.compose)(middleware, onError);
142
+ const context = await composed(c);
143
+ expect(context.res).not.toBeNull();
144
+ expect(context.res.status).toBe(401);
145
+ expect(await context.res.text()).toBe('Not authorized');
146
+ });
147
+ });
148
+ describe('compose with Context - next() below', () => {
149
+ const middleware = [];
150
+ const req = new Request('http://localhost/');
151
+ const c = new context_1.Context(req);
152
+ const onError = (error, c) => {
153
+ return c.text('onError', 500);
154
+ };
155
+ const handler = (c) => {
156
+ const message = c.req.header('x-custom') || 'blank';
157
+ return c.text(message);
158
+ };
159
+ const mHandler = async (c, next) => {
160
+ c.req.headers.append('x-custom', 'foo');
161
+ await next();
162
+ };
163
+ middleware.push(mHandler);
164
+ middleware.push(handler);
165
+ it('Should return 200 Response', async () => {
166
+ const composed = (0, compose_1.compose)(middleware, onError);
167
+ const context = await composed(c);
168
+ expect(context.res).not.toBeNull();
169
+ expect(context.res.status).toBe(200);
170
+ expect(await context.res.text()).toBe('foo');
171
+ });
172
+ });
173
+ describe('compose with Context - 500 error', () => {
174
+ const middleware = [];
175
+ const req = new Request('http://localhost/');
176
+ const c = new context_1.Context(req);
177
+ const onError = (error, c) => {
178
+ return c.text('onError', 500);
179
+ };
180
+ it('Error on handler', async () => {
181
+ const handler = () => {
182
+ throw new Error();
183
+ };
184
+ const mHandler = async (c, next) => {
185
+ await next();
186
+ };
187
+ middleware.push(mHandler);
188
+ middleware.push(handler);
189
+ const composed = (0, compose_1.compose)(middleware, onError);
190
+ const context = await composed(c);
191
+ expect(context.res).not.toBeNull();
192
+ expect(context.res.status).toBe(500);
193
+ expect(await context.res.text()).toBe('onError');
194
+ });
195
+ it('Error on middleware', async () => {
196
+ const handler = (c) => {
197
+ return c.text('OK');
198
+ };
199
+ const mHandler = async () => {
200
+ throw new Error();
201
+ };
202
+ middleware.push(mHandler);
203
+ middleware.push(handler);
204
+ const composed = (0, compose_1.compose)(middleware, onError);
205
+ const context = await composed(c);
206
+ expect(c.res).not.toBeNull();
207
+ expect(c.res.status).toBe(500);
208
+ expect(await context.res.text()).toBe('onError');
209
+ });
210
+ it('Run all the middlewares', async () => {
211
+ const ctx = { req: {}, res: {} };
212
+ const stack = [];
213
+ const middlewares = [
214
+ async (ctx, next) => {
215
+ stack.push(0);
216
+ await next();
217
+ },
218
+ async (ctx, next) => {
219
+ stack.push(1);
220
+ await next();
221
+ },
222
+ async (ctx, next) => {
223
+ stack.push(2);
224
+ await next();
225
+ },
226
+ ];
227
+ const composed = await (0, compose_1.compose)(middlewares);
228
+ await composed(ctx);
229
+ expect(stack).toEqual([0, 1, 2]);
230
+ });
231
+ });
232
+ describe('Compose', function () {
233
+ function isPromise(x) {
234
+ return x && typeof x.then === 'function';
235
+ }
236
+ it('should get executed order one by one', async () => {
237
+ const arr = [];
238
+ const stack = [];
239
+ const called = [];
240
+ stack.push(async (context, next) => {
241
+ called.push(true);
242
+ arr.push(1);
243
+ await next();
244
+ arr.push(6);
245
+ });
246
+ stack.push(async (context, next) => {
247
+ called.push(true);
248
+ arr.push(2);
249
+ await next();
250
+ arr.push(5);
251
+ });
252
+ stack.push(async (context, next) => {
253
+ called.push(true);
254
+ arr.push(3);
255
+ await next();
256
+ arr.push(4);
257
+ });
258
+ await (0, compose_1.compose)(stack)({});
259
+ expect(called).toEqual([true, true, true]);
260
+ expect(arr).toEqual([1, 2, 3, 4, 5, 6]);
261
+ });
262
+ it('should not get executed if previous next() not triggered', async () => {
263
+ const arr = [];
264
+ const stack = [];
265
+ const called = [];
266
+ stack.push(async (context, next) => {
267
+ called.push(true);
268
+ arr.push(1);
269
+ await next();
270
+ arr.push(6);
271
+ });
272
+ stack.push(async (context, next) => {
273
+ called.push(true);
274
+ arr.push(2);
275
+ });
276
+ stack.push(async (context, next) => {
277
+ called.push(true);
278
+ arr.push(3);
279
+ await next();
280
+ arr.push(4);
281
+ });
282
+ await (0, compose_1.compose)(stack)({});
283
+ expect(called).toEqual([true, true]);
284
+ expect(arr).toEqual([1, 2, 6]);
285
+ });
286
+ it('should be able to be called twice', () => {
287
+ const stack = [];
288
+ stack.push(async (context, next) => {
289
+ context.arr.push(1);
290
+ await next();
291
+ context.arr.push(6);
292
+ });
293
+ stack.push(async (context, next) => {
294
+ context.arr.push(2);
295
+ await next();
296
+ context.arr.push(5);
297
+ });
298
+ stack.push(async (context, next) => {
299
+ context.arr.push(3);
300
+ await next();
301
+ context.arr.push(4);
302
+ });
303
+ const fn = (0, compose_1.compose)(stack);
304
+ const ctx1 = { arr: [] };
305
+ const ctx2 = { arr: [] };
306
+ const out = [1, 2, 3, 4, 5, 6];
307
+ return fn(ctx1)
308
+ .then(() => {
309
+ expect(out).toEqual(ctx1.arr);
310
+ return fn(ctx2);
311
+ })
312
+ .then(() => {
313
+ expect(out).toEqual(ctx2.arr);
314
+ });
315
+ });
316
+ it('should create next functions that return a Promise', function () {
317
+ const stack = [];
318
+ const arr = [];
319
+ for (let i = 0; i < 5; i++) {
320
+ stack.push((context, next) => {
321
+ arr.push(next());
322
+ });
323
+ }
324
+ (0, compose_1.compose)(stack)({});
325
+ for (const next of arr) {
326
+ expect(isPromise(next)).toBe(true);
327
+ }
328
+ });
329
+ it('should work with 0 middleware', function () {
330
+ return (0, compose_1.compose)([])({});
331
+ });
332
+ it('should work when yielding at the end of the stack', async () => {
333
+ const stack = [];
334
+ let called = false;
335
+ stack.push(async (ctx, next) => {
336
+ await next();
337
+ called = true;
338
+ });
339
+ await (0, compose_1.compose)(stack)({});
340
+ expect(called).toBe(true);
341
+ });
342
+ it('should reject on errors in middleware', () => {
343
+ const stack = [];
344
+ stack.push(() => {
345
+ throw new Error();
346
+ });
347
+ return (0, compose_1.compose)(stack)({}).then(() => {
348
+ throw new Error('promise was not rejected');
349
+ }, (e) => {
350
+ expect(e).toBeInstanceOf(Error);
351
+ });
352
+ });
353
+ it('should keep the context', () => {
354
+ const ctx = {};
355
+ const stack = [];
356
+ stack.push(async (ctx2, next) => {
357
+ await next();
358
+ expect(ctx2).toEqual(ctx);
359
+ });
360
+ stack.push(async (ctx2, next) => {
361
+ await next();
362
+ expect(ctx2).toEqual(ctx);
363
+ });
364
+ stack.push(async (ctx2, next) => {
365
+ await next();
366
+ expect(ctx2).toEqual(ctx);
367
+ });
368
+ return (0, compose_1.compose)(stack)(ctx);
369
+ });
370
+ it('should catch downstream errors', async () => {
371
+ const arr = [];
372
+ const stack = [];
373
+ stack.push(async (ctx, next) => {
374
+ arr.push(1);
375
+ try {
376
+ arr.push(6);
377
+ await next();
378
+ arr.push(7);
379
+ }
380
+ catch (err) {
381
+ arr.push(2);
382
+ }
383
+ arr.push(3);
384
+ });
385
+ stack.push(async (ctx, next) => {
386
+ arr.push(4);
387
+ throw new Error();
388
+ });
389
+ await (0, compose_1.compose)(stack)({});
390
+ expect(arr).toEqual([1, 6, 4, 2, 3]);
391
+ });
392
+ it('should compose w/ next', () => {
393
+ let called = false;
394
+ return (0, compose_1.compose)([])({}, async () => {
395
+ called = true;
396
+ }).then(function () {
397
+ expect(called).toBe(true);
398
+ });
399
+ });
400
+ it('should handle errors in wrapped non-async functions', () => {
401
+ const stack = [];
402
+ stack.push(function () {
403
+ throw new Error();
404
+ });
405
+ return (0, compose_1.compose)(stack)({}).then(() => {
406
+ throw new Error('promise was not rejected');
407
+ }, (e) => {
408
+ expect(e).toBeInstanceOf(Error);
409
+ });
410
+ });
411
+ // https://github.com/koajs/compose/pull/27#issuecomment-143109739
412
+ it('should compose w/ other compositions', () => {
413
+ const called = [];
414
+ return (0, compose_1.compose)([
415
+ (0, compose_1.compose)([
416
+ (ctx, next) => {
417
+ called.push(1);
418
+ return next();
419
+ },
420
+ (ctx, next) => {
421
+ called.push(2);
422
+ return next();
423
+ },
424
+ ]),
425
+ (ctx, next) => {
426
+ called.push(3);
427
+ return next();
428
+ },
429
+ ])({}).then(() => expect(called).toEqual([1, 2, 3]));
430
+ });
431
+ it('should throw if next() is called multiple times', () => {
432
+ return (0, compose_1.compose)([
433
+ async (ctx, next) => {
434
+ await next();
435
+ await next();
436
+ },
437
+ ])({}).then(() => {
438
+ throw new Error('boom');
439
+ }, (err) => {
440
+ expect(/multiple times/.test(err.message)).toBe(true);
441
+ });
442
+ });
443
+ it('should return a valid middleware', () => {
444
+ let val = 0;
445
+ return (0, compose_1.compose)([
446
+ (0, compose_1.compose)([
447
+ (ctx, next) => {
448
+ val++;
449
+ return next();
450
+ },
451
+ (ctx, next) => {
452
+ val++;
453
+ return next();
454
+ },
455
+ ]),
456
+ (ctx, next) => {
457
+ val++;
458
+ return next();
459
+ },
460
+ ])({}).then(function () {
461
+ expect(val).toEqual(3);
462
+ });
463
+ });
464
+ it('should return last return value', async () => {
465
+ const stack = [];
466
+ stack.push(async (ctx, next) => {
467
+ await next();
468
+ expect(ctx.val).toEqual(2);
469
+ ctx.val = 1;
470
+ });
471
+ stack.push(async (ctx, next) => {
472
+ ctx.val = 2;
473
+ await next();
474
+ expect(ctx.val).toEqual(2);
475
+ });
476
+ const res = await (0, compose_1.compose)(stack)({ val: 0 });
477
+ expect(res).toEqual({ val: 1 });
478
+ });
479
+ it('should not affect the original middleware array', () => {
480
+ const middleware = [];
481
+ const fn1 = (ctx, next) => {
482
+ return next();
483
+ };
484
+ middleware.push(fn1);
485
+ for (const fn of middleware) {
486
+ expect(fn).toEqual(fn1);
487
+ }
488
+ (0, compose_1.compose)(middleware);
489
+ for (const fn of middleware) {
490
+ expect(fn).toEqual(fn1);
491
+ }
492
+ });
493
+ it('should not get stuck on the passed in next', () => {
494
+ const middleware = [
495
+ (ctx, next) => {
496
+ ctx.middleware++;
497
+ return next();
498
+ },
499
+ ];
500
+ const ctx = {
501
+ middleware: 0,
502
+ next: 0,
503
+ };
504
+ return (0, compose_1.compose)(middleware)(ctx, (ctx, next) => {
505
+ ctx.next++;
506
+ return next();
507
+ }).then(() => {
508
+ expect(ctx).toEqual({ middleware: 1, next: 1 });
509
+ });
510
+ });
511
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const context_1 = require("./context");
4
+ describe('Context', () => {
5
+ const req = new Request('http://localhost/');
6
+ let c;
7
+ beforeEach(() => {
8
+ c = new context_1.Context(req);
9
+ });
10
+ it('c.text()', async () => {
11
+ const res = c.text('text in c', 201, { 'X-Custom': 'Message' });
12
+ expect(res.status).toBe(201);
13
+ expect(res.headers.get('Content-Type')).toBe('text/plain; charset=UTF-8');
14
+ expect(await res.text()).toBe('text in c');
15
+ expect(res.headers.get('X-Custom')).toBe('Message');
16
+ });
17
+ it('c.json()', async () => {
18
+ const res = c.json({ message: 'Hello' }, 201, { 'X-Custom': 'Message' });
19
+ expect(res.status).toBe(201);
20
+ expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8');
21
+ const text = await res.text();
22
+ expect(text).toBe('{"message":"Hello"}');
23
+ expect(res.headers.get('X-Custom')).toBe('Message');
24
+ });
25
+ it('c.json() with c.pretty(true)', async () => {
26
+ c.pretty(true);
27
+ const res = c.json({ message: 'Hello' });
28
+ const text = await res.text();
29
+ expect(text).toBe(`{
30
+ "message": "Hello"
31
+ }`);
32
+ });
33
+ it('c.json() with c.pretty(true, 4)', async () => {
34
+ c.pretty(true, 4);
35
+ const res = c.json({ message: 'Hello' });
36
+ const text = await res.text();
37
+ expect(text).toBe(`{
38
+ "message": "Hello"
39
+ }`);
40
+ });
41
+ it('c.html()', async () => {
42
+ const res = c.html('<h1>Hello! Hono!</h1>', 201, { 'X-Custom': 'Message' });
43
+ expect(res.status).toBe(201);
44
+ expect(res.headers.get('Content-Type')).toMatch('text/html');
45
+ expect(await res.text()).toBe('<h1>Hello! Hono!</h1>');
46
+ expect(res.headers.get('X-Custom')).toBe('Message');
47
+ });
48
+ it('c.redirect()', async () => {
49
+ let res = c.redirect('/destination');
50
+ expect(res.status).toBe(302);
51
+ expect(res.headers.get('Location')).toMatch(/^https?:\/\/.+\/destination$/);
52
+ res = c.redirect('https://example.com/destination');
53
+ expect(res.status).toBe(302);
54
+ expect(res.headers.get('Location')).toBe('https://example.com/destination');
55
+ });
56
+ it('c.header()', async () => {
57
+ c.header('X-Foo', 'Bar');
58
+ const res = c.body('Hi');
59
+ const foo = res.headers.get('X-Foo');
60
+ expect(foo).toBe('Bar');
61
+ });
62
+ it('c.status() and c.statusText()', async () => {
63
+ c.status(201);
64
+ const res = c.body('Hi');
65
+ expect(res.status).toBe(201);
66
+ expect(res.statusText).toBe('Created');
67
+ });
68
+ it('Complex pattern', async () => {
69
+ c.status(404);
70
+ const res = c.json({ hono: 'great app' });
71
+ expect(res.status).toBe(404);
72
+ expect(res.statusText).toBe('Not Found');
73
+ expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8');
74
+ const obj = await res.json();
75
+ expect(obj['hono']).toBe('great app');
76
+ });
77
+ it('Has headers, status, and statusText', async () => {
78
+ c.header('X-Custom1', 'Message1');
79
+ c.header('X-Custom2', 'Message2');
80
+ c.status(200);
81
+ const res = c.newResponse('this is body', {
82
+ status: 201,
83
+ headers: {
84
+ 'X-Custom3': 'Message3',
85
+ 'X-Custom2': 'Message2-Override',
86
+ },
87
+ });
88
+ expect(res.headers.get('X-Custom1')).toBe('Message1');
89
+ expect(res.headers.get('X-Custom2')).toBe('Message2-Override');
90
+ expect(res.headers.get('X-Custom3')).toBe('Message3');
91
+ expect(res.status).toBe(201);
92
+ expect(await res.text()).toBe('this is body');
93
+ // res is already set.
94
+ c.res = res;
95
+ c.header('X-Custom4', 'Message4');
96
+ c.status(202);
97
+ expect(c.res.headers.get('X-Custom4')).toBe('Message4');
98
+ expect(c.res.status).toBe(201);
99
+ expect(c.res.statusText).toBe('OK');
100
+ });
101
+ it('Should return 200 response', async () => {
102
+ const res = c.text('Text');
103
+ expect(res.status).toBe(200);
104
+ expect(res.statusText).toBe('OK');
105
+ });
106
+ it('Should be able read env', async () => {
107
+ const req = new Request('http://localhost/');
108
+ const key = 'a-secret-key';
109
+ const ctx = new context_1.Context(req, {
110
+ env: {
111
+ API_KEY: key,
112
+ },
113
+ res: null,
114
+ event: null,
115
+ });
116
+ expect(ctx.env.API_KEY).toBe(key);
117
+ });
118
+ it('set and set', async () => {
119
+ const ctx = new context_1.Context(req);
120
+ expect(ctx.get('k-foo')).toEqual(undefined);
121
+ ctx.set('k-foo', 'v-foo');
122
+ expect(ctx.get('k-foo')).toEqual('v-foo');
123
+ expect(ctx.get('k-bar')).toEqual(undefined);
124
+ ctx.set('k-bar', { k: 'v' });
125
+ expect(ctx.get('k-bar')).toEqual({ k: 'v' });
126
+ });
127
+ });
package/dist/hono.d.ts CHANGED
@@ -10,7 +10,7 @@ declare global {
10
10
  header: (name: string) => string;
11
11
  }
12
12
  }
13
- export declare type Handler<RequestParamKeyType = string, E = Env> = (c: Context<RequestParamKeyType, E>, next?: Next) => Response | Promise<Response> | void | Promise<void>;
13
+ export declare type Handler<RequestParamKeyType = string, E = Env> = (c: Context<RequestParamKeyType, E>, next: Next) => Response | Promise<Response> | void | Promise<void>;
14
14
  export declare type NotFoundHandler<E = Env> = (c: Context<string, E>) => Response;
15
15
  export declare type ErrorHandler<E = Env> = (err: Error, c: Context<string, E>) => Response;
16
16
  export declare type Next = () => Promise<void>;
@@ -0,0 +1 @@
1
+ export {};