go-mokapi 0.51.1 → 0.52.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/package.json +1 -1
- package/types/index.d.ts +1109 -10
package/package.json
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -9,16 +9,7 @@
|
|
|
9
9
|
* https://mokapi.io/docs/javascript-api/overview
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import "./
|
|
13
|
-
import "./global";
|
|
14
|
-
import "./http";
|
|
15
|
-
import "./kafka";
|
|
16
|
-
import "./mqtt";
|
|
17
|
-
import "./mustache";
|
|
18
|
-
import "./yaml";
|
|
19
|
-
import "./encoding";
|
|
20
|
-
import "./mail";
|
|
21
|
-
import "./file";
|
|
12
|
+
import {ProduceRetry} from "./mqtt";
|
|
22
13
|
|
|
23
14
|
/**
|
|
24
15
|
* Attaches an event handler for the given event.
|
|
@@ -1472,6 +1463,69 @@ export interface App {
|
|
|
1472
1463
|
* }
|
|
1473
1464
|
*/
|
|
1474
1465
|
http(): HttpRouter
|
|
1466
|
+
|
|
1467
|
+
/**
|
|
1468
|
+
* Returns a `KafkaRouter` not bound to any specific API.
|
|
1469
|
+
* Handlers and producers registered here will run for all Kafka APIs
|
|
1470
|
+
* that match the given topic name.
|
|
1471
|
+
*
|
|
1472
|
+
* Use this for cross-cutting concerns such as adding headers to all
|
|
1473
|
+
* messages, logging, or producing to a topic when only one Kafka API
|
|
1474
|
+
* is configured.
|
|
1475
|
+
*
|
|
1476
|
+
* Use `app.api('My API').kafka()` instead when multiple Kafka APIs
|
|
1477
|
+
* share the same topic name, to avoid unintended matches.
|
|
1478
|
+
*
|
|
1479
|
+
* @returns A `KafkaRouter` with no API scope.
|
|
1480
|
+
*
|
|
1481
|
+
* @example
|
|
1482
|
+
* // Add a correlation ID header to all incoming messages across all topics
|
|
1483
|
+
* import { app } from 'mokapi'
|
|
1484
|
+
* export default function() {
|
|
1485
|
+
* app.kafka().message((msg) => {
|
|
1486
|
+
* msg.headers = { ...msg.headers, correlationId: 'abc-123' }
|
|
1487
|
+
* })
|
|
1488
|
+
* }
|
|
1489
|
+
*
|
|
1490
|
+
* @example
|
|
1491
|
+
* // Produce a message without API scope
|
|
1492
|
+
* import { app } from 'mokapi'
|
|
1493
|
+
* export default function() {
|
|
1494
|
+
* app.kafka().produce('order-placed', { data: { id: 1 } })
|
|
1495
|
+
* }
|
|
1496
|
+
*/
|
|
1497
|
+
kafka(): KafkaRouter
|
|
1498
|
+
|
|
1499
|
+
/**
|
|
1500
|
+
* Returns an `MqttRouter` not bound to any specific API.
|
|
1501
|
+
* Handlers and publishers registered here will run for all MQTT APIs
|
|
1502
|
+
* that match the given topic name.
|
|
1503
|
+
*
|
|
1504
|
+
* Use this for cross-cutting concerns such as logging incoming messages
|
|
1505
|
+
* or publishing to a topic when only one MQTT API is configured.
|
|
1506
|
+
*
|
|
1507
|
+
* Use `app.api('My API').mqtt()` instead when multiple MQTT APIs
|
|
1508
|
+
* share the same topic name, to avoid unintended matches.
|
|
1509
|
+
*
|
|
1510
|
+
* @returns An `MqttRouter` with no API scope.
|
|
1511
|
+
*
|
|
1512
|
+
* @example
|
|
1513
|
+
* // Handle all incoming MQTT messages across all topics
|
|
1514
|
+
* import { app } from 'mokapi'
|
|
1515
|
+
* export default function() {
|
|
1516
|
+
* app.mqtt().message((msg) => {
|
|
1517
|
+
* console.log(`[${msg.topic}] ${msg.value}`)
|
|
1518
|
+
* })
|
|
1519
|
+
* }
|
|
1520
|
+
*
|
|
1521
|
+
* @example
|
|
1522
|
+
* // Publish a message without API scope
|
|
1523
|
+
* import { app } from 'mokapi'
|
|
1524
|
+
* export default function() {
|
|
1525
|
+
* app.mqtt().publish({ value: 'hello' })
|
|
1526
|
+
* }
|
|
1527
|
+
*/
|
|
1528
|
+
mqtt(): MqttRouter
|
|
1475
1529
|
}
|
|
1476
1530
|
|
|
1477
1531
|
/**
|
|
@@ -1507,6 +1561,90 @@ export interface ApiScope {
|
|
|
1507
1561
|
* }
|
|
1508
1562
|
*/
|
|
1509
1563
|
http(): HttpRouter
|
|
1564
|
+
|
|
1565
|
+
/**
|
|
1566
|
+
* Returns a `KafkaRouter` scoped to this API.
|
|
1567
|
+
*
|
|
1568
|
+
* Handlers and producers registered on this router will only run for
|
|
1569
|
+
* Kafka messages that belong to the API identified by this scope's title,
|
|
1570
|
+
* as defined in the AsyncAPI `info.title` field.
|
|
1571
|
+
*
|
|
1572
|
+
* Scoping is required when multiple Kafka APIs define the same topic name,
|
|
1573
|
+
* to avoid a handler or producer running for unintended APIs.
|
|
1574
|
+
*
|
|
1575
|
+
* @returns A `KafkaRouter` bound to this API scope.
|
|
1576
|
+
*
|
|
1577
|
+
* @example
|
|
1578
|
+
* // Handle messages scoped to a specific Kafka API
|
|
1579
|
+
* import { app } from 'mokapi'
|
|
1580
|
+
* export default function() {
|
|
1581
|
+
* app.api('Order Events').kafka()
|
|
1582
|
+
* .topic('order-placed')
|
|
1583
|
+
* .message((msg) => {
|
|
1584
|
+
* msg.headers = { ...msg.headers, processed: 'true' }
|
|
1585
|
+
* })
|
|
1586
|
+
* }
|
|
1587
|
+
*
|
|
1588
|
+
* @example
|
|
1589
|
+
* // Produce scoped to a specific Kafka API
|
|
1590
|
+
* import { app } from 'mokapi'
|
|
1591
|
+
* export default function() {
|
|
1592
|
+
* app.api('Order Events').kafka()
|
|
1593
|
+
* .produce('order-placed', { data: { id: 1 } })
|
|
1594
|
+
* }
|
|
1595
|
+
*
|
|
1596
|
+
* @example
|
|
1597
|
+
* // Two APIs with the same topic name — scoping avoids conflicts
|
|
1598
|
+
* import { app } from 'mokapi'
|
|
1599
|
+
* export default function() {
|
|
1600
|
+
* app.api('Order Events').kafka()
|
|
1601
|
+
* .topic('events')
|
|
1602
|
+
* .message((msg) => { msg.value = { ...msg.value, source: 'orders' } })
|
|
1603
|
+
*
|
|
1604
|
+
* app.api('Inventory Events').kafka()
|
|
1605
|
+
* .topic('events')
|
|
1606
|
+
* .message((msg) => { msg.value = { ...msg.value, source: 'inventory' } })
|
|
1607
|
+
* }
|
|
1608
|
+
*/
|
|
1609
|
+
kafka(): KafkaRouter
|
|
1610
|
+
|
|
1611
|
+
/**
|
|
1612
|
+
* Returns an `MqttRouter` scoped to this API.
|
|
1613
|
+
*
|
|
1614
|
+
* Handlers and publishers registered on this router will only run for
|
|
1615
|
+
* MQTT messages that belong to the API identified by this scope's title,
|
|
1616
|
+
* as defined in the AsyncAPI `info.title` field.
|
|
1617
|
+
*
|
|
1618
|
+
* Scoping is required when multiple MQTT APIs define the same topic name,
|
|
1619
|
+
* to avoid a handler or publisher running for unintended APIs.
|
|
1620
|
+
*
|
|
1621
|
+
* @returns An `MqttRouter` bound to this API scope.
|
|
1622
|
+
*
|
|
1623
|
+
* @example
|
|
1624
|
+
* // Handle messages scoped to a specific MQTT API
|
|
1625
|
+
* import { app } from 'mokapi'
|
|
1626
|
+
* export default function() {
|
|
1627
|
+
* app.api('Smart Home').mqtt()
|
|
1628
|
+
* .topic('sensors/temperature')
|
|
1629
|
+
* .message((msg) => {
|
|
1630
|
+
* console.log(`Temperature: ${msg.value}`)
|
|
1631
|
+
* })
|
|
1632
|
+
* }
|
|
1633
|
+
*
|
|
1634
|
+
* @example
|
|
1635
|
+
* // Two APIs with the same topic name — scoping avoids conflicts
|
|
1636
|
+
* import { app } from 'mokapi'
|
|
1637
|
+
* export default function() {
|
|
1638
|
+
* app.api('Smart Home').mqtt()
|
|
1639
|
+
* .topic('sensors/temperature')
|
|
1640
|
+
* .message((msg) => { console.log(`Home: ${msg.value}`) })
|
|
1641
|
+
*
|
|
1642
|
+
* app.api('Factory Floor').mqtt()
|
|
1643
|
+
* .topic('sensors/temperature')
|
|
1644
|
+
* .message((msg) => { console.log(`Factory: ${msg.value}`) })
|
|
1645
|
+
* }
|
|
1646
|
+
*/
|
|
1647
|
+
mqtt(): MqttRouter
|
|
1510
1648
|
}
|
|
1511
1649
|
|
|
1512
1650
|
/**
|
|
@@ -1895,4 +2033,965 @@ export interface HttpRoute {
|
|
|
1895
2033
|
* router.route('/cache').purge((req, res) => { res.statusCode = 200 })
|
|
1896
2034
|
*/
|
|
1897
2035
|
[method: string]: any
|
|
2036
|
+
}
|
|
2037
|
+
|
|
2038
|
+
/**
|
|
2039
|
+
* Registers and organizes Kafka message handlers and producers by topic.
|
|
2040
|
+
*
|
|
2041
|
+
* Obtain a `KafkaRouter` via `app.kafka()` or `app.api('My API').kafka()`.
|
|
2042
|
+
*
|
|
2043
|
+
* Two styles are supported:
|
|
2044
|
+
* - **Shorthand**: `router.produce(topic, message)` — produces directly to a named topic.
|
|
2045
|
+
* - **Scoped**: `router.topic(name).produce(message)` — groups handlers and producers under one topic.
|
|
2046
|
+
*
|
|
2047
|
+
* When multiple APIs share the same topic name, use `app.api()` to scope the router
|
|
2048
|
+
* to a specific API title to avoid unintended matches.
|
|
2049
|
+
*
|
|
2050
|
+
* @example
|
|
2051
|
+
* // Shorthand style
|
|
2052
|
+
* import { app } from 'mokapi'
|
|
2053
|
+
* export default function() {
|
|
2054
|
+
* const router = app.api('Order Events').kafka()
|
|
2055
|
+
* router.produce('order-placed', { data: { id: 1 } })
|
|
2056
|
+
* router.message((msg) => { console.log(msg.value) })
|
|
2057
|
+
* }
|
|
2058
|
+
*
|
|
2059
|
+
* @example
|
|
2060
|
+
* // Scoped style
|
|
2061
|
+
* import { app } from 'mokapi'
|
|
2062
|
+
* export default function() {
|
|
2063
|
+
* app.api('Order Events').kafka()
|
|
2064
|
+
* .topic('order-placed')
|
|
2065
|
+
* .message((msg) => { msg.value = { ...msg.value, processed: true } })
|
|
2066
|
+
* .produce({ data: { id: 1 } })
|
|
2067
|
+
* }
|
|
2068
|
+
*/
|
|
2069
|
+
export interface KafkaRouter {
|
|
2070
|
+
/**
|
|
2071
|
+
* Returns a `KafkaTopic` scoped to the given topic name.
|
|
2072
|
+
*
|
|
2073
|
+
* Use this to register message handlers or produce messages for a specific topic
|
|
2074
|
+
* using method chaining, without repeating the topic name on every call.
|
|
2075
|
+
*
|
|
2076
|
+
* @param name The Kafka topic name as defined in the AsyncAPI specification.
|
|
2077
|
+
* @returns A `KafkaTopic` scoped to the given topic name.
|
|
2078
|
+
*
|
|
2079
|
+
* @example
|
|
2080
|
+
* import { app } from 'mokapi'
|
|
2081
|
+
* export default function() {
|
|
2082
|
+
* app.api('Order Events').kafka()
|
|
2083
|
+
* .topic('order-placed')
|
|
2084
|
+
* .message((msg) => { console.log(msg.value) })
|
|
2085
|
+
* .produce({ data: { id: 1 } })
|
|
2086
|
+
* }
|
|
2087
|
+
*/
|
|
2088
|
+
topic(name: string): KafkaTopic
|
|
2089
|
+
|
|
2090
|
+
/**
|
|
2091
|
+
* Registers a handler for incoming messages across all topics.
|
|
2092
|
+
*
|
|
2093
|
+
* The handler can read and modify the message before it is processed.
|
|
2094
|
+
* Use `topic()` to scope the handler to a specific topic.
|
|
2095
|
+
*
|
|
2096
|
+
* @param handler The event handler to invoke for each incoming message.
|
|
2097
|
+
* @param opts Optional handler configuration such as priority and tracking.
|
|
2098
|
+
* @returns This `KafkaRouter` instance for chaining.
|
|
2099
|
+
*
|
|
2100
|
+
* @example
|
|
2101
|
+
* import { app } from 'mokapi'
|
|
2102
|
+
* export default function() {
|
|
2103
|
+
* app.kafka().message((msg) => {
|
|
2104
|
+
* // Add a correlation ID to every message across all topics
|
|
2105
|
+
* msg.headers = { ...msg.headers, correlationId: 'abc-123' }
|
|
2106
|
+
* })
|
|
2107
|
+
* }
|
|
2108
|
+
*/
|
|
2109
|
+
message(handler: KafkaEventHandler, opts?: KafkaEventArgs): KafkaRouter
|
|
2110
|
+
|
|
2111
|
+
/**
|
|
2112
|
+
* Produces one or more messages to the given Kafka topic synchronously.
|
|
2113
|
+
*
|
|
2114
|
+
* If neither `value` nor `data` is set on a message, Mokapi generates
|
|
2115
|
+
* a random valid value based on the AsyncAPI schema definition.
|
|
2116
|
+
*
|
|
2117
|
+
* Use `topic()` to scope the produce to a specific topic without
|
|
2118
|
+
* repeating the topic name.
|
|
2119
|
+
*
|
|
2120
|
+
* Use `produceAsync()` if you need to await the result.
|
|
2121
|
+
*
|
|
2122
|
+
* @param topic The Kafka topic name to produce to.
|
|
2123
|
+
* @param message One or more messages to produce.
|
|
2124
|
+
* @param opts Optional configuration including a result callback and retry settings.
|
|
2125
|
+
* @returns This `KafkaRouter` instance for chaining.
|
|
2126
|
+
*
|
|
2127
|
+
* @example
|
|
2128
|
+
* // Produce a single message with explicit data
|
|
2129
|
+
* app.kafka().produce('order-placed', { data: { id: 1 } })
|
|
2130
|
+
*
|
|
2131
|
+
* @example
|
|
2132
|
+
* // Produce a message with a result callback
|
|
2133
|
+
* app.kafka().produce('order-placed', { data: { id: 1 } }, {
|
|
2134
|
+
* result: (r) => console.log(r.messages[0].offset)
|
|
2135
|
+
* })
|
|
2136
|
+
*
|
|
2137
|
+
* @example
|
|
2138
|
+
* // Produce multiple messages
|
|
2139
|
+
* app.kafka().produce('order-placed', [
|
|
2140
|
+
* { data: { id: 1 } },
|
|
2141
|
+
* { data: { id: 2 } },
|
|
2142
|
+
* {} // random generated value
|
|
2143
|
+
* ])
|
|
2144
|
+
*/
|
|
2145
|
+
produce(topic: string, message: KafkaProduceMessage | KafkaProduceMessage[], opts?: KafkaProduceArgs): KafkaRouter
|
|
2146
|
+
|
|
2147
|
+
/**
|
|
2148
|
+
* Produces one or more messages to the given Kafka topic asynchronously.
|
|
2149
|
+
*
|
|
2150
|
+
* Use this when you need to await the produce result, for example to
|
|
2151
|
+
* read the assigned offset before continuing.
|
|
2152
|
+
*
|
|
2153
|
+
* If neither `value` nor `data` is set on a message, Mokapi generates
|
|
2154
|
+
* a random valid value based on the AsyncAPI schema definition.
|
|
2155
|
+
*
|
|
2156
|
+
* @param topic The Kafka topic name to produce to.
|
|
2157
|
+
* @param message One or more messages to produce.
|
|
2158
|
+
* @param opts Optional configuration including a result callback and retry settings.
|
|
2159
|
+
* @returns A `Promise` resolving to this `KafkaRouter` instance for chaining.
|
|
2160
|
+
*
|
|
2161
|
+
* @example
|
|
2162
|
+
* import { app } from 'mokapi'
|
|
2163
|
+
* export default async function() {
|
|
2164
|
+
* await app.kafka().produceAsync('order-placed', { data: { id: 1 } }, {
|
|
2165
|
+
* result: (r) => console.log(r.messages[0].offset)
|
|
2166
|
+
* })
|
|
2167
|
+
* }
|
|
2168
|
+
*/
|
|
2169
|
+
produceAsync(topic: string, message: KafkaProduceMessage | KafkaProduceMessage[], opts?: KafkaProduceArgs): Promise<KafkaRouter>
|
|
2170
|
+
}
|
|
2171
|
+
|
|
2172
|
+
/**
|
|
2173
|
+
* Represents a single Kafka topic within a `KafkaRouter`,
|
|
2174
|
+
* allowing message handlers and producers to be registered
|
|
2175
|
+
* via method chaining.
|
|
2176
|
+
*
|
|
2177
|
+
* Obtain a `KafkaTopic` via `router.topic(name)`.
|
|
2178
|
+
*
|
|
2179
|
+
* All methods return `this`, enabling fluent chaining:
|
|
2180
|
+
* ```ts
|
|
2181
|
+
* router.topic('order-placed')
|
|
2182
|
+
* .message(handler)
|
|
2183
|
+
* .produce({ data: { id: 1 } })
|
|
2184
|
+
* ```
|
|
2185
|
+
*
|
|
2186
|
+
* @example
|
|
2187
|
+
* import { app } from 'mokapi'
|
|
2188
|
+
* export default function() {
|
|
2189
|
+
* app.api('Order Events').kafka()
|
|
2190
|
+
* .topic('order-placed')
|
|
2191
|
+
* .message((msg) => {
|
|
2192
|
+
* // Modify incoming messages for this topic
|
|
2193
|
+
* msg.headers = { ...msg.headers, processed: 'true' }
|
|
2194
|
+
* })
|
|
2195
|
+
* .produce({ data: { id: 1 } })
|
|
2196
|
+
* }
|
|
2197
|
+
*/
|
|
2198
|
+
export interface KafkaTopic {
|
|
2199
|
+
|
|
2200
|
+
/**
|
|
2201
|
+
* Registers a handler for incoming messages on this topic.
|
|
2202
|
+
*
|
|
2203
|
+
* The handler can read and modify the message before it is processed.
|
|
2204
|
+
* Multiple handlers can be registered and run in registration order.
|
|
2205
|
+
*
|
|
2206
|
+
* @param handler The event handler to invoke for each incoming message.
|
|
2207
|
+
* @param opts Optional handler configuration such as priority and tracking.
|
|
2208
|
+
* @returns This `KafkaTopic` instance for chaining.
|
|
2209
|
+
*
|
|
2210
|
+
* @example
|
|
2211
|
+
* app.api('Order Events').kafka()
|
|
2212
|
+
* .topic('order-placed')
|
|
2213
|
+
* .message((msg) => {
|
|
2214
|
+
* msg.headers = { ...msg.headers, correlationId: 'abc-123' }
|
|
2215
|
+
* })
|
|
2216
|
+
*/
|
|
2217
|
+
message(handler: KafkaEventHandler, opts?: KafkaEventArgs): KafkaTopic
|
|
2218
|
+
|
|
2219
|
+
/**
|
|
2220
|
+
* Produces one or more messages to this topic synchronously.
|
|
2221
|
+
*
|
|
2222
|
+
* If neither `value` nor `data` is set on a message, Mokapi generates
|
|
2223
|
+
* a random valid value based on the AsyncAPI schema definition.
|
|
2224
|
+
*
|
|
2225
|
+
* Use `produceAsync()` if you need to await the result.
|
|
2226
|
+
*
|
|
2227
|
+
* @param message One or more messages to produce.
|
|
2228
|
+
* @param opts Optional configuration including a result callback and retry settings.
|
|
2229
|
+
* @returns This `KafkaTopic` instance for chaining.
|
|
2230
|
+
*
|
|
2231
|
+
* @example
|
|
2232
|
+
* app.api('Order Events').kafka()
|
|
2233
|
+
* .topic('order-placed')
|
|
2234
|
+
* .produce({ data: { id: 1 } })
|
|
2235
|
+
*
|
|
2236
|
+
* @example
|
|
2237
|
+
* // Produce with result callback
|
|
2238
|
+
* app.api('Order Events').kafka()
|
|
2239
|
+
* .topic('order-placed')
|
|
2240
|
+
* .produce({ data: { id: 1 } }, {
|
|
2241
|
+
* result: (r) => console.log(r.messages[0].offset)
|
|
2242
|
+
* })
|
|
2243
|
+
*/
|
|
2244
|
+
produce(message: KafkaProduceMessage | KafkaProduceMessage[], opts?: KafkaProduceArgs): KafkaTopic
|
|
2245
|
+
|
|
2246
|
+
/**
|
|
2247
|
+
* Produces one or more messages to this topic asynchronously.
|
|
2248
|
+
*
|
|
2249
|
+
* Use this when you need to await the produce result, for example to
|
|
2250
|
+
* read the assigned offset before continuing.
|
|
2251
|
+
*
|
|
2252
|
+
* If neither `value` nor `data` is set on a message, Mokapi generates
|
|
2253
|
+
* a random valid value based on the AsyncAPI schema definition.
|
|
2254
|
+
*
|
|
2255
|
+
* @param message One or more messages to produce.
|
|
2256
|
+
* @param opts Optional configuration including a result callback and retry settings.
|
|
2257
|
+
* @returns A `Promise` resolving to this `KafkaTopic` instance for chaining.
|
|
2258
|
+
*
|
|
2259
|
+
* @example
|
|
2260
|
+
* import { app } from 'mokapi'
|
|
2261
|
+
* export default async function() {
|
|
2262
|
+
* await app.api('Order Events').kafka()
|
|
2263
|
+
* .topic('order-placed')
|
|
2264
|
+
* .produceAsync({ data: { id: 1 } }, {
|
|
2265
|
+
* result: (r) => console.log(r.messages[0].offset)
|
|
2266
|
+
* })
|
|
2267
|
+
* }
|
|
2268
|
+
*/
|
|
2269
|
+
produceAsync(message: KafkaProduceMessage | KafkaProduceMessage[], opts?: KafkaProduceArgs): Promise<KafkaTopic>
|
|
2270
|
+
}
|
|
2271
|
+
|
|
2272
|
+
/**
|
|
2273
|
+
* Represents a single Kafka message to be produced via `produce()` or `produceAsync()`.
|
|
2274
|
+
*
|
|
2275
|
+
* At least one of `data` or `value` should be set. If neither is provided,
|
|
2276
|
+
* Mokapi generates a random valid value based on the AsyncAPI schema definition.
|
|
2277
|
+
*
|
|
2278
|
+
* Use `data` when the topic has a schema defined — Mokapi handles validation
|
|
2279
|
+
* and serialization. Use `value` when you need full control over the raw bytes
|
|
2280
|
+
* written to Kafka.
|
|
2281
|
+
*
|
|
2282
|
+
* @example
|
|
2283
|
+
* // Schema-based encoding via data
|
|
2284
|
+
* app.kafka().produce('order-placed', {
|
|
2285
|
+
* key: 'order-1',
|
|
2286
|
+
* data: { id: 1, item: 'Book' }
|
|
2287
|
+
* })
|
|
2288
|
+
*
|
|
2289
|
+
* @example
|
|
2290
|
+
* // Raw value — written as-is
|
|
2291
|
+
* app.kafka().produce('order-placed', {
|
|
2292
|
+
* key: 'order-1',
|
|
2293
|
+
* value: 'raw-string-payload'
|
|
2294
|
+
* })
|
|
2295
|
+
*
|
|
2296
|
+
* @example
|
|
2297
|
+
* // Random generated value — neither data nor value set
|
|
2298
|
+
* app.kafka().produce('order-placed', {})
|
|
2299
|
+
*
|
|
2300
|
+
* @example
|
|
2301
|
+
* // With headers and explicit partition
|
|
2302
|
+
* app.kafka().produce('order-placed', {
|
|
2303
|
+
* partition: 0,
|
|
2304
|
+
* key: 'order-1',
|
|
2305
|
+
* data: { id: 1 },
|
|
2306
|
+
* headers: { correlationId: 'abc-123' }
|
|
2307
|
+
* })
|
|
2308
|
+
*/
|
|
2309
|
+
export interface KafkaProduceMessage {
|
|
2310
|
+
/**
|
|
2311
|
+
* The zero-based partition index to write the message to.
|
|
2312
|
+
* If not specified, Mokapi selects a partition automatically
|
|
2313
|
+
* based on the message key or topic configuration.
|
|
2314
|
+
*/
|
|
2315
|
+
partition?: number;
|
|
2316
|
+
|
|
2317
|
+
/**
|
|
2318
|
+
* The message key used for partitioning and identification.
|
|
2319
|
+
*
|
|
2320
|
+
* If not specified, Mokapi generates a random valid key
|
|
2321
|
+
* based on the topic schema defined in the AsyncAPI specification.
|
|
2322
|
+
*
|
|
2323
|
+
* @example
|
|
2324
|
+
* app.kafka().produce('order-placed', { key: 'order-1', data: { id: 1 } })
|
|
2325
|
+
*/
|
|
2326
|
+
key?: any;
|
|
2327
|
+
|
|
2328
|
+
/**
|
|
2329
|
+
* Structured data to be encoded and validated according to the
|
|
2330
|
+
* AsyncAPI schema definition for this topic.
|
|
2331
|
+
*
|
|
2332
|
+
* Mokapi serializes this value based on the topic's content type
|
|
2333
|
+
* (e.g. `application/json`) and validates it against the schema
|
|
2334
|
+
* before writing to Kafka.
|
|
2335
|
+
*
|
|
2336
|
+
* If neither `data` nor `value` is set, Mokapi generates a random
|
|
2337
|
+
* valid value based on the AsyncAPI schema definition.
|
|
2338
|
+
*
|
|
2339
|
+
* Prefer `data` over `value` when the topic has a schema defined,
|
|
2340
|
+
* as it ensures the message is valid according to your AsyncAPI contract.
|
|
2341
|
+
*
|
|
2342
|
+
* @example
|
|
2343
|
+
* app.kafka().produce('order-placed', {
|
|
2344
|
+
* data: { id: 1, item: 'Book', quantity: 2 }
|
|
2345
|
+
* })
|
|
2346
|
+
*/
|
|
2347
|
+
data?: any;
|
|
2348
|
+
|
|
2349
|
+
/**
|
|
2350
|
+
* Raw value written directly to Kafka without schema validation or encoding.
|
|
2351
|
+
*
|
|
2352
|
+
* Primitives (`string`, `number`, `boolean`, `null`) and `ArrayBuffer`
|
|
2353
|
+
* are written as-is. Objects are JSON-serialized at runtime.
|
|
2354
|
+
*
|
|
2355
|
+
* If neither `value` nor `data` is set, Mokapi generates a random valid
|
|
2356
|
+
* value based on the AsyncAPI schema definition.
|
|
2357
|
+
*
|
|
2358
|
+
* Use `data` instead if you want schema-based encoding and validation.
|
|
2359
|
+
* Use `value` when you need full control over the raw bytes written to Kafka,
|
|
2360
|
+
* for example when testing consumers that expect a specific binary format.
|
|
2361
|
+
*
|
|
2362
|
+
* @example
|
|
2363
|
+
* // Raw string
|
|
2364
|
+
* app.kafka().produce('order-placed', { value: 'raw-payload' })
|
|
2365
|
+
*
|
|
2366
|
+
* @example
|
|
2367
|
+
* // Pre-encoded binary via ArrayBuffer
|
|
2368
|
+
* app.kafka().produce('order-placed', { value: myArrayBuffer })
|
|
2369
|
+
*/
|
|
2370
|
+
value?: any;
|
|
2371
|
+
|
|
2372
|
+
/**
|
|
2373
|
+
* Key-value pairs attached to the Kafka message as metadata headers.
|
|
2374
|
+
*
|
|
2375
|
+
* Headers are forwarded as-is and are not validated against the
|
|
2376
|
+
* AsyncAPI specification. Use them for cross-cutting concerns such
|
|
2377
|
+
* as correlation IDs, tracing, or content type hints.
|
|
2378
|
+
*
|
|
2379
|
+
* @example
|
|
2380
|
+
* app.kafka().produce('order-placed', {
|
|
2381
|
+
* data: { id: 1 },
|
|
2382
|
+
* headers: { correlationId: 'abc-123', source: 'mokapi' }
|
|
2383
|
+
* })
|
|
2384
|
+
*/
|
|
2385
|
+
headers?: Record<string, any>;
|
|
2386
|
+
}
|
|
2387
|
+
|
|
2388
|
+
/**
|
|
2389
|
+
* Optional configuration for `produce()` and `produceAsync()`.
|
|
2390
|
+
*
|
|
2391
|
+
* Use `result` to inspect the outcome of a produce operation such as
|
|
2392
|
+
* the assigned partition and offset. Use `retry` to configure retry
|
|
2393
|
+
* behavior when the Kafka topic is not yet available.
|
|
2394
|
+
*
|
|
2395
|
+
* @example
|
|
2396
|
+
* // Read the assigned offset after producing
|
|
2397
|
+
* app.kafka().produce('order-placed', { data: { id: 1 } }, {
|
|
2398
|
+
* result: (r) => console.log(`Written to offset ${r.messages[0].offset}`)
|
|
2399
|
+
* })
|
|
2400
|
+
*
|
|
2401
|
+
* @example
|
|
2402
|
+
* // Retry up to 10 times with a 5 second initial wait
|
|
2403
|
+
* app.kafka().produce('order-placed', { data: { id: 1 } }, {
|
|
2404
|
+
* retry: { retries: 10, initialRetryTime: '5s' }
|
|
2405
|
+
* })
|
|
2406
|
+
*/
|
|
2407
|
+
export interface KafkaProduceArgs {
|
|
2408
|
+
/**
|
|
2409
|
+
* Callback invoked with the produce result after the message has been
|
|
2410
|
+
* successfully written to Kafka.
|
|
2411
|
+
*
|
|
2412
|
+
* Use this to read the assigned partition, offset, key, or value of
|
|
2413
|
+
* each produced message. The callback receives a single `KafkaProduceResult`
|
|
2414
|
+
* containing one `KafkaMessageResult` per produced message.
|
|
2415
|
+
*
|
|
2416
|
+
* @example
|
|
2417
|
+
* app.kafka().produce('order-placed', [
|
|
2418
|
+
* { data: { id: 1 } },
|
|
2419
|
+
* { data: { id: 2 } }
|
|
2420
|
+
* ], {
|
|
2421
|
+
* result: (r) => {
|
|
2422
|
+
* for (const msg of r.messages) {
|
|
2423
|
+
* console.log(`partition=${msg.partition} offset=${msg.offset}`)
|
|
2424
|
+
* }
|
|
2425
|
+
* }
|
|
2426
|
+
* })
|
|
2427
|
+
*/
|
|
2428
|
+
result?: (result: KafkaProduceResult) => void
|
|
2429
|
+
|
|
2430
|
+
/**
|
|
2431
|
+
* Retry configuration applied when the script executes before the
|
|
2432
|
+
* Kafka topic is fully set up in Mokapi.
|
|
2433
|
+
*
|
|
2434
|
+
* Mokapi uses an exponential backoff strategy based on these settings.
|
|
2435
|
+
* If all retries are exhausted, the produce operation fails with an error.
|
|
2436
|
+
*
|
|
2437
|
+
* @example
|
|
2438
|
+
* app.kafka().produce('order-placed', { data: { id: 1 } }, {
|
|
2439
|
+
* retry: { retries: 10, maxRetryTime: '60s' }
|
|
2440
|
+
* })
|
|
2441
|
+
*/
|
|
2442
|
+
retry?: Retry;
|
|
2443
|
+
}
|
|
2444
|
+
|
|
2445
|
+
/**
|
|
2446
|
+
* The result of a `produce()` or `produceAsync()` operation.
|
|
2447
|
+
*
|
|
2448
|
+
* Contains the cluster and topic where the messages were written,
|
|
2449
|
+
* along with a per-message result including the assigned partition and offset.
|
|
2450
|
+
*
|
|
2451
|
+
* Received via the `result` callback in `KafkaProduceArgs`:
|
|
2452
|
+
*
|
|
2453
|
+
* @example
|
|
2454
|
+
* app.kafka().produce('order-placed', { data: { id: 1 } }, {
|
|
2455
|
+
* result: (r) => {
|
|
2456
|
+
* console.log(r.cluster) // e.g. 'Order Events'
|
|
2457
|
+
* console.log(r.topic) // e.g. 'order-placed'
|
|
2458
|
+
* console.log(r.messages[0].offset) // e.g. 42
|
|
2459
|
+
* }
|
|
2460
|
+
* })
|
|
2461
|
+
*
|
|
2462
|
+
* https://mokapi.io/docs/javascript-api/mokapi-kafka/kafkaproduceresult
|
|
2463
|
+
*/
|
|
2464
|
+
export interface KafkaProduceResult {
|
|
2465
|
+
/**
|
|
2466
|
+
* The name of the Kafka cluster (AsyncAPI `info.title`) where
|
|
2467
|
+
* the messages were written.
|
|
2468
|
+
*/
|
|
2469
|
+
readonly api: string;
|
|
2470
|
+
|
|
2471
|
+
/**
|
|
2472
|
+
* The name of the Kafka topic where the messages were written.
|
|
2473
|
+
*/
|
|
2474
|
+
readonly topic: string;
|
|
2475
|
+
|
|
2476
|
+
/**
|
|
2477
|
+
* One result entry per produced message, in the same order as the
|
|
2478
|
+
* input messages passed to `produce()`.
|
|
2479
|
+
*/
|
|
2480
|
+
messages: KafkaMessageResult[];
|
|
2481
|
+
}
|
|
2482
|
+
|
|
2483
|
+
/**
|
|
2484
|
+
* Represents the outcome of producing a single Kafka message.
|
|
2485
|
+
*
|
|
2486
|
+
* One `KafkaMessageResult` is returned per message passed to `produce()`,
|
|
2487
|
+
* accessible via `KafkaProduceResult.messages`.
|
|
2488
|
+
*
|
|
2489
|
+
* @example
|
|
2490
|
+
* app.kafka().produce('order-placed', [
|
|
2491
|
+
* { data: { id: 1 } },
|
|
2492
|
+
* { data: { id: 2 } }
|
|
2493
|
+
* ], {
|
|
2494
|
+
* result: (r) => {
|
|
2495
|
+
* for (const msg of r.messages) {
|
|
2496
|
+
* console.log(`key=${msg.key} partition=${msg.partition} offset=${msg.offset}`)
|
|
2497
|
+
* }
|
|
2498
|
+
* }
|
|
2499
|
+
* })
|
|
2500
|
+
*
|
|
2501
|
+
* https://mokapi.io/docs/javascript-api/mokapi-kafka/kafkamessageresult
|
|
2502
|
+
*/
|
|
2503
|
+
export interface KafkaMessageResult {
|
|
2504
|
+
/**
|
|
2505
|
+
* The zero-based index of the Kafka partition where the message was written.
|
|
2506
|
+
*/
|
|
2507
|
+
readonly partition: number;
|
|
2508
|
+
|
|
2509
|
+
/**
|
|
2510
|
+
* The unique sequential identifier assigned to the message within its partition.
|
|
2511
|
+
* Use this to track or reference the message after it has been written.
|
|
2512
|
+
*/
|
|
2513
|
+
readonly offset: number;
|
|
2514
|
+
|
|
2515
|
+
/**
|
|
2516
|
+
* The key of the written Kafka message as a string.
|
|
2517
|
+
* Reflects the key that was used for partitioning, whether explicitly
|
|
2518
|
+
* set or randomly generated by Mokapi.
|
|
2519
|
+
*/
|
|
2520
|
+
readonly key: string;
|
|
2521
|
+
|
|
2522
|
+
/**
|
|
2523
|
+
* The serialized payload of the written Kafka message as a string.
|
|
2524
|
+
* Reflects the encoded form of `data` or `value` as written to Kafka.
|
|
2525
|
+
*/
|
|
2526
|
+
readonly value: string;
|
|
2527
|
+
|
|
2528
|
+
/**
|
|
2529
|
+
* The metadata headers attached to the written Kafka message.
|
|
2530
|
+
* Reflects the headers passed in `KafkaProduceMessage.headers`.
|
|
2531
|
+
*/
|
|
2532
|
+
readonly headers: { [name: string]: string };
|
|
2533
|
+
}
|
|
2534
|
+
|
|
2535
|
+
/**
|
|
2536
|
+
* Retry configuration for Kafka produce operations.
|
|
2537
|
+
*
|
|
2538
|
+
* Applied when `produce()` or `produceAsync()` is called before the target
|
|
2539
|
+
* Kafka topic is fully initialized in Mokapi. Mokapi retries using an
|
|
2540
|
+
* exponential backoff strategy based on these settings.
|
|
2541
|
+
*
|
|
2542
|
+
* The wait time before each retry is calculated as:
|
|
2543
|
+
* `min(initialRetryTime * factor^attempt, maxRetryTime)`
|
|
2544
|
+
*
|
|
2545
|
+
* @example
|
|
2546
|
+
* // Default retry behavior
|
|
2547
|
+
* app.kafka().produce('order-placed', { data: { id: 1 } }, {
|
|
2548
|
+
* retry: {}
|
|
2549
|
+
* })
|
|
2550
|
+
*
|
|
2551
|
+
* @example
|
|
2552
|
+
* // Custom retry — faster initial retry, more attempts
|
|
2553
|
+
* app.kafka().produce('order-placed', { data: { id: 1 } }, {
|
|
2554
|
+
* retry: {
|
|
2555
|
+
* retries: 10,
|
|
2556
|
+
* initialRetryTime: '500ms',
|
|
2557
|
+
* maxRetryTime: '60s',
|
|
2558
|
+
* factor: 2
|
|
2559
|
+
* }
|
|
2560
|
+
* })
|
|
2561
|
+
*/
|
|
2562
|
+
export interface Retry {
|
|
2563
|
+
/**
|
|
2564
|
+
* Maximum total wait time across all retry attempts.
|
|
2565
|
+
*
|
|
2566
|
+
* Accepts milliseconds as a `number` or a duration string such as
|
|
2567
|
+
* `"30s"` or `"2h45m"`. Valid time units are `ns`, `us` (or `µs`),
|
|
2568
|
+
* `ms`, `s`, `m`, `h`.
|
|
2569
|
+
*
|
|
2570
|
+
* @default "30000ms"
|
|
2571
|
+
*
|
|
2572
|
+
* @example
|
|
2573
|
+
* app.kafka().produce('order-placed', {}, { retry: { maxRetryTime: '60s' } })
|
|
2574
|
+
*/
|
|
2575
|
+
maxRetryTime?: string | number;
|
|
2576
|
+
|
|
2577
|
+
/**
|
|
2578
|
+
* Wait time before the first retry attempt. Subsequent retries multiply
|
|
2579
|
+
* this value by `factor` on each attempt until `maxRetryTime` is reached.
|
|
2580
|
+
*
|
|
2581
|
+
* Accepts milliseconds as a `number` or a duration string such as
|
|
2582
|
+
* `"200ms"` or `"2s"`. Valid time units are `ns`, `us` (or `µs`),
|
|
2583
|
+
* `ms`, `s`, `m`, `h`.
|
|
2584
|
+
*
|
|
2585
|
+
* @default "200ms"
|
|
2586
|
+
*
|
|
2587
|
+
* @example
|
|
2588
|
+
* app.kafka().produce('order-placed', {}, { retry: { initialRetryTime: '1s' } })
|
|
2589
|
+
*/
|
|
2590
|
+
initialRetryTime?: string | number;
|
|
2591
|
+
|
|
2592
|
+
/**
|
|
2593
|
+
* Multiplier applied to the wait time after each retry attempt.
|
|
2594
|
+
*
|
|
2595
|
+
* With the default values, the wait times are:
|
|
2596
|
+
* - 1st retry: 200ms
|
|
2597
|
+
* - 2nd retry: 4 × 200ms = 800ms
|
|
2598
|
+
* - 3rd retry: 4 × 800ms = 3200ms
|
|
2599
|
+
* - ...capped at `maxRetryTime`
|
|
2600
|
+
*
|
|
2601
|
+
* @default 4
|
|
2602
|
+
*/
|
|
2603
|
+
factor?: number;
|
|
2604
|
+
|
|
2605
|
+
/**
|
|
2606
|
+
* Maximum number of retry attempts before the produce operation fails.
|
|
2607
|
+
* Set to `0` to disable retries entirely.
|
|
2608
|
+
*
|
|
2609
|
+
* @default 5
|
|
2610
|
+
*/
|
|
2611
|
+
retries?: number;
|
|
2612
|
+
}
|
|
2613
|
+
|
|
2614
|
+
/**
|
|
2615
|
+
* Registers and organizes MQTT message handlers and publishers by topic.
|
|
2616
|
+
*
|
|
2617
|
+
* Obtain an `MqttRouter` via `app.mqtt()` or `app.api('My API').mqtt()`.
|
|
2618
|
+
*
|
|
2619
|
+
* Two styles are supported:
|
|
2620
|
+
* - **Shorthand**: `router.publish(message)` — publishes directly without topic scope.
|
|
2621
|
+
* - **Scoped**: `router.topic(name).publish(message)` — groups handlers and publishers under one topic.
|
|
2622
|
+
*
|
|
2623
|
+
* When multiple APIs share the same topic name, use `app.api()` to scope the router
|
|
2624
|
+
* to a specific API title to avoid unintended matches.
|
|
2625
|
+
*
|
|
2626
|
+
* @example
|
|
2627
|
+
* // Shorthand style
|
|
2628
|
+
* import { app } from 'mokapi'
|
|
2629
|
+
* export default function() {
|
|
2630
|
+
* app.api('Smart Home').mqtt()
|
|
2631
|
+
* .message((msg) => { console.log(msg.value) })
|
|
2632
|
+
* .publish({ value: '21.5' })
|
|
2633
|
+
* }
|
|
2634
|
+
*
|
|
2635
|
+
* @example
|
|
2636
|
+
* // Scoped style
|
|
2637
|
+
* import { app } from 'mokapi'
|
|
2638
|
+
* export default function() {
|
|
2639
|
+
* app.api('Smart Home').mqtt()
|
|
2640
|
+
* .topic('sensors/temperature')
|
|
2641
|
+
* .message((msg) => { console.log(`Temperature: ${msg.value}`) })
|
|
2642
|
+
* .publish({ value: '21.5', retain: true })
|
|
2643
|
+
* }
|
|
2644
|
+
*/
|
|
2645
|
+
export interface MqttRouter {
|
|
2646
|
+
/**
|
|
2647
|
+
* Returns an `MqttTopicRouter` scoped to the given topic name.
|
|
2648
|
+
*
|
|
2649
|
+
* Use this to register message handlers or publish messages for a specific
|
|
2650
|
+
* MQTT topic using method chaining, without repeating the topic name on every call.
|
|
2651
|
+
*
|
|
2652
|
+
* MQTT topic names support wildcards in subscriptions (`+` for single level,
|
|
2653
|
+
* `#` for multi-level), but for publishing an exact topic name is required.
|
|
2654
|
+
*
|
|
2655
|
+
* @param name The MQTT topic name as defined in the AsyncAPI specification.
|
|
2656
|
+
* @returns An `MqttTopicRouter` scoped to the given topic name.
|
|
2657
|
+
*
|
|
2658
|
+
* @example
|
|
2659
|
+
* import { app } from 'mokapi'
|
|
2660
|
+
* export default function() {
|
|
2661
|
+
* app.api('Smart Home').mqtt()
|
|
2662
|
+
* .topic('sensors/temperature')
|
|
2663
|
+
* .message((msg) => { console.log(`Temperature: ${msg.value}`) })
|
|
2664
|
+
* .publish({ value: '21.5', retain: true })
|
|
2665
|
+
* }
|
|
2666
|
+
*/
|
|
2667
|
+
topic(name: string): MqttTopicRouter
|
|
2668
|
+
|
|
2669
|
+
/**
|
|
2670
|
+
* Registers a handler for incoming messages across all topics.
|
|
2671
|
+
*
|
|
2672
|
+
* The handler can read and modify the message before it is processed.
|
|
2673
|
+
* Use `topic()` to scope the handler to a specific topic.
|
|
2674
|
+
*
|
|
2675
|
+
* @param handler The event handler to invoke for each incoming message.
|
|
2676
|
+
* @param opts Optional handler configuration such as priority and tracking.
|
|
2677
|
+
* @returns This `MqttRouter` instance for chaining.
|
|
2678
|
+
*
|
|
2679
|
+
* @example
|
|
2680
|
+
* import { app } from 'mokapi'
|
|
2681
|
+
* export default function() {
|
|
2682
|
+
* app.mqtt().message((msg) => {
|
|
2683
|
+
* console.log(`[${msg.topic}] ${msg.value}`)
|
|
2684
|
+
* })
|
|
2685
|
+
* }
|
|
2686
|
+
*/
|
|
2687
|
+
message(handler: MqttEventHandler, opts?: MqttEventArgs): MqttRouter
|
|
2688
|
+
|
|
2689
|
+
/**
|
|
2690
|
+
* Publishes a message to the MQTT broker.
|
|
2691
|
+
*
|
|
2692
|
+
* If neither `value` nor `data` is set, Mokapi generates a random valid
|
|
2693
|
+
* value based on the AsyncAPI schema definition for the topic.
|
|
2694
|
+
*
|
|
2695
|
+
* Use `topic()` to scope the publish to a specific topic without
|
|
2696
|
+
* repeating the topic name. Use `publishAsync()` if you need to await the result.
|
|
2697
|
+
*
|
|
2698
|
+
* @param topic The MQTT topic name to produce to.
|
|
2699
|
+
* @param message The message to publish.
|
|
2700
|
+
* @param opts Optional configuration including a result callback and retry settings.
|
|
2701
|
+
* @returns This `MqttRouter` instance for chaining.
|
|
2702
|
+
*
|
|
2703
|
+
* @example
|
|
2704
|
+
* // Publish a retained message
|
|
2705
|
+
* app.mqtt().publish('sensors/temperature', { value: '21.5', retain: true })
|
|
2706
|
+
*
|
|
2707
|
+
* @example
|
|
2708
|
+
* // Publish with result callback
|
|
2709
|
+
* app.mqtt().publish('sensors/temperature', { value: '21.5' }, {
|
|
2710
|
+
* result: (r) => console.log(`Published to ${r.topic}: ${r.value}`)
|
|
2711
|
+
* })
|
|
2712
|
+
*/
|
|
2713
|
+
publish(topic: string, message: MqttPublishMessage, opts?: MqttPublishArgs): MqttRouter
|
|
2714
|
+
|
|
2715
|
+
/**
|
|
2716
|
+
* Publishes a message to the MQTT broker asynchronously.
|
|
2717
|
+
*
|
|
2718
|
+
* Use this when you need to await the publish result, for example to
|
|
2719
|
+
* confirm the topic and value before continuing.
|
|
2720
|
+
*
|
|
2721
|
+
* If neither `value` nor `data` is set, Mokapi generates a random valid
|
|
2722
|
+
* value based on the AsyncAPI schema definition for the topic.
|
|
2723
|
+
*
|
|
2724
|
+
* @param topic The MQTT topic name to publish to.
|
|
2725
|
+
* @param message The message to publish.
|
|
2726
|
+
* @param opts Optional configuration including a result callback and retry settings.
|
|
2727
|
+
* @returns A `Promise` resolving to this `MqttRouter` instance for chaining.
|
|
2728
|
+
*
|
|
2729
|
+
* @example
|
|
2730
|
+
* import { app } from 'mokapi'
|
|
2731
|
+
* export default async function() {
|
|
2732
|
+
* await app.api('Smart Home').mqtt()
|
|
2733
|
+
* .publishAsync('sensors/temperature', { value: '21.5' }, {
|
|
2734
|
+
* result: (r) => console.log(`Published to ${r.topic}: ${r.value}`)
|
|
2735
|
+
* })
|
|
2736
|
+
* }
|
|
2737
|
+
*/
|
|
2738
|
+
publishAsync(topic: string, message: MqttPublishMessage, opts?: MqttPublishArgs): Promise<MqttRouter>
|
|
2739
|
+
}
|
|
2740
|
+
|
|
2741
|
+
/**
|
|
2742
|
+
* Represents a single MQTT topic within an `MqttRouter`,
|
|
2743
|
+
* allowing message handlers and publishers to be registered
|
|
2744
|
+
* via method chaining.
|
|
2745
|
+
*
|
|
2746
|
+
* Obtain an `MqttTopicRouter` via `router.topic(name)`.
|
|
2747
|
+
*
|
|
2748
|
+
* All methods return `this`, enabling fluent chaining:
|
|
2749
|
+
* ```ts
|
|
2750
|
+
* router.topic('sensors/temperature')
|
|
2751
|
+
* .message(handler)
|
|
2752
|
+
* .publish({ value: '21.5' })
|
|
2753
|
+
* ```
|
|
2754
|
+
*
|
|
2755
|
+
* @example
|
|
2756
|
+
* import { app } from 'mokapi'
|
|
2757
|
+
* export default function() {
|
|
2758
|
+
* app.api('Smart Home').mqtt()
|
|
2759
|
+
* .topic('sensors/temperature')
|
|
2760
|
+
* .message((msg) => {
|
|
2761
|
+
* console.log(`Temperature reading: ${msg.value}`)
|
|
2762
|
+
* })
|
|
2763
|
+
* .publish({ value: '21.5', retain: true })
|
|
2764
|
+
* }
|
|
2765
|
+
*/
|
|
2766
|
+
export interface MqttTopicRouter {
|
|
2767
|
+
/**
|
|
2768
|
+
* Registers a handler for incoming messages on this topic.
|
|
2769
|
+
*
|
|
2770
|
+
* The handler can read and modify the message before it is processed.
|
|
2771
|
+
* Multiple handlers can be registered and run in registration order.
|
|
2772
|
+
*
|
|
2773
|
+
* @param handler The event handler to invoke for each incoming message.
|
|
2774
|
+
* @param opts Optional handler configuration such as priority and tracking.
|
|
2775
|
+
* @returns This `MqttTopicRouter` instance for chaining.
|
|
2776
|
+
*
|
|
2777
|
+
* @example
|
|
2778
|
+
* app.api('Smart Home').mqtt()
|
|
2779
|
+
* .topic('sensors/temperature')
|
|
2780
|
+
* .message((msg) => {
|
|
2781
|
+
* const temp = parseFloat(msg.value)
|
|
2782
|
+
* if (temp > 30) {
|
|
2783
|
+
* console.log('High temperature alert!')
|
|
2784
|
+
* }
|
|
2785
|
+
* })
|
|
2786
|
+
*/
|
|
2787
|
+
message(handler: MqttEventHandler, opts?: MqttEventArgs): MqttTopicRouter
|
|
2788
|
+
|
|
2789
|
+
/**
|
|
2790
|
+
* Publishes a message to this topic synchronously.
|
|
2791
|
+
*
|
|
2792
|
+
* If neither `value` nor `data` is set, Mokapi generates a random valid
|
|
2793
|
+
* value based on the AsyncAPI schema definition for this topic.
|
|
2794
|
+
*
|
|
2795
|
+
* Use `publishAsync()` if you need to await the result.
|
|
2796
|
+
*
|
|
2797
|
+
* @param message The message to publish.
|
|
2798
|
+
* @param opts Optional configuration including a result callback and retry settings.
|
|
2799
|
+
* @returns This `MqttTopicRouter` instance for chaining.
|
|
2800
|
+
*
|
|
2801
|
+
* @example
|
|
2802
|
+
* app.api('Smart Home').mqtt()
|
|
2803
|
+
* .topic('sensors/temperature')
|
|
2804
|
+
* .publish({ value: '21.5', retain: true }, {
|
|
2805
|
+
* result: (r) => console.log(`Published to ${r.topic}: ${r.value}`)
|
|
2806
|
+
* })
|
|
2807
|
+
*/
|
|
2808
|
+
publish(message: MqttPublishMessage, opts?: MqttPublishArgs): MqttTopicRouter
|
|
2809
|
+
|
|
2810
|
+
/**
|
|
2811
|
+
* Publishes a message to this topic asynchronously.
|
|
2812
|
+
*
|
|
2813
|
+
* Use this when you need to await the publish result, for example to
|
|
2814
|
+
* confirm the topic and value before continuing.
|
|
2815
|
+
*
|
|
2816
|
+
* If neither `value` nor `data` is set, Mokapi generates a random valid
|
|
2817
|
+
* value based on the AsyncAPI schema definition for this topic.
|
|
2818
|
+
*
|
|
2819
|
+
* @param message The message to publish.
|
|
2820
|
+
* @param opts Optional configuration including a result callback and retry settings.
|
|
2821
|
+
* @returns A `Promise` resolving to this `MqttTopicRouter` instance for chaining.
|
|
2822
|
+
*
|
|
2823
|
+
* @example
|
|
2824
|
+
* import { app } from 'mokapi'
|
|
2825
|
+
* export default async function() {
|
|
2826
|
+
* await app.api('Smart Home').mqtt()
|
|
2827
|
+
* .topic('sensors/temperature')
|
|
2828
|
+
* .publishAsync({ value: '21.5' }, {
|
|
2829
|
+
* result: (r) => console.log(`Published to ${r.topic}: ${r.value}`)
|
|
2830
|
+
* })
|
|
2831
|
+
* }
|
|
2832
|
+
*/
|
|
2833
|
+
publishAsync(message: MqttPublishMessage, opts?: MqttPublishArgs): Promise<MqttTopicRouter>
|
|
2834
|
+
}
|
|
2835
|
+
|
|
2836
|
+
/**
|
|
2837
|
+
* Represents a single MQTT message to be published via `publish()`.
|
|
2838
|
+
*
|
|
2839
|
+
* At least one of `data` or `value` should be set. If neither is provided,
|
|
2840
|
+
* Mokapi generates a random valid value based on the AsyncAPI schema definition.
|
|
2841
|
+
*
|
|
2842
|
+
* Use `data` when the topic has a schema defined — Mokapi handles validation
|
|
2843
|
+
* and serialization. Use `value` when you need full control over the raw bytes
|
|
2844
|
+
* written to MQTT.
|
|
2845
|
+
*
|
|
2846
|
+
* @example
|
|
2847
|
+
* // Publish with explicit value
|
|
2848
|
+
* app.mqtt().topic('sensors/temperature').publish({ value: '21.5' })
|
|
2849
|
+
*
|
|
2850
|
+
* @example
|
|
2851
|
+
* // Publish a retained message
|
|
2852
|
+
* app.mqtt().topic('sensors/temperature').publish({ value: '21.5', retain: true })
|
|
2853
|
+
*
|
|
2854
|
+
* @example
|
|
2855
|
+
* // Random generated value — value not set
|
|
2856
|
+
* app.mqtt().topic('sensors/temperature').publish({})
|
|
2857
|
+
*/
|
|
2858
|
+
export interface MqttPublishMessage {
|
|
2859
|
+
/**
|
|
2860
|
+
* Structured data to be encoded and validated according to the
|
|
2861
|
+
* AsyncAPI schema definition for this topic.
|
|
2862
|
+
*
|
|
2863
|
+
* Mokapi serializes this value based on the topic's content type
|
|
2864
|
+
* (e.g. `application/json`) and validates it against the schema
|
|
2865
|
+
* before writing to MQTT.
|
|
2866
|
+
*
|
|
2867
|
+
* If neither `data` nor `value` is set, Mokapi generates a random
|
|
2868
|
+
* valid value based on the AsyncAPI schema definition.
|
|
2869
|
+
*
|
|
2870
|
+
* Prefer `data` over `value` when the topic has a schema defined,
|
|
2871
|
+
* as it ensures the message is valid according to your AsyncAPI contract.
|
|
2872
|
+
*
|
|
2873
|
+
* @example
|
|
2874
|
+
* app.mqtt().publish('sensors/temperature', {
|
|
2875
|
+
* data: { id: 1, item: 'Book', quantity: 2 }
|
|
2876
|
+
* })
|
|
2877
|
+
*/
|
|
2878
|
+
data?: any;
|
|
2879
|
+
|
|
2880
|
+
/**
|
|
2881
|
+
* Raw value written directly to MQTT without schema validation or encoding.
|
|
2882
|
+
*
|
|
2883
|
+
* Primitives (`string`, `number`, `boolean`, `null`) and `ArrayBuffer`
|
|
2884
|
+
* are written as-is. Objects are JSON-serialized at runtime.
|
|
2885
|
+
*
|
|
2886
|
+
* If neither `value` nor `data` is set, Mokapi generates a random valid
|
|
2887
|
+
* value based on the AsyncAPI schema definition.
|
|
2888
|
+
*
|
|
2889
|
+
* Use `data` instead if you want schema-based encoding and validation.
|
|
2890
|
+
* Use `value` when you need full control over the raw bytes written to MQTT,
|
|
2891
|
+
* for example when testing subscribers that expect a specific binary format.
|
|
2892
|
+
*
|
|
2893
|
+
* @example
|
|
2894
|
+
* // Raw string
|
|
2895
|
+
* app.mqtt().publish('sensors/temperature', { value: 'raw-payload' })
|
|
2896
|
+
*
|
|
2897
|
+
* @example
|
|
2898
|
+
* // Pre-encoded binary via ArrayBuffer
|
|
2899
|
+
* app.mqtt().publish('sensors/temperature', { value: myArrayBuffer })
|
|
2900
|
+
*/
|
|
2901
|
+
value?: any
|
|
2902
|
+
|
|
2903
|
+
/**
|
|
2904
|
+
* Whether the MQTT broker should retain this message.
|
|
2905
|
+
*
|
|
2906
|
+
* When `true`, the broker stores the message and delivers it immediately
|
|
2907
|
+
* to any new subscriber on this topic. Only the last retained message
|
|
2908
|
+
* per topic is stored.
|
|
2909
|
+
*
|
|
2910
|
+
* @default false
|
|
2911
|
+
*
|
|
2912
|
+
* @example
|
|
2913
|
+
* app.mqtt().topic('sensors/temperature').publish({ value: '21.5', retain: true })
|
|
2914
|
+
*/
|
|
2915
|
+
retain?: boolean
|
|
2916
|
+
}
|
|
2917
|
+
|
|
2918
|
+
/**
|
|
2919
|
+
* Optional configuration for `publish()`.
|
|
2920
|
+
*
|
|
2921
|
+
* Use `result` to inspect the outcome of a publish operation.
|
|
2922
|
+
* Use `retry` to configure retry behavior when the MQTT topic is not yet available.
|
|
2923
|
+
*
|
|
2924
|
+
* @example
|
|
2925
|
+
* app.mqtt().topic('sensors/temperature').publish({ value: '21.5' }, {
|
|
2926
|
+
* result: (r) => console.log(`Published to ${r.topic}: ${r.value}`)
|
|
2927
|
+
* })
|
|
2928
|
+
*/
|
|
2929
|
+
export interface MqttPublishArgs {
|
|
2930
|
+
/**
|
|
2931
|
+
* Callback invoked with the publish result after the message has been
|
|
2932
|
+
* successfully delivered to the MQTT broker.
|
|
2933
|
+
*
|
|
2934
|
+
* Use this to confirm the topic and value of the published message.
|
|
2935
|
+
*
|
|
2936
|
+
* @example
|
|
2937
|
+
* app.mqtt().publish('sensors/temperature', { value: '21.5' }, {
|
|
2938
|
+
* result: (r) => {
|
|
2939
|
+
* console.log(r.api) // e.g. 'Smart Home'
|
|
2940
|
+
* console.log(r.topic) // e.g. 'sensors/temperature'
|
|
2941
|
+
* console.log(r.value) // e.g. '21.5'
|
|
2942
|
+
* }
|
|
2943
|
+
* })
|
|
2944
|
+
*/
|
|
2945
|
+
result?: (result: MqttPublishResult) => void
|
|
2946
|
+
|
|
2947
|
+
/**
|
|
2948
|
+
* Retry configuration applied when the script executes before the
|
|
2949
|
+
* MQTT topic is fully set up in Mokapi.
|
|
2950
|
+
*
|
|
2951
|
+
* Mokapi uses an exponential backoff strategy based on these settings.
|
|
2952
|
+
* If all retries are exhausted, the publish operation fails with an error.
|
|
2953
|
+
*
|
|
2954
|
+
* @example
|
|
2955
|
+
* app.mqtt().publish('sensors/temperature', { value: '21.5' }, {
|
|
2956
|
+
* retry: { retries: 5, initialRetryTime: '1s' }
|
|
2957
|
+
* })
|
|
2958
|
+
*/
|
|
2959
|
+
retry?: Retry;
|
|
2960
|
+
}
|
|
2961
|
+
|
|
2962
|
+
/**
|
|
2963
|
+
* The result of a `publish()` operation.
|
|
2964
|
+
*
|
|
2965
|
+
* Contains the API, topic, and value of the published message.
|
|
2966
|
+
* Received via the `result` callback in `MqttPublishArgs`.
|
|
2967
|
+
*
|
|
2968
|
+
* @example
|
|
2969
|
+
* app.mqtt().publish({ value: '21.5' }, {
|
|
2970
|
+
* result: (r) => {
|
|
2971
|
+
* console.log(r.api) // e.g. 'Smart Home'
|
|
2972
|
+
* console.log(r.topic) // e.g. 'sensors/temperature'
|
|
2973
|
+
* console.log(r.value) // e.g. '21.5'
|
|
2974
|
+
* }
|
|
2975
|
+
* })
|
|
2976
|
+
*
|
|
2977
|
+
* https://mokapi.io/docs/javascript-api/mokapi-mqtt/mqttpublishresult
|
|
2978
|
+
*/
|
|
2979
|
+
export interface MqttPublishResult {
|
|
2980
|
+
/**
|
|
2981
|
+
* The name of the MQTT API (AsyncAPI `info.title`) where
|
|
2982
|
+
* the message was published.
|
|
2983
|
+
*/
|
|
2984
|
+
readonly api: string;
|
|
2985
|
+
|
|
2986
|
+
/**
|
|
2987
|
+
* The name of the MQTT topic where the message was published.
|
|
2988
|
+
*/
|
|
2989
|
+
readonly topic: string;
|
|
2990
|
+
|
|
2991
|
+
/**
|
|
2992
|
+
* The payload of the published message as a string.
|
|
2993
|
+
* Reflects the value that was written to the broker, whether
|
|
2994
|
+
* explicitly set or randomly generated by Mokapi.
|
|
2995
|
+
*/
|
|
2996
|
+
readonly value: string;
|
|
1898
2997
|
}
|