@3sixty/api-core-server 0.20.0-next.0 → 0.20.0-next.1

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 (31) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +20 -2
  3. package/dist/es/baseServer.js +666 -0
  4. package/dist/es/baseServer.js.map +1 -0
  5. package/dist/es/index.js +8 -0
  6. package/dist/es/index.js.map +1 -0
  7. package/dist/es/models/IBaseServerConstructorOptions.js +2 -0
  8. package/dist/es/models/IBaseServerConstructorOptions.js.map +1 -0
  9. package/dist/es/models/IRestProcessorChains.js +2 -0
  10. package/dist/es/models/IRestProcessorChains.js.map +1 -0
  11. package/dist/es/models/IServerCorsOptions.js +2 -0
  12. package/dist/es/models/IServerCorsOptions.js.map +1 -0
  13. package/dist/es/models/ISocketProcessorChains.js +2 -0
  14. package/dist/es/models/ISocketProcessorChains.js.map +1 -0
  15. package/dist/types/baseServer.d.ts +232 -0
  16. package/dist/types/index.d.ts +5 -0
  17. package/dist/types/models/IBaseServerConstructorOptions.d.ts +19 -0
  18. package/dist/types/models/IRestProcessorChains.d.ts +18 -0
  19. package/dist/types/models/IServerCorsOptions.d.ts +26 -0
  20. package/dist/types/models/ISocketProcessorChains.d.ts +26 -0
  21. package/docs/changelog.md +16 -0
  22. package/docs/examples.md +78 -0
  23. package/docs/reference/classes/BaseServer.md +730 -0
  24. package/docs/reference/index.md +12 -0
  25. package/docs/reference/interfaces/IBaseServerConstructorOptions.md +33 -0
  26. package/docs/reference/interfaces/IRestProcessorChains.md +171 -0
  27. package/docs/reference/interfaces/IServerCorsOptions.md +43 -0
  28. package/docs/reference/interfaces/ISocketProcessorChains.md +245 -0
  29. package/locales/.validate-ignore +3 -0
  30. package/locales/en.json +33 -0
  31. package/package.json +54 -6
