@twin.org/api-server-fastify 0.0.1-next.14 → 0.0.1-next.16

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.
@@ -429,8 +429,8 @@ class FastifyWebServer {
429
429
  delete httpServerRequest.query?.EIO;
430
430
  delete httpServerRequest.query?.transport;
431
431
  httpServerRequest.body = data;
432
- await this.runProcessorsSocket(socketRouteProcessors, socketRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState, async () => {
433
- await socket.emit(emitTopic, httpResponse);
432
+ await this.runProcessorsSocket(socketRouteProcessors, socketRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState, emitTopic, async (topic, response) => {
433
+ await socket.emit(topic, response);
434
434
  });
435
435
  }
436
436
  /**
@@ -440,12 +440,14 @@ class FastifyWebServer {
440
440
  * @param httpServerRequest The incoming request.
441
441
  * @param httpResponse The outgoing response.
442
442
  * @param httpRequestIdentity The identity context for the request.
443
+ * @param processorState The state handed through the processors.
444
+ * @param requestTopic The topic of the request.
443
445
  * @internal
444
446
  */
445
- async runProcessorsSocket(socketRouteProcessors, socketRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState, responseEmitter) {
447
+ async runProcessorsSocket(socketRouteProcessors, socketRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState, requestTopic, responseEmitter) {
446
448
  // Custom emit method which will also call the post processors
447
- const postProcessEmit = async (response, responseProcessorState) => {
448
- await responseEmitter(response);
449
+ const postProcessEmit = async (topic, response, responseProcessorState) => {
450
+ await responseEmitter(topic, response);
449
451
  // The post processors are called after the response has been emitted
450
452
  for (const postSocketRouteProcessor of socketRouteProcessors) {
451
453
  if (core.Is.function(postSocketRouteProcessor.post)) {
@@ -463,26 +465,26 @@ class FastifyWebServer {
463
465
  // But if a pre processor sets a status code, we will emit the response manually, as the pre
464
466
  // and post processors do not receive the emit method, they just populate the response object.
465
467
  if (!core.Is.empty(httpResponse.statusCode)) {
466
- await postProcessEmit(httpResponse, processorState);
468
+ await postProcessEmit(requestTopic, httpResponse, processorState);
467
469
  }
468
470
  for (const socketRouteProcessor of socketRouteProcessors) {
469
471
  if (core.Is.function(socketRouteProcessor.process)) {
470
- await socketRouteProcessor.process(httpServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState, async (processResponse) => {
471
- await postProcessEmit(processResponse, processorState);
472
+ await socketRouteProcessor.process(httpServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState, async (topic, processResponse) => {
473
+ await postProcessEmit(topic, processResponse, processorState);
472
474
  });
473
475
  }
474
476
  }
475
477
  // If the processors set the status to any kind of error then we should emit this manually
476
478
  if (core.Is.integer(httpResponse.statusCode) &&
477
479
  httpResponse.statusCode >= web.HttpStatusCode.badRequest) {
478
- await postProcessEmit(httpResponse, processorState);
480
+ await postProcessEmit(requestTopic, httpResponse, processorState);
479
481
  }
480
482
  }
481
483
  catch (err) {
482
484
  // Emit any unhandled errors manually
483
485
  const { error, httpStatusCode } = apiModels.HttpErrorHelper.processError(err, this._includeErrorStack);
484
486
  apiModels.HttpErrorHelper.buildResponse(httpResponse, error, httpStatusCode);
485
- await postProcessEmit(httpResponse, processorState);
487
+ await postProcessEmit(requestTopic, httpResponse, processorState);
486
488
  }
487
489
  }
488
490
  /**
@@ -427,8 +427,8 @@ class FastifyWebServer {
427
427
  delete httpServerRequest.query?.EIO;
428
428
  delete httpServerRequest.query?.transport;
429
429
  httpServerRequest.body = data;
430
- await this.runProcessorsSocket(socketRouteProcessors, socketRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState, async () => {
431
- await socket.emit(emitTopic, httpResponse);
430
+ await this.runProcessorsSocket(socketRouteProcessors, socketRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState, emitTopic, async (topic, response) => {
431
+ await socket.emit(topic, response);
432
432
  });
433
433
  }
434
434
  /**
@@ -438,12 +438,14 @@ class FastifyWebServer {
438
438
  * @param httpServerRequest The incoming request.
439
439
  * @param httpResponse The outgoing response.
440
440
  * @param httpRequestIdentity The identity context for the request.
441
+ * @param processorState The state handed through the processors.
442
+ * @param requestTopic The topic of the request.
441
443
  * @internal
442
444
  */
443
- async runProcessorsSocket(socketRouteProcessors, socketRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState, responseEmitter) {
445
+ async runProcessorsSocket(socketRouteProcessors, socketRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState, requestTopic, responseEmitter) {
444
446
  // Custom emit method which will also call the post processors
445
- const postProcessEmit = async (response, responseProcessorState) => {
446
- await responseEmitter(response);
447
+ const postProcessEmit = async (topic, response, responseProcessorState) => {
448
+ await responseEmitter(topic, response);
447
449
  // The post processors are called after the response has been emitted
448
450
  for (const postSocketRouteProcessor of socketRouteProcessors) {
449
451
  if (Is.function(postSocketRouteProcessor.post)) {
@@ -461,26 +463,26 @@ class FastifyWebServer {
461
463
  // But if a pre processor sets a status code, we will emit the response manually, as the pre
462
464
  // and post processors do not receive the emit method, they just populate the response object.
463
465
  if (!Is.empty(httpResponse.statusCode)) {
464
- await postProcessEmit(httpResponse, processorState);
466
+ await postProcessEmit(requestTopic, httpResponse, processorState);
465
467
  }
466
468
  for (const socketRouteProcessor of socketRouteProcessors) {
467
469
  if (Is.function(socketRouteProcessor.process)) {
468
- await socketRouteProcessor.process(httpServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState, async (processResponse) => {
469
- await postProcessEmit(processResponse, processorState);
470
+ await socketRouteProcessor.process(httpServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState, async (topic, processResponse) => {
471
+ await postProcessEmit(topic, processResponse, processorState);
470
472
  });
471
473
  }
472
474
  }
473
475
  // If the processors set the status to any kind of error then we should emit this manually
474
476
  if (Is.integer(httpResponse.statusCode) &&
475
477
  httpResponse.statusCode >= HttpStatusCode.badRequest) {
476
- await postProcessEmit(httpResponse, processorState);
478
+ await postProcessEmit(requestTopic, httpResponse, processorState);
477
479
  }
478
480
  }
479
481
  catch (err) {
480
482
  // Emit any unhandled errors manually
481
483
  const { error, httpStatusCode } = HttpErrorHelper.processError(err, this._includeErrorStack);
482
484
  HttpErrorHelper.buildResponse(httpResponse, error, httpStatusCode);
483
- await postProcessEmit(httpResponse, processorState);
485
+ await postProcessEmit(requestTopic, httpResponse, processorState);
484
486
  }
485
487
  }
486
488
  /**
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/api-server-fastify - Changelog
2
2
 
3
- ## v0.0.1-next.14
3
+ ## v0.0.1-next.16
4
4
 
5
5
  - Initial Release
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/api-server-fastify",
3
- "version": "0.0.1-next.14",
3
+ "version": "0.0.1-next.16",
4
4
  "description": "Use Fastify as the core web server for APIs",
5
5
  "repository": {
6
6
  "type": "git",
@@ -16,8 +16,8 @@
16
16
  "dependencies": {
17
17
  "@fastify/compress": "8.0.1",
18
18
  "@fastify/cors": "10.0.1",
19
- "@twin.org/api-core": "0.0.1-next.14",
20
- "@twin.org/api-models": "0.0.1-next.14",
19
+ "@twin.org/api-core": "0.0.1-next.16",
20
+ "@twin.org/api-models": "0.0.1-next.16",
21
21
  "@twin.org/core": "next",
22
22
  "@twin.org/logging-models": "next",
23
23
  "@twin.org/nameof": "next",