@moostjs/event-http 0.3.11 → 0.3.13

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/dist/index.cjs CHANGED
@@ -9,31 +9,6 @@ var eventCore = require('@wooksjs/event-core');
9
9
 
10
10
  const LOGGER_TITLE = 'moost-http';
11
11
  const CONTEXT_TYPE = 'HTTP';
12
- /**
13
- * ## Moost HTTP Adapter
14
- *
15
- * Moost Adapter for HTTP events
16
- *
17
- * ```ts
18
- * │ // HTTP server example
19
- * │ import { MoostHttp, Get } from '@moostjs/event-http'
20
- * │ import { Moost, Param } from 'moost'
21
- * │
22
- * │ class MyServer extends Moost {
23
- * │ @Get('test/:name')
24
- * │ test(@Param('name') name: string) {
25
- * │ return { message: `Hello ${name}!` }
26
- * │ }
27
- * │ }
28
- * │
29
- * │ const app = new MyServer()
30
- * │ const http = new MoostHttp()
31
- * │ app.adapter(http).listen(3000, () => {
32
- * │ app.getLogger('MyApp').log('Up on port 3000')
33
- * │ })
34
- * │ app.init()
35
- * ```
36
- */
37
12
  class MoostHttp {
38
13
  constructor(httpApp) {
39
14
  this.pathBuilders = {};
@@ -99,7 +74,7 @@ class MoostHttp {
99
74
  : typeof opts.method === 'string'
100
75
  ? opts.method
101
76
  : '';
102
- const targetPath = `${opts.prefix || ''}/${path}`.replace(/\/\/+/g, '/') + `${path.endsWith('//') ? '/' : ''}`; // explicit double slash "//" -> force url to end with slash
77
+ const targetPath = `${opts.prefix || ''}/${path}`.replace(/\/\/+/g, '/') + `${path.endsWith('//') ? '/' : ''}`;
103
78
  if (!fn) {
104
79
  fn = moost.defineMoostEventHandler({
105
80
  contextType: CONTEXT_TYPE,
@@ -112,7 +87,7 @@ class MoostHttp {
112
87
  hooks: {
113
88
  init: ({ unscope }) => {
114
89
  const { rawRequest } = eventHttp.useRequest();
115
- rawRequest.on('end', unscope); // will unscope on request end
90
+ rawRequest.on('end', unscope);
116
91
  },
117
92
  },
118
93
  });
@@ -303,11 +278,6 @@ function useBody() {
303
278
  };
304
279
  }
305
280
 
306
- /**
307
- * Hook to the Response Status
308
- * @decorator
309
- * @paramType TStatusHook
310
- */
311
281
  const StatusHook = () => moost.Resolve((metas, level) => {
312
282
  const hook = eventHttp.useStatus();
313
283
  if (level === 'PARAM') {
@@ -322,12 +292,6 @@ const StatusHook = () => moost.Resolve((metas, level) => {
322
292
  return typeof initialValue === 'number' ? initialValue : 200;
323
293
  }
324
294
  }, 'statusCode');
325
- /**
326
- * Hook to the Response Header
327
- * @decorator
328
- * @param name - header name
329
- * @paramType THeaderHook
330
- */
331
295
  const HeaderHook = (name) => moost.Resolve((metas, level) => {
332
296
  const hook = eventHttp.useSetHeader(name);
333
297
  if (level === 'PARAM') {
@@ -342,12 +306,6 @@ const HeaderHook = (name) => moost.Resolve((metas, level) => {
342
306
  return typeof initialValue === 'string' ? initialValue : '';
343
307
  }
344
308
  }, name);
345
- /**
346
- * Hook to the Response Cookie
347
- * @decorator
348
- * @param name - cookie name
349
- * @paramType TCookieHook
350
- */
351
309
  const CookieHook = (name) => moost.Resolve((metas, level) => {
352
310
  const hook = eventHttp.useSetCookie(name);
353
311
  if (level === 'PARAM') {
@@ -362,12 +320,6 @@ const CookieHook = (name) => moost.Resolve((metas, level) => {
362
320
  return typeof initialValue === 'string' ? initialValue : '';
363
321
  }
364
322
  }, name);
365
- /**
366
- * Hook to the Response Cookie Attributes
367
- * @decorator
368
- * @param name - cookie name
369
- * @paramType TCookieAttributes
370
- */
371
323
  const CookieAttrsHook = (name) => moost.Resolve((metas, level) => {
372
324
  const hook = eventHttp.useSetCookie(name);
373
325
  if (level === 'PARAM') {
@@ -385,12 +337,6 @@ const CookieAttrsHook = (name) => moost.Resolve((metas, level) => {
385
337
  return typeof initialValue === 'object' ? initialValue : {};
386
338
  }
387
339
  }, name);
388
- /**
389
- * Parse Authorisation Header
390
- * @decorator
391
- * @param name - define what to take from the Auth header
392
- * @paramType string
393
- */
394
340
  function Authorization(name) {
395
341
  return moost.Resolve(() => {
396
342
  const auth = eventHttp.useAuthorization();
@@ -412,33 +358,15 @@ function Authorization(name) {
412
358
  }
413
359
  }, 'authorization');
414
360
  }
415
- /**
416
- * Get Request Header Value
417
- * @decorator
418
- * @param name - header name
419
- * @paramType string
420
- */
421
361
  function Header(name) {
422
362
  return moost.Resolve(() => {
423
363
  const headers = eventHttp.useHeaders();
424
364
  return headers[name];
425
365
  }, 'header: ' + name);
426
366
  }
427
- /**
428
- * Get Request Cookie Value
429
- * @decorator
430
- * @param name - cookie name
431
- * @paramType string
432
- */
433
367
  function Cookie(name) {
434
368
  return moost.Resolve(() => eventHttp.useCookies().getCookie(name), 'cookie: ' + name);
435
369
  }
436
- /**
437
- * Get Query Item value or the whole parsed Query as an object
438
- * @decorator
439
- * @param name - query item name (optional)
440
- * @paramType string | object
441
- */
442
370
  function Query(name) {
443
371
  const isItem = !!name;
444
372
  const _name = isItem ? name : 'Query';
@@ -453,76 +381,30 @@ function Query(name) {
453
381
  return Object.keys(json).length ? json : undefined;
454
382
  }, _name));
455
383
  }
456
- /**
457
- * Get Requested URL
458
- * @decorator
459
- * @paramType string
460
- */
461
384
  function Url() {
462
385
  return moost.Resolve(() => eventHttp.useRequest().url || '', 'url');
463
386
  }
464
- /**
465
- * Get Requested HTTP Method
466
- * @decorator
467
- * @paramType string
468
- */
469
387
  function Method() {
470
388
  return moost.Resolve(() => eventHttp.useRequest().method, 'http_method');
471
389
  }
472
- /**
473
- * Get Raw Request Instance
474
- * @decorator
475
- * @paramType IncomingMessage
476
- */
477
390
  function Req() {
478
391
  return moost.Resolve(() => eventHttp.useRequest().rawRequest, 'request');
479
392
  }
480
- /**
481
- * Get Raw Response Instance
482
- * @decorator
483
- * @param opts (optional) { passthrough: boolean }
484
- * @paramType ServerResponse
485
- */
486
393
  function Res(opts) {
487
394
  return moost.Resolve(() => eventHttp.useResponse().rawResponse(opts), 'response');
488
395
  }
489
- /**
490
- * Get Request Unique Identificator (UUID)
491
- * @decorator
492
- * @paramType string
493
- */
494
396
  function ReqId() {
495
397
  return moost.Resolve(() => eventHttp.useRequest().reqId(), 'reqId');
496
398
  }
497
- /**
498
- * Get Request IP Address
499
- * @decorator
500
- * @paramType string
501
- */
502
399
  function Ip(opts) {
503
400
  return moost.Resolve(() => eventHttp.useRequest().getIp(opts), 'ip');
504
401
  }
505
- /**
506
- * Get Request IP Address list
507
- * @decorator
508
- * @paramType string[]
509
- */
510
402
  function IpList() {
511
403
  return moost.Resolve(() => eventHttp.useRequest().getIpList(), 'ipList');
512
404
  }
513
- /**
514
- * Get Parsed Request Body
515
- * @decorator
516
- * @paramType object | string | unknown
517
- */
518
405
  function Body() {
519
406
  return moost.getMoostMate().apply(moost.getMoostMate().decorate('paramSource', 'BODY'), moost.Resolve(() => useBody().parseBody(), 'body'));
520
407
  }
521
- /**
522
- * Get Raw Request Body Buffer
523
- * @decorator
524
- * @paramType Promise<Buffer>
525
- */
526
408
  function RawBody() {
527
409
  return moost.Resolve(() => useBody().rawBody(), 'body');
528
410
  }
@@ -547,43 +429,6 @@ const setHeaderInterceptor = (name, value, opts) => {
547
429
  fn.priority = moost.TInterceptorPriority.AFTER_ALL;
548
430
  return fn;
549
431
  };
550
- /**
551
- * Set Header for Request Handler
552
- *
553
- * ```ts
554
- * import { Get, SetHeader } from '@moostjs/event-http';
555
- * import { Controller } from 'moost';
556
- *
557
- * @Controller()
558
- * export class ExampleController {
559
- * @Get('test')
560
- * // setting header for request handler
561
- * @SetHeader('x-server', 'my-server')
562
- * testHandler() {
563
- * return '...'
564
- * }
565
- * }
566
- * ```
567
- *
568
- * ```ts
569
- * import { Get, SetHeader } from '@moostjs/event-http';
570
- * import { Controller } from 'moost';
571
- *
572
- * @Controller()
573
- * export class ExampleController {
574
- * @Get('test')
575
- * // setting header only if status = 400
576
- * @SetHeader('content-type', 'text/plain', { status: 400 })
577
- * testHandler() {
578
- * return '...'
579
- * }
580
- * }
581
- * ```
582
- *
583
- * @param name name of header
584
- * @param value value for header
585
- * @param options options { status?: number, force?: boolean }
586
- */
587
432
  function SetHeader(...args) {
588
433
  return moost.Intercept(setHeaderInterceptor(...args));
589
434
  }
@@ -599,27 +444,6 @@ const setCookieInterceptor = (name, value, attrs) => {
599
444
  fn.priority = moost.TInterceptorPriority.AFTER_ALL;
600
445
  return fn;
601
446
  };
602
- /**
603
- * Set Cookie for Request Handler
604
- * ```ts
605
- * import { Get, SetCookie } from '@moostjs/event-http';
606
- * import { Controller } from 'moost';
607
- *
608
- * @Controller()
609
- * export class ExampleController {
610
- * @Get('test')
611
- * // setting 'my-cookie' = 'value' with maxAge of 10 minutes
612
- * @SetCookie('my-cookie', 'value', { maxAge: '10m' })
613
- * testHandler() {
614
- * return '...'
615
- * }
616
- * }
617
- * ```
618
- *
619
- * @param name name of cookie
620
- * @param value value for cookie
621
- * @param attrs cookie attributes
622
- */
623
447
  function SetCookie(...args) {
624
448
  return moost.Intercept(setCookieInterceptor(...args));
625
449
  }
@@ -633,25 +457,6 @@ const setStatusInterceptor = (code, opts) => {
633
457
  });
634
458
  });
635
459
  };
636
- /**
637
- * Set Response Status for Request Handler
638
- *
639
- * ```ts
640
- * import { Get, SetStatus } from '@moostjs/event-http';
641
- * import { Controller } from 'moost';
642
- *
643
- * @Controller()
644
- * export class ExampleController {
645
- * @Get('test')
646
- * @SetStatus(201)
647
- * testHandler() {
648
- * return '...'
649
- * }
650
- * }
651
- * ```
652
- * @param code number
653
- * @param opts optional { force?: boolean }
654
- */
655
460
  function SetStatus(...args) {
656
461
  return moost.Intercept(setStatusInterceptor(...args));
657
462
  }
package/dist/index.d.ts CHANGED
@@ -1,329 +1,279 @@
1
- /// <reference types="node" />
2
-
3
- import { HttpError } from '@wooksjs/event-http';
4
- import { IncomingMessage } from 'http';
5
- import { Moost } from 'moost';
6
- import { ServerResponse } from 'http';
7
- import { TConsoleBase } from '@prostojs/logger';
8
- import { TCookieAttributes as TCookieAttributes_2 } from '@wooksjs/event-http';
9
- import { TCookieAttributesInput } from '@wooksjs/event-http';
10
- import { THook } from '@wooksjs/event-core';
11
- import { TInterceptorFn } from 'moost';
12
- import { TMoostAdapter } from 'moost';
13
- import { TMoostAdapterOptions } from 'moost';
14
- import { TProstoRouterPathBuilder } from '@prostojs/router';
15
- import { TProvideRegistry } from 'moost';
16
- import { TWooksHttpOptions } from '@wooksjs/event-http';
17
- import { useHttpContext } from '@wooksjs/event-http';
18
- import { useSetCookies } from '@wooksjs/event-http';
19
- import { WooksHttp } from '@wooksjs/event-http';
20
-
21
- export declare const All: (path?: string) => MethodDecorator;
22
-
23
- /**
24
- * Parse Authorisation Header
25
- * @decorator
26
- * @param name - define what to take from the Auth header
27
- * @paramType string
28
- */
29
- export declare function Authorization(name: 'username' | 'password' | 'bearer' | 'raw' | 'type'): ParameterDecorator & PropertyDecorator;
30
-
31
- /**
32
- * Get Parsed Request Body
33
- * @decorator
34
- * @paramType object | string | unknown
35
- */
36
- declare function Body_2(): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
37
- export { Body_2 as Body }
38
-
39
- /**
40
- * Get Request Cookie Value
41
- * @decorator
42
- * @param name - cookie name
43
- * @paramType string
44
- */
45
- export declare function Cookie(name: string): ParameterDecorator & PropertyDecorator;
46
-
47
- /**
48
- * Hook to the Response Cookie Attributes
49
- * @decorator
50
- * @param name - cookie name
51
- * @paramType TCookieAttributes
52
- */
53
- export declare const CookieAttrsHook: (name: string) => ParameterDecorator & PropertyDecorator;
54
-
55
- /**
56
- * Hook to the Response Cookie
57
- * @decorator
58
- * @param name - cookie name
59
- * @paramType TCookieHook
60
- */
61
- export declare const CookieHook: (name: string) => ParameterDecorator & PropertyDecorator;
62
-
63
- export declare const Delete: (path?: string) => MethodDecorator;
64
-
65
- export declare const Get: (path?: string) => MethodDecorator;
66
-
67
- /**
68
- * Get Request Header Value
69
- * @decorator
70
- * @param name - header name
71
- * @paramType string
72
- */
73
- export declare function Header(name: string): ParameterDecorator & PropertyDecorator;
74
-
75
- /**
76
- * Hook to the Response Header
77
- * @decorator
78
- * @param name - header name
79
- * @paramType THeaderHook
80
- */
81
- export declare const HeaderHook: (name: string) => ParameterDecorator & PropertyDecorator;
82
-
83
- export { HttpError }
84
-
85
- export declare function HttpMethod(method: '*' | 'GET' | 'PUT' | 'POST' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS', path?: string): MethodDecorator;
86
-
87
- /**
88
- * Get Request IP Address
89
- * @decorator
90
- * @paramType string
91
- */
92
- export declare function Ip(opts?: {
93
- trustProxy: boolean;
94
- }): ParameterDecorator & PropertyDecorator;
95
-
96
- /**
97
- * Get Request IP Address list
98
- * @decorator
99
- * @paramType string[]
100
- */
101
- export declare function IpList(): ParameterDecorator & PropertyDecorator;
102
-
103
- /**
104
- * Get Requested HTTP Method
105
- * @decorator
106
- * @paramType string
107
- */
108
- export declare function Method(): ParameterDecorator & PropertyDecorator;
109
-
110
- /**
111
- * ## Moost HTTP Adapter
112
- *
113
- * Moost Adapter for HTTP events
114
- *
115
- * ```ts
116
- * │ // HTTP server example
117
- * │ import { MoostHttp, Get } from '@moostjs/event-http'
118
- * │ import { Moost, Param } from 'moost'
119
- *
120
- * │ class MyServer extends Moost {
121
- * @Get('test/:name')
122
- * │ test(@Param('name') name: string) {
123
- * │ return { message: `Hello ${name}!` }
124
- * │ }
125
- * │ }
126
- *
127
- * │ const app = new MyServer()
128
- * │ const http = new MoostHttp()
129
- * │ app.adapter(http).listen(3000, () => {
130
- * │ app.getLogger('MyApp').log('Up on port 3000')
131
- * │ })
132
- * │ app.init()
133
- * ```
134
- */
135
- export declare class MoostHttp implements TMoostAdapter<THttpHandlerMeta> {
136
- protected httpApp: WooksHttp;
137
- constructor(httpApp?: WooksHttp | TWooksHttpOptions);
138
- getHttpApp(): WooksHttp;
139
- getServerCb(): (req: IncomingMessage, res: ServerResponse<IncomingMessage>) => Promise<void>;
140
- listen(...args: Parameters<WooksHttp['listen']>): Promise<unknown>;
141
- readonly pathBuilders: {
142
- [id: string]: {
143
- GET?: TProstoRouterPathBuilder<Record<string, string | string[]>>;
144
- PUT?: TProstoRouterPathBuilder<Record<string, string | string[]>>;
145
- PATCH?: TProstoRouterPathBuilder<Record<string, string | string[]>>;
146
- POST?: TProstoRouterPathBuilder<Record<string, string | string[]>>;
147
- DELETE?: TProstoRouterPathBuilder<Record<string, string | string[]>>;
148
- };
149
- };
150
- onNotFound(): Promise<{}>;
151
- protected moost?: Moost;
152
- onInit(moost: Moost): void;
153
- getProvideRegistry(): TProvideRegistry;
154
- getLogger(): TConsoleBase;
155
- bindHandler<T extends object = object>(opts: TMoostAdapterOptions<THttpHandlerMeta, T>): void;
156
- }
157
-
158
- export declare const Patch: (path?: string) => MethodDecorator;
159
-
160
- export declare const Post: (path?: string) => MethodDecorator;
161
-
162
- export declare const Put: (path?: string) => MethodDecorator;
163
-
164
- /**
165
- * Get Query Item value or the whole parsed Query as an object
166
- * @decorator
167
- * @param name - query item name (optional)
168
- * @paramType string | object
169
- */
170
- export declare function Query(name?: string): ParameterDecorator;
171
-
172
- /**
173
- * Get Raw Request Body Buffer
174
- * @decorator
175
- * @paramType Promise<Buffer>
176
- */
177
- export declare function RawBody(): ParameterDecorator & PropertyDecorator;
178
-
179
- /**
180
- * Get Raw Request Instance
181
- * @decorator
182
- * @paramType IncomingMessage
183
- */
184
- export declare function Req(): ParameterDecorator & PropertyDecorator;
185
-
186
- /**
187
- * Get Request Unique Identificator (UUID)
188
- * @decorator
189
- * @paramType string
190
- */
191
- export declare function ReqId(): ParameterDecorator & PropertyDecorator;
192
-
193
- /**
194
- * Get Raw Response Instance
195
- * @decorator
196
- * @param opts (optional) { passthrough: boolean }
197
- * @paramType ServerResponse
198
- */
199
- export declare function Res(opts?: {
200
- passthrough: boolean;
201
- }): ParameterDecorator & PropertyDecorator;
202
-
203
- /**
204
- * Set Cookie for Request Handler
205
- * ```ts
206
- * import { Get, SetCookie } from '@moostjs/event-http';
207
- * import { Controller } from 'moost';
208
- *
209
- * @Controller()
210
- * export class ExampleController {
211
- * @Get('test')
212
- * // setting 'my-cookie' = 'value' with maxAge of 10 minutes
213
- * @SetCookie('my-cookie', 'value', { maxAge: '10m' })
214
- * testHandler() {
215
- * return '...'
216
- * }
217
- * }
218
- * ```
219
- *
220
- * @param name name of cookie
221
- * @param value value for cookie
222
- * @param attrs cookie attributes
223
- */
224
- export declare function SetCookie(...args: Parameters<typeof setCookieInterceptor>): ClassDecorator & MethodDecorator;
225
-
226
- declare const setCookieInterceptor: (...args: Parameters<ReturnType<typeof useSetCookies>['setCookie']>) => TInterceptorFn;
227
-
228
- /**
229
- * Set Header for Request Handler
230
- *
231
- * ```ts
232
- * import { Get, SetHeader } from '@moostjs/event-http';
233
- * import { Controller } from 'moost';
234
- *
235
- * @Controller()
236
- * export class ExampleController {
237
- * @Get('test')
238
- * // setting header for request handler
239
- * @SetHeader('x-server', 'my-server')
240
- * testHandler() {
241
- * return '...'
242
- * }
243
- * }
244
- * ```
245
- *
246
- * ```ts
247
- * import { Get, SetHeader } from '@moostjs/event-http';
248
- * import { Controller } from 'moost';
249
- *
250
- * @Controller()
251
- * export class ExampleController {
252
- * @Get('test')
253
- * // setting header only if status = 400
254
- * @SetHeader('content-type', 'text/plain', { status: 400 })
255
- * testHandler() {
256
- * return '...'
257
- * }
258
- * }
259
- * ```
260
- *
261
- * @param name name of header
262
- * @param value value for header
263
- * @param options options { status?: number, force?: boolean }
264
- */
265
- export declare function SetHeader(...args: Parameters<typeof setHeaderInterceptor>): ClassDecorator & MethodDecorator;
266
-
267
- declare const setHeaderInterceptor: (name: string, value: string, opts?: {
268
- force?: boolean;
269
- status?: number;
270
- when?: 'always' | 'error' | 'ok';
271
- }) => TInterceptorFn;
272
-
273
- /**
274
- * Set Response Status for Request Handler
275
- *
276
- * ```ts
277
- * import { Get, SetStatus } from '@moostjs/event-http';
278
- * import { Controller } from 'moost';
279
- *
280
- * @Controller()
281
- * export class ExampleController {
282
- * @Get('test')
283
- * @SetStatus(201)
284
- * testHandler() {
285
- * return '...'
286
- * }
287
- * }
288
- * ```
289
- * @param code number
290
- * @param opts optional { force?: boolean }
291
- */
292
- export declare function SetStatus(...args: Parameters<typeof setStatusInterceptor>): ClassDecorator & MethodDecorator;
293
-
294
- declare const setStatusInterceptor: (code: number, opts?: {
295
- force?: boolean;
296
- }) => TInterceptorFn;
297
-
298
- /**
299
- * Hook to the Response Status
300
- * @decorator
301
- * @paramType TStatusHook
302
- */
303
- export declare const StatusHook: () => ParameterDecorator & PropertyDecorator;
304
-
305
- export declare type TCookieAttributes = Partial<TCookieAttributes_2>;
306
-
307
- export { TCookieAttributesInput }
308
-
309
- export declare type TCookieHook = THook & Partial<THook<TCookieAttributes, 'attrs'>>;
310
-
311
- export declare type THeaderHook = THook;
312
-
313
- export declare interface THttpHandlerMeta {
314
- method: string;
315
- path: string;
316
- }
317
-
318
- export declare type TStatusHook = THook<number>;
319
-
320
- /**
321
- * Get Requested URL
322
- * @decorator
323
- * @paramType string
324
- */
325
- export declare function Url(): ParameterDecorator & PropertyDecorator;
326
-
327
- export { useHttpContext }
328
-
329
- export { }
1
+ import * as _prostojs_logger from '@prostojs/logger';
2
+ import * as moost from 'moost';
3
+ import { TMoostAdapter, Moost, TMoostAdapterOptions, TInterceptorFn } from 'moost';
4
+ import * as http from 'http';
5
+ import { WooksHttp, TWooksHttpOptions, TCookieAttributes as TCookieAttributes$1, useSetCookies } from '@wooksjs/event-http';
6
+ export { HttpError, TCookieAttributesInput, useHttpContext } from '@wooksjs/event-http';
7
+ import { TProstoRouterPathBuilder } from '@prostojs/router';
8
+ import { THook } from '@wooksjs/event-core';
9
+
10
+ interface THttpHandlerMeta {
11
+ method: string;
12
+ path: string;
13
+ }
14
+ /**
15
+ * ## Moost HTTP Adapter
16
+ *
17
+ * Moost Adapter for HTTP events
18
+ *
19
+ * ```ts
20
+ * │ // HTTP server example
21
+ * │ import { MoostHttp, Get } from '@moostjs/event-http'
22
+ * │ import { Moost, Param } from 'moost'
23
+ * │
24
+ * │ class MyServer extends Moost {
25
+ * @Get('test/:name')
26
+ * │ test(@Param('name') name: string) {
27
+ * │ return { message: `Hello ${name}!` }
28
+ * │ }
29
+ * │ }
30
+ * │
31
+ * │ const app = new MyServer()
32
+ * │ const http = new MoostHttp()
33
+ * │ app.adapter(http).listen(3000, () => {
34
+ * │ app.getLogger('MyApp').log('Up on port 3000')
35
+ * │ })
36
+ * │ app.init()
37
+ * ```
38
+ */
39
+ declare class MoostHttp implements TMoostAdapter<THttpHandlerMeta> {
40
+ protected httpApp: WooksHttp;
41
+ constructor(httpApp?: WooksHttp | TWooksHttpOptions);
42
+ getHttpApp(): WooksHttp;
43
+ getServerCb(): (req: http.IncomingMessage, res: http.ServerResponse<http.IncomingMessage>) => Promise<void>;
44
+ listen(...args: Parameters<WooksHttp['listen']>): Promise<unknown>;
45
+ readonly pathBuilders: {
46
+ [id: string]: {
47
+ GET?: TProstoRouterPathBuilder<Record<string, string | string[]>>;
48
+ PUT?: TProstoRouterPathBuilder<Record<string, string | string[]>>;
49
+ PATCH?: TProstoRouterPathBuilder<Record<string, string | string[]>>;
50
+ POST?: TProstoRouterPathBuilder<Record<string, string | string[]>>;
51
+ DELETE?: TProstoRouterPathBuilder<Record<string, string | string[]>>;
52
+ };
53
+ };
54
+ onNotFound(): Promise<{}>;
55
+ protected moost?: Moost;
56
+ onInit(moost: Moost): void;
57
+ getProvideRegistry(): moost.TProvideRegistry;
58
+ getLogger(): _prostojs_logger.TConsoleBase;
59
+ bindHandler<T extends object = object>(opts: TMoostAdapterOptions<THttpHandlerMeta, T>): void;
60
+ }
61
+
62
+ declare function HttpMethod(method: '*' | 'GET' | 'PUT' | 'POST' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS', path?: string): MethodDecorator;
63
+ declare const All: (path?: string) => MethodDecorator;
64
+ declare const Get: (path?: string) => MethodDecorator;
65
+ declare const Post: (path?: string) => MethodDecorator;
66
+ declare const Put: (path?: string) => MethodDecorator;
67
+ declare const Delete: (path?: string) => MethodDecorator;
68
+ declare const Patch: (path?: string) => MethodDecorator;
69
+
70
+ /**
71
+ * Hook to the Response Status
72
+ * @decorator
73
+ * @paramType TStatusHook
74
+ */
75
+ declare const StatusHook: () => ParameterDecorator & PropertyDecorator;
76
+ /**
77
+ * Hook to the Response Header
78
+ * @decorator
79
+ * @param name - header name
80
+ * @paramType THeaderHook
81
+ */
82
+ declare const HeaderHook: (name: string) => ParameterDecorator & PropertyDecorator;
83
+ /**
84
+ * Hook to the Response Cookie
85
+ * @decorator
86
+ * @param name - cookie name
87
+ * @paramType TCookieHook
88
+ */
89
+ declare const CookieHook: (name: string) => ParameterDecorator & PropertyDecorator;
90
+ /**
91
+ * Hook to the Response Cookie Attributes
92
+ * @decorator
93
+ * @param name - cookie name
94
+ * @paramType TCookieAttributes
95
+ */
96
+ declare const CookieAttrsHook: (name: string) => ParameterDecorator & PropertyDecorator;
97
+ /**
98
+ * Parse Authorisation Header
99
+ * @decorator
100
+ * @param name - define what to take from the Auth header
101
+ * @paramType string
102
+ */
103
+ declare function Authorization(name: 'username' | 'password' | 'bearer' | 'raw' | 'type'): ParameterDecorator & PropertyDecorator;
104
+ /**
105
+ * Get Request Header Value
106
+ * @decorator
107
+ * @param name - header name
108
+ * @paramType string
109
+ */
110
+ declare function Header(name: string): ParameterDecorator & PropertyDecorator;
111
+ /**
112
+ * Get Request Cookie Value
113
+ * @decorator
114
+ * @param name - cookie name
115
+ * @paramType string
116
+ */
117
+ declare function Cookie(name: string): ParameterDecorator & PropertyDecorator;
118
+ /**
119
+ * Get Query Item value or the whole parsed Query as an object
120
+ * @decorator
121
+ * @param name - query item name (optional)
122
+ * @paramType string | object
123
+ */
124
+ declare function Query(name?: string): ParameterDecorator;
125
+ /**
126
+ * Get Requested URL
127
+ * @decorator
128
+ * @paramType string
129
+ */
130
+ declare function Url(): ParameterDecorator & PropertyDecorator;
131
+ /**
132
+ * Get Requested HTTP Method
133
+ * @decorator
134
+ * @paramType string
135
+ */
136
+ declare function Method(): ParameterDecorator & PropertyDecorator;
137
+ /**
138
+ * Get Raw Request Instance
139
+ * @decorator
140
+ * @paramType IncomingMessage
141
+ */
142
+ declare function Req(): ParameterDecorator & PropertyDecorator;
143
+ /**
144
+ * Get Raw Response Instance
145
+ * @decorator
146
+ * @param opts (optional) { passthrough: boolean }
147
+ * @paramType ServerResponse
148
+ */
149
+ declare function Res(opts?: {
150
+ passthrough: boolean;
151
+ }): ParameterDecorator & PropertyDecorator;
152
+ /**
153
+ * Get Request Unique Identificator (UUID)
154
+ * @decorator
155
+ * @paramType string
156
+ */
157
+ declare function ReqId(): ParameterDecorator & PropertyDecorator;
158
+ /**
159
+ * Get Request IP Address
160
+ * @decorator
161
+ * @paramType string
162
+ */
163
+ declare function Ip(opts?: {
164
+ trustProxy: boolean;
165
+ }): ParameterDecorator & PropertyDecorator;
166
+ /**
167
+ * Get Request IP Address list
168
+ * @decorator
169
+ * @paramType string[]
170
+ */
171
+ declare function IpList(): ParameterDecorator & PropertyDecorator;
172
+ /**
173
+ * Get Parsed Request Body
174
+ * @decorator
175
+ * @paramType object | string | unknown
176
+ */
177
+ declare function Body(): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
178
+ /**
179
+ * Get Raw Request Body Buffer
180
+ * @decorator
181
+ * @paramType Promise<Buffer>
182
+ */
183
+ declare function RawBody(): ParameterDecorator & PropertyDecorator;
184
+ type TStatusHook = THook<number>;
185
+ type THeaderHook = THook;
186
+ type TCookieAttributes = Partial<TCookieAttributes$1>;
187
+ type TCookieHook = THook & Partial<THook<TCookieAttributes, 'attrs'>>;
188
+
189
+ declare const setHeaderInterceptor: (name: string, value: string, opts?: {
190
+ force?: boolean;
191
+ status?: number;
192
+ when?: 'always' | 'error' | 'ok';
193
+ }) => TInterceptorFn;
194
+ /**
195
+ * Set Header for Request Handler
196
+ *
197
+ * ```ts
198
+ * import { Get, SetHeader } from '@moostjs/event-http';
199
+ * import { Controller } from 'moost';
200
+ *
201
+ * @Controller()
202
+ * export class ExampleController {
203
+ * @Get('test')
204
+ * // setting header for request handler
205
+ * @SetHeader('x-server', 'my-server')
206
+ * testHandler() {
207
+ * return '...'
208
+ * }
209
+ * }
210
+ * ```
211
+ *
212
+ * ```ts
213
+ * import { Get, SetHeader } from '@moostjs/event-http';
214
+ * import { Controller } from 'moost';
215
+ *
216
+ * @Controller()
217
+ * export class ExampleController {
218
+ * @Get('test')
219
+ * // setting header only if status = 400
220
+ * @SetHeader('content-type', 'text/plain', { status: 400 })
221
+ * testHandler() {
222
+ * return '...'
223
+ * }
224
+ * }
225
+ * ```
226
+ *
227
+ * @param name name of header
228
+ * @param value value for header
229
+ * @param options options { status?: number, force?: boolean }
230
+ */
231
+ declare function SetHeader(...args: Parameters<typeof setHeaderInterceptor>): ClassDecorator & MethodDecorator;
232
+ declare const setCookieInterceptor: (...args: Parameters<ReturnType<typeof useSetCookies>['setCookie']>) => TInterceptorFn;
233
+ /**
234
+ * Set Cookie for Request Handler
235
+ * ```ts
236
+ * import { Get, SetCookie } from '@moostjs/event-http';
237
+ * import { Controller } from 'moost';
238
+ *
239
+ * @Controller()
240
+ * export class ExampleController {
241
+ * @Get('test')
242
+ * // setting 'my-cookie' = 'value' with maxAge of 10 minutes
243
+ * @SetCookie('my-cookie', 'value', { maxAge: '10m' })
244
+ * testHandler() {
245
+ * return '...'
246
+ * }
247
+ * }
248
+ * ```
249
+ *
250
+ * @param name name of cookie
251
+ * @param value value for cookie
252
+ * @param attrs cookie attributes
253
+ */
254
+ declare function SetCookie(...args: Parameters<typeof setCookieInterceptor>): ClassDecorator & MethodDecorator;
255
+ declare const setStatusInterceptor: (code: number, opts?: {
256
+ force?: boolean;
257
+ }) => TInterceptorFn;
258
+ /**
259
+ * Set Response Status for Request Handler
260
+ *
261
+ * ```ts
262
+ * import { Get, SetStatus } from '@moostjs/event-http';
263
+ * import { Controller } from 'moost';
264
+ *
265
+ * @Controller()
266
+ * export class ExampleController {
267
+ * @Get('test')
268
+ * @SetStatus(201)
269
+ * testHandler() {
270
+ * return '...'
271
+ * }
272
+ * }
273
+ * ```
274
+ * @param code number
275
+ * @param opts optional { force?: boolean }
276
+ */
277
+ declare function SetStatus(...args: Parameters<typeof setStatusInterceptor>): ClassDecorator & MethodDecorator;
278
+
279
+ export { All, Authorization, Body, Cookie, CookieAttrsHook, CookieHook, Delete, Get, Header, HeaderHook, HttpMethod, Ip, IpList, Method, MoostHttp, Patch, Post, Put, Query, RawBody, Req, ReqId, Res, SetCookie, SetHeader, SetStatus, StatusHook, type TCookieAttributes, type TCookieHook, type THeaderHook, type THttpHandlerMeta, type TStatusHook, Url };
package/dist/index.mjs CHANGED
@@ -8,31 +8,6 @@ import { attachHook } from '@wooksjs/event-core';
8
8
 
9
9
  const LOGGER_TITLE = 'moost-http';
10
10
  const CONTEXT_TYPE = 'HTTP';
11
- /**
12
- * ## Moost HTTP Adapter
13
- *
14
- * Moost Adapter for HTTP events
15
- *
16
- * ```ts
17
- * │ // HTTP server example
18
- * │ import { MoostHttp, Get } from '@moostjs/event-http'
19
- * │ import { Moost, Param } from 'moost'
20
- * │
21
- * │ class MyServer extends Moost {
22
- * │ @Get('test/:name')
23
- * │ test(@Param('name') name: string) {
24
- * │ return { message: `Hello ${name}!` }
25
- * │ }
26
- * │ }
27
- * │
28
- * │ const app = new MyServer()
29
- * │ const http = new MoostHttp()
30
- * │ app.adapter(http).listen(3000, () => {
31
- * │ app.getLogger('MyApp').log('Up on port 3000')
32
- * │ })
33
- * │ app.init()
34
- * ```
35
- */
36
11
  class MoostHttp {
37
12
  constructor(httpApp) {
38
13
  this.pathBuilders = {};
@@ -98,7 +73,7 @@ class MoostHttp {
98
73
  : typeof opts.method === 'string'
99
74
  ? opts.method
100
75
  : '';
101
- const targetPath = `${opts.prefix || ''}/${path}`.replace(/\/\/+/g, '/') + `${path.endsWith('//') ? '/' : ''}`; // explicit double slash "//" -> force url to end with slash
76
+ const targetPath = `${opts.prefix || ''}/${path}`.replace(/\/\/+/g, '/') + `${path.endsWith('//') ? '/' : ''}`;
102
77
  if (!fn) {
103
78
  fn = defineMoostEventHandler({
104
79
  contextType: CONTEXT_TYPE,
@@ -111,7 +86,7 @@ class MoostHttp {
111
86
  hooks: {
112
87
  init: ({ unscope }) => {
113
88
  const { rawRequest } = useRequest();
114
- rawRequest.on('end', unscope); // will unscope on request end
89
+ rawRequest.on('end', unscope);
115
90
  },
116
91
  },
117
92
  });
@@ -302,11 +277,6 @@ function useBody() {
302
277
  };
303
278
  }
304
279
 
305
- /**
306
- * Hook to the Response Status
307
- * @decorator
308
- * @paramType TStatusHook
309
- */
310
280
  const StatusHook = () => Resolve((metas, level) => {
311
281
  const hook = useStatus();
312
282
  if (level === 'PARAM') {
@@ -321,12 +291,6 @@ const StatusHook = () => Resolve((metas, level) => {
321
291
  return typeof initialValue === 'number' ? initialValue : 200;
322
292
  }
323
293
  }, 'statusCode');
324
- /**
325
- * Hook to the Response Header
326
- * @decorator
327
- * @param name - header name
328
- * @paramType THeaderHook
329
- */
330
294
  const HeaderHook = (name) => Resolve((metas, level) => {
331
295
  const hook = useSetHeader(name);
332
296
  if (level === 'PARAM') {
@@ -341,12 +305,6 @@ const HeaderHook = (name) => Resolve((metas, level) => {
341
305
  return typeof initialValue === 'string' ? initialValue : '';
342
306
  }
343
307
  }, name);
344
- /**
345
- * Hook to the Response Cookie
346
- * @decorator
347
- * @param name - cookie name
348
- * @paramType TCookieHook
349
- */
350
308
  const CookieHook = (name) => Resolve((metas, level) => {
351
309
  const hook = useSetCookie(name);
352
310
  if (level === 'PARAM') {
@@ -361,12 +319,6 @@ const CookieHook = (name) => Resolve((metas, level) => {
361
319
  return typeof initialValue === 'string' ? initialValue : '';
362
320
  }
363
321
  }, name);
364
- /**
365
- * Hook to the Response Cookie Attributes
366
- * @decorator
367
- * @param name - cookie name
368
- * @paramType TCookieAttributes
369
- */
370
322
  const CookieAttrsHook = (name) => Resolve((metas, level) => {
371
323
  const hook = useSetCookie(name);
372
324
  if (level === 'PARAM') {
@@ -384,12 +336,6 @@ const CookieAttrsHook = (name) => Resolve((metas, level) => {
384
336
  return typeof initialValue === 'object' ? initialValue : {};
385
337
  }
386
338
  }, name);
387
- /**
388
- * Parse Authorisation Header
389
- * @decorator
390
- * @param name - define what to take from the Auth header
391
- * @paramType string
392
- */
393
339
  function Authorization(name) {
394
340
  return Resolve(() => {
395
341
  const auth = useAuthorization();
@@ -411,33 +357,15 @@ function Authorization(name) {
411
357
  }
412
358
  }, 'authorization');
413
359
  }
414
- /**
415
- * Get Request Header Value
416
- * @decorator
417
- * @param name - header name
418
- * @paramType string
419
- */
420
360
  function Header(name) {
421
361
  return Resolve(() => {
422
362
  const headers = useHeaders();
423
363
  return headers[name];
424
364
  }, 'header: ' + name);
425
365
  }
426
- /**
427
- * Get Request Cookie Value
428
- * @decorator
429
- * @param name - cookie name
430
- * @paramType string
431
- */
432
366
  function Cookie(name) {
433
367
  return Resolve(() => useCookies().getCookie(name), 'cookie: ' + name);
434
368
  }
435
- /**
436
- * Get Query Item value or the whole parsed Query as an object
437
- * @decorator
438
- * @param name - query item name (optional)
439
- * @paramType string | object
440
- */
441
369
  function Query(name) {
442
370
  const isItem = !!name;
443
371
  const _name = isItem ? name : 'Query';
@@ -452,76 +380,30 @@ function Query(name) {
452
380
  return Object.keys(json).length ? json : undefined;
453
381
  }, _name));
454
382
  }
455
- /**
456
- * Get Requested URL
457
- * @decorator
458
- * @paramType string
459
- */
460
383
  function Url() {
461
384
  return Resolve(() => useRequest().url || '', 'url');
462
385
  }
463
- /**
464
- * Get Requested HTTP Method
465
- * @decorator
466
- * @paramType string
467
- */
468
386
  function Method() {
469
387
  return Resolve(() => useRequest().method, 'http_method');
470
388
  }
471
- /**
472
- * Get Raw Request Instance
473
- * @decorator
474
- * @paramType IncomingMessage
475
- */
476
389
  function Req() {
477
390
  return Resolve(() => useRequest().rawRequest, 'request');
478
391
  }
479
- /**
480
- * Get Raw Response Instance
481
- * @decorator
482
- * @param opts (optional) { passthrough: boolean }
483
- * @paramType ServerResponse
484
- */
485
392
  function Res(opts) {
486
393
  return Resolve(() => useResponse().rawResponse(opts), 'response');
487
394
  }
488
- /**
489
- * Get Request Unique Identificator (UUID)
490
- * @decorator
491
- * @paramType string
492
- */
493
395
  function ReqId() {
494
396
  return Resolve(() => useRequest().reqId(), 'reqId');
495
397
  }
496
- /**
497
- * Get Request IP Address
498
- * @decorator
499
- * @paramType string
500
- */
501
398
  function Ip(opts) {
502
399
  return Resolve(() => useRequest().getIp(opts), 'ip');
503
400
  }
504
- /**
505
- * Get Request IP Address list
506
- * @decorator
507
- * @paramType string[]
508
- */
509
401
  function IpList() {
510
402
  return Resolve(() => useRequest().getIpList(), 'ipList');
511
403
  }
512
- /**
513
- * Get Parsed Request Body
514
- * @decorator
515
- * @paramType object | string | unknown
516
- */
517
404
  function Body() {
518
405
  return getMoostMate().apply(getMoostMate().decorate('paramSource', 'BODY'), Resolve(() => useBody().parseBody(), 'body'));
519
406
  }
520
- /**
521
- * Get Raw Request Body Buffer
522
- * @decorator
523
- * @paramType Promise<Buffer>
524
- */
525
407
  function RawBody() {
526
408
  return Resolve(() => useBody().rawBody(), 'body');
527
409
  }
@@ -546,43 +428,6 @@ const setHeaderInterceptor = (name, value, opts) => {
546
428
  fn.priority = TInterceptorPriority.AFTER_ALL;
547
429
  return fn;
548
430
  };
549
- /**
550
- * Set Header for Request Handler
551
- *
552
- * ```ts
553
- * import { Get, SetHeader } from '@moostjs/event-http';
554
- * import { Controller } from 'moost';
555
- *
556
- * @Controller()
557
- * export class ExampleController {
558
- * @Get('test')
559
- * // setting header for request handler
560
- * @SetHeader('x-server', 'my-server')
561
- * testHandler() {
562
- * return '...'
563
- * }
564
- * }
565
- * ```
566
- *
567
- * ```ts
568
- * import { Get, SetHeader } from '@moostjs/event-http';
569
- * import { Controller } from 'moost';
570
- *
571
- * @Controller()
572
- * export class ExampleController {
573
- * @Get('test')
574
- * // setting header only if status = 400
575
- * @SetHeader('content-type', 'text/plain', { status: 400 })
576
- * testHandler() {
577
- * return '...'
578
- * }
579
- * }
580
- * ```
581
- *
582
- * @param name name of header
583
- * @param value value for header
584
- * @param options options { status?: number, force?: boolean }
585
- */
586
431
  function SetHeader(...args) {
587
432
  return Intercept(setHeaderInterceptor(...args));
588
433
  }
@@ -598,27 +443,6 @@ const setCookieInterceptor = (name, value, attrs) => {
598
443
  fn.priority = TInterceptorPriority.AFTER_ALL;
599
444
  return fn;
600
445
  };
601
- /**
602
- * Set Cookie for Request Handler
603
- * ```ts
604
- * import { Get, SetCookie } from '@moostjs/event-http';
605
- * import { Controller } from 'moost';
606
- *
607
- * @Controller()
608
- * export class ExampleController {
609
- * @Get('test')
610
- * // setting 'my-cookie' = 'value' with maxAge of 10 minutes
611
- * @SetCookie('my-cookie', 'value', { maxAge: '10m' })
612
- * testHandler() {
613
- * return '...'
614
- * }
615
- * }
616
- * ```
617
- *
618
- * @param name name of cookie
619
- * @param value value for cookie
620
- * @param attrs cookie attributes
621
- */
622
446
  function SetCookie(...args) {
623
447
  return Intercept(setCookieInterceptor(...args));
624
448
  }
@@ -632,25 +456,6 @@ const setStatusInterceptor = (code, opts) => {
632
456
  });
633
457
  });
634
458
  };
635
- /**
636
- * Set Response Status for Request Handler
637
- *
638
- * ```ts
639
- * import { Get, SetStatus } from '@moostjs/event-http';
640
- * import { Controller } from 'moost';
641
- *
642
- * @Controller()
643
- * export class ExampleController {
644
- * @Get('test')
645
- * @SetStatus(201)
646
- * testHandler() {
647
- * return '...'
648
- * }
649
- * }
650
- * ```
651
- * @param code number
652
- * @param opts optional { force?: boolean }
653
- */
654
459
  function SetStatus(...args) {
655
460
  return Intercept(setStatusInterceptor(...args));
656
461
  }
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@moostjs/event-http",
3
- "version": "0.3.11",
3
+ "version": "0.3.13",
4
4
  "description": "@moostjs/event-http",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
7
7
  "types": "dist/index.d.ts",
8
8
  "sideEffects": false,
9
9
  "exports": {
10
+ "./package.json": "./package.json",
10
11
  ".": {
11
12
  "import": "./dist/index.mjs",
12
13
  "require": "./dist/index.cjs",
@@ -37,9 +38,9 @@
37
38
  "homepage": "https://github.com/moostjs/moostjs/tree/main/packages/event-http#readme",
38
39
  "peerDependencies": {},
39
40
  "dependencies": {
40
- "moost": "0.3.11",
41
- "@wooksjs/event-core": "^0.4.11",
42
- "@wooksjs/event-http": "^0.4.11",
41
+ "moost": "0.3.13",
42
+ "@wooksjs/event-core": "^0.4.15",
43
+ "@wooksjs/event-http": "^0.4.15",
43
44
  "@prostojs/infact": "^0.1.13",
44
45
  "@prostojs/router": "^0.2.1"
45
46
  }