@@ -0,0 +1,730 @@
1
+ # Abstract Class: BaseServer\<T\>
2
+
3
+ Base class for web servers, containing the transport agnostic route processing.
4
+ Derived classes supply the transport specific behaviour through the abstract server methods.
5
+
6
+ ## Type Parameters
7
+
8
+ ### T {#t}
9
+
10
+ `T`
11
+
12
+ ## Implements
13
+
14
+ - `IWebServer`\<`T`\>
15
+ - `IHealthProviderComponent`
16
+
17
+ ## Constructors
18
+
19
+ ### Constructor
20
+
21
+ > **new BaseServer**\<`T`\>(`options?`): `BaseServer`\<`T`\>
22
+
23
+ Create a new instance of BaseServer.
24
+
25
+ #### Parameters
26
+
27
+ ##### options?
28
+
29
+ [`IBaseServerConstructorOptions`](../interfaces/IBaseServerConstructorOptions.md)
30
+
31
+ The options for the server.
32
+
33
+ #### Returns
34
+
35
+ `BaseServer`\<`T`\>
36
+
37
+ ## Properties
38
+
39
+ ### CLASS\_NAME {#class_name}
40
+
41
+ > `readonly` `static` **CLASS\_NAME**: `string`
42
+
43
+ Runtime name for the class.
44
+
45
+ ***
46
+
47
+ ### \_loggingComponentType? {#_loggingcomponenttype}
48
+
49
+ > `protected` `readonly` `optional` **\_loggingComponentType?**: `string`
50
+
51
+ The logging component type.
52
+
53
+ ***
54
+
55
+ ### \_logging? {#_logging}
56
+
57
+ > `protected` `readonly` `optional` **\_logging?**: `ILoggingComponent`
58
+
59
+ The logging component.
60
+
61
+ ***
62
+
63
+ ### \_options? {#_options}
64
+
65
+ > `protected` `optional` **\_options?**: `IWebServerOptions`
66
+
67
+ The options for the server.
68
+
69
+ ***
70
+
71
+ ### \_started {#_started}
72
+
73
+ > `protected` **\_started**: `boolean`
74
+
75
+ Whether the server has been started.
76
+
77
+ ***
78
+
79
+ ### \_mimeTypeProcessors {#_mimetypeprocessors}
80
+
81
+ > `protected` `readonly` **\_mimeTypeProcessors**: `IMimeTypeProcessor`[]
82
+
83
+ The mime type processors.
84
+
85
+ ***
86
+
87
+ ### \_includeErrorStack {#_includeerrorstack}
88
+
89
+ > `protected` `readonly` **\_includeErrorStack**: `boolean`
90
+
91
+ Include the stack with errors.
92
+
93
+ ***
94
+
95
+ ### \_publicOrigin? {#_publicorigin}
96
+
97
+ > `protected` `optional` **\_publicOrigin?**: `string`
98
+
99
+ The public origin of the server, used for constructing the request URL and for CORS.
100
+
101
+ ***
102
+
103
+ ### \_localOrigin? {#_localorigin}
104
+
105
+ > `protected` `optional` **\_localOrigin?**: `string`
106
+
107
+ The local origin of the server, used for constructing the request URL and for CORS.
108
+
109
+ ***
110
+
111
+ ### \_restChains {#_restchains}
112
+
113
+ > `protected` **\_restChains**: [`IRestProcessorChains`](../interfaces/IRestProcessorChains.md)
114
+
115
+ The REST processor chains resolved when the server is built.
116
+
117
+ ***
118
+
119
+ ### \_socketChains {#_socketchains}
120
+
121
+ > `protected` **\_socketChains**: [`ISocketProcessorChains`](../interfaces/ISocketProcessorChains.md)
122
+
123
+ The socket processor chains resolved when the server is built.
124
+
125
+ ## Methods
126
+
127
+ ### start() {#start}
128
+
129
+ > **start**(): `Promise`\<`void`\>
130
+
131
+ Start the server.
132
+
133
+ #### Returns
134
+
135
+ `Promise`\<`void`\>
136
+
137
+ A promise that resolves when the server is listening for connections.
138
+
139
+ #### Implementation of
140
+
141
+ `IWebServer.start`
142
+
143
+ ***
144
+
145
+ ### stop() {#stop}
146
+
147
+ > **stop**(): `Promise`\<`void`\>
148
+
149
+ Stop the server.
150
+
151
+ #### Returns
152
+
153
+ `Promise`\<`void`\>
154
+
155
+ A promise that resolves when the server has shut down all connections.
156
+
157
+ #### Implementation of
158
+
159
+ `IWebServer.stop`
160
+
161
+ ***
162
+
163
+ ### health() {#health}
164
+
165
+ > **health**(): `Promise`\<`IHealth`[]\>
166
+
167
+ Returns the health status of the component.
168
+
169
+ #### Returns
170
+
171
+ `Promise`\<`IHealth`[]\>
172
+
173
+ The health status of the component, can return multiple entries for elements within the component.
174
+
175
+ #### Implementation of
176
+
177
+ `IHealthProviderComponent.health`
178
+
179
+ ***
180
+
181
+ ### healthApplication() {#healthapplication}
182
+
183
+ > **healthApplication**(`callback`): `Promise`\<`IHealth`[] \| `undefined`\>
184
+
185
+ Verify the root endpoint is reachable and returns a body by making a real HTTP request.
186
+ Skipped when GET / is not registered on this server instance.
187
+
188
+ #### Parameters
189
+
190
+ ##### callback
191
+
192
+ `HealthApplicationCallback`
193
+
194
+ The callback to invoke when a deferred health result is ready.
195
+
196
+ #### Returns
197
+
198
+ `Promise`\<`IHealth`[] \| `undefined`\>
199
+
200
+ The application health status of the component.
201
+
202
+ #### Implementation of
203
+
204
+ `IHealthProviderComponent.healthApplication`
205
+
206
+ ***
207
+
208
+ ### requestLocalRoot() {#requestlocalroot}
209
+
210
+ > `protected` **requestLocalRoot**(`url`): `Promise`\<\{ `ok`: `boolean`; `body`: `string`; \}\>
211
+
212
+ Request the root endpoint of this server over the loopback address.
213
+
214
+ #### Parameters
215
+
216
+ ##### url
217
+
218
+ `string`
219
+
220
+ The url of the root endpoint.
221
+
222
+ #### Returns
223
+
224
+ `Promise`\<\{ `ok`: `boolean`; `body`: `string`; \}\>
225
+
226
+ Whether the response was successful and its body.
227
+
228
+ ***
229
+
230
+ ### prepareBuild() {#preparebuild}
231
+
232
+ > `protected` **prepareBuild**(`restRouteProcessors?`, `restRoutes?`, `socketRouteProcessors?`, `socketRoutes?`, `options?`): `Promise`\<`void`\>
233
+
234
+ Perform the transport agnostic part of building the server, validating the routes and
235
+ processors, resolving the origins and resolving the processor chains.
236
+
237
+ #### Parameters
238
+
239
+ ##### restRouteProcessors?
240
+
241
+ `IRestRouteProcessor`[]
242
+
243
+ The processors for incoming requests over REST.
244
+
245
+ ##### restRoutes?
246
+
247
+ `IRestRoute`\<`any`, `any`\>[]
248
+
249
+ The REST routes.
250
+
251
+ ##### socketRouteProcessors?
252
+
253
+ `ISocketRouteProcessor`[]
254
+
255
+ The processors for incoming requests over Sockets.
256
+
257
+ ##### socketRoutes?
258
+
259
+ `ISocketRoute`\<`any`, `any`\>[]
260
+
261
+ The socket routes.
262
+
263
+ ##### options?
264
+
265
+ `IWebServerOptions`
266
+
267
+ Options for building the server.
268
+
269
+ #### Returns
270
+
271
+ `Promise`\<`void`\>
272
+
273
+ A promise that resolves when the shared build steps are complete.
274
+
275
+ ***
276
+
277
+ ### buildProcessorChains() {#buildprocessorchains}
278
+
279
+ > `protected` **buildProcessorChains**(`restRouteProcessors?`, `socketRouteProcessors?`): `void`
280
+
281
+ Resolve the route processors in to per phase chains, so that the lookups and bindings
282
+ are performed once when the server is built instead of on every request.
283
+
284
+ #### Parameters
285
+
286
+ ##### restRouteProcessors?
287
+
288
+ `IRestRouteProcessor`[]
289
+
290
+ The processors for the incoming REST requests.
291
+
292
+ ##### socketRouteProcessors?
293
+
294
+ `ISocketRouteProcessor`[]
295
+
296
+ The processors for the incoming socket requests.
297
+
298
+ #### Returns
299
+
300
+ `void`
301
+
302
+ ***
303
+
304
+ ### resolveBodyLimits() {#resolvebodylimits}
305
+
306
+ > `protected` **resolveBodyLimits**(): `object`
307
+
308
+ Merge the configured body limits over the built-in ones and validate them.
309
+
310
+ #### Returns
311
+
312
+ `object`
313
+
314
+ The named body limits in bytes.
315
+
316
+ #### Throws
317
+
318
+ GeneralError If a limit is not a positive integer.
319
+
320
+ ***
321
+
322
+ ### resolveCorsOptions() {#resolvecorsoptions}
323
+
324
+ > `protected` **resolveCorsOptions**(`options?`): [`IServerCorsOptions`](../interfaces/IServerCorsOptions.md)
325
+
326
+ Resolve the CORS options from the web server options, merging in the defaults.
327
+
328
+ #### Parameters
329
+
330
+ ##### options?
331
+
332
+ `IWebServerOptions`
333
+
334
+ The web server options.
335
+
336
+ #### Returns
337
+
338
+ [`IServerCorsOptions`](../interfaces/IServerCorsOptions.md)
339
+
340
+ The resolved CORS options.
341
+
342
+ ***
343
+
344
+ ### buildServerRequest() {#buildserverrequest}
345
+
346
+ > `protected` **buildServerRequest**(`method`, `url`, `body`, `query?`, `pathParams?`, `headers?`): `IHttpServerRequest`
347
+
348
+ Build the server request from the transport values, the path params and query must already
349
+ have been decoded by the transport, as decoding them again corrupts values containing %.
350
+
351
+ #### Parameters
352
+
353
+ ##### method
354
+
355
+ `HttpMethod`
356
+
357
+ The request method.
358
+
359
+ ##### url
360
+
361
+ `string`
362
+
363
+ The full request url, including the origin.
364
+
365
+ ##### body
366
+
367
+ `unknown`
368
+
369
+ The request body.
370
+
371
+ ##### query?
372
+
373
+ `IHttpRequestQuery`
374
+
375
+ The request query params.
376
+
377
+ ##### pathParams?
378
+
379
+ `IHttpRequestPathParams`
380
+
381
+ The request path params.
382
+
383
+ ##### headers?
384
+
385
+ `IHttpHeaders`
386
+
387
+ The request headers.
388
+
389
+ #### Returns
390
+
391
+ `IHttpServerRequest`
392
+
393
+ The server request.
394
+
395
+ ***
396
+
397
+ ### validateResponse() {#validateresponse}
398
+
399
+ > `protected` **validateResponse**(`httpResponse`): `void`
400
+
401
+ Validate the processed response before it is sent, so that every transport fails an invalid
402
+ response in the same way rather than with its own transport specific error.
403
+
404
+ #### Parameters
405
+
406
+ ##### httpResponse
407
+
408
+ `IHttpResponse`
409
+
410
+ The processed response.
411
+
412
+ #### Returns
413
+
414
+ `void`
415
+
416
+ #### Throws
417
+
418
+ GeneralError If the status code or a header is invalid.
419
+
420
+ ***
421
+
422
+ ### buildContextIds() {#buildcontextids}
423
+
424
+ > `protected` **buildContextIds**(`requestOrigin`, `headers?`): `IContextIds`
425
+
426
+ Build the context ids for an incoming request.
427
+
428
+ #### Parameters
429
+
430
+ ##### requestOrigin
431
+
432
+ `string`
433
+
434
+ The origin the request was received on.
435
+
436
+ ##### headers?
437
+
438
+ `IHttpHeaders`
439
+
440
+ The request headers.
441
+
442
+ #### Returns
443
+
444
+ `IContextIds`
445
+
446
+ The context ids.
447
+
448
+ ***
449
+
450
+ ### runProcessorsRest() {#runprocessorsrest}
451
+
452
+ > `protected` **runProcessorsRest**(`restRoute`, `httpServerRequest`, `httpResponse`, `contextIds`, `processorState`): `Promise`\<`void`\>
453
+
454
+ Run the REST processors for the route.
455
+
456
+ #### Parameters
457
+
458
+ ##### restRoute
459
+
460
+ `IRestRoute`\<`any`, `any`\> \| `undefined`
461
+
462
+ The route to process.
463
+
464
+ ##### httpServerRequest
465
+
466
+ `IHttpServerRequest`
467
+
468
+ The incoming request.
469
+
470
+ ##### httpResponse
471
+
472
+ `IHttpResponse`
473
+
474
+ The outgoing response.
475
+
476
+ ##### contextIds
477
+
478
+ `IContextIds`
479
+
480
+ The context IDs of the request.
481
+
482
+ ##### processorState
483
+
484
+ \{\[`id`: `string`\]: `unknown`; \}
485
+
486
+ The state handed through the processors.
487
+
488
+ #### Returns
489
+
490
+ `Promise`\<`void`\>
491
+
492
+ ***
493
+
494
+ ### runProcessorsSocket() {#runprocessorssocket}
495
+
496
+ > `protected` **runProcessorsSocket**(`socketRoute`, `socketServerRequest`, `httpResponse`, `contextIds`, `processorState`, `requestTopic`, `responseEmitter`): `Promise`\<`void`\>
497
+
498
+ Run the socket processors for the route.
499
+
500
+ #### Parameters
501
+
502
+ ##### socketRoute
503
+
504
+ `ISocketRoute`
505
+
506
+ The route to process.
507
+
508
+ ##### socketServerRequest
509
+
510
+ `ISocketServerRequest`
511
+
512
+ The incoming request.
513
+
514
+ ##### httpResponse
515
+
516
+ `IHttpResponse`
517
+
518
+ The outgoing response.
519
+
520
+ ##### contextIds
521
+
522
+ `IContextIds`
523
+
524
+ The context IDs of the request.
525
+
526
+ ##### processorState
527
+
528
+ \{\[`id`: `string`\]: `unknown`; \}
529
+
530
+ The state handed through the processors.
531
+
532
+ ##### requestTopic
533
+
534
+ `string`
535
+
536
+ The topic of the request.
537
+
538
+ ##### responseEmitter
539
+
540
+ (`topic`, `response`) => `Promise`\<`void`\>
541
+
542
+ The emitter to send the response on.
543
+
544
+ #### Returns
545
+
546
+ `Promise`\<`void`\>
547
+
548
+ ***
549
+
550
+ ### className() {#classname}
551
+
552
+ > `abstract` **className**(): `string`
553
+
554
+ Returns the class name of the component.
555
+
556
+ #### Returns
557
+
558
+ `string`
559
+
560
+ The class name of the component.
561
+
562
+ #### Implementation of
563
+
564
+ `IWebServer.className`
565
+
566
+ ***
567
+
568
+ ### getInstance() {#getinstance}
569
+
570
+ > `abstract` **getInstance**(): `T`
571
+
572
+ Get the web server instance.
573
+
574
+ #### Returns
575
+
576
+ `T`
577
+
578
+ The web server instance.
579
+
580
+ #### Implementation of
581
+
582
+ `IWebServer.getInstance`
583
+
584
+ ***
585
+
586
+ ### build() {#build}
587
+
588
+ > `abstract` **build**(`restRouteProcessors?`, `restRoutes?`, `socketRouteProcessors?`, `socketRoutes?`, `options?`): `Promise`\<`void`\>
589
+
590
+ Build the server.
591
+
592
+ #### Parameters
593
+
594
+ ##### restRouteProcessors?
595
+
596
+ `IRestRouteProcessor`[]
597
+
598
+ The processors for incoming requests over REST.
599
+
600
+ ##### restRoutes?
601
+
602
+ `IRestRoute`\<`any`, `any`\>[]
603
+
604
+ The REST routes.
605
+
606
+ ##### socketRouteProcessors?
607
+
608
+ `ISocketRouteProcessor`[]
609
+
610
+ The processors for incoming requests over Sockets.
611
+
612
+ ##### socketRoutes?
613
+
614
+ `ISocketRoute`\<`any`, `any`\>[]
615
+
616
+ The socket routes.
617
+
618
+ ##### options?
619
+
620
+ `IWebServerOptions`
621
+
622
+ Options for building the server.
623
+
624
+ #### Returns
625
+
626
+ `Promise`\<`void`\>
627
+
628
+ A promise that resolves when the server is fully built and ready to start.
629
+
630
+ #### Implementation of
631
+
632
+ `IWebServer.build`
633
+
634
+ ***
635
+
636
+ ### serverListen() {#serverlisten}
637
+
638
+ > `abstract` `protected` **serverListen**(`host`, `port`): `Promise`\<`void`\>
639
+
640
+ Start listening for connections on the transport.
641
+
642
+ #### Parameters
643
+
644
+ ##### host
645
+
646
+ `string`
647
+
648
+ The host to bind to.
649
+
650
+ ##### port
651
+
652
+ `number`
653
+
654
+ The port to bind to.
655
+
656
+ #### Returns
657
+
658
+ `Promise`\<`void`\>
659
+
660
+ A promise that resolves when the transport is listening.
661
+
662
+ ***
663
+
664
+ ### serverClose() {#serverclose}
665
+
666
+ > `abstract` `protected` **serverClose**(): `Promise`\<`void`\>
667
+
668
+ Close the transport and all of its connections.
669
+
670
+ #### Returns
671
+
672
+ `Promise`\<`void`\>
673
+
674
+ A promise that resolves when the transport has closed.
675
+
676
+ ***
677
+
678
+ ### serverAddresses() {#serveraddresses}
679
+
680
+ > `abstract` `protected` **serverAddresses**(): `object`[]
681
+
682
+ Get the addresses the transport is bound to.
683
+
684
+ #### Returns
685
+
686
+ `object`[]
687
+
688
+ The bound addresses.
689
+
690
+ ***
691
+
692
+ ### serverIsSecure() {#serverissecure}
693
+
694
+ > `abstract` `protected` **serverIsSecure**(): `boolean`
695
+
696
+ Whether the transport is serving over TLS.
697
+
698
+ #### Returns
699
+
700
+ `boolean`
701
+
702
+ True if the transport is secure.
703
+
704
+ ***
705
+
706
+ ### serverIsListening() {#serverislistening}
707
+
708
+ > `abstract` `protected` **serverIsListening**(): `boolean`
709
+
710
+ Whether the transport is currently listening for connections.
711
+
712
+ #### Returns
713
+
714
+ `boolean`
715
+
716
+ True if the transport is listening.
717
+
718
+ ***
719
+
720
+ ### serverHasRootRoute() {#serverhasrootroute}
721
+
722
+ > `abstract` `protected` **serverHasRootRoute**(): `boolean`
723
+
724
+ Whether a GET route is registered for the root path.
725
+
726
+ #### Returns
727
+
728
+ `boolean`
729
+
730
+ True if the root route is registered.
@@ -0,0 +1,12 @@
1
+ # @3sixty/api-core-server
2
+
3
+ ## Classes
4
+
5
+ - [BaseServer](classes/BaseServer.md)
6
+
7
+ ## Interfaces
8
+
9
+ - [IBaseServerConstructorOptions](interfaces/IBaseServerConstructorOptions.md)
10
+ - [IRestProcessorChains](interfaces/IRestProcessorChains.md)
11
+ - [IServerCorsOptions](interfaces/IServerCorsOptions.md)
12
+ - [ISocketProcessorChains](interfaces/ISocketProcessorChains.md)