go-mokapi 0.48.0 → 0.49.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 +82 -76
- package/package.json +3 -1
- package/types/encoding.d.ts +1 -1
- package/types/faker.d.ts +1 -1
- package/types/file.d.ts +1 -1
- package/types/global.d.ts +1 -1
- package/types/http.d.ts +1 -1
- package/types/index.d.ts +290 -9
- package/types/mail.d.ts +1 -1
- package/types/mqtt.d.ts +6 -2
- package/types/mustache.d.ts +1 -1
- package/types/yaml.d.ts +1 -1
package/README.md
CHANGED
|
@@ -1,27 +1,35 @@
|
|
|
1
1
|
<p align="center">
|
|
2
2
|
<a href="https://mokapi.io">
|
|
3
|
-
<img src="https://raw.githubusercontent.com/marle3003/mokapi/
|
|
3
|
+
<img src="https://raw.githubusercontent.com/marle3003/mokapi/main/logo.svg" alt="Mokapi - Open Source Mock API Server" width="300" />
|
|
4
4
|
</a>
|
|
5
5
|
</p>
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
<h2 align="center">The Open-Source Mock API Tool Across Protocols</h2>
|
|
7
|
+
<p align="center">
|
|
8
|
+
<a href="https://github.com/marle3003/mokapi/releases"><img src="https://img.shields.io/github/release/marle3003/mokapi.svg" alt="Github release"></a>
|
|
9
|
+
<a href="https://github.com/marle3003/mokapi/actions/workflows/test.yml"><img src="https://github.com/marle3003/mokapi/actions/workflows/build.yml/badge.svg" alt="Build status"></a>
|
|
10
|
+
<a href="https://codecov.io/gh/marle3003/mokapi"><img src="https://img.shields.io/codecov/c/gh/marle3003/mokapi/main.svg" alt="Codecov branch"></a>
|
|
11
|
+
<a href="https://github.com/marle3003/mokapi/blob/master/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="MIT License"></a>
|
|
12
|
+
</p>
|
|
9
13
|
<p align="center">
|
|
10
|
-
<a href="https://
|
|
11
|
-
<a href="https://github.com/marle3003/mokapi/
|
|
12
|
-
<a href="https://
|
|
14
|
+
<a href="https://mokapi.io/docs/welcome">Documentation</a> ·
|
|
15
|
+
<a href="https://github.com/marle3003/mokapi/releases">Releases</a> ·
|
|
16
|
+
<a href="https://mokapi.io/resources/tutorials">Tutorials</a> ·
|
|
17
|
+
<a href="https://mokapi.io/resources/blogs">Blog</a>
|
|
13
18
|
</p>
|
|
14
19
|
|
|
15
|
-
##
|
|
20
|
+
## About
|
|
21
|
+
|
|
22
|
+
Mokapi is an open-source, local-first **mock API tool** to develop and test faster. Simulate complete environments driven
|
|
23
|
+
by OpenAPI and AsyncAPI specifications without external dependencies.
|
|
16
24
|
|
|
17
|
-
|
|
18
|
-
|
|
25
|
+
It supports HTTP/REST, Apache Kafka, MQTT, Websocket, LDAP, and SMTP from a single tool, making it useful across the
|
|
26
|
+
whole stack, not just for REST APIs.
|
|
19
27
|
|
|
20
|
-
|
|
21
|
-
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
24
|
-
- API
|
|
28
|
+
Use it to:
|
|
29
|
+
- Build frontend UIs before the backend exists
|
|
30
|
+
- Test error states, timeouts, and edge cases safely
|
|
31
|
+
- Run reliable CI/CD pipelines without external dependencies
|
|
32
|
+
- Validate API contracts before writing implementation code
|
|
25
33
|
|
|
26
34
|
## Quick Start
|
|
27
35
|
|
|
@@ -31,79 +39,77 @@ Try the **mock API** instantly:
|
|
|
31
39
|
npx go-mokapi https://petstore3.swagger.io/api/v3/openapi.json
|
|
32
40
|
```
|
|
33
41
|
|
|
34
|
-
Then test your local **mock API**:
|
|
35
|
-
|
|
36
42
|
```
|
|
37
43
|
curl http://localhost/api/v3/pet/1 -H 'Accept: application/json'
|
|
38
44
|
```
|
|
39
45
|
|
|
40
|
-
|
|
46
|
+
A working mock from a public OpenAPI spec in under a minute. No installation required.
|
|
41
47
|
|
|
42
|
-
|
|
43
|
-
npm install -g go-mokapi
|
|
44
|
-
mokapi https://petstore3.swagger.io/api/v3/openapi.json
|
|
45
|
-
```
|
|
48
|
+
## Feature
|
|
46
49
|
|
|
47
|
-
|
|
50
|
+
| Feature | Description |
|
|
51
|
+
|----------------------|---------------------------------------------------------|
|
|
52
|
+
| Multi-protocol | HTTP/HTTPS, Apache Kafka, MQTT, Websocket, LDAP, SMTP |
|
|
53
|
+
| Spec-driven | Uses OpenAPI and AsyncAPI as the source of truth |
|
|
54
|
+
| JavaScript scripting | Customize responses, simulate errors, add delays |
|
|
55
|
+
| Everything as code | Version-controlled, CI/CD-ready, no UI required |
|
|
56
|
+
| Config patching | Override parts of a spec without modifying the original |
|
|
57
|
+
| Built-in dashboard | Real-time request and response viewer at localhost:8080 |
|
|
58
|
+
| Flexible providers | Load specs from files, URLs, Git repos, or NPM packages |
|
|
48
59
|
|
|
49
|
-
|
|
60
|
+
## Usage
|
|
50
61
|
|
|
51
|
-
|
|
62
|
+
### Basic HTTP mock
|
|
52
63
|
|
|
53
|
-
|
|
54
|
-
|
|
64
|
+
```bash
|
|
65
|
+
npx go-mokapi ./openapi.yaml
|
|
66
|
+
```
|
|
55
67
|
|
|
56
|
-
###
|
|
57
|
-
Uses OpenAPI and AsyncAPI specs as the source of truth. Your **mock API** stays aligned with your API contracts.
|
|
68
|
+
### With a remote spec
|
|
58
69
|
|
|
59
|
-
|
|
70
|
+
```bash
|
|
71
|
+
npx go-mokapi https://example.com/api/openapi.json
|
|
72
|
+
```
|
|
60
73
|
|
|
61
|
-
|
|
62
|
-
```javascript
|
|
63
|
-
import { on } from 'mokapi'
|
|
74
|
+
### Docker
|
|
64
75
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if (request.path.petId === '999') {
|
|
69
|
-
response.statusCode = 404
|
|
70
|
-
return
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// Customize response data
|
|
74
|
-
response.data.name = 'Custom Pet Name'
|
|
75
|
-
})
|
|
76
|
-
}
|
|
76
|
+
```bash
|
|
77
|
+
docker run -it -p 8080:8080 -p 80:80 \
|
|
78
|
+
-v $(pwd):/data mokapi/mokapi /data/openapi.yaml
|
|
77
79
|
```
|
|
78
80
|
|
|
79
|
-
###
|
|
80
|
-
Version control your **mock API** alongside your code. Run them in CI/CD pipelines. No UI configuration required.
|
|
81
|
-
|
|
82
|
-
### Configuration Patching
|
|
83
|
-
Override parts of your OpenAPI spec without modifying the original file. Perfect for testing different scenarios.
|
|
81
|
+
### Dashboard
|
|
84
82
|
|
|
85
|
-
|
|
86
|
-
Visualize requests, responses, and logs in real-time at http://localhost:8080
|
|
87
|
-
<img src="https://raw.githubusercontent.com/marle3003/mokapi/refs/heads/main/webui.png" alt="Mokapi Web UI" title="Mokapi Web UI" />
|
|
83
|
+
Open http://localhost:8080 to view live requests, responses, and logs.
|
|
88
84
|
|
|
89
|
-
|
|
90
|
-
Load specs from local files, HTTP URLs, Git repositories, or NPM packages.
|
|
85
|
+
<img src="https://raw.githubusercontent.com/marle3003/mokapi/refs/heads/main/webui.png" alt="Mokapi Web UI" />
|
|
91
86
|
|
|
92
|
-
##
|
|
87
|
+
## Configuration
|
|
93
88
|
|
|
94
|
-
###
|
|
95
|
-
Mock backend APIs while building UIs. Test loading states, errors, and edge cases without waiting for real endpoints.
|
|
89
|
+
### JavaScript scripting
|
|
96
90
|
|
|
97
|
-
|
|
98
|
-
|
|
91
|
+
```javascript
|
|
92
|
+
import { on } from 'mokapi'
|
|
99
93
|
|
|
100
|
-
|
|
101
|
-
|
|
94
|
+
export default function() {
|
|
95
|
+
on('http', (request, response) => {
|
|
96
|
+
// Return 404 for specific IDs
|
|
97
|
+
if (request.path.petId === '999') {
|
|
98
|
+
response.statusCode = 404
|
|
99
|
+
return
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Customize response data
|
|
103
|
+
response.data.name = 'Custom Pet Name'
|
|
104
|
+
})
|
|
105
|
+
}
|
|
106
|
+
```
|
|
102
107
|
|
|
103
|
-
###
|
|
104
|
-
|
|
108
|
+
### Spec patching
|
|
109
|
+
Override parts of your OpenAPI spec for specific test scenarios without touching the original file. See the
|
|
110
|
+
[configuration guide](https://mokapi.io/docs/configuration/overview) for details.
|
|
105
111
|
|
|
106
|
-
#
|
|
112
|
+
# Tutorials
|
|
107
113
|
|
|
108
114
|
Explore tutorials that walk you through mocking different protocols and scenarios:
|
|
109
115
|
|
|
@@ -122,7 +128,7 @@ Explore tutorials that walk you through mocking different protocols and scenario
|
|
|
122
128
|
- [CI/CD Integration with GitHub Actions](https://mokapi.io/resources/tutorials/running-mokapi-in-a-ci-cd-pipeline)\
|
|
123
129
|
Run Mokapi in automated test pipelines
|
|
124
130
|
|
|
125
|
-
> More
|
|
131
|
+
> More at [mokapi.io/resources](https://mokapi.io/resources)
|
|
126
132
|
|
|
127
133
|
## Documentation
|
|
128
134
|
|
|
@@ -136,20 +142,20 @@ Explore tutorials that walk you through mocking different protocols and scenario
|
|
|
136
142
|
|
|
137
143
|
## Support the Project
|
|
138
144
|
|
|
139
|
-
If Mokapi
|
|
145
|
+
If Mokapi saves you time, consider buying me a coffee. It helps keep the project going.
|
|
140
146
|
|
|
141
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: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>
|
|
142
148
|
|
|
149
|
+
## Merch
|
|
150
|
+
|
|
151
|
+
[](https://mokapi.myspreadshop.com/)
|
|
152
|
+
|
|
143
153
|
## License
|
|
144
154
|
|
|
145
155
|
MIT License - see [LICENSE](https://github.com/marle3003/mokapi/blob/main/LICENSE) for details.
|
|
146
156
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
- Docker Image: [https://hub.docker.com/r/mokapi/mokapi](https://hub.docker.com/r/mokapi/mokapi)
|
|
153
|
-
- Documentation: [mokapi.io/docs](https://mokapi.io/docs)
|
|
154
|
-
- Tutorials: [mokapi.io/resources/tutorials](https://mokapi.io/resources/tutorials)
|
|
155
|
-
- Blog: [mokapi.io/resources/blogs](https://mokapi.io/resources/blogs)
|
|
157
|
+
<p align="center">
|
|
158
|
+
<a href="https://mokapi.io">Website</a> ·
|
|
159
|
+
<a href="https://npmjs.com/package/go-mokapi">NPM</a> ·
|
|
160
|
+
<a href="https://hub.docker.com/r/mokapi/mokapi">Docker Hub</a>
|
|
161
|
+
</p>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "go-mokapi",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.49.0",
|
|
4
4
|
"description": "Easy and flexible API mocking",
|
|
5
5
|
"homepage": "https://mokapi.io",
|
|
6
6
|
"repository": {
|
|
@@ -24,6 +24,8 @@
|
|
|
24
24
|
"api mock",
|
|
25
25
|
"api mocking",
|
|
26
26
|
"kafka mock",
|
|
27
|
+
"mqtt mock",
|
|
28
|
+
"websocket mock",
|
|
27
29
|
"ldap mock",
|
|
28
30
|
"smtp mock",
|
|
29
31
|
"imap mock",
|
package/types/encoding.d.ts
CHANGED
package/types/faker.d.ts
CHANGED
package/types/file.d.ts
CHANGED
package/types/global.d.ts
CHANGED
package/types/http.d.ts
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -120,6 +120,7 @@ export interface EventHandler {
|
|
|
120
120
|
mqtt: MqttEventHandler;
|
|
121
121
|
ldap: LdapEventHandler;
|
|
122
122
|
smtp: SmtpEventHandler;
|
|
123
|
+
websocket: WebsocketEventHandler;
|
|
123
124
|
}
|
|
124
125
|
|
|
125
126
|
/**
|
|
@@ -333,6 +334,269 @@ export interface MqttEventMessage {
|
|
|
333
334
|
value: string;
|
|
334
335
|
}
|
|
335
336
|
|
|
337
|
+
export type WebsocketEventHandler = (message: WebsocketEventMessage) => void | Promise<void>;
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* WebsocketEventMessage is an object passed to a WebSocket event handler whenever
|
|
341
|
+
* a message is received from a client. It provides access to the message payload,
|
|
342
|
+
* connection metadata, and methods to send messages back.
|
|
343
|
+
*
|
|
344
|
+
* @example
|
|
345
|
+
* // Simple request/reply
|
|
346
|
+
* import { on } from 'mokapi'
|
|
347
|
+
* export default function() {
|
|
348
|
+
* on('websocket', function(event) {
|
|
349
|
+
* event.reply({ text: 'pong' })
|
|
350
|
+
* })
|
|
351
|
+
* }
|
|
352
|
+
*
|
|
353
|
+
* @example
|
|
354
|
+
* // Broadcast to all connected clients (e.g. chat)
|
|
355
|
+
* import { on } from 'mokapi'
|
|
356
|
+
* export default function() {
|
|
357
|
+
* on('websocket', function(event) {
|
|
358
|
+
* event.broadcast({ from: event.client.query['userId'], text: event.message.text })
|
|
359
|
+
* })
|
|
360
|
+
* }
|
|
361
|
+
*
|
|
362
|
+
* @example
|
|
363
|
+
* // Store clients and send later
|
|
364
|
+
* import { on } from 'mokapi'
|
|
365
|
+
* const clients: WebsocketClient[] = []
|
|
366
|
+
* export default function() {
|
|
367
|
+
* on('websocket', function(event) {
|
|
368
|
+
* clients.push(event.client)
|
|
369
|
+
* })
|
|
370
|
+
* }
|
|
371
|
+
*
|
|
372
|
+
* @see https://mokapi.io/docs/javascript-api/mokapi/eventhandler/WebsocketEventMessage
|
|
373
|
+
*/
|
|
374
|
+
export interface WebsocketEventMessage {
|
|
375
|
+
/**
|
|
376
|
+
* The name of the API, matching the `info.title` field in the AsyncAPI specification.
|
|
377
|
+
*
|
|
378
|
+
* @example
|
|
379
|
+
* on('websocket', function(event) {
|
|
380
|
+
* console.log(event.api) // e.g. "Chat API"
|
|
381
|
+
* })
|
|
382
|
+
*/
|
|
383
|
+
readonly api: string;
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* The channel on which the message was received.
|
|
387
|
+
*
|
|
388
|
+
* @example
|
|
389
|
+
* on('websocket', function(event) {
|
|
390
|
+
* console.log(event.channel.name) // e.g. "/chat"
|
|
391
|
+
* })
|
|
392
|
+
*/
|
|
393
|
+
readonly channel: WebsocketChannel;
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* The client that sent the message. Can be stored and used later
|
|
397
|
+
* to send messages outside the current event handler.
|
|
398
|
+
*
|
|
399
|
+
* @example
|
|
400
|
+
* on('websocket', function(event) {
|
|
401
|
+
* const userId = event.client.query['userId']
|
|
402
|
+
* const token = event.client.headers['authorization']
|
|
403
|
+
* })
|
|
404
|
+
*/
|
|
405
|
+
readonly client: WebsocketClient;
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* The decoded message payload. The type depends on the `contentType`
|
|
409
|
+
* defined in the AsyncAPI specification — typically an object for
|
|
410
|
+
* `application/json` or a string for `text/plain`.
|
|
411
|
+
*
|
|
412
|
+
* @example
|
|
413
|
+
* on('websocket', function(event) {
|
|
414
|
+
* console.log(event.message.text)
|
|
415
|
+
* })
|
|
416
|
+
*/
|
|
417
|
+
readonly message: any;
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Sends a message back to the client that sent the current message.
|
|
421
|
+
* Shorthand for `event.client.send(message)`.
|
|
422
|
+
*
|
|
423
|
+
* The message is validated against the AsyncAPI specification before
|
|
424
|
+
* being sent. If validation fails, the error is recorded in the
|
|
425
|
+
* dashboard event log.
|
|
426
|
+
*
|
|
427
|
+
* @param message - The message payload to send. Will be encoded
|
|
428
|
+
* according to the channel's `contentType`.
|
|
429
|
+
*
|
|
430
|
+
* @example
|
|
431
|
+
* on('websocket', function(event) {
|
|
432
|
+
* event.reply({ text: 'got your message' })
|
|
433
|
+
* })
|
|
434
|
+
*/
|
|
435
|
+
reply(message: any): void;
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Sends a message to all clients currently connected to this channel,
|
|
439
|
+
* including the sender. Shorthand for looping over `event.channel.clients`.
|
|
440
|
+
*
|
|
441
|
+
* @param message - The message payload to send to every connected client.
|
|
442
|
+
*
|
|
443
|
+
* @example
|
|
444
|
+
* // Broadcast a chat message to everyone
|
|
445
|
+
* on('websocket', function(event) {
|
|
446
|
+
* event.broadcast({ from: event.client.query['userId'], text: event.message.text })
|
|
447
|
+
* })
|
|
448
|
+
*
|
|
449
|
+
* @example
|
|
450
|
+
* // Broadcast excluding the sender
|
|
451
|
+
* on('websocket', function(event) {
|
|
452
|
+
* for (const client of event.channel.clients) {
|
|
453
|
+
* if (client.remoteAddr !== event.client.remoteAddr) {
|
|
454
|
+
* client.send(event.message)
|
|
455
|
+
* }
|
|
456
|
+
* }
|
|
457
|
+
* })
|
|
458
|
+
*/
|
|
459
|
+
broadcast(message: any): void;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Represents a WebSocket client connected to a channel.
|
|
464
|
+
* The client object is populated from the HTTP upgrade handshake,
|
|
465
|
+
* so `query` and `headers` reflect the values sent during the
|
|
466
|
+
* initial connection request.
|
|
467
|
+
*
|
|
468
|
+
* Client references to remain valid after the event handler returns,
|
|
469
|
+
* so they can be stored and used to push messages at any time
|
|
470
|
+
* while the connection is open.
|
|
471
|
+
*
|
|
472
|
+
* @example
|
|
473
|
+
* // Store clients on connect, send later from another event
|
|
474
|
+
* import { on } from 'mokapi'
|
|
475
|
+
* const clients: WebsocketClient[] = []
|
|
476
|
+
* export default function() {
|
|
477
|
+
* on('websocket', function(event) {
|
|
478
|
+
* if (event.message.type === 'join') {
|
|
479
|
+
* clients.push(event.client)
|
|
480
|
+
* }
|
|
481
|
+
* })
|
|
482
|
+
* }
|
|
483
|
+
*
|
|
484
|
+
* @see https://mokapi.io/docs/javascript-api/mokapi/eventhandler/WebsocketClient
|
|
485
|
+
*/
|
|
486
|
+
export interface WebsocketClient {
|
|
487
|
+
/**
|
|
488
|
+
* The remote address of the client in `host:port` format.
|
|
489
|
+
* Can be used to distinguish clients when no application-level
|
|
490
|
+
* identity is available.
|
|
491
|
+
*
|
|
492
|
+
* @example
|
|
493
|
+
* on('websocket', function(event) {
|
|
494
|
+
* console.log(event.client.remoteAddr) // e.g. "127.0.0.1:54321"
|
|
495
|
+
* })
|
|
496
|
+
*/
|
|
497
|
+
readonly remoteAddr: string;
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Query parameters from the HTTP upgrade request, as defined
|
|
501
|
+
* in the channel's WebSocket binding (`bindings.ws.query`).
|
|
502
|
+
*
|
|
503
|
+
* @example
|
|
504
|
+
* // AsyncAPI binding:
|
|
505
|
+
* // bindings:
|
|
506
|
+
* // ws:
|
|
507
|
+
* // query:
|
|
508
|
+
* // properties:
|
|
509
|
+
* // userId:
|
|
510
|
+
* // type: string
|
|
511
|
+
*
|
|
512
|
+
* on('websocket', function(event) {
|
|
513
|
+
* const userId = event.client.query['userId']
|
|
514
|
+
* })
|
|
515
|
+
*/
|
|
516
|
+
readonly query: Record<string, string>;
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* HTTP headers from the upgrade request, as defined in the
|
|
520
|
+
* channel's WebSocket binding (`bindings.ws.headers`).
|
|
521
|
+
*
|
|
522
|
+
* @example
|
|
523
|
+
* // AsyncAPI binding:
|
|
524
|
+
* // bindings:
|
|
525
|
+
* // ws:
|
|
526
|
+
* // headers:
|
|
527
|
+
* // properties:
|
|
528
|
+
* // Authorization:
|
|
529
|
+
* // type: string
|
|
530
|
+
*
|
|
531
|
+
* on('websocket', function(event) {
|
|
532
|
+
* const token = event.client.headers['authorization']
|
|
533
|
+
* })
|
|
534
|
+
*/
|
|
535
|
+
readonly headers: Record<string, string>;
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* Sends a message to this specific client. The client reference
|
|
539
|
+
* can be stored and called outside of the event handler to push
|
|
540
|
+
* messages at any time while the connection is open.
|
|
541
|
+
*
|
|
542
|
+
* @param message - The message payload to send. Will be encoded
|
|
543
|
+
* according to the channel's `contentType`.
|
|
544
|
+
*
|
|
545
|
+
* @example
|
|
546
|
+
* // Immediate send (equivalent to event.reply)
|
|
547
|
+
* on('websocket', function(event) {
|
|
548
|
+
* event.client.send({ text: 'hello' })
|
|
549
|
+
* })
|
|
550
|
+
*
|
|
551
|
+
* @example
|
|
552
|
+
* // Deferred send after storing the client
|
|
553
|
+
* const clients: WebsocketClient[] = []
|
|
554
|
+
* on('websocket', function(event) {
|
|
555
|
+
* clients.push(event.client)
|
|
556
|
+
* })
|
|
557
|
+
* // later, from a timer or another event:
|
|
558
|
+
* clients.forEach(c => c.send({ text: 'server push' }))
|
|
559
|
+
*/
|
|
560
|
+
send(message: any): void;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/**
|
|
564
|
+
* Represents the WebSocket channel on which a message was received.
|
|
565
|
+
* Provides access to all currently connected clients, which is useful
|
|
566
|
+
* for targeted sends or building broadcast logic manually.
|
|
567
|
+
*
|
|
568
|
+
* @see https://mokapi.io/docs/javascript-api/mokapi/eventhandler/WebsocketChannel
|
|
569
|
+
*/
|
|
570
|
+
export interface WebsocketChannel {
|
|
571
|
+
/**
|
|
572
|
+
* The channel path, matching the channel address in the AsyncAPI
|
|
573
|
+
* specification.
|
|
574
|
+
*
|
|
575
|
+
* @example
|
|
576
|
+
* on('websocket', function(event) {
|
|
577
|
+
* console.log(event.channel.name) // e.g. "/chat"
|
|
578
|
+
* })
|
|
579
|
+
*/
|
|
580
|
+
readonly name: string;
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* All clients currently connected to this channel. Use this for
|
|
584
|
+
* custom fan-out logic beyond what `broadcast` provides — for
|
|
585
|
+
* example, filtering by query parameter or sending to a subset.
|
|
586
|
+
*
|
|
587
|
+
* @example
|
|
588
|
+
* // Send to all clients with a specific userId
|
|
589
|
+
* on('websocket', function(event) {
|
|
590
|
+
* for (const client of event.channel.clients) {
|
|
591
|
+
* if (client.query['room'] === event.message.room) {
|
|
592
|
+
* client.send(event.message)
|
|
593
|
+
* }
|
|
594
|
+
* }
|
|
595
|
+
* })
|
|
596
|
+
*/
|
|
597
|
+
readonly clients: readonly WebsocketClient[];
|
|
598
|
+
}
|
|
599
|
+
|
|
336
600
|
/**
|
|
337
601
|
* LdapEventHandler is a function that is executed when a LDAP search query is triggered.
|
|
338
602
|
* @example
|
|
@@ -574,6 +838,11 @@ export interface TypedEventArgs {
|
|
|
574
838
|
* Arguments for MQTT event handlers.
|
|
575
839
|
*/
|
|
576
840
|
mqtt: MqttEventArgs;
|
|
841
|
+
|
|
842
|
+
/**
|
|
843
|
+
* Arguments for Websocket event handlers.
|
|
844
|
+
*/
|
|
845
|
+
websocket: WebsocketEventArgs
|
|
577
846
|
/**
|
|
578
847
|
* Arguments for LDAP event handlers.
|
|
579
848
|
*/
|
|
@@ -584,12 +853,6 @@ export interface TypedEventArgs {
|
|
|
584
853
|
smtp: SmtpEventArgs;
|
|
585
854
|
}
|
|
586
855
|
|
|
587
|
-
/**
|
|
588
|
-
* Configuration options for HTTP event handlers.
|
|
589
|
-
*
|
|
590
|
-
* These arguments control execution behavior such as
|
|
591
|
-
* priority, tagging, and dashboard tracking.
|
|
592
|
-
*/
|
|
593
856
|
export interface HttpEventArgs extends EventArgs {
|
|
594
857
|
/**
|
|
595
858
|
* Controls whether this event handler is tracked in the dashboard.
|
|
@@ -638,6 +901,24 @@ export interface MqttEventArgs extends EventArgs {
|
|
|
638
901
|
track?: boolean | ((message: MqttEventMessage) => boolean);
|
|
639
902
|
}
|
|
640
903
|
|
|
904
|
+
/**
|
|
905
|
+
* Configuration options for Websocket event handlers.
|
|
906
|
+
*
|
|
907
|
+
* These arguments control execution behavior such as
|
|
908
|
+
* priority, tagging, and dashboard tracking.
|
|
909
|
+
*/
|
|
910
|
+
export interface WebsocketEventArgs extends EventArgs {
|
|
911
|
+
/**
|
|
912
|
+
* Controls whether this event handler is tracked in the dashboard.
|
|
913
|
+
*
|
|
914
|
+
* - true: always track this handler
|
|
915
|
+
* - false: never track this handler
|
|
916
|
+
* - undefined: Mokapi determines tracking automatically based on
|
|
917
|
+
* whether the message was modified or acknowledged by the handler
|
|
918
|
+
*/
|
|
919
|
+
track?: boolean | ((message: WebsocketEventMessage) => boolean);
|
|
920
|
+
}
|
|
921
|
+
|
|
641
922
|
/**
|
|
642
923
|
* Configuration options for LDAP event handlers.
|
|
643
924
|
*
|
|
@@ -687,9 +968,9 @@ export interface SmtpEventArgs extends EventArgs {
|
|
|
687
968
|
export type ScheduledEventHandler = () => void | Promise<void>;
|
|
688
969
|
|
|
689
970
|
/**
|
|
690
|
-
* Configuration options for scheduled event handlers
|
|
691
|
-
* created via `every` or `cron`.
|
|
692
|
-
*/
|
|
971
|
+
* Configuration options for scheduled event handlers
|
|
972
|
+
* created via `every` or `cron`.
|
|
973
|
+
*/
|
|
693
974
|
export interface ScheduledEventArgs {
|
|
694
975
|
/**
|
|
695
976
|
* Adds or overrides existing tags used in dashboard
|
package/types/mail.d.ts
CHANGED
package/types/mqtt.d.ts
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* });
|
|
12
12
|
* }
|
|
13
13
|
*/
|
|
14
|
-
export function
|
|
14
|
+
export function publish(args?: PublishArgs): PublishResult;
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* Sends a single message to a MQTT topic asynchronously.
|
|
@@ -44,10 +44,14 @@ export interface PublishArgs {
|
|
|
44
44
|
cluster?: string;
|
|
45
45
|
|
|
46
46
|
/** MQTT topic name. If not specified, message will be written to a random topic. */
|
|
47
|
+
topic?: string
|
|
47
48
|
|
|
48
49
|
/** MQTT message value. If not specified, a random value will be generated based on the topic configuration. */
|
|
49
50
|
value: string;
|
|
50
51
|
|
|
52
|
+
/** Whether the message should be retained */
|
|
53
|
+
retain?: boolean
|
|
54
|
+
|
|
51
55
|
/**
|
|
52
56
|
* The retry option is used if script is executed before Kafka topic is set up.
|
|
53
57
|
*/
|
|
@@ -122,4 +126,4 @@ export interface ProduceRetry {
|
|
|
122
126
|
* @default 5
|
|
123
127
|
*/
|
|
124
128
|
retries: number;
|
|
125
|
-
}
|
|
129
|
+
}
|
package/types/mustache.d.ts
CHANGED
package/types/yaml.d.ts
CHANGED