go-mokapi 0.44.1 → 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 +371 -15
- package/types/kafka.d.ts +10 -6
- package/types/mail.d.ts +1 -1
- package/types/mqtt.d.ts +129 -0
- 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
|
@@ -12,6 +12,8 @@
|
|
|
12
12
|
import "./faker";
|
|
13
13
|
import "./global";
|
|
14
14
|
import "./http";
|
|
15
|
+
import "./kafka";
|
|
16
|
+
import "./mqtt";
|
|
15
17
|
import "./mustache";
|
|
16
18
|
import "./yaml";
|
|
17
19
|
import "./encoding";
|
|
@@ -115,8 +117,10 @@ export type Interval = string;
|
|
|
115
117
|
export interface EventHandler {
|
|
116
118
|
http: HttpEventHandler;
|
|
117
119
|
kafka: KafkaEventHandler;
|
|
120
|
+
mqtt: MqttEventHandler;
|
|
118
121
|
ldap: LdapEventHandler;
|
|
119
122
|
smtp: SmtpEventHandler;
|
|
123
|
+
websocket: WebsocketEventHandler;
|
|
120
124
|
}
|
|
121
125
|
|
|
122
126
|
/**
|
|
@@ -256,7 +260,7 @@ export interface Url {
|
|
|
256
260
|
* export default function() {
|
|
257
261
|
* on('kafka', function(msg) {
|
|
258
262
|
* // add header 'foo' to every Kafka message
|
|
259
|
-
*
|
|
263
|
+
* message.headers['correlationId'] = 'abc-123'
|
|
260
264
|
* })
|
|
261
265
|
* }
|
|
262
266
|
*/
|
|
@@ -267,17 +271,330 @@ export type KafkaEventHandler = (message: KafkaEventMessage) => void | Promise<v
|
|
|
267
271
|
* https://mokapi.io/docs/javascript-api/mokapi/eventhandler/KafkaEventMessage
|
|
268
272
|
*/
|
|
269
273
|
export interface KafkaEventMessage {
|
|
270
|
-
/**
|
|
274
|
+
/** The name of the API, matching the `info.title` field in the AsyncAPI specification */
|
|
275
|
+
readonly api: string;
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* The name of the Kafka topic where this message originates.
|
|
279
|
+
*/
|
|
280
|
+
readonly topic: string;
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* The partition index within the Kafka topic.
|
|
284
|
+
*/
|
|
285
|
+
readonly partition: string;
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* The unique sequential offset assigned to the message within its partition.
|
|
289
|
+
*/
|
|
271
290
|
readonly offset: number;
|
|
272
291
|
|
|
273
|
-
/**
|
|
292
|
+
/**
|
|
293
|
+
* The key of the Kafka message, used for partitioning and identification.
|
|
294
|
+
*/
|
|
274
295
|
key: string;
|
|
275
296
|
|
|
276
|
-
/** Kafka message value */
|
|
297
|
+
/** The Kafka message value */
|
|
277
298
|
value: string;
|
|
278
299
|
|
|
279
|
-
/** Kafka message headers */
|
|
300
|
+
/** Key-value pairs representing optional Kafka message headers. */
|
|
280
301
|
headers: { [name: string]: string } | null;
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* The identifier of the schema used to validate or serialize this message payload.
|
|
305
|
+
*
|
|
306
|
+
* @remarks
|
|
307
|
+
* * This property is **only set** automatically if the schema ID is embedded within the
|
|
308
|
+
* message payload itself (matching `SchemaIdLocation: "payload"`).
|
|
309
|
+
* * If the ID is transmitted via message metadata instead (matching `SchemaIdLocation: "header"`),
|
|
310
|
+
* it will **not** populate this field and must instead be extracted manually from the {@link headers} object.
|
|
311
|
+
*/
|
|
312
|
+
schemaId: number;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export type MqttEventHandler = (message: MqttEventMessage) => void | Promise<void>;
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* KafkaEventMessage is an object used by KafkaEventHandler that contains Kafka-specific message data.
|
|
319
|
+
* https://mokapi.io/docs/javascript-api/mokapi/eventhandler/KafkaEventMessage
|
|
320
|
+
*/
|
|
321
|
+
export interface MqttEventMessage {
|
|
322
|
+
/** The name of the API, matching the `info.title` field in the AsyncAPI specification */
|
|
323
|
+
readonly api: string;
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* The name of the MQTT topic where this message originates.
|
|
327
|
+
*/
|
|
328
|
+
readonly topic: string;
|
|
329
|
+
|
|
330
|
+
/** Specifies whether message should be retained */
|
|
331
|
+
retain: boolean;
|
|
332
|
+
|
|
333
|
+
/** MQTT message value */
|
|
334
|
+
value: string;
|
|
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[];
|
|
281
598
|
}
|
|
282
599
|
|
|
283
600
|
/**
|
|
@@ -517,6 +834,15 @@ export interface TypedEventArgs {
|
|
|
517
834
|
* Arguments for Kafka event handlers.
|
|
518
835
|
*/
|
|
519
836
|
kafka: KafkaEventArgs;
|
|
837
|
+
/**
|
|
838
|
+
* Arguments for MQTT event handlers.
|
|
839
|
+
*/
|
|
840
|
+
mqtt: MqttEventArgs;
|
|
841
|
+
|
|
842
|
+
/**
|
|
843
|
+
* Arguments for Websocket event handlers.
|
|
844
|
+
*/
|
|
845
|
+
websocket: WebsocketEventArgs
|
|
520
846
|
/**
|
|
521
847
|
* Arguments for LDAP event handlers.
|
|
522
848
|
*/
|
|
@@ -527,12 +853,6 @@ export interface TypedEventArgs {
|
|
|
527
853
|
smtp: SmtpEventArgs;
|
|
528
854
|
}
|
|
529
855
|
|
|
530
|
-
/**
|
|
531
|
-
* Configuration options for HTTP event handlers.
|
|
532
|
-
*
|
|
533
|
-
* These arguments control execution behavior such as
|
|
534
|
-
* priority, tagging, and dashboard tracking.
|
|
535
|
-
*/
|
|
536
856
|
export interface HttpEventArgs extends EventArgs {
|
|
537
857
|
/**
|
|
538
858
|
* Controls whether this event handler is tracked in the dashboard.
|
|
@@ -563,6 +883,42 @@ export interface KafkaEventArgs extends EventArgs {
|
|
|
563
883
|
track?: boolean | ((message: KafkaEventMessage) => boolean);
|
|
564
884
|
}
|
|
565
885
|
|
|
886
|
+
/**
|
|
887
|
+
* Configuration options for MQTT event handlers.
|
|
888
|
+
*
|
|
889
|
+
* These arguments control execution behavior such as
|
|
890
|
+
* priority, tagging, and dashboard tracking.
|
|
891
|
+
*/
|
|
892
|
+
export interface MqttEventArgs extends EventArgs {
|
|
893
|
+
/**
|
|
894
|
+
* Controls whether this event handler is tracked in the dashboard.
|
|
895
|
+
*
|
|
896
|
+
* - true: always track this handler
|
|
897
|
+
* - false: never track this handler
|
|
898
|
+
* - undefined: Mokapi determines tracking automatically based on
|
|
899
|
+
* whether the message was modified or acknowledged by the handler
|
|
900
|
+
*/
|
|
901
|
+
track?: boolean | ((message: MqttEventMessage) => boolean);
|
|
902
|
+
}
|
|
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
|
+
|
|
566
922
|
/**
|
|
567
923
|
* Configuration options for LDAP event handlers.
|
|
568
924
|
*
|
|
@@ -612,9 +968,9 @@ export interface SmtpEventArgs extends EventArgs {
|
|
|
612
968
|
export type ScheduledEventHandler = () => void | Promise<void>;
|
|
613
969
|
|
|
614
970
|
/**
|
|
615
|
-
* Configuration options for scheduled event handlers
|
|
616
|
-
* created via `every` or `cron`.
|
|
617
|
-
*/
|
|
971
|
+
* Configuration options for scheduled event handlers
|
|
972
|
+
* created via `every` or `cron`.
|
|
973
|
+
*/
|
|
618
974
|
export interface ScheduledEventArgs {
|
|
619
975
|
/**
|
|
620
976
|
* Adds or overrides existing tags used in dashboard
|
|
@@ -791,7 +1147,7 @@ export interface SharedMemory {
|
|
|
791
1147
|
*
|
|
792
1148
|
* // Retrieve the current counter value
|
|
793
1149
|
* const count = mokapi.shared.get("counter")
|
|
794
|
-
*
|
|
1150
|
+
* console.log(`Current counter: ${count}`)
|
|
795
1151
|
* ```
|
|
796
1152
|
*/
|
|
797
1153
|
export const shared: SharedMemory;
|
package/types/kafka.d.ts
CHANGED
|
@@ -165,32 +165,36 @@ export interface ProduceResult {
|
|
|
165
165
|
}
|
|
166
166
|
|
|
167
167
|
/**
|
|
168
|
-
*
|
|
168
|
+
* Represents the outcome of producing a Kafka message in Mokapi.
|
|
169
|
+
*
|
|
170
|
+
* Provides details about the target topic location, message payloads,
|
|
171
|
+
* and schema validation outcomes.
|
|
172
|
+
*
|
|
169
173
|
* https://mokapi.io/docs/javascript-api/mokapi-kafka/kafkamessageresult
|
|
170
174
|
*/
|
|
171
175
|
export interface MessageResult {
|
|
172
176
|
/**
|
|
173
|
-
*
|
|
177
|
+
* The zero-based index of the Kafka partition where the message was written.
|
|
174
178
|
*/
|
|
175
179
|
readonly partition: number;
|
|
176
180
|
|
|
177
181
|
/**
|
|
178
|
-
*
|
|
182
|
+
* The unique sequential identifier (offset) assigned to the message within its partition.
|
|
179
183
|
*/
|
|
180
184
|
readonly offset: number;
|
|
181
185
|
|
|
182
186
|
/**
|
|
183
|
-
*
|
|
187
|
+
* The key of the written Kafka message, typically used for partitioning.
|
|
184
188
|
*/
|
|
185
189
|
readonly key: string;
|
|
186
190
|
|
|
187
191
|
/**
|
|
188
|
-
*
|
|
192
|
+
* The main payload data of the written Kafka message.
|
|
189
193
|
*/
|
|
190
194
|
readonly value: string;
|
|
191
195
|
|
|
192
196
|
/**
|
|
193
|
-
*
|
|
197
|
+
* Key-value pairs representing the metadata headers attached to the Kafka message.
|
|
194
198
|
*/
|
|
195
199
|
readonly headers: { [name: string]: string };
|
|
196
200
|
}
|
package/types/mail.d.ts
CHANGED
package/types/mqtt.d.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sends a single message to a MQTT topic.
|
|
3
|
+
* https://mokapi.io/docs/javascript-api/mokapi-mqtt/publish
|
|
4
|
+
* @param args - PublishArgs object contains MQTT publish arguments.
|
|
5
|
+
* @returns The publish result.
|
|
6
|
+
* @example
|
|
7
|
+
* export default function() {
|
|
8
|
+
* publish({
|
|
9
|
+
* topic: 'foo',
|
|
10
|
+
* value: JSON.stringify({ foo: bar })
|
|
11
|
+
* });
|
|
12
|
+
* }
|
|
13
|
+
*/
|
|
14
|
+
export function publish(args?: PublishArgs): PublishResult;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Sends a single message to a MQTT topic asynchronously.
|
|
18
|
+
* https://mokapi.io/docs/javascript-api/mokapi-mqtt/publishAsync
|
|
19
|
+
* @param args - PublishArgs object contains MQTT publish arguments.
|
|
20
|
+
* @returns The publish result.
|
|
21
|
+
* @example
|
|
22
|
+
* export default function() {
|
|
23
|
+
* publish({
|
|
24
|
+
* topic: 'foo',
|
|
25
|
+
* value: JSON.stringify({ foo: bar })
|
|
26
|
+
* });
|
|
27
|
+
* }
|
|
28
|
+
*/
|
|
29
|
+
export function publishAsync(args?: PublishArgs): Promise<PublishResult>;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Contains publish-specific arguments.
|
|
33
|
+
* https://mokapi.io/docs/javascript-api/mokapi-mqtt/publishargs
|
|
34
|
+
* @example
|
|
35
|
+
* export default function() {
|
|
36
|
+
* const res = publish({
|
|
37
|
+
* topic: 'foo',
|
|
38
|
+
* value: `{"foo": "bar"}`
|
|
39
|
+
* });
|
|
40
|
+
* }
|
|
41
|
+
*/
|
|
42
|
+
export interface PublishArgs {
|
|
43
|
+
/** MQTT cluster name. Used when topic name is not unique. */
|
|
44
|
+
cluster?: string;
|
|
45
|
+
|
|
46
|
+
/** MQTT topic name. If not specified, message will be written to a random topic. */
|
|
47
|
+
topic?: string
|
|
48
|
+
|
|
49
|
+
/** MQTT message value. If not specified, a random value will be generated based on the topic configuration. */
|
|
50
|
+
value: string;
|
|
51
|
+
|
|
52
|
+
/** Whether the message should be retained */
|
|
53
|
+
retain?: boolean
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The retry option is used if script is executed before Kafka topic is set up.
|
|
57
|
+
*/
|
|
58
|
+
retry?: ProduceRetry;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Contains information of the written Kafka message.
|
|
63
|
+
* https://mokapi.io/docs/javascript-api/mokapi-kafka/produceresult
|
|
64
|
+
* @example
|
|
65
|
+
* export default function() {
|
|
66
|
+
* const res = produce({ topic: 'foo' })
|
|
67
|
+
* console.log(`new kafka message written with offset: ${res.offset}`)
|
|
68
|
+
* }
|
|
69
|
+
*/
|
|
70
|
+
export interface PublishResult {
|
|
71
|
+
/** Name of the Kafka cluster where the message was written. */
|
|
72
|
+
readonly cluster: string;
|
|
73
|
+
|
|
74
|
+
/** Kafka topic name where the message was written. */
|
|
75
|
+
readonly topic: string;
|
|
76
|
+
|
|
77
|
+
/** The value of the written message. */
|
|
78
|
+
readonly value: string;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The retry option can be used to customize the configuration of the retry mechanism.
|
|
83
|
+
*/
|
|
84
|
+
export interface ProduceRetry {
|
|
85
|
+
/**
|
|
86
|
+
* Maximum wait time for a retry
|
|
87
|
+
* MaxRetryTime number express the wait time in milliseconds.
|
|
88
|
+
* MaxRetryTime string is a possibly signed sequence of
|
|
89
|
+
* decimal numbers, each with optional fraction and a unit suffix,
|
|
90
|
+
* such as "300ms", "-1.5h" or "2h45m".
|
|
91
|
+
* Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
|
92
|
+
* @default 30000ms
|
|
93
|
+
* @example
|
|
94
|
+
* export default function() {
|
|
95
|
+
* produce({ topic: 'foo', messages: [{ value: 'value-1' }], retry: { maxRetryTime: '30s' } })
|
|
96
|
+
* }
|
|
97
|
+
*/
|
|
98
|
+
maxRetryTime: string | number;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Initial value used to calculate the wait time
|
|
102
|
+
* InitialRetryTime number express the wait time in milliseconds.
|
|
103
|
+
* InitialRetryTime string is a possibly signed sequence of
|
|
104
|
+
* decimal numbers, each with optional fraction and a unit suffix,
|
|
105
|
+
* such as "300ms", "-1.5h" or "2h45m".
|
|
106
|
+
* Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
|
107
|
+
* @default 200ms
|
|
108
|
+
* @example
|
|
109
|
+
* export default function() {
|
|
110
|
+
* produce({ topic: 'foo', messages: [{ value: 'value-1' }], retry: { initialRetryTime: '2s' } })
|
|
111
|
+
* }
|
|
112
|
+
*/
|
|
113
|
+
initialRetryTime: string | number;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Factor for increasing the wait time for next retry.
|
|
117
|
+
* 1st retry: 200ms
|
|
118
|
+
* 2nd retry: 4 * 200ms = 800ms
|
|
119
|
+
* 3th retry: 4 * 800ms = 3200ms
|
|
120
|
+
* @default 4
|
|
121
|
+
*/
|
|
122
|
+
factor: number;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Max number of retries
|
|
126
|
+
* @default 5
|
|
127
|
+
*/
|
|
128
|
+
retries: number;
|
|
129
|
+
}
|
package/types/mustache.d.ts
CHANGED
package/types/yaml.d.ts
CHANGED