go-mokapi 0.49.0 → 0.50.0
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.
- package/README.md +2 -2
- package/package.json +1 -1
- package/types/index.d.ts +198 -27
package/README.md
CHANGED
|
@@ -128,7 +128,7 @@ Explore tutorials that walk you through mocking different protocols and scenario
|
|
|
128
128
|
- [CI/CD Integration with GitHub Actions](https://mokapi.io/resources/tutorials/running-mokapi-in-a-ci-cd-pipeline)\
|
|
129
129
|
Run Mokapi in automated test pipelines
|
|
130
130
|
|
|
131
|
-
|
|
131
|
+
More at [mokapi.io/resources](https://mokapi.io/resources)
|
|
132
132
|
|
|
133
133
|
## Documentation
|
|
134
134
|
|
|
@@ -144,7 +144,7 @@ Explore tutorials that walk you through mocking different protocols and scenario
|
|
|
144
144
|
|
|
145
145
|
If Mokapi saves you time, consider buying me a coffee. It helps keep the project going.
|
|
146
146
|
|
|
147
|
-
<a href="https://www.buymeacoffee.com/mokapi" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height:
|
|
147
|
+
[](https://ko-fi.com/mokapi) <a href="https://www.buymeacoffee.com/mokapi" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 30px !important;width: 174px !important;" ></a>
|
|
148
148
|
|
|
149
149
|
## Merch
|
|
150
150
|
|
package/package.json
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -334,44 +334,82 @@ export interface MqttEventMessage {
|
|
|
334
334
|
value: string;
|
|
335
335
|
}
|
|
336
336
|
|
|
337
|
-
export type WebsocketEventHandler = (message: WebsocketEventMessage) => void | Promise<void>;
|
|
338
|
-
|
|
339
337
|
/**
|
|
340
|
-
*
|
|
341
|
-
*
|
|
342
|
-
*
|
|
338
|
+
* Event handler function passed to `on('websocket', handler)`. Called for every
|
|
339
|
+
* WebSocket lifecycle event — connect, message, and close — on any channel
|
|
340
|
+
* defined in the AsyncAPI specification.
|
|
341
|
+
*
|
|
342
|
+
* Use the `event.type` field to distinguish between event types and access
|
|
343
|
+
* type-specific fields like `message` and `reply`.
|
|
344
|
+
*
|
|
345
|
+
* Async handlers are supported — return a `Promise` if you need to perform
|
|
346
|
+
* asynchronous work before responding.
|
|
343
347
|
*
|
|
344
348
|
* @example
|
|
345
|
-
* //
|
|
349
|
+
* // Handle all event types in one handler
|
|
346
350
|
* import { on } from 'mokapi'
|
|
347
351
|
* export default function() {
|
|
348
352
|
* on('websocket', function(event) {
|
|
349
|
-
* event.
|
|
353
|
+
* if (event.type === 'connect') {
|
|
354
|
+
* event.client.send({ text: 'welcome!' })
|
|
355
|
+
* }
|
|
356
|
+
* if (event.type === 'message') {
|
|
357
|
+
* event.reply({ text: 'pong' })
|
|
358
|
+
* }
|
|
359
|
+
* if (event.type === 'close') {
|
|
360
|
+
* console.log(`connection closed by ${event.closedBy}: ${event.reason}`)
|
|
361
|
+
* }
|
|
350
362
|
* })
|
|
351
363
|
* }
|
|
352
364
|
*
|
|
353
365
|
* @example
|
|
354
|
-
* //
|
|
366
|
+
* // Async handler
|
|
355
367
|
* import { on } from 'mokapi'
|
|
356
368
|
* export default function() {
|
|
357
|
-
* on('websocket', function(event) {
|
|
358
|
-
* event.
|
|
369
|
+
* on('websocket', async function(event) {
|
|
370
|
+
* if (event.type === 'message') {
|
|
371
|
+
* await someAsyncOperation()
|
|
372
|
+
* event.reply({ text: 'done' })
|
|
373
|
+
* }
|
|
359
374
|
* })
|
|
360
375
|
* }
|
|
361
376
|
*
|
|
377
|
+
* @see https://mokapi.io/docs/javascript-api/mokapi/eventhandler/WebsocketEventHandler
|
|
378
|
+
*/
|
|
379
|
+
export type WebsocketEventHandler = (message: WebsocketEvent) => void | Promise<void>;
|
|
380
|
+
|
|
381
|
+
export type WebsocketEvent = WebsocketEventConnect | WebsocketEventMessage | WebsocketEventClose;
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Base interface shared by all WebSocket events. Use the `type` field to
|
|
385
|
+
* discriminate between connect, message, and close events.
|
|
386
|
+
*
|
|
362
387
|
* @example
|
|
363
|
-
* // Store clients and send later
|
|
364
388
|
* import { on } from 'mokapi'
|
|
365
|
-
* const clients: WebsocketClient[] = []
|
|
366
389
|
* export default function() {
|
|
367
390
|
* on('websocket', function(event) {
|
|
368
|
-
*
|
|
391
|
+
* if (event.type === 'connect') {
|
|
392
|
+
* event.broadcast({ text: 'a new user joined' })
|
|
393
|
+
* }
|
|
394
|
+
* if (event.type === 'message') {
|
|
395
|
+
* event.reply({ text: 'pong' })
|
|
396
|
+
* }
|
|
397
|
+
* if (event.type === 'close') {
|
|
398
|
+
* console.log(`client disconnected: ${event.reason}`)
|
|
399
|
+
* }
|
|
369
400
|
* })
|
|
370
401
|
* }
|
|
371
402
|
*
|
|
372
|
-
* @see https://mokapi.io/docs/javascript-api/mokapi/eventhandler/
|
|
403
|
+
* @see https://mokapi.io/docs/javascript-api/mokapi/eventhandler/WebsocketEvent
|
|
373
404
|
*/
|
|
374
|
-
export interface
|
|
405
|
+
export interface WebsocketEventBase {
|
|
406
|
+
/**
|
|
407
|
+
* Discriminates between the three WebSocket event types.
|
|
408
|
+
* Use this in a conditional to access type-specific fields like
|
|
409
|
+
* `message` and `reply` (only on `'message'`) or `reason` (only on `'close'`).
|
|
410
|
+
*/
|
|
411
|
+
readonly type: 'connect' | 'message' | 'close'
|
|
412
|
+
|
|
375
413
|
/**
|
|
376
414
|
* The name of the API, matching the `info.title` field in the AsyncAPI specification.
|
|
377
415
|
*
|
|
@@ -404,6 +442,69 @@ export interface WebsocketEventMessage {
|
|
|
404
442
|
*/
|
|
405
443
|
readonly client: WebsocketClient;
|
|
406
444
|
|
|
445
|
+
/**
|
|
446
|
+
* Sends a message to all clients currently connected to this channel,
|
|
447
|
+
* including the sender. Shorthand for looping over `event.channel.clients`.
|
|
448
|
+
*
|
|
449
|
+
* @param message - The message payload to send to every connected client.
|
|
450
|
+
*
|
|
451
|
+
* @example
|
|
452
|
+
* // Broadcast a chat message to everyone
|
|
453
|
+
* on('websocket', function(event) {
|
|
454
|
+
* event.broadcast({ from: event.client.query['userId'], text: event.message.text })
|
|
455
|
+
* })
|
|
456
|
+
*
|
|
457
|
+
* @example
|
|
458
|
+
* // Broadcast excluding the sender
|
|
459
|
+
* on('websocket', function(event) {
|
|
460
|
+
* for (const client of event.channel.clients) {
|
|
461
|
+
* if (client.remoteAddr !== event.client.remoteAddr) {
|
|
462
|
+
* client.send(event.message)
|
|
463
|
+
* }
|
|
464
|
+
* }
|
|
465
|
+
* })
|
|
466
|
+
*/
|
|
467
|
+
broadcast(message: any): void;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* WebsocketEventMessage is an object passed to a WebSocket event handler whenever
|
|
472
|
+
* a message is received from a client. It provides access to the message payload,
|
|
473
|
+
* connection metadata, and methods to send messages back.
|
|
474
|
+
*
|
|
475
|
+
* @example
|
|
476
|
+
* // Simple request/reply
|
|
477
|
+
* import { on } from 'mokapi'
|
|
478
|
+
* export default function() {
|
|
479
|
+
* on('websocket', function(event) {
|
|
480
|
+
* event.reply({ text: 'pong' })
|
|
481
|
+
* })
|
|
482
|
+
* }
|
|
483
|
+
*
|
|
484
|
+
* @example
|
|
485
|
+
* // Broadcast to all connected clients (e.g. chat)
|
|
486
|
+
* import { on } from 'mokapi'
|
|
487
|
+
* export default function() {
|
|
488
|
+
* on('websocket', function(event) {
|
|
489
|
+
* event.broadcast({ from: event.client.query['userId'], text: event.message.text })
|
|
490
|
+
* })
|
|
491
|
+
* }
|
|
492
|
+
*
|
|
493
|
+
* @example
|
|
494
|
+
* // Store clients and send later
|
|
495
|
+
* import { on } from 'mokapi'
|
|
496
|
+
* const clients: WebsocketClient[] = []
|
|
497
|
+
* export default function() {
|
|
498
|
+
* on('websocket', function(event) {
|
|
499
|
+
* clients.push(event.client)
|
|
500
|
+
* })
|
|
501
|
+
* }
|
|
502
|
+
*
|
|
503
|
+
* @see https://mokapi.io/docs/javascript-api/mokapi/eventhandler/WebsocketEventMessage
|
|
504
|
+
*/
|
|
505
|
+
export interface WebsocketEventMessage extends WebsocketEventBase {
|
|
506
|
+
readonly type: 'message'
|
|
507
|
+
|
|
407
508
|
/**
|
|
408
509
|
* The decoded message payload. The type depends on the `contentType`
|
|
409
510
|
* defined in the AsyncAPI specification — typically an object for
|
|
@@ -433,30 +534,100 @@ export interface WebsocketEventMessage {
|
|
|
433
534
|
* })
|
|
434
535
|
*/
|
|
435
536
|
reply(message: any): void;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* Fired once when a client establishes a WebSocket connection.
|
|
541
|
+
* Use this to send a welcome message to the connecting client or
|
|
542
|
+
* notify all other clients that someone joined.
|
|
543
|
+
*
|
|
544
|
+
* @example
|
|
545
|
+
* // Send a welcome message to the connecting client
|
|
546
|
+
* import { on } from 'mokapi'
|
|
547
|
+
* export default function() {
|
|
548
|
+
* on('websocket', function(event) {
|
|
549
|
+
* if (event.type === 'connect') {
|
|
550
|
+
* event.client.send({ text: 'welcome!' })
|
|
551
|
+
* }
|
|
552
|
+
* })
|
|
553
|
+
* }
|
|
554
|
+
*
|
|
555
|
+
* @example
|
|
556
|
+
* // Notify all existing clients that someone joined
|
|
557
|
+
* import { on } from 'mokapi'
|
|
558
|
+
* export default function() {
|
|
559
|
+
* on('websocket', function(event) {
|
|
560
|
+
* if (event.type === 'connect') {
|
|
561
|
+
* event.broadcast({ text: `${event.client.query['userId']} has joined` })
|
|
562
|
+
* }
|
|
563
|
+
* })
|
|
564
|
+
* }
|
|
565
|
+
*
|
|
566
|
+
* @see https://mokapi.io/docs/javascript-api/mokapi/eventhandler/WebsocketEventConnect
|
|
567
|
+
*/
|
|
568
|
+
export interface WebsocketEventConnect extends WebsocketEventBase {
|
|
569
|
+
readonly type: 'connect'
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* Fired when a WebSocket connection is closed, either by the client or the server.
|
|
574
|
+
* Use `closedBy` to determine who initiated the close and `reason` for the explanation.
|
|
575
|
+
*
|
|
576
|
+
* @example
|
|
577
|
+
* // Log when a client disconnects
|
|
578
|
+
* import { on } from 'mokapi'
|
|
579
|
+
* export default function() {
|
|
580
|
+
* on('websocket', function(event) {
|
|
581
|
+
* if (event.type === 'close') {
|
|
582
|
+
* console.log(`connection closed by ${event.closedBy}: ${event.reason}`)
|
|
583
|
+
* }
|
|
584
|
+
* })
|
|
585
|
+
* }
|
|
586
|
+
*
|
|
587
|
+
* @example
|
|
588
|
+
* // Notify remaining clients when someone leaves
|
|
589
|
+
* import { on } from 'mokapi'
|
|
590
|
+
* export default function() {
|
|
591
|
+
* on('websocket', function(event) {
|
|
592
|
+
* if (event.type === 'close') {
|
|
593
|
+
* event.broadcast({ text: `${event.client.query['userId']} has left` })
|
|
594
|
+
* }
|
|
595
|
+
* })
|
|
596
|
+
* }
|
|
597
|
+
*
|
|
598
|
+
* @see https://mokapi.io/docs/javascript-api/mokapi/eventhandler/WebsocketEventClose
|
|
599
|
+
*/
|
|
600
|
+
export interface WebsocketEventClose extends WebsocketEventBase {
|
|
601
|
+
readonly type: 'close'
|
|
436
602
|
|
|
437
603
|
/**
|
|
438
|
-
*
|
|
439
|
-
*
|
|
440
|
-
*
|
|
441
|
-
* @param message - The message payload to send to every connected client.
|
|
604
|
+
* The reason the connection was closed. Maps to the WebSocket close frame
|
|
605
|
+
* reason string. Empty if no reason was provided.
|
|
442
606
|
*
|
|
443
607
|
* @example
|
|
444
|
-
* // Broadcast a chat message to everyone
|
|
445
608
|
* on('websocket', function(event) {
|
|
446
|
-
* event.
|
|
609
|
+
* if (event.type === 'close') {
|
|
610
|
+
* console.log(event.reason) // e.g. "going away"
|
|
611
|
+
* }
|
|
447
612
|
* })
|
|
613
|
+
*/
|
|
614
|
+
readonly reason: string
|
|
615
|
+
|
|
616
|
+
/**
|
|
617
|
+
* Indicates who initiated the close — either the client or the server.
|
|
448
618
|
*
|
|
449
619
|
* @example
|
|
450
|
-
* // Broadcast excluding the sender
|
|
451
620
|
* on('websocket', function(event) {
|
|
452
|
-
*
|
|
453
|
-
* if (
|
|
454
|
-
*
|
|
621
|
+
* if (event.type === 'close') {
|
|
622
|
+
* if (event.closedBy === 'client') {
|
|
623
|
+
* console.log('client disconnected')
|
|
624
|
+
* } else {
|
|
625
|
+
* console.log('server closed the connection')
|
|
455
626
|
* }
|
|
456
627
|
* }
|
|
457
628
|
* })
|
|
458
629
|
*/
|
|
459
|
-
|
|
630
|
+
readonly closedBy: 'client' | 'server'
|
|
460
631
|
}
|
|
461
632
|
|
|
462
633
|
/**
|
|
@@ -916,7 +1087,7 @@ export interface WebsocketEventArgs extends EventArgs {
|
|
|
916
1087
|
* - undefined: Mokapi determines tracking automatically based on
|
|
917
1088
|
* whether the message was modified or acknowledged by the handler
|
|
918
1089
|
*/
|
|
919
|
-
track?: boolean | ((message:
|
|
1090
|
+
track?: boolean | ((message: WebsocketEvent) => boolean);
|
|
920
1091
|
}
|
|
921
1092
|
|
|
922
1093
|
/**
|