hono 1.1.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 (91) hide show
  1. package/README.md +126 -117
  2. package/dist/compose.d.ts +2 -2
  3. package/dist/compose.js +20 -8
  4. package/dist/compose.test.d.ts +1 -0
  5. package/dist/compose.test.js +511 -0
  6. package/dist/context.d.ts +8 -1
  7. package/dist/context.js +27 -37
  8. package/dist/context.test.d.ts +1 -0
  9. package/dist/context.test.js +127 -0
  10. package/dist/hono.d.ts +44 -21
  11. package/dist/hono.js +74 -43
  12. package/dist/hono.test.d.ts +1 -0
  13. package/dist/hono.test.js +592 -0
  14. package/dist/index.d.ts +2 -2
  15. package/dist/index.js +2 -1
  16. package/dist/middleware/basic-auth/index.js +2 -2
  17. package/dist/middleware/basic-auth/index.test.d.ts +1 -0
  18. package/dist/middleware/basic-auth/index.test.js +119 -0
  19. package/dist/middleware/body-parse/index.test.d.ts +1 -0
  20. package/dist/middleware/body-parse/index.test.js +59 -0
  21. package/dist/middleware/cookie/index.d.ts +1 -1
  22. package/dist/middleware/cookie/index.test.d.ts +1 -0
  23. package/dist/middleware/cookie/index.test.js +54 -0
  24. package/dist/middleware/cors/index.test.d.ts +1 -0
  25. package/dist/middleware/cors/index.test.js +59 -0
  26. package/dist/middleware/etag/index.test.d.ts +1 -0
  27. package/dist/middleware/etag/index.test.js +45 -0
  28. package/dist/middleware/graphql-server/index.js +1 -1
  29. package/dist/middleware/graphql-server/index.test.d.ts +1 -0
  30. package/dist/middleware/graphql-server/index.test.js +480 -0
  31. package/dist/middleware/graphql-server/parse-body.test.d.ts +1 -0
  32. package/dist/middleware/graphql-server/parse-body.test.js +57 -0
  33. package/dist/middleware/jwt/index.js +9 -11
  34. package/dist/middleware/jwt/index.test.d.ts +1 -0
  35. package/dist/middleware/jwt/index.test.js +51 -0
  36. package/dist/middleware/logger/index.test.d.ts +1 -0
  37. package/dist/middleware/logger/index.test.js +49 -0
  38. package/dist/middleware/mustache/index.test.d.ts +1 -0
  39. package/dist/middleware/mustache/index.test.js +49 -0
  40. package/dist/middleware/powered-by/index.test.d.ts +1 -0
  41. package/dist/middleware/powered-by/index.test.js +15 -0
  42. package/dist/middleware/pretty-json/index.test.d.ts +1 -0
  43. package/dist/middleware/pretty-json/index.test.js +28 -0
  44. package/dist/middleware/serve-static/index.test.d.ts +1 -0
  45. package/dist/middleware/serve-static/index.test.js +58 -0
  46. package/dist/router/reg-exp-router/index.d.ts +1 -1
  47. package/dist/router/reg-exp-router/index.js +1 -1
  48. package/dist/router/reg-exp-router/node.d.ts +3 -0
  49. package/dist/router/reg-exp-router/node.js +13 -7
  50. package/dist/router/reg-exp-router/router.d.ts +21 -2
  51. package/dist/router/reg-exp-router/router.js +301 -81
  52. package/dist/router/reg-exp-router/router.test.d.ts +1 -0
  53. package/dist/router/reg-exp-router/router.test.js +212 -0
  54. package/dist/router/reg-exp-router/trie.d.ts +7 -3
  55. package/dist/router/reg-exp-router/trie.js +3 -3
  56. package/dist/router/trie-router/index.d.ts +1 -1
  57. package/dist/router/trie-router/index.js +1 -1
  58. package/dist/router/trie-router/node.d.ts +4 -3
  59. package/dist/router/trie-router/node.js +123 -55
  60. package/dist/router/trie-router/node.test.d.ts +1 -0
  61. package/dist/router/trie-router/node.test.js +351 -0
  62. package/dist/router/trie-router/router.d.ts +2 -2
  63. package/dist/router/trie-router/router.js +1 -1
  64. package/dist/router/trie-router/router.test.d.ts +1 -0
  65. package/dist/router/trie-router/router.test.js +98 -0
  66. package/dist/router.d.ts +4 -3
  67. package/dist/router.js +5 -4
  68. package/dist/utils/body.js +2 -2
  69. package/dist/utils/body.test.d.ts +1 -0
  70. package/dist/utils/body.test.js +45 -0
  71. package/dist/utils/buffer.js +1 -1
  72. package/dist/utils/buffer.test.d.ts +1 -0
  73. package/dist/utils/buffer.test.js +36 -0
  74. package/dist/utils/cloudflare.test.d.ts +1 -0
  75. package/dist/utils/cloudflare.test.js +42 -0
  76. package/dist/utils/crypto.test.d.ts +1 -0
  77. package/dist/utils/crypto.test.js +15 -0
  78. package/dist/utils/encode.test.d.ts +1 -0
  79. package/dist/utils/encode.test.js +54 -0
  80. package/dist/utils/http-status.test.d.ts +1 -0
  81. package/dist/utils/http-status.test.js +8 -0
  82. package/dist/utils/jwt/jwt.test.d.ts +1 -0
  83. package/dist/utils/jwt/jwt.test.js +171 -0
  84. package/dist/utils/jwt/types.test.d.ts +1 -0
  85. package/dist/utils/jwt/types.test.js +12 -0
  86. package/dist/utils/mime.test.d.ts +1 -0
  87. package/dist/utils/mime.test.js +13 -0
  88. package/dist/utils/url.js +4 -4
  89. package/dist/utils/url.test.d.ts +1 -0
  90. package/dist/utils/url.test.js +87 -0
  91. package/package.json +3 -4
