@twin.org/api-server-fastify 0.0.2-next.3 → 0.0.2-next.5

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.
@@ -5,7 +5,6 @@ var FastifyCors = require('@fastify/cors');
5
5
  var apiModels = require('@twin.org/api-models');
6
6
  var apiProcessors = require('@twin.org/api-processors');
7
7
  var core = require('@twin.org/core');
8
- var loggingModels = require('@twin.org/logging-models');
9
8
  var web = require('@twin.org/web');
10
9
  var Fastify = require('fastify');
11
10
  var fp = require('fastify-plugin');
@@ -48,10 +47,15 @@ class FastifyWebServer {
48
47
  */
49
48
  CLASS_NAME = "FastifyWebServer";
50
49
  /**
51
- * The logging connector.
50
+ * The logging component type.
52
51
  * @internal
53
52
  */
54
- _loggingConnector;
53
+ _loggingComponentType;
54
+ /**
55
+ * The logging component.
56
+ * @internal
57
+ */
58
+ _loggingComponent;
55
59
  /**
56
60
  * The options for the server.
57
61
  * @internal
@@ -87,8 +91,9 @@ class FastifyWebServer {
87
91
  * @param options The options for the server.
88
92
  */
89
93
  constructor(options) {
90
- this._loggingConnector = core.Is.stringValue(options?.loggingConnectorType)
91
- ? loggingModels.LoggingConnectorFactory.get(options.loggingConnectorType)
94
+ this._loggingComponentType = options?.loggingComponentType;
95
+ this._loggingComponent = core.Is.stringValue(options?.loggingComponentType)
96
+ ? core.ComponentFactory.get(options.loggingComponentType)
92
97
  : undefined;
93
98
  this._fastify = Fastify({
94
99
  maxParamLength: 2000,
@@ -129,7 +134,7 @@ class FastifyWebServer {
129
134
  if (core.Is.arrayValue(socketRoutes) && !core.Is.arrayValue(socketRouteProcessors)) {
130
135
  throw new core.GeneralError(this.CLASS_NAME, "noSocketProcessors");
131
136
  }
132
- await this._loggingConnector?.log({
137
+ await this._loggingComponent?.log({
133
138
  level: "info",
134
139
  ts: Date.now(),
135
140
  source: this.CLASS_NAME,
@@ -174,7 +179,7 @@ class FastifyWebServer {
174
179
  err = errorAndCode.error;
175
180
  httpStatusCode = errorAndCode.httpStatusCode;
176
181
  }
177
- await this._loggingConnector?.log({
182
+ await this._loggingComponent?.log({
178
183
  level: "error",
179
184
  ts: Date.now(),
180
185
  source: this.CLASS_NAME,
@@ -195,7 +200,7 @@ class FastifyWebServer {
195
200
  async start() {
196
201
  const host = this._options?.host ?? FastifyWebServer._DEFAULT_HOST;
197
202
  const port = this._options?.port ?? FastifyWebServer._DEFAULT_PORT;
198
- await this._loggingConnector?.log({
203
+ await this._loggingComponent?.log({
199
204
  level: "info",
200
205
  ts: Date.now(),
201
206
  source: this.CLASS_NAME,
@@ -210,7 +215,7 @@ class FastifyWebServer {
210
215
  await this._fastify.listen({ port, host });
211
216
  const addresses = this._fastify.addresses();
212
217
  const protocol = core.Is.object(this._fastify.initialConfig.https) ? "https://" : "http://";
213
- await this._loggingConnector?.log({
218
+ await this._loggingComponent?.log({
214
219
  level: "info",
215
220
  ts: Date.now(),
216
221
  source: this.CLASS_NAME,
@@ -224,7 +229,7 @@ class FastifyWebServer {
224
229
  this._started = true;
225
230
  }
226
231
  catch (err) {
227
- await this._loggingConnector?.log({
232
+ await this._loggingComponent?.log({
228
233
  level: "error",
229
234
  ts: Date.now(),
230
235
  source: this.CLASS_NAME,
@@ -242,7 +247,7 @@ class FastifyWebServer {
242
247
  if (this._started) {
243
248
  this._started = false;
244
249
  await this._fastify.close();
245
- await this._loggingConnector?.log({
250
+ await this._loggingComponent?.log({
246
251
  level: "info",
247
252
  ts: Date.now(),
248
253
  source: this.CLASS_NAME,
@@ -263,7 +268,7 @@ class FastifyWebServer {
263
268
  if (!path.startsWith("/")) {
264
269
  path = `/${path}`;
265
270
  }
266
- await this._loggingConnector?.log({
271
+ await this._loggingComponent?.log({
267
272
  level: "info",
268
273
  ts: Date.now(),
269
274
  source: this.CLASS_NAME,
@@ -290,7 +295,7 @@ class FastifyWebServer {
290
295
  const pathParts = path.split("/");
291
296
  const namespace = `/${pathParts[0]}`;
292
297
  const topic = pathParts.slice(1).join("/");
293
- await this._loggingConnector?.log({
298
+ await this._loggingComponent?.log({
294
299
  level: "info",
295
300
  ts: Date.now(),
296
301
  source: this.CLASS_NAME,
@@ -314,7 +319,7 @@ class FastifyWebServer {
314
319
  try {
315
320
  for (const socketRouteProcessor of socketRouteProcessors) {
316
321
  if (socketRouteProcessor.connected) {
317
- await socketRouteProcessor.connected(socketServerRequest, socketRoute);
322
+ await socketRouteProcessor.connected(socketServerRequest, socketRoute, this._loggingComponentType);
318
323
  }
319
324
  }
320
325
  }
@@ -329,7 +334,7 @@ class FastifyWebServer {
329
334
  // The socket disconnected so notify any processors
330
335
  for (const socketRouteProcessor of socketRouteProcessors) {
331
336
  if (socketRouteProcessor.disconnected) {
332
- await socketRouteProcessor.disconnected(socketServerRequest, socketRoute);
337
+ await socketRouteProcessor.disconnected(socketServerRequest, socketRoute, this._loggingComponentType);
333
338
  }
334
339
  }
335
340
  }
@@ -388,17 +393,17 @@ class FastifyWebServer {
388
393
  try {
389
394
  for (const routeProcessor of restRouteProcessors) {
390
395
  if (routeProcessor.pre) {
391
- await routeProcessor.pre(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
396
+ await routeProcessor.pre(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState, this._loggingComponentType);
392
397
  }
393
398
  }
394
399
  for (const routeProcessor of restRouteProcessors) {
395
400
  if (routeProcessor.process) {
396
- await routeProcessor.process(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
401
+ await routeProcessor.process(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState, this._loggingComponentType);
397
402
  }
398
403
  }
399
404
  for (const routeProcessor of restRouteProcessors) {
400
405
  if (routeProcessor.post) {
401
- await routeProcessor.post(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
406
+ await routeProcessor.post(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState, this._loggingComponentType);
402
407
  }
403
408
  }
404
409
  }
@@ -455,12 +460,12 @@ class FastifyWebServer {
455
460
  // The post processors are called after the response has been emitted
456
461
  for (const postSocketRouteProcessor of socketRouteProcessors) {
457
462
  if (postSocketRouteProcessor.post) {
458
- await postSocketRouteProcessor.post(socketServerRequest, response, socketRoute, httpRequestIdentity, responseProcessorState);
463
+ await postSocketRouteProcessor.post(socketServerRequest, response, socketRoute, httpRequestIdentity, responseProcessorState, this._loggingComponentType);
459
464
  }
460
465
  }
461
466
  }
462
467
  catch (err) {
463
- this._loggingConnector?.log({
468
+ this._loggingComponent?.log({
464
469
  level: "error",
465
470
  ts: Date.now(),
466
471
  source: this.CLASS_NAME,
@@ -475,7 +480,7 @@ class FastifyWebServer {
475
480
  try {
476
481
  for (const socketRouteProcessor of socketRouteProcessors) {
477
482
  if (socketRouteProcessor.pre) {
478
- await socketRouteProcessor.pre(socketServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState);
483
+ await socketRouteProcessor.pre(socketServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState, this._loggingComponentType);
479
484
  }
480
485
  }
481
486
  // We always call all the processors regardless of any response set by a previous processor.
@@ -488,7 +493,7 @@ class FastifyWebServer {
488
493
  if (socketRouteProcessor.process) {
489
494
  await socketRouteProcessor.process(socketServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState, async (topic, processResponse) => {
490
495
  await postProcessEmit(topic, processResponse, processorState);
491
- });
496
+ }, this._loggingComponentType);
492
497
  }
493
498
  }
494
499
  // If the processors set the status to any kind of error then we should emit this manually
@@ -2,8 +2,7 @@ import FastifyCompress from '@fastify/compress';
2
2
  import FastifyCors from '@fastify/cors';
3
3
  import { HttpErrorHelper } from '@twin.org/api-models';
4
4
  import { JsonLdMimeTypeProcessor } from '@twin.org/api-processors';
5
- import { StringHelper, Is, GeneralError, BaseError } from '@twin.org/core';
6
- import { LoggingConnectorFactory } from '@twin.org/logging-models';
5
+ import { StringHelper, ComponentFactory, Is, GeneralError, BaseError } from '@twin.org/core';
7
6
  import { HttpStatusCode, HttpMethod, HeaderTypes } from '@twin.org/web';
8
7
  import Fastify from 'fastify';
9
8
  import fp from 'fastify-plugin';
@@ -46,10 +45,15 @@ class FastifyWebServer {
46
45
  */
47
46
  CLASS_NAME = "FastifyWebServer";
48
47
  /**
49
- * The logging connector.
48
+ * The logging component type.
50
49
  * @internal
51
50
  */
52
- _loggingConnector;
51
+ _loggingComponentType;
52
+ /**
53
+ * The logging component.
54
+ * @internal
55
+ */
56
+ _loggingComponent;
53
57
  /**
54
58
  * The options for the server.
55
59
  * @internal
@@ -85,8 +89,9 @@ class FastifyWebServer {
85
89
  * @param options The options for the server.
86
90
  */
87
91
  constructor(options) {
88
- this._loggingConnector = Is.stringValue(options?.loggingConnectorType)
89
- ? LoggingConnectorFactory.get(options.loggingConnectorType)
92
+ this._loggingComponentType = options?.loggingComponentType;
93
+ this._loggingComponent = Is.stringValue(options?.loggingComponentType)
94
+ ? ComponentFactory.get(options.loggingComponentType)
90
95
  : undefined;
91
96
  this._fastify = Fastify({
92
97
  maxParamLength: 2000,
@@ -127,7 +132,7 @@ class FastifyWebServer {
127
132
  if (Is.arrayValue(socketRoutes) && !Is.arrayValue(socketRouteProcessors)) {
128
133
  throw new GeneralError(this.CLASS_NAME, "noSocketProcessors");
129
134
  }
130
- await this._loggingConnector?.log({
135
+ await this._loggingComponent?.log({
131
136
  level: "info",
132
137
  ts: Date.now(),
133
138
  source: this.CLASS_NAME,
@@ -172,7 +177,7 @@ class FastifyWebServer {
172
177
  err = errorAndCode.error;
173
178
  httpStatusCode = errorAndCode.httpStatusCode;
174
179
  }
175
- await this._loggingConnector?.log({
180
+ await this._loggingComponent?.log({
176
181
  level: "error",
177
182
  ts: Date.now(),
178
183
  source: this.CLASS_NAME,
@@ -193,7 +198,7 @@ class FastifyWebServer {
193
198
  async start() {
194
199
  const host = this._options?.host ?? FastifyWebServer._DEFAULT_HOST;
195
200
  const port = this._options?.port ?? FastifyWebServer._DEFAULT_PORT;
196
- await this._loggingConnector?.log({
201
+ await this._loggingComponent?.log({
197
202
  level: "info",
198
203
  ts: Date.now(),
199
204
  source: this.CLASS_NAME,
@@ -208,7 +213,7 @@ class FastifyWebServer {
208
213
  await this._fastify.listen({ port, host });
209
214
  const addresses = this._fastify.addresses();
210
215
  const protocol = Is.object(this._fastify.initialConfig.https) ? "https://" : "http://";
211
- await this._loggingConnector?.log({
216
+ await this._loggingComponent?.log({
212
217
  level: "info",
213
218
  ts: Date.now(),
214
219
  source: this.CLASS_NAME,
@@ -222,7 +227,7 @@ class FastifyWebServer {
222
227
  this._started = true;
223
228
  }
224
229
  catch (err) {
225
- await this._loggingConnector?.log({
230
+ await this._loggingComponent?.log({
226
231
  level: "error",
227
232
  ts: Date.now(),
228
233
  source: this.CLASS_NAME,
@@ -240,7 +245,7 @@ class FastifyWebServer {
240
245
  if (this._started) {
241
246
  this._started = false;
242
247
  await this._fastify.close();
243
- await this._loggingConnector?.log({
248
+ await this._loggingComponent?.log({
244
249
  level: "info",
245
250
  ts: Date.now(),
246
251
  source: this.CLASS_NAME,
@@ -261,7 +266,7 @@ class FastifyWebServer {
261
266
  if (!path.startsWith("/")) {
262
267
  path = `/${path}`;
263
268
  }
264
- await this._loggingConnector?.log({
269
+ await this._loggingComponent?.log({
265
270
  level: "info",
266
271
  ts: Date.now(),
267
272
  source: this.CLASS_NAME,
@@ -288,7 +293,7 @@ class FastifyWebServer {
288
293
  const pathParts = path.split("/");
289
294
  const namespace = `/${pathParts[0]}`;
290
295
  const topic = pathParts.slice(1).join("/");
291
- await this._loggingConnector?.log({
296
+ await this._loggingComponent?.log({
292
297
  level: "info",
293
298
  ts: Date.now(),
294
299
  source: this.CLASS_NAME,
@@ -312,7 +317,7 @@ class FastifyWebServer {
312
317
  try {
313
318
  for (const socketRouteProcessor of socketRouteProcessors) {
314
319
  if (socketRouteProcessor.connected) {
315
- await socketRouteProcessor.connected(socketServerRequest, socketRoute);
320
+ await socketRouteProcessor.connected(socketServerRequest, socketRoute, this._loggingComponentType);
316
321
  }
317
322
  }
318
323
  }
@@ -327,7 +332,7 @@ class FastifyWebServer {
327
332
  // The socket disconnected so notify any processors
328
333
  for (const socketRouteProcessor of socketRouteProcessors) {
329
334
  if (socketRouteProcessor.disconnected) {
330
- await socketRouteProcessor.disconnected(socketServerRequest, socketRoute);
335
+ await socketRouteProcessor.disconnected(socketServerRequest, socketRoute, this._loggingComponentType);
331
336
  }
332
337
  }
333
338
  }
@@ -386,17 +391,17 @@ class FastifyWebServer {
386
391
  try {
387
392
  for (const routeProcessor of restRouteProcessors) {
388
393
  if (routeProcessor.pre) {
389
- await routeProcessor.pre(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
394
+ await routeProcessor.pre(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState, this._loggingComponentType);
390
395
  }
391
396
  }
392
397
  for (const routeProcessor of restRouteProcessors) {
393
398
  if (routeProcessor.process) {
394
- await routeProcessor.process(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
399
+ await routeProcessor.process(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState, this._loggingComponentType);
395
400
  }
396
401
  }
397
402
  for (const routeProcessor of restRouteProcessors) {
398
403
  if (routeProcessor.post) {
399
- await routeProcessor.post(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
404
+ await routeProcessor.post(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState, this._loggingComponentType);
400
405
  }
401
406
  }
402
407
  }
@@ -453,12 +458,12 @@ class FastifyWebServer {
453
458
  // The post processors are called after the response has been emitted
454
459
  for (const postSocketRouteProcessor of socketRouteProcessors) {
455
460
  if (postSocketRouteProcessor.post) {
456
- await postSocketRouteProcessor.post(socketServerRequest, response, socketRoute, httpRequestIdentity, responseProcessorState);
461
+ await postSocketRouteProcessor.post(socketServerRequest, response, socketRoute, httpRequestIdentity, responseProcessorState, this._loggingComponentType);
457
462
  }
458
463
  }
459
464
  }
460
465
  catch (err) {
461
- this._loggingConnector?.log({
466
+ this._loggingComponent?.log({
462
467
  level: "error",
463
468
  ts: Date.now(),
464
469
  source: this.CLASS_NAME,
@@ -473,7 +478,7 @@ class FastifyWebServer {
473
478
  try {
474
479
  for (const socketRouteProcessor of socketRouteProcessors) {
475
480
  if (socketRouteProcessor.pre) {
476
- await socketRouteProcessor.pre(socketServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState);
481
+ await socketRouteProcessor.pre(socketServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState, this._loggingComponentType);
477
482
  }
478
483
  }
479
484
  // We always call all the processors regardless of any response set by a previous processor.
@@ -486,7 +491,7 @@ class FastifyWebServer {
486
491
  if (socketRouteProcessor.process) {
487
492
  await socketRouteProcessor.process(socketServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState, async (topic, processResponse) => {
488
493
  await postProcessEmit(topic, processResponse, processorState);
489
- });
494
+ }, this._loggingComponentType);
490
495
  }
491
496
  }
492
497
  // If the processors set the status to any kind of error then we should emit this manually
@@ -5,9 +5,9 @@ import type { IFastifyWebServerConfig } from "./IFastifyWebServerConfig";
5
5
  */
6
6
  export interface IFastifyWebServerConstructorOptions {
7
7
  /**
8
- * The type of the logging connector to use, if undefined, no logging will happen.
8
+ * The type of the logging component to use, if undefined, no logging will happen.
9
9
  */
10
- loggingConnectorType?: string;
10
+ loggingComponentType?: string;
11
11
  /**
12
12
  * Additional configuration for the server.
13
13
  */
package/docs/changelog.md CHANGED
@@ -1,5 +1,42 @@
1
1
  # @twin.org/api-server-fastify - Changelog
2
2
 
3
+ ## [0.0.2-next.5](https://github.com/twinfoundation/api/compare/api-server-fastify-v0.0.2-next.4...api-server-fastify-v0.0.2-next.5) (2025-07-25)
4
+
5
+
6
+ ### Features
7
+
8
+ * add json-ld mime type processor and auth admin component ([8861791](https://github.com/twinfoundation/api/commit/88617916e23bfbca023dbae1976fe421983a02ff))
9
+ * add logging component type to request contexts ([210de1b](https://github.com/twinfoundation/api/commit/210de1b9e1c91079b59a2b90ddd57569668d647d))
10
+ * add socket id, connect and disconnect ([20b0d0e](https://github.com/twinfoundation/api/commit/20b0d0ec279cab46141fee09de2c4a7087cdce16))
11
+ * improve socket route logging ([b8d9519](https://github.com/twinfoundation/api/commit/b8d95199f838ac6ba9f45c30ef7c4e613201ff53))
12
+ * update dependencies ([1171dc4](https://github.com/twinfoundation/api/commit/1171dc416a9481737f6a640e3cf30145768f37e9))
13
+ * use shared store mechanism ([#19](https://github.com/twinfoundation/api/issues/19)) ([32116df](https://github.com/twinfoundation/api/commit/32116df3b4380a30137f5056f242a5c99afa2df9))
14
+
15
+
16
+ ### Dependencies
17
+
18
+ * The following workspace dependencies were updated
19
+ * dependencies
20
+ * @twin.org/api-core bumped from 0.0.2-next.4 to 0.0.2-next.5
21
+ * @twin.org/api-models bumped from 0.0.2-next.4 to 0.0.2-next.5
22
+ * @twin.org/api-processors bumped from 0.0.2-next.4 to 0.0.2-next.5
23
+
24
+ ## [0.0.2-next.4](https://github.com/twinfoundation/api/compare/api-server-fastify-v0.0.2-next.3...api-server-fastify-v0.0.2-next.4) (2025-07-25)
25
+
26
+
27
+ ### Features
28
+
29
+ * add logging component type to request contexts ([210de1b](https://github.com/twinfoundation/api/commit/210de1b9e1c91079b59a2b90ddd57569668d647d))
30
+
31
+
32
+ ### Dependencies
33
+
34
+ * The following workspace dependencies were updated
35
+ * dependencies
36
+ * @twin.org/api-core bumped from 0.0.2-next.3 to 0.0.2-next.4
37
+ * @twin.org/api-models bumped from 0.0.2-next.3 to 0.0.2-next.4
38
+ * @twin.org/api-processors bumped from 0.0.2-next.3 to 0.0.2-next.4
39
+
3
40
  ## [0.0.2-next.3](https://github.com/twinfoundation/api/compare/api-server-fastify-v0.0.2-next.2...api-server-fastify-v0.0.2-next.3) (2025-07-24)
4
41
 
5
42
 
@@ -4,11 +4,11 @@ The options for the Fastify web server constructor.
4
4
 
5
5
  ## Properties
6
6
 
7
- ### loggingConnectorType?
7
+ ### loggingComponentType?
8
8
 
9
- > `optional` **loggingConnectorType**: `string`
9
+ > `optional` **loggingComponentType**: `string`
10
10
 
11
- The type of the logging connector to use, if undefined, no logging will happen.
11
+ The type of the logging component to use, if undefined, no logging will happen.
12
12
 
13
13
  ***
14
14
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/api-server-fastify",
3
- "version": "0.0.2-next.3",
3
+ "version": "0.0.2-next.5",
4
4
  "description": "Use Fastify as the core web server for APIs",
5
5
  "repository": {
6
6
  "type": "git",
@@ -16,9 +16,9 @@
16
16
  "dependencies": {
17
17
  "@fastify/compress": "8.1.0",
18
18
  "@fastify/cors": "11.0.1",
19
- "@twin.org/api-core": "0.0.2-next.3",
20
- "@twin.org/api-models": "0.0.2-next.3",
21
- "@twin.org/api-processors": "0.0.2-next.3",
19
+ "@twin.org/api-core": "0.0.2-next.5",
20
+ "@twin.org/api-models": "0.0.2-next.5",
21
+ "@twin.org/api-processors": "0.0.2-next.5",
22
22
  "@twin.org/core": "next",
23
23
  "@twin.org/logging-models": "next",
24
24
  "@twin.org/nameof": "next",