go-mokapi 0.53.0 → 0.55.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 +7 -7
- package/types/http.d.ts +7 -7
- package/types/index.d.ts +514 -18
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "go-mokapi",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.55.0",
|
|
4
4
|
"description": "Easy and flexible API mocking",
|
|
5
5
|
"homepage": "https://mokapi.io",
|
|
6
6
|
"repository": {
|
|
@@ -12,12 +12,12 @@
|
|
|
12
12
|
},
|
|
13
13
|
"preferUnplugged": true,
|
|
14
14
|
"optionalDependencies": {
|
|
15
|
-
"@go-mokapi/darwin-arm64": "
|
|
16
|
-
"@go-mokapi/darwin-x64": "
|
|
17
|
-
"@go-mokapi/linux-arm64": "
|
|
18
|
-
"@go-mokapi/linux-x64": "
|
|
19
|
-
"@go-mokapi/win32-arm64": "
|
|
20
|
-
"@go-mokapi/win32-x64": "
|
|
15
|
+
"@go-mokapi/darwin-arm64": "0.55.0",
|
|
16
|
+
"@go-mokapi/darwin-x64": "0.55.0",
|
|
17
|
+
"@go-mokapi/linux-arm64": "0.55.0",
|
|
18
|
+
"@go-mokapi/linux-x64": "0.55.0",
|
|
19
|
+
"@go-mokapi/win32-arm64": "0.55.0",
|
|
20
|
+
"@go-mokapi/win32-x64": "0.55.0"
|
|
21
21
|
},
|
|
22
22
|
"keywords": [
|
|
23
23
|
"mock api",
|
package/types/http.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Make GET request.
|
|
2
|
+
* Make a GET request.
|
|
3
3
|
* https://mokapi.io/docs/javascript-api/mokapi-http/get
|
|
4
4
|
* @param url url - Request URL.
|
|
5
5
|
* @param args args - Request arguments.
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
export function get(url: string, args?: Args): Response;
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* Make POST request.
|
|
13
|
+
* Make a POST request.
|
|
14
14
|
* https://mokapi.io/docs/javascript-api/mokapi-http/post
|
|
15
15
|
* @param url url - Request URL.
|
|
16
16
|
* @param body body - Request body.
|
|
@@ -24,7 +24,7 @@ export function get(url: string, args?: Args): Response;
|
|
|
24
24
|
export function post(url: string, body?: any, args?: Args): Response;
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
* Make PUT request.
|
|
27
|
+
* Make a PUT request.
|
|
28
28
|
* https://mokapi.io/docs/javascript-api/mokapi-http/put
|
|
29
29
|
* @param url url - Request URL.
|
|
30
30
|
* @param body body - Request body.
|
|
@@ -38,7 +38,7 @@ export function post(url: string, body?: any, args?: Args): Response;
|
|
|
38
38
|
export function put(url: string, body?: any, args?: Args): Response;
|
|
39
39
|
|
|
40
40
|
/**
|
|
41
|
-
* Make HEAD request.
|
|
41
|
+
* Make a HEAD request.
|
|
42
42
|
* https://mokapi.io/docs/javascript-api/mokapi-http/head
|
|
43
43
|
* @param url url - Request URL.
|
|
44
44
|
* @param args args - Request arguments.
|
|
@@ -49,7 +49,7 @@ export function put(url: string, body?: any, args?: Args): Response;
|
|
|
49
49
|
export function head(url: string, args?: Args): Response;
|
|
50
50
|
|
|
51
51
|
/**
|
|
52
|
-
* Make PATCH request.
|
|
52
|
+
* Make a PATCH request.
|
|
53
53
|
* https://mokapi.io/docs/javascript-api/mokapi-http/patch
|
|
54
54
|
* @param url url - Request URL.
|
|
55
55
|
* @param body body - Request body.
|
|
@@ -61,7 +61,7 @@ export function head(url: string, args?: Args): Response;
|
|
|
61
61
|
export function patch(url: string, body?: any, args?: Args): Response;
|
|
62
62
|
|
|
63
63
|
/**
|
|
64
|
-
* Make PATCH request.
|
|
64
|
+
* Make a PATCH request.
|
|
65
65
|
* https://mokapi.io/docs/javascript-api/mokapi-http/delete
|
|
66
66
|
* @param url url - Request URL.
|
|
67
67
|
* @param body body - Request body.
|
|
@@ -73,7 +73,7 @@ export function patch(url: string, body?: any, args?: Args): Response;
|
|
|
73
73
|
export function del(url: string, body?: any, args?: Args): Response;
|
|
74
74
|
|
|
75
75
|
/**
|
|
76
|
-
* Make OPTIONS request.
|
|
76
|
+
* Make an OPTIONS request.
|
|
77
77
|
* https://mokapi.io/docs/javascript-api/mokapi-http/options
|
|
78
78
|
* @param url url - Request URL.
|
|
79
79
|
* @param body body - Request body.
|
package/types/index.d.ts
CHANGED
|
@@ -9,6 +9,8 @@
|
|
|
9
9
|
* https://mokapi.io/docs/javascript-api/overview
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
+
import "./global";
|
|
13
|
+
|
|
12
14
|
/**
|
|
13
15
|
* Attaches an event handler for the given event.
|
|
14
16
|
*
|
|
@@ -364,7 +366,7 @@ export type MqttEventHandler = (message: MqttEventMessage) => void | Promise<voi
|
|
|
364
366
|
* https://mokapi.io/docs/javascript-api/mokapi/eventhandler/KafkaEventMessage
|
|
365
367
|
*/
|
|
366
368
|
export interface MqttEventMessage {
|
|
367
|
-
/** The name of the API
|
|
369
|
+
/** The name of the API matching the `info.title` field in the AsyncAPI specification */
|
|
368
370
|
readonly api: string;
|
|
369
371
|
|
|
370
372
|
/**
|
|
@@ -372,7 +374,7 @@ export interface MqttEventMessage {
|
|
|
372
374
|
*/
|
|
373
375
|
readonly topic: string;
|
|
374
376
|
|
|
375
|
-
/** Specifies whether message should be retained */
|
|
377
|
+
/** Specifies whether a message should be retained */
|
|
376
378
|
retain: boolean;
|
|
377
379
|
|
|
378
380
|
/** MQTT message value */
|
|
@@ -424,6 +426,9 @@ export interface MqttEventMessage {
|
|
|
424
426
|
export type WebsocketEventHandler = (message: WebsocketEvent) => void | Promise<void>;
|
|
425
427
|
|
|
426
428
|
export type WebsocketEvent = WebsocketEventConnect | WebsocketEventMessage | WebsocketEventClose;
|
|
429
|
+
export type WebsocketConnectEventHandler = (event: WebsocketEventConnect) => void | Promise<void>;
|
|
430
|
+
export type WebsocketMessageEventHandler = (event: WebsocketEventMessage) => void | Promise<void>;
|
|
431
|
+
export type WebsocketCloseEventHandler = (event: WebsocketEventClose) => void | Promise<void>;
|
|
427
432
|
|
|
428
433
|
/**
|
|
429
434
|
* Base interface shared by all WebSocket events. Use the `type` field to
|
|
@@ -482,7 +487,7 @@ export interface WebsocketEventBase {
|
|
|
482
487
|
* @example
|
|
483
488
|
* on('websocket', function(event) {
|
|
484
489
|
* const userId = event.client.query['userId']
|
|
485
|
-
* const token
|
|
490
|
+
* const token = event.client.headers['authorization']
|
|
486
491
|
* })
|
|
487
492
|
*/
|
|
488
493
|
readonly client: WebsocketClient;
|
|
@@ -612,6 +617,7 @@ export interface WebsocketEventMessage extends WebsocketEventBase {
|
|
|
612
617
|
*/
|
|
613
618
|
export interface WebsocketEventConnect extends WebsocketEventBase {
|
|
614
619
|
readonly type: 'connect'
|
|
620
|
+
reply(message: any): void;
|
|
615
621
|
}
|
|
616
622
|
|
|
617
623
|
/**
|
|
@@ -790,7 +796,17 @@ export interface WebsocketChannel {
|
|
|
790
796
|
*
|
|
791
797
|
* @example
|
|
792
798
|
* on('websocket', function(event) {
|
|
793
|
-
* console.log(event.channel.name) // e.g. "/chat"
|
|
799
|
+
* console.log(event.channel.name) // e.g. "/chat/{roomId}"
|
|
800
|
+
* })
|
|
801
|
+
*/
|
|
802
|
+
readonly key: string;
|
|
803
|
+
|
|
804
|
+
/**
|
|
805
|
+
* The resolved channel path for this specific connection instance.
|
|
806
|
+
*
|
|
807
|
+
* @example
|
|
808
|
+
* on('websocket', function(event) {
|
|
809
|
+
* console.log(event.channel.name) // e.g. "/chat/general"
|
|
794
810
|
* })
|
|
795
811
|
*/
|
|
796
812
|
readonly name: string;
|
|
@@ -826,7 +842,7 @@ export interface WebsocketChannel {
|
|
|
826
842
|
}
|
|
827
843
|
|
|
828
844
|
/**
|
|
829
|
-
* LdapEventHandler is a function
|
|
845
|
+
* LdapEventHandler is a function executed when an LDAP search query is triggered.
|
|
830
846
|
* @example
|
|
831
847
|
* export default function() {
|
|
832
848
|
* on('ldap', function(request, response) {
|
|
@@ -1512,12 +1528,30 @@ export interface App {
|
|
|
1512
1528
|
* }
|
|
1513
1529
|
*/
|
|
1514
1530
|
mqtt(): MqttRouter
|
|
1531
|
+
|
|
1532
|
+
/**
|
|
1533
|
+
* Returns a `WebsocketRouter` not bound to any specific API.
|
|
1534
|
+
* Handlers registered here will run for all WebSocket APIs
|
|
1535
|
+
* that match the given channel.
|
|
1536
|
+
*
|
|
1537
|
+
* Use this for cross-cutting concerns such as logging connections
|
|
1538
|
+
* or broadcasting to a channel when only one WebSocket API is configured.
|
|
1539
|
+
*
|
|
1540
|
+
* When calling `send()` without an API scope, Mokapi searches all APIs
|
|
1541
|
+
* for the given channel name. An error is returned if the channel is found
|
|
1542
|
+
* in more than one API or in none — use `app.api('My API').websocket()`
|
|
1543
|
+
* to scope the send to a specific API when ambiguity is possible.
|
|
1544
|
+
*
|
|
1545
|
+
* @returns A `WebsocketRouter` with no API scope.
|
|
1546
|
+
* ...
|
|
1547
|
+
*/
|
|
1548
|
+
websocket(): WebsocketRouter
|
|
1515
1549
|
}
|
|
1516
1550
|
|
|
1517
1551
|
/**
|
|
1518
1552
|
* A scoped application context bound to a specific API by its `info.title`.
|
|
1519
1553
|
*
|
|
1520
|
-
*
|
|
1554
|
+
* Get an `ApiScope` via `app.api('My API')`. All handlers registered
|
|
1521
1555
|
* through this scope will only run for requests matched to the named API.
|
|
1522
1556
|
*
|
|
1523
1557
|
* @example
|
|
@@ -1631,12 +1665,50 @@ export interface ApiScope {
|
|
|
1631
1665
|
* }
|
|
1632
1666
|
*/
|
|
1633
1667
|
mqtt(): MqttRouter
|
|
1668
|
+
|
|
1669
|
+
/**
|
|
1670
|
+
* Returns a `WebsocketRouter` scoped to this API.
|
|
1671
|
+
*
|
|
1672
|
+
* Handlers registered on this router will only run for WebSocket events
|
|
1673
|
+
* that belong to the API identified by this scope's title,
|
|
1674
|
+
* as defined in the AsyncAPI `info.title` field.
|
|
1675
|
+
*
|
|
1676
|
+
* Scoping is required when multiple WebSocket APIs define the same channel,
|
|
1677
|
+
* to avoid a handler running for unintended APIs.
|
|
1678
|
+
*
|
|
1679
|
+
* @returns A `WebsocketRouter` bound to this API scope.
|
|
1680
|
+
*
|
|
1681
|
+
* @example
|
|
1682
|
+
* // Handle connect events scoped to a specific WebSocket API
|
|
1683
|
+
* import { app } from 'mokapi'
|
|
1684
|
+
* export default function() {
|
|
1685
|
+
* app.api('Chat API').websocket()
|
|
1686
|
+
* .channel('/chat/{roomId}')
|
|
1687
|
+
* .connect((event) => {
|
|
1688
|
+
* event.client.send({ text: `Welcome to room ${event.channel.params['roomId']}!` })
|
|
1689
|
+
* })
|
|
1690
|
+
* }
|
|
1691
|
+
*
|
|
1692
|
+
* @example
|
|
1693
|
+
* // Two APIs with the same channel — scoping avoids conflicts
|
|
1694
|
+
* import { app } from 'mokapi'
|
|
1695
|
+
* export default function() {
|
|
1696
|
+
* app.api('Chat API').websocket()
|
|
1697
|
+
* .channel('/updates')
|
|
1698
|
+
* .message((event) => { console.log(`Chat: ${event.message}`) })
|
|
1699
|
+
*
|
|
1700
|
+
* app.api('Notifications API').websocket()
|
|
1701
|
+
* .channel('/updates')
|
|
1702
|
+
* .message((event) => { console.log(`Notification: ${event.message}`) })
|
|
1703
|
+
* }
|
|
1704
|
+
*/
|
|
1705
|
+
websocket(): WebsocketRouter
|
|
1634
1706
|
}
|
|
1635
1707
|
|
|
1636
1708
|
/**
|
|
1637
1709
|
* Registers and organizes HTTP event handlers by path, operation, and method.
|
|
1638
1710
|
*
|
|
1639
|
-
*
|
|
1711
|
+
* Get an `HttpRouter` via `app.http()` or `app.api('My API').http()`.
|
|
1640
1712
|
*
|
|
1641
1713
|
* Two styles are supported:
|
|
1642
1714
|
* - **Shorthand**: `router.get(path, handler)` — registers a handler directly for a path and method.
|
|
@@ -1716,7 +1788,7 @@ export interface HttpRouter {
|
|
|
1716
1788
|
* Use this to register multiple method handlers under the same path
|
|
1717
1789
|
* using method chaining, or to attach middleware via `.use()`.
|
|
1718
1790
|
*
|
|
1719
|
-
* The path must match the path template in the OpenAPI specification
|
|
1791
|
+
* The path must match the path template in the OpenAPI specification.
|
|
1720
1792
|
* e.g. `/pets/{petId}`, not the absolute URL.
|
|
1721
1793
|
*
|
|
1722
1794
|
* @param path The OpenAPI path template, e.g. `/pets/{petId}`.
|
|
@@ -1780,7 +1852,7 @@ export interface HttpRouter {
|
|
|
1780
1852
|
* Narrows this router to a specific API by title.
|
|
1781
1853
|
*
|
|
1782
1854
|
* This is an alternative to `app.api(title).http()` for cases where
|
|
1783
|
-
* the router is already obtained and you want to further scope it.
|
|
1855
|
+
* the router is already obtained, and you want to further scope it.
|
|
1784
1856
|
*
|
|
1785
1857
|
* @param title The API title as defined in `info.title` of the OpenAPI spec.
|
|
1786
1858
|
* @returns A new `HttpRouter` scoped to the given API title.
|
|
@@ -1885,7 +1957,7 @@ export interface HttpRouter {
|
|
|
1885
1957
|
* allowing multiple method handlers and middleware to be registered
|
|
1886
1958
|
* via method chaining.
|
|
1887
1959
|
*
|
|
1888
|
-
*
|
|
1960
|
+
* Get an `HttpRoute` via `router.route(path)` or `router.operation(operationId)`.
|
|
1889
1961
|
*
|
|
1890
1962
|
* All methods return `this`, enabling fluent chaining:
|
|
1891
1963
|
* ```ts
|
|
@@ -2039,7 +2111,7 @@ export interface HttpRoute {
|
|
|
2039
2111
|
/**
|
|
2040
2112
|
* Registers and organizes Kafka message handlers and producers by topic.
|
|
2041
2113
|
*
|
|
2042
|
-
*
|
|
2114
|
+
* Get a `KafkaRouter` via `app.kafka()` or `app.api('My API').kafka()`.
|
|
2043
2115
|
*
|
|
2044
2116
|
* Two styles are supported:
|
|
2045
2117
|
* - **Shorthand**: `router.produce(topic, message)` — produces directly to a named topic.
|
|
@@ -2068,6 +2140,26 @@ export interface HttpRoute {
|
|
|
2068
2140
|
* }
|
|
2069
2141
|
*/
|
|
2070
2142
|
export interface KafkaRouter {
|
|
2143
|
+
/**
|
|
2144
|
+
* Narrows this router to a specific API by title.
|
|
2145
|
+
*
|
|
2146
|
+
* This is an alternative to `app.api(title).kafka()` for cases where
|
|
2147
|
+
* the router is already obtained, and you want to further scope it.
|
|
2148
|
+
*
|
|
2149
|
+
* @param title The API title as defined in `info.title` of the OpenAPI spec.
|
|
2150
|
+
* @returns A new `KafkaRouter` scoped to the given API title.
|
|
2151
|
+
*
|
|
2152
|
+
* @example
|
|
2153
|
+
* import { app } from 'mokapi'
|
|
2154
|
+
* export default function() {
|
|
2155
|
+
* app.kafka().api('Order Events').topic('order-placed').message((msg) => {
|
|
2156
|
+
* res.data = []
|
|
2157
|
+
* })
|
|
2158
|
+
* }
|
|
2159
|
+
*/
|
|
2160
|
+
api(title: string): HttpRouter
|
|
2161
|
+
|
|
2162
|
+
|
|
2071
2163
|
/**
|
|
2072
2164
|
* Returns a `KafkaTopic` scoped to the given topic name.
|
|
2073
2165
|
*
|
|
@@ -2140,7 +2232,7 @@ export interface KafkaRouter {
|
|
|
2140
2232
|
* app.kafka().produce('order-placed', [
|
|
2141
2233
|
* { data: { id: 1 } },
|
|
2142
2234
|
* { data: { id: 2 } },
|
|
2143
|
-
* {}
|
|
2235
|
+
* {} // random generated value
|
|
2144
2236
|
* ])
|
|
2145
2237
|
*/
|
|
2146
2238
|
produce(topic: string, message: KafkaProduceMessage | KafkaProduceMessage[], opts?: KafkaProduceArgs): KafkaRouter
|
|
@@ -2358,7 +2450,7 @@ export interface KafkaProduceMessage {
|
|
|
2358
2450
|
*
|
|
2359
2451
|
* Use `data` instead if you want schema-based encoding and validation.
|
|
2360
2452
|
* Use `value` when you need full control over the raw bytes written to Kafka,
|
|
2361
|
-
* for example when testing consumers that expect a specific binary format.
|
|
2453
|
+
* for example, when testing consumers that expect a specific binary format.
|
|
2362
2454
|
*
|
|
2363
2455
|
* @example
|
|
2364
2456
|
* // Raw string
|
|
@@ -2515,7 +2607,7 @@ export interface KafkaMessageResult {
|
|
|
2515
2607
|
|
|
2516
2608
|
/**
|
|
2517
2609
|
* The key of the written Kafka message as a string.
|
|
2518
|
-
* Reflects the key
|
|
2610
|
+
* Reflects the key used for partitioning, whether explicitly
|
|
2519
2611
|
* set or randomly generated by Mokapi.
|
|
2520
2612
|
*/
|
|
2521
2613
|
readonly key: string;
|
|
@@ -2615,7 +2707,7 @@ export interface Retry {
|
|
|
2615
2707
|
/**
|
|
2616
2708
|
* Registers and organizes MQTT message handlers and publishers by topic.
|
|
2617
2709
|
*
|
|
2618
|
-
*
|
|
2710
|
+
* Get an `MqttRouter` via `app.mqtt()` or `app.api('My API').mqtt()`.
|
|
2619
2711
|
*
|
|
2620
2712
|
* Two styles are supported:
|
|
2621
2713
|
* - **Shorthand**: `router.publish(message)` — publishes directly without topic scope.
|
|
@@ -2744,7 +2836,7 @@ export interface MqttRouter {
|
|
|
2744
2836
|
* allowing message handlers and publishers to be registered
|
|
2745
2837
|
* via method chaining.
|
|
2746
2838
|
*
|
|
2747
|
-
*
|
|
2839
|
+
* Get an `MqttTopicRouter` via `router.topic(name)`.
|
|
2748
2840
|
*
|
|
2749
2841
|
* All methods return `this`, enabling fluent chaining:
|
|
2750
2842
|
* ```ts
|
|
@@ -2889,7 +2981,7 @@ export interface MqttPublishMessage {
|
|
|
2889
2981
|
*
|
|
2890
2982
|
* Use `data` instead if you want schema-based encoding and validation.
|
|
2891
2983
|
* Use `value` when you need full control over the raw bytes written to MQTT,
|
|
2892
|
-
* for example when testing subscribers that expect a specific binary format.
|
|
2984
|
+
* for example, when testing subscribers that expect a specific binary format.
|
|
2893
2985
|
*
|
|
2894
2986
|
* @example
|
|
2895
2987
|
* // Raw string
|
|
@@ -2991,8 +3083,412 @@ export interface MqttPublishResult {
|
|
|
2991
3083
|
|
|
2992
3084
|
/**
|
|
2993
3085
|
* The payload of the published message as a string.
|
|
2994
|
-
* Reflects the value
|
|
3086
|
+
* Reflects the value written to the broker, whether
|
|
2995
3087
|
* explicitly set or randomly generated by Mokapi.
|
|
2996
3088
|
*/
|
|
2997
3089
|
readonly value: string;
|
|
3090
|
+
}
|
|
3091
|
+
|
|
3092
|
+
/**
|
|
3093
|
+
* Registers and organizes WebSocket event handlers and server-side sends by channel.
|
|
3094
|
+
*
|
|
3095
|
+
* Get a `WebsocketRouter` via `app.websocket()` or `app.api('My API').websocket()`.
|
|
3096
|
+
*
|
|
3097
|
+
* Two styles are supported:
|
|
3098
|
+
* - **Shorthand**: `router.connect(handler)` — registers a handler for all channels.
|
|
3099
|
+
* - **Scoped**: `router.channel(name).connect(handler)` — scopes handlers to a specific channel.
|
|
3100
|
+
*
|
|
3101
|
+
* Three lifecycle events are available:
|
|
3102
|
+
* - `connect` — fired when a client establishes a connection.
|
|
3103
|
+
* - `message` — fired when a client sends a message.
|
|
3104
|
+
* - `close` — fired when a client disconnects.
|
|
3105
|
+
*
|
|
3106
|
+
* When multiple APIs share the same channel name, use `app.api()` to scope the router
|
|
3107
|
+
* to a specific API title to avoid unintended matches.
|
|
3108
|
+
*
|
|
3109
|
+
* @example
|
|
3110
|
+
* // Shorthand style — all channels
|
|
3111
|
+
* import { app } from 'mokapi'
|
|
3112
|
+
* export default function() {
|
|
3113
|
+
* app.api('Chat API').websocket()
|
|
3114
|
+
* .connect((event) => {
|
|
3115
|
+
* event.broadcast({ text: `${event.client.query['userId']} has joined` })
|
|
3116
|
+
* })
|
|
3117
|
+
* .message((event) => {
|
|
3118
|
+
* event.reply({ text: 'pong' })
|
|
3119
|
+
* })
|
|
3120
|
+
* }
|
|
3121
|
+
*
|
|
3122
|
+
* @example
|
|
3123
|
+
* // Scoped style — specific channel
|
|
3124
|
+
* import { app } from 'mokapi'
|
|
3125
|
+
* export default function() {
|
|
3126
|
+
* app.api('Chat API').websocket()
|
|
3127
|
+
* .channel('/chat/{roomId}')
|
|
3128
|
+
* .connect((event) => {
|
|
3129
|
+
* const room = event.channel.params['roomId']
|
|
3130
|
+
* event.client.send({ text: `Welcome to ${room}!` })
|
|
3131
|
+
* })
|
|
3132
|
+
* .message((event) => {
|
|
3133
|
+
* event.broadcast(event.message)
|
|
3134
|
+
* })
|
|
3135
|
+
* .close((event) => {
|
|
3136
|
+
* event.broadcast({ text: `${event.client.query['userId']} has left` })
|
|
3137
|
+
* })
|
|
3138
|
+
* }
|
|
3139
|
+
*/
|
|
3140
|
+
export interface WebsocketRouter {
|
|
3141
|
+
/**
|
|
3142
|
+
* Narrows this router to a specific API by title.
|
|
3143
|
+
*
|
|
3144
|
+
* This is an alternative to `app.api(title).websocket()` for cases where
|
|
3145
|
+
* the router is already obtained, and you want to further scope it.
|
|
3146
|
+
*
|
|
3147
|
+
* @param name The API title as defined in `info.title` of the AsyncAPI spec.
|
|
3148
|
+
* @returns A new `WebsocketRouter` scoped to the given API title.
|
|
3149
|
+
*
|
|
3150
|
+
* @example
|
|
3151
|
+
* import { app } from 'mokapi'
|
|
3152
|
+
* export default function() {
|
|
3153
|
+
* app.websocket().api('Chat API').message((event) => {
|
|
3154
|
+
* event.reply({ text: 'pong' })
|
|
3155
|
+
* })
|
|
3156
|
+
* }
|
|
3157
|
+
*/
|
|
3158
|
+
api(name: string): WebsocketRouter
|
|
3159
|
+
|
|
3160
|
+
/**
|
|
3161
|
+
* Returns a `WebsocketChannelRouter` scoped to the given channel name.
|
|
3162
|
+
*
|
|
3163
|
+
* Use this to register event handlers or send messages for a specific
|
|
3164
|
+
* WebSocket channel using method chaining, without repeating the channel
|
|
3165
|
+
* name on every call.
|
|
3166
|
+
*
|
|
3167
|
+
* The channel name must match the channel address defined in the
|
|
3168
|
+
* AsyncAPI specification, e.g. `/chat/{roomId}`.
|
|
3169
|
+
*
|
|
3170
|
+
* @param name The channel name as defined in the AsyncAPI specification.
|
|
3171
|
+
* @returns A `WebsocketChannelRouter` scoped to the given channel.
|
|
3172
|
+
*
|
|
3173
|
+
* @example
|
|
3174
|
+
* import { app } from 'mokapi'
|
|
3175
|
+
* export default function() {
|
|
3176
|
+
* app.api('Chat API').websocket()
|
|
3177
|
+
* .channel('/chat/{roomId}')
|
|
3178
|
+
* .connect((event) => {
|
|
3179
|
+
* event.client.send({ text: 'Welcome!' })
|
|
3180
|
+
* })
|
|
3181
|
+
* .message((event) => {
|
|
3182
|
+
* event.broadcast(event.message)
|
|
3183
|
+
* })
|
|
3184
|
+
* }
|
|
3185
|
+
*/
|
|
3186
|
+
channel(name: string): WebsocketChannelRouter
|
|
3187
|
+
|
|
3188
|
+
/**
|
|
3189
|
+
* Registers a handler for `connect` events across all channels.
|
|
3190
|
+
*
|
|
3191
|
+
* Called once when a client establishes a WebSocket connection.
|
|
3192
|
+
* Use this to send a welcome message or notify other clients.
|
|
3193
|
+
*
|
|
3194
|
+
* Use `channel()` to scope the handler to a specific channel.
|
|
3195
|
+
*
|
|
3196
|
+
* @param handler The event handler to invoke on connect.
|
|
3197
|
+
* @param opts Optional handler configuration such as priority and tracking.
|
|
3198
|
+
* @returns This `WebsocketRouter` instance for chaining.
|
|
3199
|
+
*
|
|
3200
|
+
* @example
|
|
3201
|
+
* import { app } from 'mokapi'
|
|
3202
|
+
* export default function() {
|
|
3203
|
+
* app.websocket().connect((event) => {
|
|
3204
|
+
* event.broadcast({ text: `${event.client.query['userId']} has joined` })
|
|
3205
|
+
* })
|
|
3206
|
+
* }
|
|
3207
|
+
*/
|
|
3208
|
+
connect(handler: WebsocketConnectEventHandler, opts?: WebsocketEventArgs): WebsocketRouter
|
|
3209
|
+
|
|
3210
|
+
/**
|
|
3211
|
+
* Registers a handler for `message` events across all channels.
|
|
3212
|
+
*
|
|
3213
|
+
* Called whenever a client sends a message. Use `event.reply()` to
|
|
3214
|
+
* respond to the sender, or `event.broadcast()` to send to all clients.
|
|
3215
|
+
*
|
|
3216
|
+
* Use `channel()` to scope the handler to a specific channel.
|
|
3217
|
+
*
|
|
3218
|
+
* @param handler The event handler to invoke on each incoming message.
|
|
3219
|
+
* @param opts Optional handler configuration such as priority and tracking.
|
|
3220
|
+
* @returns This `WebsocketRouter` instance for chaining.
|
|
3221
|
+
*
|
|
3222
|
+
* @example
|
|
3223
|
+
* import { app } from 'mokapi'
|
|
3224
|
+
* export default function() {
|
|
3225
|
+
* app.websocket().message((event) => {
|
|
3226
|
+
* event.reply({ text: `Echo: ${event.message.text}` })
|
|
3227
|
+
* })
|
|
3228
|
+
* }
|
|
3229
|
+
*/
|
|
3230
|
+
message(handler: WebsocketMessageEventHandler, opts?: WebsocketEventArgs): WebsocketRouter
|
|
3231
|
+
|
|
3232
|
+
/**
|
|
3233
|
+
* Registers a handler for `close` events across all channels.
|
|
3234
|
+
*
|
|
3235
|
+
* Called when a client disconnects, either by the client or the server.
|
|
3236
|
+
* Use `event.closedBy` to determine who initiated the close.
|
|
3237
|
+
*
|
|
3238
|
+
* Use `channel()` to scope the handler to a specific channel.
|
|
3239
|
+
*
|
|
3240
|
+
* @param handler The event handler to invoke on close.
|
|
3241
|
+
* @param opts Optional handler configuration such as priority and tracking.
|
|
3242
|
+
* @returns This `WebsocketRouter` instance for chaining.
|
|
3243
|
+
*
|
|
3244
|
+
* @example
|
|
3245
|
+
* import { app } from 'mokapi'
|
|
3246
|
+
* export default function() {
|
|
3247
|
+
* app.websocket().close((event) => {
|
|
3248
|
+
* console.log(`Client disconnected: ${event.closedBy} — ${event.reason}`)
|
|
3249
|
+
* })
|
|
3250
|
+
* }
|
|
3251
|
+
*/
|
|
3252
|
+
close(handler: WebsocketCloseEventHandler, opts?: WebsocketEventArgs): WebsocketRouter
|
|
3253
|
+
|
|
3254
|
+
/**
|
|
3255
|
+
* Sends a message to all clients currently connected to the given channel.
|
|
3256
|
+
*
|
|
3257
|
+
* Use this for server-initiated pushes outside of an event handler —
|
|
3258
|
+
* for example, from a scheduled job or in response to an external event.
|
|
3259
|
+
*
|
|
3260
|
+
* When no API scope is set, Mokapi searches all APIs for the given channel name.
|
|
3261
|
+
* An error is thrown if the channel is found in more than one API or in none.
|
|
3262
|
+
* Use `app.api('My API').websocket().send()` to avoid ambiguity when
|
|
3263
|
+
* multiple APIs define the same channel.
|
|
3264
|
+
*
|
|
3265
|
+
* @param channel The channel name to send to, as defined in the AsyncAPI specification.
|
|
3266
|
+
* @param message The message payload to send. Will be encoded according to the channel's `contentType`.
|
|
3267
|
+
* @param args Optional configuration including a result callback and retry settings.
|
|
3268
|
+
* @throws Error if the channel is found in more than one API, or if no matching channel exists.
|
|
3269
|
+
*
|
|
3270
|
+
* @example
|
|
3271
|
+
* // Safe when exactly one API defines this channel
|
|
3272
|
+
* import { app, every } from 'mokapi'
|
|
3273
|
+
* export default function() {
|
|
3274
|
+
* every('10s', () => {
|
|
3275
|
+
* app.websocket().send('/updates', { text: 'ping' })
|
|
3276
|
+
* })
|
|
3277
|
+
* }
|
|
3278
|
+
*
|
|
3279
|
+
* @example
|
|
3280
|
+
* // Explicit API scope avoids ambiguity when multiple APIs share the channel name
|
|
3281
|
+
* import { app, every } from 'mokapi'
|
|
3282
|
+
* export default function() {
|
|
3283
|
+
* every('10s', () => {
|
|
3284
|
+
* app.api('Chat API').websocket().send('/updates', { text: 'ping' })
|
|
3285
|
+
* })
|
|
3286
|
+
* }
|
|
3287
|
+
*/
|
|
3288
|
+
send(channel: string, message: any, args?: WebsocketSendArgs): void
|
|
3289
|
+
}
|
|
3290
|
+
|
|
3291
|
+
/**
|
|
3292
|
+
* Represents a single WebSocket channel within a `WebsocketRouter`,
|
|
3293
|
+
* allowing event handlers and server-side sends to be registered
|
|
3294
|
+
* via method chaining.
|
|
3295
|
+
*
|
|
3296
|
+
* Get a `WebsocketChannelRouter` via `router.channel(name)`.
|
|
3297
|
+
*
|
|
3298
|
+
* All event-registration methods return `this`, enabling fluent chaining:
|
|
3299
|
+
* ```ts
|
|
3300
|
+
* router.channel('/chat/{roomId}')
|
|
3301
|
+
* .connect(connectHandler)
|
|
3302
|
+
* .message(messageHandler)
|
|
3303
|
+
* .close(closeHandler)
|
|
3304
|
+
* ```
|
|
3305
|
+
*
|
|
3306
|
+
* @example
|
|
3307
|
+
* import { app } from 'mokapi'
|
|
3308
|
+
* export default function() {
|
|
3309
|
+
* app.api('Chat API').websocket()
|
|
3310
|
+
* .channel('/chat/{roomId}')
|
|
3311
|
+
* .connect((event) => {
|
|
3312
|
+
* const room = event.channel.params['roomId']
|
|
3313
|
+
* event.client.send({ text: `Welcome to ${room}!` })
|
|
3314
|
+
* })
|
|
3315
|
+
* .message((event) => {
|
|
3316
|
+
* event.broadcast(event.message)
|
|
3317
|
+
* })
|
|
3318
|
+
* .close((event) => {
|
|
3319
|
+
* event.broadcast({ text: `${event.client.query['userId']} has left` })
|
|
3320
|
+
* })
|
|
3321
|
+
* }
|
|
3322
|
+
*/
|
|
3323
|
+
export interface WebsocketChannelRouter {
|
|
3324
|
+
/**
|
|
3325
|
+
* Registers a handler for `connect` events on this channel.
|
|
3326
|
+
*
|
|
3327
|
+
* Called once when a client establishes a connection to this channel.
|
|
3328
|
+
* Use `event.client.send()` to send a welcome message, or
|
|
3329
|
+
* `event.broadcast()` to notify all connected clients.
|
|
3330
|
+
*
|
|
3331
|
+
* @param handler The event handler to invoke on connect.
|
|
3332
|
+
* @param opts Optional handler configuration such as priority and tracking.
|
|
3333
|
+
* @returns This `WebsocketChannelRouter` instance for chaining.
|
|
3334
|
+
*
|
|
3335
|
+
* @example
|
|
3336
|
+
* router.channel('/chat/{roomId}')
|
|
3337
|
+
* .connect((event) => {
|
|
3338
|
+
* event.client.send({ text: 'Welcome!' })
|
|
3339
|
+
* })
|
|
3340
|
+
*/
|
|
3341
|
+
connect(handler: WebsocketConnectEventHandler, opts?: WebsocketEventArgs): WebsocketChannelRouter
|
|
3342
|
+
|
|
3343
|
+
/**
|
|
3344
|
+
* Registers a handler for `message` events on this channel.
|
|
3345
|
+
*
|
|
3346
|
+
* Called whenever a client sends a message to this channel.
|
|
3347
|
+
* Use `event.reply()` to respond to the sender, or
|
|
3348
|
+
* `event.broadcast()` to send to all connected clients.
|
|
3349
|
+
*
|
|
3350
|
+
* @param handler The event handler to invoke on each incoming message.
|
|
3351
|
+
* @param opts Optional handler configuration such as priority and tracking.
|
|
3352
|
+
* @returns This `WebsocketChannelRouter` instance for chaining.
|
|
3353
|
+
*
|
|
3354
|
+
* @example
|
|
3355
|
+
* router.channel('/chat/{roomId}')
|
|
3356
|
+
* .message((event) => {
|
|
3357
|
+
* event.broadcast(event.message)
|
|
3358
|
+
* })
|
|
3359
|
+
*/
|
|
3360
|
+
message(handler: WebsocketMessageEventHandler, opts?: WebsocketEventArgs): WebsocketChannelRouter
|
|
3361
|
+
|
|
3362
|
+
/**
|
|
3363
|
+
* Registers a handler for `close` events on this channel.
|
|
3364
|
+
*
|
|
3365
|
+
* Called when a client disconnects from this channel.
|
|
3366
|
+
* Use `event.closedBy` to determine who initiated the close
|
|
3367
|
+
* and `event.reason` for the close explanation.
|
|
3368
|
+
*
|
|
3369
|
+
* @param handler The event handler to invoke on close.
|
|
3370
|
+
* @param opts Optional handler configuration such as priority and tracking.
|
|
3371
|
+
* @returns This `WebsocketChannelRouter` instance for chaining.
|
|
3372
|
+
*
|
|
3373
|
+
* @example
|
|
3374
|
+
* router.channel('/chat/{roomId}')
|
|
3375
|
+
* .close((event) => {
|
|
3376
|
+
* event.broadcast({ text: `${event.client.query['userId']} has left` })
|
|
3377
|
+
* })
|
|
3378
|
+
*/
|
|
3379
|
+
close(handler: WebsocketCloseEventHandler, opts?: WebsocketEventArgs): WebsocketChannelRouter
|
|
3380
|
+
|
|
3381
|
+
/**
|
|
3382
|
+
* Sends a message to all clients currently connected to the given channel.
|
|
3383
|
+
*
|
|
3384
|
+
* Use this for server-initiated pushes outside of an event handler —
|
|
3385
|
+
* for example, from a scheduled job or in response to an external event.
|
|
3386
|
+
*
|
|
3387
|
+
* When no API scope is set, Mokapi searches all APIs for the given channel name.
|
|
3388
|
+
* An error is thrown if the channel is found in more than one API or in none.
|
|
3389
|
+
* Use `app.api('My API').websocket().send()` to avoid ambiguity when
|
|
3390
|
+
* multiple APIs define the same channel.
|
|
3391
|
+
*
|
|
3392
|
+
* @param channel The channel name to send to, as defined in the AsyncAPI specification.
|
|
3393
|
+
* @param message The message payload to send. Will be encoded according to the channel's `contentType`.
|
|
3394
|
+
* @param args Optional configuration including a result callback and retry settings.
|
|
3395
|
+
* @throws Error if the channel is found in more than one API, or if no matching channel exists.
|
|
3396
|
+
*
|
|
3397
|
+
* @example
|
|
3398
|
+
* // Safe when exactly one API defines this channel
|
|
3399
|
+
* import { app, every } from 'mokapi'
|
|
3400
|
+
* export default function() {
|
|
3401
|
+
* every('10s', () => {
|
|
3402
|
+
* app.websocket().send('/updates', { text: 'ping' })
|
|
3403
|
+
* })
|
|
3404
|
+
* }
|
|
3405
|
+
*
|
|
3406
|
+
* @example
|
|
3407
|
+
* // Explicit API scope avoids ambiguity when multiple APIs share the channel name
|
|
3408
|
+
* import { app, every } from 'mokapi'
|
|
3409
|
+
* export default function() {
|
|
3410
|
+
* every('10s', () => {
|
|
3411
|
+
* app.api('Chat API').websocket().send('/updates', { text: 'ping' })
|
|
3412
|
+
* })
|
|
3413
|
+
* }
|
|
3414
|
+
*/
|
|
3415
|
+
send(message: any, args?: WebsocketSendArgs): void
|
|
3416
|
+
}
|
|
3417
|
+
|
|
3418
|
+
/**
|
|
3419
|
+
* Optional configuration for `send()` on `WebsocketRouter` and `WebsocketChannelRouter`.
|
|
3420
|
+
*
|
|
3421
|
+
* Use `result` to inspect the outcome of a send operation.
|
|
3422
|
+
* Use `retry` to configure retry behavior when the channel is not yet available.
|
|
3423
|
+
*
|
|
3424
|
+
* @example
|
|
3425
|
+
* app.api('Chat API').websocket().send('/updates', { text: 'ping' }, {
|
|
3426
|
+
* result: (r) => console.log(`Sent to ${r.channel}: ${r.message}`)
|
|
3427
|
+
* })
|
|
3428
|
+
*/
|
|
3429
|
+
export interface WebsocketSendArgs {
|
|
3430
|
+
/**
|
|
3431
|
+
* Callback invoked after the message has been successfully sent
|
|
3432
|
+
* to all connected clients on the channel.
|
|
3433
|
+
*
|
|
3434
|
+
* @example
|
|
3435
|
+
* app.api('Chat API').websocket()
|
|
3436
|
+
* .channel('/updates')
|
|
3437
|
+
* .send({ text: 'ping' }, {
|
|
3438
|
+
* result: (r) => {
|
|
3439
|
+
* console.log(r.api) // e.g. 'Chat API'
|
|
3440
|
+
* console.log(r.channel) // e.g. '/updates'
|
|
3441
|
+
* console.log(r.message) // e.g. { text: 'ping' }
|
|
3442
|
+
* }
|
|
3443
|
+
* })
|
|
3444
|
+
*/
|
|
3445
|
+
result?: (result: WebsocketSendResult) => void
|
|
3446
|
+
|
|
3447
|
+
/**
|
|
3448
|
+
* Retry configuration applied when the script executes before the
|
|
3449
|
+
* WebSocket channel is fully set up in Mokapi.
|
|
3450
|
+
*
|
|
3451
|
+
* Mokapi uses an exponential backoff strategy based on these settings.
|
|
3452
|
+
* If all retries are exhausted, the send operation fails with an error.
|
|
3453
|
+
*
|
|
3454
|
+
* @example
|
|
3455
|
+
* app.websocket().send('/updates', { text: 'ping' }, {
|
|
3456
|
+
* retry: { retries: 5, initialRetryTime: '1s' }
|
|
3457
|
+
* })
|
|
3458
|
+
*/
|
|
3459
|
+
retry?: Retry;
|
|
3460
|
+
}
|
|
3461
|
+
|
|
3462
|
+
/**
|
|
3463
|
+
* The result of a `send()` operation on a WebSocket channel.
|
|
3464
|
+
*
|
|
3465
|
+
* Contains the API, channel, and message that was sent.
|
|
3466
|
+
* Received via the `result` callback in `WebsocketSendArgs`.
|
|
3467
|
+
*
|
|
3468
|
+
* @example
|
|
3469
|
+
* app.api('Chat API').websocket().send('/updates', { text: 'ping' }, {
|
|
3470
|
+
* result: (r) => {
|
|
3471
|
+
* console.log(r.api) // e.g. 'Chat API'
|
|
3472
|
+
* console.log(r.channel) // e.g. '/updates'
|
|
3473
|
+
* console.log(r.message) // e.g. { text: 'ping' }
|
|
3474
|
+
* }
|
|
3475
|
+
* })
|
|
3476
|
+
*/
|
|
3477
|
+
export interface WebsocketSendResult {
|
|
3478
|
+
/**
|
|
3479
|
+
* The name of the WebSocket API (AsyncAPI `info.title`) where
|
|
3480
|
+
* the message was sent.
|
|
3481
|
+
*/
|
|
3482
|
+
readonly api: string;
|
|
3483
|
+
|
|
3484
|
+
/**
|
|
3485
|
+
* The channel name where the message was sent,
|
|
3486
|
+
* matching the channel address in the AsyncAPI specification.
|
|
3487
|
+
*/
|
|
3488
|
+
readonly channel: string;
|
|
3489
|
+
|
|
3490
|
+
/**
|
|
3491
|
+
* The message payload that was sent to the connected clients.
|
|
3492
|
+
*/
|
|
3493
|
+
readonly message: any;
|
|
2998
3494
|
}
|