@twin.org/api-server-fastify 0.0.1-next.24 → 0.0.1-next.26

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.
@@ -84,9 +84,6 @@ class FastifyWebServer {
84
84
  /**
85
85
  * Create a new instance of FastifyWebServer.
86
86
  * @param options The options for the server.
87
- * @param options.loggingConnectorType The type of the logging connector to use, if undefined, no logging will happen.
88
- * @param options.mimeTypeProcessors Additional MIME type processors.
89
- * @param options.config Additional configuration for the server.
90
87
  */
91
88
  constructor(options) {
92
89
  this._loggingConnector = core.Is.stringValue(options?.loggingConnectorType)
@@ -290,7 +287,7 @@ class FastifyWebServer {
290
287
  ts: Date.now(),
291
288
  source: this.CLASS_NAME,
292
289
  message: `${FastifyWebServer._CLASS_NAME_CAMEL_CASE}.socketRouteAdded`,
293
- data: { route: path }
290
+ data: { route: `/${path}` }
294
291
  });
295
292
  const socketNamespace = io.of(`/${pathParts[0]}`);
296
293
  const topic = pathParts.slice(1).join("/");
@@ -448,12 +445,26 @@ class FastifyWebServer {
448
445
  // Custom emit method which will also call the post processors
449
446
  const postProcessEmit = async (topic, response, responseProcessorState) => {
450
447
  await responseEmitter(topic, response);
451
- // The post processors are called after the response has been emitted
452
- for (const postSocketRouteProcessor of socketRouteProcessors) {
453
- if (core.Is.function(postSocketRouteProcessor.post)) {
454
- await postSocketRouteProcessor.post(httpServerRequest, response, socketRoute, httpRequestIdentity, responseProcessorState);
448
+ try {
449
+ // The post processors are called after the response has been emitted
450
+ for (const postSocketRouteProcessor of socketRouteProcessors) {
451
+ if (core.Is.function(postSocketRouteProcessor.post)) {
452
+ await postSocketRouteProcessor.post(httpServerRequest, response, socketRoute, httpRequestIdentity, responseProcessorState);
453
+ }
455
454
  }
456
455
  }
456
+ catch (err) {
457
+ this._loggingConnector?.log({
458
+ level: "error",
459
+ ts: Date.now(),
460
+ source: this.CLASS_NAME,
461
+ message: `${FastifyWebServer._CLASS_NAME_CAMEL_CASE}.postProcessorError`,
462
+ error: core.BaseError.fromError(err),
463
+ data: {
464
+ route: socketRoute.path
465
+ }
466
+ });
467
+ }
457
468
  };
458
469
  try {
459
470
  for (const socketRouteProcessor of socketRouteProcessors) {
@@ -82,9 +82,6 @@ class FastifyWebServer {
82
82
  /**
83
83
  * Create a new instance of FastifyWebServer.
84
84
  * @param options The options for the server.
85
- * @param options.loggingConnectorType The type of the logging connector to use, if undefined, no logging will happen.
86
- * @param options.mimeTypeProcessors Additional MIME type processors.
87
- * @param options.config Additional configuration for the server.
88
85
  */
89
86
  constructor(options) {
90
87
  this._loggingConnector = Is.stringValue(options?.loggingConnectorType)
@@ -288,7 +285,7 @@ class FastifyWebServer {
288
285
  ts: Date.now(),
289
286
  source: this.CLASS_NAME,
290
287
  message: `${FastifyWebServer._CLASS_NAME_CAMEL_CASE}.socketRouteAdded`,
291
- data: { route: path }
288
+ data: { route: `/${path}` }
292
289
  });
293
290
  const socketNamespace = io.of(`/${pathParts[0]}`);
294
291
  const topic = pathParts.slice(1).join("/");
@@ -446,12 +443,26 @@ class FastifyWebServer {
446
443
  // Custom emit method which will also call the post processors
447
444
  const postProcessEmit = async (topic, response, responseProcessorState) => {
448
445
  await responseEmitter(topic, response);
449
- // The post processors are called after the response has been emitted
450
- for (const postSocketRouteProcessor of socketRouteProcessors) {
451
- if (Is.function(postSocketRouteProcessor.post)) {
452
- await postSocketRouteProcessor.post(httpServerRequest, response, socketRoute, httpRequestIdentity, responseProcessorState);
446
+ try {
447
+ // The post processors are called after the response has been emitted
448
+ for (const postSocketRouteProcessor of socketRouteProcessors) {
449
+ if (Is.function(postSocketRouteProcessor.post)) {
450
+ await postSocketRouteProcessor.post(httpServerRequest, response, socketRoute, httpRequestIdentity, responseProcessorState);
451
+ }
453
452
  }
454
453
  }
454
+ catch (err) {
455
+ this._loggingConnector?.log({
456
+ level: "error",
457
+ ts: Date.now(),
458
+ source: this.CLASS_NAME,
459
+ message: `${FastifyWebServer._CLASS_NAME_CAMEL_CASE}.postProcessorError`,
460
+ error: BaseError.fromError(err),
461
+ data: {
462
+ route: socketRoute.path
463
+ }
464
+ });
465
+ }
455
466
  };
456
467
  try {
457
468
  for (const socketRouteProcessor of socketRouteProcessors) {
@@ -1,6 +1,6 @@
1
- import { type IMimeTypeProcessor, type IRestRoute, type IRestRouteProcessor, type ISocketRoute, type ISocketRouteProcessor, type IWebServer, type IWebServerOptions } from "@twin.org/api-models";
1
+ import { type IRestRoute, type IRestRouteProcessor, type ISocketRoute, type ISocketRouteProcessor, type IWebServer, type IWebServerOptions } from "@twin.org/api-models";
2
2
  import { type FastifyInstance } from "fastify";
3
- import type { IFastifyWebServerConfig } from "./models/IFastifyWebServerConfig";
3
+ import type { IFastifyWebServerConstructorOptions } from "./models/IFastifyWebServerConstructorOptions";
4
4
  /**
5
5
  * Implementation of the web server using Fastify.
6
6
  */
@@ -12,15 +12,8 @@ export declare class FastifyWebServer implements IWebServer<FastifyInstance> {
12
12
  /**
13
13
  * Create a new instance of FastifyWebServer.
14
14
  * @param options The options for the server.
15
- * @param options.loggingConnectorType The type of the logging connector to use, if undefined, no logging will happen.
16
- * @param options.mimeTypeProcessors Additional MIME type processors.
17
- * @param options.config Additional configuration for the server.
18
15
  */
19
- constructor(options?: {
20
- loggingConnectorType?: string;
21
- config?: IFastifyWebServerConfig;
22
- mimeTypeProcessors?: IMimeTypeProcessor[];
23
- });
16
+ constructor(options?: IFastifyWebServerConstructorOptions);
24
17
  /**
25
18
  * Get the web server instance.
26
19
  * @returns The web server instance.
@@ -1,2 +1,3 @@
1
1
  export * from "./fastifyWebServer";
2
2
  export * from "./models/IFastifyWebServerConfig";
3
+ export * from "./models/IFastifyWebServerConstructorOptions";
@@ -0,0 +1,19 @@
1
+ import type { IMimeTypeProcessor } from "@twin.org/api-models";
2
+ import type { IFastifyWebServerConfig } from "./IFastifyWebServerConfig";
3
+ /**
4
+ * The options for the Fastify web server constructor.
5
+ */
6
+ export interface IFastifyWebServerConstructorOptions {
7
+ /**
8
+ * The type of the logging connector to use, if undefined, no logging will happen.
9
+ */
10
+ loggingConnectorType?: string;
11
+ /**
12
+ * Additional configuration for the server.
13
+ */
14
+ config?: IFastifyWebServerConfig;
15
+ /**
16
+ * Additional MIME type processors.
17
+ */
18
+ mimeTypeProcessors?: IMimeTypeProcessor[];
19
+ }
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/api-server-fastify - Changelog
2
2
 
3
- ## v0.0.1-next.24
3
+ ## v0.0.1-next.26
4
4
 
5
5
  - Initial Release
@@ -16,21 +16,11 @@ Create a new instance of FastifyWebServer.
16
16
 
17
17
  #### Parameters
18
18
 
19
- **options?**
19
+ ##### options?
20
20
 
21
- The options for the server.
22
-
23
- • **options.loggingConnectorType?**: `string`
24
-
25
- The type of the logging connector to use, if undefined, no logging will happen.
26
-
27
- • **options.config?**: [`IFastifyWebServerConfig`](../interfaces/IFastifyWebServerConfig.md)
28
-
29
- Additional configuration for the server.
30
-
31
- • **options.mimeTypeProcessors?**: `IMimeTypeProcessor`[]
21
+ [`IFastifyWebServerConstructorOptions`](../interfaces/IFastifyWebServerConstructorOptions.md)
32
22
 
33
- Additional MIME type processors.
23
+ The options for the server.
34
24
 
35
25
  #### Returns
36
26
 
@@ -48,13 +38,13 @@ Runtime name for the class.
48
38
 
49
39
  ### getInstance()
50
40
 
51
- > **getInstance**(): `FastifyInstance`\<`RawServerDefault`, `IncomingMessage`, `ServerResponse`\<`IncomingMessage`\>, `FastifyBaseLogger`, `FastifyTypeProviderDefault`\>
41
+ > **getInstance**(): `FastifyInstance`\<`IncomingMessage`, `ServerResponse`\>
52
42
 
53
43
  Get the web server instance.
54
44
 
55
45
  #### Returns
56
46
 
57
- `FastifyInstance`\<`RawServerDefault`, `IncomingMessage`, `ServerResponse`\<`IncomingMessage`\>, `FastifyBaseLogger`, `FastifyTypeProviderDefault`\>
47
+ `FastifyInstance`\<`IncomingMessage`, `ServerResponse`\>
58
48
 
59
49
  The web server instance.
60
50
 
@@ -72,23 +62,33 @@ Build the server.
72
62
 
73
63
  #### Parameters
74
64
 
75
- **restRouteProcessors?**: `IRestRouteProcessor`[]
65
+ ##### restRouteProcessors?
66
+
67
+ `IRestRouteProcessor`[]
76
68
 
77
69
  The processors for incoming requests over REST.
78
70
 
79
- **restRoutes?**: `IRestRoute`\<`any`, `any`\>[]
71
+ ##### restRoutes?
72
+
73
+ `IRestRoute`[]
80
74
 
81
75
  The REST routes.
82
76
 
83
- **socketRouteProcessors?**: `ISocketRouteProcessor`[]
77
+ ##### socketRouteProcessors?
78
+
79
+ `ISocketRouteProcessor`[]
84
80
 
85
81
  The processors for incoming requests over Sockets.
86
82
 
87
- **socketRoutes?**: `ISocketRoute`\<`any`, `any`\>[]
83
+ ##### socketRoutes?
84
+
85
+ `ISocketRoute`[]
88
86
 
89
87
  The socket routes.
90
88
 
91
- **options?**: `IWebServerOptions`
89
+ ##### options?
90
+
91
+ `IWebServerOptions`
92
92
 
93
93
  Options for building the server.
94
94
 
@@ -7,3 +7,4 @@
7
7
  ## Interfaces
8
8
 
9
9
  - [IFastifyWebServerConfig](interfaces/IFastifyWebServerConfig.md)
10
+ - [IFastifyWebServerConstructorOptions](interfaces/IFastifyWebServerConstructorOptions.md)
@@ -0,0 +1,27 @@
1
+ # Interface: IFastifyWebServerConstructorOptions
2
+
3
+ The options for the Fastify web server constructor.
4
+
5
+ ## Properties
6
+
7
+ ### loggingConnectorType?
8
+
9
+ > `optional` **loggingConnectorType**: `string`
10
+
11
+ The type of the logging connector to use, if undefined, no logging will happen.
12
+
13
+ ***
14
+
15
+ ### config?
16
+
17
+ > `optional` **config**: [`IFastifyWebServerConfig`](IFastifyWebServerConfig.md)
18
+
19
+ Additional configuration for the server.
20
+
21
+ ***
22
+
23
+ ### mimeTypeProcessors?
24
+
25
+ > `optional` **mimeTypeProcessors**: `IMimeTypeProcessor`[]
26
+
27
+ Additional MIME type processors.
package/locales/en.json CHANGED
@@ -9,6 +9,7 @@
9
9
  "restRouteAdded": "Added REST route \"{route}\" \"{method}\"",
10
10
  "socketRouteAdded": "Added socket route \"{route}\"",
11
11
  "noRestProcessors": "You must configure at least one REST processor",
12
- "noSocketProcessors": "You must configure at least one socket processor"
12
+ "noSocketProcessors": "You must configure at least one socket processor",
13
+ "postProcessorError": "There was a failure after in a post processor for route \"{route}\""
13
14
  }
14
15
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/api-server-fastify",
3
- "version": "0.0.1-next.24",
3
+ "version": "0.0.1-next.26",
4
4
  "description": "Use Fastify as the core web server for APIs",
5
5
  "repository": {
6
6
  "type": "git",
@@ -15,14 +15,14 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "@fastify/compress": "8.0.1",
18
- "@fastify/cors": "10.0.1",
19
- "@twin.org/api-core": "0.0.1-next.24",
20
- "@twin.org/api-models": "0.0.1-next.24",
18
+ "@fastify/cors": "10.0.2",
19
+ "@twin.org/api-core": "0.0.1-next.26",
20
+ "@twin.org/api-models": "0.0.1-next.26",
21
21
  "@twin.org/core": "next",
22
22
  "@twin.org/logging-models": "next",
23
23
  "@twin.org/nameof": "next",
24
24
  "@twin.org/web": "next",
25
- "fastify": "5.1.0",
25
+ "fastify": "5.2.1",
26
26
  "socket.io": "4.8.1"
27
27
  },
28
28
  "main": "./dist/cjs/index.cjs",