@@ -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
+ });
package/dist/context.d.ts CHANGED
@@ -4,11 +4,16 @@ declare type Headers = Record<string, string>;
4
4
  declare type Data = string | ArrayBuffer | ReadableStream;
5
5
  export declare type Env = Record<string, any>;
6
6
  export declare class Context<RequestParamKeyType = string, E = Env> {
7
- #private;
8
7
  req: Request<RequestParamKeyType>;
9
8
  res: Response;
10
9
  env: E;
11
10
  event: FetchEvent;
11
+ private _headers;
12
+ private _status;
13
+ private _statusText;
14
+ private _pretty;
15
+ private _prettySpace;
16
+ private _map;
12
17
  render: (template: string, params?: object, options?: object) => Promise<Response>;
13
18
  notFound: () => Response | Promise<Response>;
14
19
  constructor(req: Request<RequestParamKeyType>, opts?: {
@@ -19,6 +24,8 @@ export declare class Context<RequestParamKeyType = string, E = Env> {
19
24
  private initRequest;
20
25
  header(name: string, value: string): void;
21
26
  status(status: StatusCode): void;
27
+ set(key: string, value: any): void;
28
+ get(key: string): any;
22
29
  pretty(prettyJSON: boolean, space?: number): void;
23
30
  newResponse(data: Data, init?: ResponseInit): Response;
24
31
  body(data: Data, status?: StatusCode, headers?: Headers): Response;
package/dist/context.js CHANGED
@@ -1,30 +1,19 @@
1
1
  "use strict";
2
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
- if (kind === "m") throw new TypeError("Private method is not writable");
4
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
- };
8
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
- };
13
- var _Context_headers, _Context_status, _Context_statusText, _Context_pretty, _Context_prettySpace;
14
2
  Object.defineProperty(exports, "__esModule", { value: true });
15
3
  exports.Context = void 0;
16
4
  const http_status_1 = require("./utils/http-status");
17
5
  const url_1 = require("./utils/url");
18
6
  class Context {
19
7
  constructor(req, opts) {
20
- _Context_headers.set(this, void 0);
21
- _Context_status.set(this, void 0);
22
- _Context_statusText.set(this, void 0);
23
- _Context_pretty.set(this, void 0);
24
- _Context_prettySpace.set(this, 2);
8
+ this.res = undefined;
9
+ this._headers = {};
10
+ this._status = 200;
11
+ this._statusText = '';
12
+ this._pretty = false;
13
+ this._prettySpace = 2;
25
14
  this.req = this.initRequest(req);
15
+ this._map = {};
26
16
  Object.assign(this, opts);
27
- __classPrivateFieldSet(this, _Context_headers, {}, "f");
28
17
  }
29
18
  initRequest(req) {
30
19
  req.header = (name) => {
@@ -40,51 +29,53 @@ class Context {
40
29
  if (this.res) {
41
30
  this.res.headers.set(name, value);
42
31
  }
43
- __classPrivateFieldGet(this, _Context_headers, "f")[name] = value;
32
+ this._headers[name] = value;
44
33
  }
45
34
  status(status) {
46
- if (this.res) {
47
- console.warn('c.res.status is already set.');
48
- return;
49
- }
50
- __classPrivateFieldSet(this, _Context_status, status, "f");
51
- __classPrivateFieldSet(this, _Context_statusText, (0, http_status_1.getStatusText)(status), "f");
35
+ this._status = status;
36
+ this._statusText = (0, http_status_1.getStatusText)(status);
37
+ }
38
+ set(key, value) {
39
+ this._map[key] = value;
40
+ }
41
+ get(key) {
42
+ return this._map[key];
52
43
  }
53
44
  pretty(prettyJSON, space = 2) {
54
- __classPrivateFieldSet(this, _Context_pretty, prettyJSON, "f");
55
- __classPrivateFieldSet(this, _Context_prettySpace, space, "f");
45
+ this._pretty = prettyJSON;
46
+ this._prettySpace = space;
56
47
  }
57
48
  newResponse(data, init = {}) {
58
- init.status = init.status || __classPrivateFieldGet(this, _Context_status, "f") || 200;
49
+ init.status = init.status || this._status || 200;
59
50
  init.statusText =
60
- init.statusText || __classPrivateFieldGet(this, _Context_statusText, "f") || (0, http_status_1.getStatusText)(init.status);
61
- init.headers = Object.assign(Object.assign({}, __classPrivateFieldGet(this, _Context_headers, "f")), init.headers);
51
+ init.statusText || this._statusText || (0, http_status_1.getStatusText)(init.status);
52
+ init.headers = Object.assign(Object.assign({}, this._headers), init.headers);
62
53
  return new Response(data, init);
63
54
  }
64
- body(data, status = __classPrivateFieldGet(this, _Context_status, "f"), headers = __classPrivateFieldGet(this, _Context_headers, "f")) {
55
+ body(data, status = this._status, headers = this._headers) {
65
56
  return this.newResponse(data, {
66
57
  status: status,
67
58
  headers: headers,
68
59
  });
69
60
  }
70
- text(text, status = __classPrivateFieldGet(this, _Context_status, "f"), headers = {}) {
61
+ text(text, status = this._status, headers = {}) {
71
62
  if (typeof text !== 'string') {
72
63
  throw new TypeError('text method arg must be a string!');
73
64
  }
74
65
  headers['Content-Type'] || (headers['Content-Type'] = 'text/plain; charset=UTF-8');
75
66
  return this.body(text, status, headers);
76
67
  }
77
- json(object, status = __classPrivateFieldGet(this, _Context_status, "f"), headers = {}) {
68
+ json(object, status = this._status, headers = {}) {
78
69
  if (typeof object !== 'object') {
79
70
  throw new TypeError('json method arg must be an object!');
80
71
  }
81
- const body = __classPrivateFieldGet(this, _Context_pretty, "f")
82
- ? JSON.stringify(object, null, __classPrivateFieldGet(this, _Context_prettySpace, "f"))
72
+ const body = this._pretty
73
+ ? JSON.stringify(object, null, this._prettySpace)
83
74
  : JSON.stringify(object);
84
75
  headers['Content-Type'] || (headers['Content-Type'] = 'application/json; charset=UTF-8');
85
76
  return this.body(body, status, headers);
86
77
  }
87
- html(html, status = __classPrivateFieldGet(this, _Context_status, "f"), headers = {}) {
78
+ html(html, status = this._status, headers = {}) {
88
79
  if (typeof html !== 'string') {
89
80
  throw new TypeError('html method arg must be a string!');
90
81
  }
@@ -109,4 +100,3 @@ class Context {
109
100
  }
110
101
  }
111
102
  exports.Context = Context;
112
- _Context_headers = new WeakMap(), _Context_status = new WeakMap(), _Context_statusText = new WeakMap(), _Context_pretty = new WeakMap(), _Context_prettySpace = new WeakMap();
@@ -0,0 +1 @@
1
+ export {};