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

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.
@@ -303,20 +303,18 @@ class FastifyWebServer {
303
303
  });
304
304
  const socketNamespace = io.of(namespace);
305
305
  socketNamespace.on("connection", async (socket) => {
306
- const httpServerRequest = {
306
+ const socketServerRequest = {
307
307
  method: web.HttpMethod.GET,
308
308
  url: socket.handshake.url,
309
309
  query: socket.handshake.query,
310
- headers: socket.handshake.headers
310
+ headers: socket.handshake.headers,
311
+ socketId: socket.id
311
312
  };
312
313
  // Pass the connected information on to any processors
313
314
  try {
314
- const processorState = {
315
- socketId: socket.id
316
- };
317
315
  for (const socketRouteProcessor of socketRouteProcessors) {
318
- if (core.Is.function(socketRouteProcessor.connected)) {
319
- await socketRouteProcessor.connected(httpServerRequest, socketRoute, processorState);
316
+ if (socketRouteProcessor.connected) {
317
+ await socketRouteProcessor.connected(socketServerRequest, socketRoute);
320
318
  }
321
319
  }
322
320
  }
@@ -328,13 +326,10 @@ class FastifyWebServer {
328
326
  }
329
327
  socket.on("disconnect", async () => {
330
328
  try {
331
- const processorState = {
332
- socketId: socket.id
333
- };
334
329
  // The socket disconnected so notify any processors
335
330
  for (const socketRouteProcessor of socketRouteProcessors) {
336
- if (core.Is.function(socketRouteProcessor.disconnected)) {
337
- await socketRouteProcessor.disconnected(httpServerRequest, socketRoute, processorState);
331
+ if (socketRouteProcessor.disconnected) {
332
+ await socketRouteProcessor.disconnected(socketServerRequest, socketRoute);
338
333
  }
339
334
  }
340
335
  }
@@ -392,17 +387,17 @@ class FastifyWebServer {
392
387
  async runProcessorsRest(restRouteProcessors, restRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState) {
393
388
  try {
394
389
  for (const routeProcessor of restRouteProcessors) {
395
- if (core.Is.function(routeProcessor.pre)) {
390
+ if (routeProcessor.pre) {
396
391
  await routeProcessor.pre(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
397
392
  }
398
393
  }
399
394
  for (const routeProcessor of restRouteProcessors) {
400
- if (core.Is.function(routeProcessor.process)) {
395
+ if (routeProcessor.process) {
401
396
  await routeProcessor.process(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
402
397
  }
403
398
  }
404
399
  for (const routeProcessor of restRouteProcessors) {
405
- if (core.Is.function(routeProcessor.post)) {
400
+ if (routeProcessor.post) {
406
401
  await routeProcessor.post(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
407
402
  }
408
403
  }
@@ -423,44 +418,44 @@ class FastifyWebServer {
423
418
  * @internal
424
419
  */
425
420
  async handleRequestSocket(socketRouteProcessors, socketRoute, socket, fullPath, emitTopic, request) {
426
- const httpServerRequest = {
421
+ const socketServerRequest = {
427
422
  method: web.HttpMethod.GET,
428
423
  url: fullPath,
429
424
  query: socket.handshake.query,
430
425
  headers: socket.handshake.headers,
431
- body: request.body
426
+ body: request.body,
427
+ socketId: socket.id
432
428
  };
433
429
  const httpResponse = {};
434
430
  const httpRequestIdentity = {};
435
- const processorState = {
436
- socketId: socket.id
437
- };
438
- delete httpServerRequest.query?.EIO;
439
- delete httpServerRequest.query?.transport;
440
- await this.runProcessorsSocket(socketRouteProcessors, socketRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState, emitTopic, async (topic, response) => {
431
+ const processorState = {};
432
+ delete socketServerRequest.query?.EIO;
433
+ delete socketServerRequest.query?.transport;
434
+ await this.runProcessorsSocket(socketRouteProcessors, socketRoute, socketServerRequest, httpResponse, httpRequestIdentity, processorState, emitTopic, async (topic, response) => {
441
435
  await socket.emit(topic, response);
442
436
  });
443
437
  }
444
438
  /**
445
439
  * Run the socket processors for the route.
440
+ * @param socketId The id of the socket.
446
441
  * @param socketRouteProcessors The processors to run.
447
442
  * @param socketRoute The route to process.
448
- * @param httpServerRequest The incoming request.
443
+ * @param socketServerRequest The incoming request.
449
444
  * @param httpResponse The outgoing response.
450
445
  * @param httpRequestIdentity The identity context for the request.
451
446
  * @param processorState The state handed through the processors.
452
447
  * @param requestTopic The topic of the request.
453
448
  * @internal
454
449
  */
455
- async runProcessorsSocket(socketRouteProcessors, socketRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState, requestTopic, responseEmitter) {
450
+ async runProcessorsSocket(socketRouteProcessors, socketRoute, socketServerRequest, httpResponse, httpRequestIdentity, processorState, requestTopic, responseEmitter) {
456
451
  // Custom emit method which will also call the post processors
457
452
  const postProcessEmit = async (topic, response, responseProcessorState) => {
458
453
  await responseEmitter(topic, response);
459
454
  try {
460
455
  // The post processors are called after the response has been emitted
461
456
  for (const postSocketRouteProcessor of socketRouteProcessors) {
462
- if (core.Is.function(postSocketRouteProcessor.post)) {
463
- await postSocketRouteProcessor.post(httpServerRequest, response, socketRoute, httpRequestIdentity, responseProcessorState);
457
+ if (postSocketRouteProcessor.post) {
458
+ await postSocketRouteProcessor.post(socketServerRequest, response, socketRoute, httpRequestIdentity, responseProcessorState);
464
459
  }
465
460
  }
466
461
  }
@@ -479,8 +474,8 @@ class FastifyWebServer {
479
474
  };
480
475
  try {
481
476
  for (const socketRouteProcessor of socketRouteProcessors) {
482
- if (core.Is.function(socketRouteProcessor.pre)) {
483
- await socketRouteProcessor.pre(httpServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState);
477
+ if (socketRouteProcessor.pre) {
478
+ await socketRouteProcessor.pre(socketServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState);
484
479
  }
485
480
  }
486
481
  // We always call all the processors regardless of any response set by a previous processor.
@@ -490,8 +485,8 @@ class FastifyWebServer {
490
485
  await postProcessEmit(requestTopic, httpResponse, processorState);
491
486
  }
492
487
  for (const socketRouteProcessor of socketRouteProcessors) {
493
- if (core.Is.function(socketRouteProcessor.process)) {
494
- await socketRouteProcessor.process(httpServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState, async (topic, processResponse) => {
488
+ if (socketRouteProcessor.process) {
489
+ await socketRouteProcessor.process(socketServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState, async (topic, processResponse) => {
495
490
  await postProcessEmit(topic, processResponse, processorState);
496
491
  });
497
492
  }
@@ -301,20 +301,18 @@ class FastifyWebServer {
301
301
  });
302
302
  const socketNamespace = io.of(namespace);
303
303
  socketNamespace.on("connection", async (socket) => {
304
- const httpServerRequest = {
304
+ const socketServerRequest = {
305
305
  method: HttpMethod.GET,
306
306
  url: socket.handshake.url,
307
307
  query: socket.handshake.query,
308
- headers: socket.handshake.headers
308
+ headers: socket.handshake.headers,
309
+ socketId: socket.id
309
310
  };
310
311
  // Pass the connected information on to any processors
311
312
  try {
312
- const processorState = {
313
- socketId: socket.id
314
- };
315
313
  for (const socketRouteProcessor of socketRouteProcessors) {
316
- if (Is.function(socketRouteProcessor.connected)) {
317
- await socketRouteProcessor.connected(httpServerRequest, socketRoute, processorState);
314
+ if (socketRouteProcessor.connected) {
315
+ await socketRouteProcessor.connected(socketServerRequest, socketRoute);
318
316
  }
319
317
  }
320
318
  }
@@ -326,13 +324,10 @@ class FastifyWebServer {
326
324
  }
327
325
  socket.on("disconnect", async () => {
328
326
  try {
329
- const processorState = {
330
- socketId: socket.id
331
- };
332
327
  // The socket disconnected so notify any processors
333
328
  for (const socketRouteProcessor of socketRouteProcessors) {
334
- if (Is.function(socketRouteProcessor.disconnected)) {
335
- await socketRouteProcessor.disconnected(httpServerRequest, socketRoute, processorState);
329
+ if (socketRouteProcessor.disconnected) {
330
+ await socketRouteProcessor.disconnected(socketServerRequest, socketRoute);
336
331
  }
337
332
  }
338
333
  }
@@ -390,17 +385,17 @@ class FastifyWebServer {
390
385
  async runProcessorsRest(restRouteProcessors, restRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState) {
391
386
  try {
392
387
  for (const routeProcessor of restRouteProcessors) {
393
- if (Is.function(routeProcessor.pre)) {
388
+ if (routeProcessor.pre) {
394
389
  await routeProcessor.pre(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
395
390
  }
396
391
  }
397
392
  for (const routeProcessor of restRouteProcessors) {
398
- if (Is.function(routeProcessor.process)) {
393
+ if (routeProcessor.process) {
399
394
  await routeProcessor.process(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
400
395
  }
401
396
  }
402
397
  for (const routeProcessor of restRouteProcessors) {
403
- if (Is.function(routeProcessor.post)) {
398
+ if (routeProcessor.post) {
404
399
  await routeProcessor.post(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
405
400
  }
406
401
  }
@@ -421,44 +416,44 @@ class FastifyWebServer {
421
416
  * @internal
422
417
  */
423
418
  async handleRequestSocket(socketRouteProcessors, socketRoute, socket, fullPath, emitTopic, request) {
424
- const httpServerRequest = {
419
+ const socketServerRequest = {
425
420
  method: HttpMethod.GET,
426
421
  url: fullPath,
427
422
  query: socket.handshake.query,
428
423
  headers: socket.handshake.headers,
429
- body: request.body
424
+ body: request.body,
425
+ socketId: socket.id
430
426
  };
431
427
  const httpResponse = {};
432
428
  const httpRequestIdentity = {};
433
- const processorState = {
434
- socketId: socket.id
435
- };
436
- delete httpServerRequest.query?.EIO;
437
- delete httpServerRequest.query?.transport;
438
- await this.runProcessorsSocket(socketRouteProcessors, socketRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState, emitTopic, async (topic, response) => {
429
+ const processorState = {};
430
+ delete socketServerRequest.query?.EIO;
431
+ delete socketServerRequest.query?.transport;
432
+ await this.runProcessorsSocket(socketRouteProcessors, socketRoute, socketServerRequest, httpResponse, httpRequestIdentity, processorState, emitTopic, async (topic, response) => {
439
433
  await socket.emit(topic, response);
440
434
  });
441
435
  }
442
436
  /**
443
437
  * Run the socket processors for the route.
438
+ * @param socketId The id of the socket.
444
439
  * @param socketRouteProcessors The processors to run.
445
440
  * @param socketRoute The route to process.
446
- * @param httpServerRequest The incoming request.
441
+ * @param socketServerRequest The incoming request.
447
442
  * @param httpResponse The outgoing response.
448
443
  * @param httpRequestIdentity The identity context for the request.
449
444
  * @param processorState The state handed through the processors.
450
445
  * @param requestTopic The topic of the request.
451
446
  * @internal
452
447
  */
453
- async runProcessorsSocket(socketRouteProcessors, socketRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState, requestTopic, responseEmitter) {
448
+ async runProcessorsSocket(socketRouteProcessors, socketRoute, socketServerRequest, httpResponse, httpRequestIdentity, processorState, requestTopic, responseEmitter) {
454
449
  // Custom emit method which will also call the post processors
455
450
  const postProcessEmit = async (topic, response, responseProcessorState) => {
456
451
  await responseEmitter(topic, response);
457
452
  try {
458
453
  // The post processors are called after the response has been emitted
459
454
  for (const postSocketRouteProcessor of socketRouteProcessors) {
460
- if (Is.function(postSocketRouteProcessor.post)) {
461
- await postSocketRouteProcessor.post(httpServerRequest, response, socketRoute, httpRequestIdentity, responseProcessorState);
455
+ if (postSocketRouteProcessor.post) {
456
+ await postSocketRouteProcessor.post(socketServerRequest, response, socketRoute, httpRequestIdentity, responseProcessorState);
462
457
  }
463
458
  }
464
459
  }
@@ -477,8 +472,8 @@ class FastifyWebServer {
477
472
  };
478
473
  try {
479
474
  for (const socketRouteProcessor of socketRouteProcessors) {
480
- if (Is.function(socketRouteProcessor.pre)) {
481
- await socketRouteProcessor.pre(httpServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState);
475
+ if (socketRouteProcessor.pre) {
476
+ await socketRouteProcessor.pre(socketServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState);
482
477
  }
483
478
  }
484
479
  // We always call all the processors regardless of any response set by a previous processor.
@@ -488,8 +483,8 @@ class FastifyWebServer {
488
483
  await postProcessEmit(requestTopic, httpResponse, processorState);
489
484
  }
490
485
  for (const socketRouteProcessor of socketRouteProcessors) {
491
- if (Is.function(socketRouteProcessor.process)) {
492
- await socketRouteProcessor.process(httpServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState, async (topic, processResponse) => {
486
+ if (socketRouteProcessor.process) {
487
+ await socketRouteProcessor.process(socketServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState, async (topic, processResponse) => {
493
488
  await postProcessEmit(topic, processResponse, processorState);
494
489
  });
495
490
  }
package/docs/changelog.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @twin.org/api-server-fastify - Changelog
2
2
 
3
+ ## [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
+
5
+
6
+ ### Features
7
+
8
+ * add socket id, connect and disconnect ([20b0d0e](https://github.com/twinfoundation/api/commit/20b0d0ec279cab46141fee09de2c4a7087cdce16))
9
+
10
+
11
+ ### Dependencies
12
+
13
+ * The following workspace dependencies were updated
14
+ * dependencies
15
+ * @twin.org/api-core bumped from 0.0.2-next.2 to 0.0.2-next.3
16
+ * @twin.org/api-models bumped from 0.0.2-next.2 to 0.0.2-next.3
17
+ * @twin.org/api-processors bumped from 0.0.2-next.2 to 0.0.2-next.3
18
+
3
19
  ## [0.0.2-next.2](https://github.com/twinfoundation/api/compare/api-server-fastify-v0.0.2-next.1...api-server-fastify-v0.0.2-next.2) (2025-07-17)
4
20
 
5
21
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/api-server-fastify",
3
- "version": "0.0.2-next.2",
3
+ "version": "0.0.2-next.3",
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.2",
20
- "@twin.org/api-models": "0.0.2-next.2",
21
- "@twin.org/api-processors": "0.0.2-next.2",
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",
22
22
  "@twin.org/core": "next",
23
23
  "@twin.org/logging-models": "next",
24
24
  "@twin.org/nameof": "next",