go-mokapi 0.48.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 +83 -77
- 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 +461 -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
|
-
|
|
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.
|
|
146
|
+
|
|
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>
|
|
140
148
|
|
|
141
|
-
|
|
149
|
+
## Merch
|
|
150
|
+
|
|
151
|
+
[](https://mokapi.myspreadshop.com/)
|
|
142
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.50.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,440 @@ export interface MqttEventMessage {
|
|
|
333
334
|
value: string;
|
|
334
335
|
}
|
|
335
336
|
|
|
337
|
+
/**
|
|
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.
|
|
347
|
+
*
|
|
348
|
+
* @example
|
|
349
|
+
* // Handle all event types in one handler
|
|
350
|
+
* import { on } from 'mokapi'
|
|
351
|
+
* export default function() {
|
|
352
|
+
* on('websocket', function(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
|
+
* }
|
|
362
|
+
* })
|
|
363
|
+
* }
|
|
364
|
+
*
|
|
365
|
+
* @example
|
|
366
|
+
* // Async handler
|
|
367
|
+
* import { on } from 'mokapi'
|
|
368
|
+
* export default function() {
|
|
369
|
+
* on('websocket', async function(event) {
|
|
370
|
+
* if (event.type === 'message') {
|
|
371
|
+
* await someAsyncOperation()
|
|
372
|
+
* event.reply({ text: 'done' })
|
|
373
|
+
* }
|
|
374
|
+
* })
|
|
375
|
+
* }
|
|
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
|
+
*
|
|
387
|
+
* @example
|
|
388
|
+
* import { on } from 'mokapi'
|
|
389
|
+
* export default function() {
|
|
390
|
+
* on('websocket', function(event) {
|
|
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
|
+
* }
|
|
400
|
+
* })
|
|
401
|
+
* }
|
|
402
|
+
*
|
|
403
|
+
* @see https://mokapi.io/docs/javascript-api/mokapi/eventhandler/WebsocketEvent
|
|
404
|
+
*/
|
|
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
|
+
|
|
413
|
+
/**
|
|
414
|
+
* The name of the API, matching the `info.title` field in the AsyncAPI specification.
|
|
415
|
+
*
|
|
416
|
+
* @example
|
|
417
|
+
* on('websocket', function(event) {
|
|
418
|
+
* console.log(event.api) // e.g. "Chat API"
|
|
419
|
+
* })
|
|
420
|
+
*/
|
|
421
|
+
readonly api: string;
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* The channel on which the message was received.
|
|
425
|
+
*
|
|
426
|
+
* @example
|
|
427
|
+
* on('websocket', function(event) {
|
|
428
|
+
* console.log(event.channel.name) // e.g. "/chat"
|
|
429
|
+
* })
|
|
430
|
+
*/
|
|
431
|
+
readonly channel: WebsocketChannel;
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* The client that sent the message. Can be stored and used later
|
|
435
|
+
* to send messages outside the current event handler.
|
|
436
|
+
*
|
|
437
|
+
* @example
|
|
438
|
+
* on('websocket', function(event) {
|
|
439
|
+
* const userId = event.client.query['userId']
|
|
440
|
+
* const token = event.client.headers['authorization']
|
|
441
|
+
* })
|
|
442
|
+
*/
|
|
443
|
+
readonly client: WebsocketClient;
|
|
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
|
+
|
|
508
|
+
/**
|
|
509
|
+
* The decoded message payload. The type depends on the `contentType`
|
|
510
|
+
* defined in the AsyncAPI specification — typically an object for
|
|
511
|
+
* `application/json` or a string for `text/plain`.
|
|
512
|
+
*
|
|
513
|
+
* @example
|
|
514
|
+
* on('websocket', function(event) {
|
|
515
|
+
* console.log(event.message.text)
|
|
516
|
+
* })
|
|
517
|
+
*/
|
|
518
|
+
readonly message: any;
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* Sends a message back to the client that sent the current message.
|
|
522
|
+
* Shorthand for `event.client.send(message)`.
|
|
523
|
+
*
|
|
524
|
+
* The message is validated against the AsyncAPI specification before
|
|
525
|
+
* being sent. If validation fails, the error is recorded in the
|
|
526
|
+
* dashboard event log.
|
|
527
|
+
*
|
|
528
|
+
* @param message - The message payload to send. Will be encoded
|
|
529
|
+
* according to the channel's `contentType`.
|
|
530
|
+
*
|
|
531
|
+
* @example
|
|
532
|
+
* on('websocket', function(event) {
|
|
533
|
+
* event.reply({ text: 'got your message' })
|
|
534
|
+
* })
|
|
535
|
+
*/
|
|
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'
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* The reason the connection was closed. Maps to the WebSocket close frame
|
|
605
|
+
* reason string. Empty if no reason was provided.
|
|
606
|
+
*
|
|
607
|
+
* @example
|
|
608
|
+
* on('websocket', function(event) {
|
|
609
|
+
* if (event.type === 'close') {
|
|
610
|
+
* console.log(event.reason) // e.g. "going away"
|
|
611
|
+
* }
|
|
612
|
+
* })
|
|
613
|
+
*/
|
|
614
|
+
readonly reason: string
|
|
615
|
+
|
|
616
|
+
/**
|
|
617
|
+
* Indicates who initiated the close — either the client or the server.
|
|
618
|
+
*
|
|
619
|
+
* @example
|
|
620
|
+
* on('websocket', function(event) {
|
|
621
|
+
* if (event.type === 'close') {
|
|
622
|
+
* if (event.closedBy === 'client') {
|
|
623
|
+
* console.log('client disconnected')
|
|
624
|
+
* } else {
|
|
625
|
+
* console.log('server closed the connection')
|
|
626
|
+
* }
|
|
627
|
+
* }
|
|
628
|
+
* })
|
|
629
|
+
*/
|
|
630
|
+
readonly closedBy: 'client' | 'server'
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
/**
|
|
634
|
+
* Represents a WebSocket client connected to a channel.
|
|
635
|
+
* The client object is populated from the HTTP upgrade handshake,
|
|
636
|
+
* so `query` and `headers` reflect the values sent during the
|
|
637
|
+
* initial connection request.
|
|
638
|
+
*
|
|
639
|
+
* Client references to remain valid after the event handler returns,
|
|
640
|
+
* so they can be stored and used to push messages at any time
|
|
641
|
+
* while the connection is open.
|
|
642
|
+
*
|
|
643
|
+
* @example
|
|
644
|
+
* // Store clients on connect, send later from another event
|
|
645
|
+
* import { on } from 'mokapi'
|
|
646
|
+
* const clients: WebsocketClient[] = []
|
|
647
|
+
* export default function() {
|
|
648
|
+
* on('websocket', function(event) {
|
|
649
|
+
* if (event.message.type === 'join') {
|
|
650
|
+
* clients.push(event.client)
|
|
651
|
+
* }
|
|
652
|
+
* })
|
|
653
|
+
* }
|
|
654
|
+
*
|
|
655
|
+
* @see https://mokapi.io/docs/javascript-api/mokapi/eventhandler/WebsocketClient
|
|
656
|
+
*/
|
|
657
|
+
export interface WebsocketClient {
|
|
658
|
+
/**
|
|
659
|
+
* The remote address of the client in `host:port` format.
|
|
660
|
+
* Can be used to distinguish clients when no application-level
|
|
661
|
+
* identity is available.
|
|
662
|
+
*
|
|
663
|
+
* @example
|
|
664
|
+
* on('websocket', function(event) {
|
|
665
|
+
* console.log(event.client.remoteAddr) // e.g. "127.0.0.1:54321"
|
|
666
|
+
* })
|
|
667
|
+
*/
|
|
668
|
+
readonly remoteAddr: string;
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* Query parameters from the HTTP upgrade request, as defined
|
|
672
|
+
* in the channel's WebSocket binding (`bindings.ws.query`).
|
|
673
|
+
*
|
|
674
|
+
* @example
|
|
675
|
+
* // AsyncAPI binding:
|
|
676
|
+
* // bindings:
|
|
677
|
+
* // ws:
|
|
678
|
+
* // query:
|
|
679
|
+
* // properties:
|
|
680
|
+
* // userId:
|
|
681
|
+
* // type: string
|
|
682
|
+
*
|
|
683
|
+
* on('websocket', function(event) {
|
|
684
|
+
* const userId = event.client.query['userId']
|
|
685
|
+
* })
|
|
686
|
+
*/
|
|
687
|
+
readonly query: Record<string, string>;
|
|
688
|
+
|
|
689
|
+
/**
|
|
690
|
+
* HTTP headers from the upgrade request, as defined in the
|
|
691
|
+
* channel's WebSocket binding (`bindings.ws.headers`).
|
|
692
|
+
*
|
|
693
|
+
* @example
|
|
694
|
+
* // AsyncAPI binding:
|
|
695
|
+
* // bindings:
|
|
696
|
+
* // ws:
|
|
697
|
+
* // headers:
|
|
698
|
+
* // properties:
|
|
699
|
+
* // Authorization:
|
|
700
|
+
* // type: string
|
|
701
|
+
*
|
|
702
|
+
* on('websocket', function(event) {
|
|
703
|
+
* const token = event.client.headers['authorization']
|
|
704
|
+
* })
|
|
705
|
+
*/
|
|
706
|
+
readonly headers: Record<string, string>;
|
|
707
|
+
|
|
708
|
+
/**
|
|
709
|
+
* Sends a message to this specific client. The client reference
|
|
710
|
+
* can be stored and called outside of the event handler to push
|
|
711
|
+
* messages at any time while the connection is open.
|
|
712
|
+
*
|
|
713
|
+
* @param message - The message payload to send. Will be encoded
|
|
714
|
+
* according to the channel's `contentType`.
|
|
715
|
+
*
|
|
716
|
+
* @example
|
|
717
|
+
* // Immediate send (equivalent to event.reply)
|
|
718
|
+
* on('websocket', function(event) {
|
|
719
|
+
* event.client.send({ text: 'hello' })
|
|
720
|
+
* })
|
|
721
|
+
*
|
|
722
|
+
* @example
|
|
723
|
+
* // Deferred send after storing the client
|
|
724
|
+
* const clients: WebsocketClient[] = []
|
|
725
|
+
* on('websocket', function(event) {
|
|
726
|
+
* clients.push(event.client)
|
|
727
|
+
* })
|
|
728
|
+
* // later, from a timer or another event:
|
|
729
|
+
* clients.forEach(c => c.send({ text: 'server push' }))
|
|
730
|
+
*/
|
|
731
|
+
send(message: any): void;
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
/**
|
|
735
|
+
* Represents the WebSocket channel on which a message was received.
|
|
736
|
+
* Provides access to all currently connected clients, which is useful
|
|
737
|
+
* for targeted sends or building broadcast logic manually.
|
|
738
|
+
*
|
|
739
|
+
* @see https://mokapi.io/docs/javascript-api/mokapi/eventhandler/WebsocketChannel
|
|
740
|
+
*/
|
|
741
|
+
export interface WebsocketChannel {
|
|
742
|
+
/**
|
|
743
|
+
* The channel path, matching the channel address in the AsyncAPI
|
|
744
|
+
* specification.
|
|
745
|
+
*
|
|
746
|
+
* @example
|
|
747
|
+
* on('websocket', function(event) {
|
|
748
|
+
* console.log(event.channel.name) // e.g. "/chat"
|
|
749
|
+
* })
|
|
750
|
+
*/
|
|
751
|
+
readonly name: string;
|
|
752
|
+
|
|
753
|
+
/**
|
|
754
|
+
* All clients currently connected to this channel. Use this for
|
|
755
|
+
* custom fan-out logic beyond what `broadcast` provides — for
|
|
756
|
+
* example, filtering by query parameter or sending to a subset.
|
|
757
|
+
*
|
|
758
|
+
* @example
|
|
759
|
+
* // Send to all clients with a specific userId
|
|
760
|
+
* on('websocket', function(event) {
|
|
761
|
+
* for (const client of event.channel.clients) {
|
|
762
|
+
* if (client.query['room'] === event.message.room) {
|
|
763
|
+
* client.send(event.message)
|
|
764
|
+
* }
|
|
765
|
+
* }
|
|
766
|
+
* })
|
|
767
|
+
*/
|
|
768
|
+
readonly clients: readonly WebsocketClient[];
|
|
769
|
+
}
|
|
770
|
+
|
|
336
771
|
/**
|
|
337
772
|
* LdapEventHandler is a function that is executed when a LDAP search query is triggered.
|
|
338
773
|
* @example
|
|
@@ -574,6 +1009,11 @@ export interface TypedEventArgs {
|
|
|
574
1009
|
* Arguments for MQTT event handlers.
|
|
575
1010
|
*/
|
|
576
1011
|
mqtt: MqttEventArgs;
|
|
1012
|
+
|
|
1013
|
+
/**
|
|
1014
|
+
* Arguments for Websocket event handlers.
|
|
1015
|
+
*/
|
|
1016
|
+
websocket: WebsocketEventArgs
|
|
577
1017
|
/**
|
|
578
1018
|
* Arguments for LDAP event handlers.
|
|
579
1019
|
*/
|
|
@@ -584,12 +1024,6 @@ export interface TypedEventArgs {
|
|
|
584
1024
|
smtp: SmtpEventArgs;
|
|
585
1025
|
}
|
|
586
1026
|
|
|
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
1027
|
export interface HttpEventArgs extends EventArgs {
|
|
594
1028
|
/**
|
|
595
1029
|
* Controls whether this event handler is tracked in the dashboard.
|
|
@@ -638,6 +1072,24 @@ export interface MqttEventArgs extends EventArgs {
|
|
|
638
1072
|
track?: boolean | ((message: MqttEventMessage) => boolean);
|
|
639
1073
|
}
|
|
640
1074
|
|
|
1075
|
+
/**
|
|
1076
|
+
* Configuration options for Websocket event handlers.
|
|
1077
|
+
*
|
|
1078
|
+
* These arguments control execution behavior such as
|
|
1079
|
+
* priority, tagging, and dashboard tracking.
|
|
1080
|
+
*/
|
|
1081
|
+
export interface WebsocketEventArgs extends EventArgs {
|
|
1082
|
+
/**
|
|
1083
|
+
* Controls whether this event handler is tracked in the dashboard.
|
|
1084
|
+
*
|
|
1085
|
+
* - true: always track this handler
|
|
1086
|
+
* - false: never track this handler
|
|
1087
|
+
* - undefined: Mokapi determines tracking automatically based on
|
|
1088
|
+
* whether the message was modified or acknowledged by the handler
|
|
1089
|
+
*/
|
|
1090
|
+
track?: boolean | ((message: WebsocketEvent) => boolean);
|
|
1091
|
+
}
|
|
1092
|
+
|
|
641
1093
|
/**
|
|
642
1094
|
* Configuration options for LDAP event handlers.
|
|
643
1095
|
*
|
|
@@ -687,9 +1139,9 @@ export interface SmtpEventArgs extends EventArgs {
|
|
|
687
1139
|
export type ScheduledEventHandler = () => void | Promise<void>;
|
|
688
1140
|
|
|
689
1141
|
/**
|
|
690
|
-
* Configuration options for scheduled event handlers
|
|
691
|
-
* created via `every` or `cron`.
|
|
692
|
-
*/
|
|
1142
|
+
* Configuration options for scheduled event handlers
|
|
1143
|
+
* created via `every` or `cron`.
|
|
1144
|
+
*/
|
|
693
1145
|
export interface ScheduledEventArgs {
|
|
694
1146
|
/**
|
|
695
1147
|
* 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