go-mokapi 0.55.0 → 0.57.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "go-mokapi",
3
- "version": "0.55.0",
3
+ "version": "0.57.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": "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"
15
+ "@go-mokapi/darwin-arm64": "0.57.0",
16
+ "@go-mokapi/darwin-x64": "0.57.0",
17
+ "@go-mokapi/linux-arm64": "0.57.0",
18
+ "@go-mokapi/linux-x64": "0.57.0",
19
+ "@go-mokapi/win32-arm64": "0.57.0",
20
+ "@go-mokapi/win32-x64": "0.57.0"
21
21
  },
22
22
  "keywords": [
23
23
  "mock api",
package/types/http.d.ts CHANGED
@@ -87,6 +87,8 @@ export function options(url: string, body?: any, args?: Args): Response;
87
87
  /**
88
88
  * Sends an HTTP request and returns a Promise resolving to the response.
89
89
  *
90
+ * The URL and options are read when you call the function. Invalid options throw an error at the call. Request errors and timeouts reject the returned promise.
91
+ *
90
92
  * @param {string} url - The URL to request.
91
93
  * @param {FetchOptions} opts - Optional fetch configuration.
92
94
  * @returns {Promise<Response>} A Promise that resolves with the response.
package/types/index.d.ts CHANGED
@@ -2157,7 +2157,7 @@ export interface KafkaRouter {
2157
2157
  * })
2158
2158
  * }
2159
2159
  */
2160
- api(title: string): HttpRouter
2160
+ api(title: string): KafkaRouter
2161
2161
 
2162
2162
 
2163
2163
  /**
@@ -2246,6 +2246,8 @@ export interface KafkaRouter {
2246
2246
  * If neither `value` nor `data` is set on a message, Mokapi generates
2247
2247
  * a random valid value based on the AsyncAPI schema definition.
2248
2248
  *
2249
+ * The arguments are read when you call the method, so changing the message afterwards doesn't affect what is sent. Invalid arguments throw an error at the call. Client errors reject the returned promise. An error thrown in the `result` callback also rejects it.
2250
+ *
2249
2251
  * @param topic The Kafka topic name to produce to.
2250
2252
  * @param message One or more messages to produce.
2251
2253
  * @param opts Optional configuration including a result callback and retry settings.
@@ -2345,6 +2347,8 @@ export interface KafkaTopic {
2345
2347
  * If neither `value` nor `data` is set on a message, Mokapi generates
2346
2348
  * a random valid value based on the AsyncAPI schema definition.
2347
2349
  *
2350
+ * The arguments are read when you call the method, so changing the message afterwards doesn't affect what is sent. Invalid arguments throw an error at the call. Client errors reject the returned promise. An error thrown in the `result` callback also rejects it.
2351
+ *
2348
2352
  * @param message One or more messages to produce.
2349
2353
  * @param opts Optional configuration including a result callback and retry settings.
2350
2354
  * @returns A `Promise` resolving to this `KafkaTopic` instance for chaining.
@@ -2400,6 +2404,22 @@ export interface KafkaTopic {
2400
2404
  * })
2401
2405
  */
2402
2406
  export interface KafkaProduceMessage {
2407
+ /**
2408
+ * The key of the message under `channels.<channel>.messages` in the AsyncAPI specification.
2409
+ *
2410
+ * Selects the message used to generate the value and, if no `key` is set, the key
2411
+ * (`bindings.kafka.key`). With `data`, the data is validated against this message only.
2412
+ * An unknown `messageId` throws an error. Without `messageId`, Mokapi chooses a matching
2413
+ * message of the topic.
2414
+ *
2415
+ * @example
2416
+ * app.kafka().produce('orders', [
2417
+ * { messageId: 'orderCreated' },
2418
+ * { messageId: 'orderDeleted', key: 'order-1', value: null } // tombstone
2419
+ * ])
2420
+ */
2421
+ messageId?: string;
2422
+
2403
2423
  /**
2404
2424
  * The zero-based partition index to write the message to.
2405
2425
  * If not specified, Mokapi selects a partition automatically
@@ -2814,6 +2834,8 @@ export interface MqttRouter {
2814
2834
  * If neither `value` nor `data` is set, Mokapi generates a random valid
2815
2835
  * value based on the AsyncAPI schema definition for the topic.
2816
2836
  *
2837
+ * The arguments are read when you call the method, so changing the message afterwards doesn't affect what is sent. Invalid arguments throw an error at the call. Client errors reject the returned promise. An error thrown in the `result` callback also rejects it.
2838
+ *
2817
2839
  * @param topic The MQTT topic name to publish to.
2818
2840
  * @param message The message to publish.
2819
2841
  * @param opts Optional configuration including a result callback and retry settings.
@@ -2909,6 +2931,8 @@ export interface MqttTopicRouter {
2909
2931
  * If neither `value` nor `data` is set, Mokapi generates a random valid
2910
2932
  * value based on the AsyncAPI schema definition for this topic.
2911
2933
  *
2934
+ * The arguments are read when you call the method, so changing the message afterwards doesn't affect what is sent. Invalid arguments throw an error at the call. Client errors reject the returned promise. An error thrown in the `result` callback also rejects it.
2935
+ *
2912
2936
  * @param message The message to publish.
2913
2937
  * @param opts Optional configuration including a result callback and retry settings.
2914
2938
  * @returns A `Promise` resolving to this `MqttTopicRouter` instance for chaining.
@@ -2949,6 +2973,18 @@ export interface MqttTopicRouter {
2949
2973
  * app.mqtt().topic('sensors/temperature').publish({})
2950
2974
  */
2951
2975
  export interface MqttPublishMessage {
2976
+ /**
2977
+ * The key of the message under `channels.<channel>.messages` in the AsyncAPI specification.
2978
+ *
2979
+ * Selects the message used to generate the payload or to encode and validate `data`.
2980
+ * An unknown `messageId` throws an error. Without `messageId`, Mokapi chooses a message
2981
+ * of the topic.
2982
+ *
2983
+ * @example
2984
+ * app.mqtt().publish('sensors/events', { messageId: 'alarm' })
2985
+ */
2986
+ messageId?: string;
2987
+
2952
2988
  /**
2953
2989
  * Structured data to be encoded and validated according to the
2954
2990
  * AsyncAPI schema definition for this topic.
@@ -3262,10 +3298,14 @@ export interface WebsocketRouter {
3262
3298
  * Use `app.api('My API').websocket().send()` to avoid ambiguity when
3263
3299
  * multiple APIs define the same channel.
3264
3300
  *
3301
+ * Use `sendAsync()` if you need to await the result.
3302
+ *
3265
3303
  * @param channel The channel name to send to, as defined in the AsyncAPI specification.
3266
3304
  * @param message The message payload to send. Will be encoded according to the channel's `contentType`.
3305
+ * If omitted, a payload is generated from the channel's message (see `args.messageId`).
3267
3306
  * @param args Optional configuration including a result callback and retry settings.
3268
3307
  * @throws Error if the channel is found in more than one API, or if no matching channel exists.
3308
+ * @returns This `WebsocketRouter` instance for chaining.
3269
3309
  *
3270
3310
  * @example
3271
3311
  * // Safe when exactly one API defines this channel
@@ -3285,7 +3325,36 @@ export interface WebsocketRouter {
3285
3325
  * })
3286
3326
  * }
3287
3327
  */
3288
- send(channel: string, message: any, args?: WebsocketSendArgs): void
3328
+ send(channel: string, message?: any, args?: WebsocketSendArgs): WebsocketRouter
3329
+
3330
+ /**
3331
+ * Sends a message to all clients currently connected to the given channel asynchronously.
3332
+ *
3333
+ * Use this when you need to await the send, for example to continue
3334
+ * only after the `result` callback has been invoked.
3335
+ *
3336
+ * When no API scope is set, Mokapi searches all APIs for the given channel name.
3337
+ * An error is thrown if the channel is found in more than one API or in none.
3338
+ * Use `app.api('My API').websocket().sendAsync()` to avoid ambiguity when
3339
+ * multiple APIs define the same channel.
3340
+ *
3341
+ * The arguments are read when you call the method, so changing the message afterwards doesn't affect what is sent. Invalid arguments throw an error at the call. Client errors reject the returned promise. An error thrown in the `result` callback also rejects it.
3342
+ *
3343
+ * @param channel The channel name to send to, as defined in the AsyncAPI specification.
3344
+ * @param message The message payload to send. Will be encoded according to the channel's `contentType`.
3345
+ * If omitted, a payload is generated from the channel's message (see `args.messageId`).
3346
+ * @param args Optional configuration including a result callback and retry settings.
3347
+ * @returns A `Promise` resolving to this `WebsocketRouter` instance for chaining.
3348
+ *
3349
+ * @example
3350
+ * import { app } from 'mokapi'
3351
+ * export default async function() {
3352
+ * await app.api('Chat API').websocket().sendAsync('/updates', { text: 'ping' }, {
3353
+ * result: (r) => console.log(`Sent to ${r.channel}`)
3354
+ * })
3355
+ * }
3356
+ */
3357
+ sendAsync(channel: string, message?: any, args?: WebsocketSendArgs): Promise<WebsocketRouter>
3289
3358
  }
3290
3359
 
3291
3360
  /**
@@ -3379,44 +3448,63 @@ export interface WebsocketChannelRouter {
3379
3448
  close(handler: WebsocketCloseEventHandler, opts?: WebsocketEventArgs): WebsocketChannelRouter
3380
3449
 
3381
3450
  /**
3382
- * Sends a message to all clients currently connected to the given channel.
3451
+ * Sends a message to all clients currently connected to this channel.
3383
3452
  *
3384
3453
  * Use this for server-initiated pushes outside of an event handler —
3385
3454
  * for example, from a scheduled job or in response to an external event.
3386
3455
  *
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
3456
+ * When no API scope is set, Mokapi searches all APIs for this channel name.
3457
+ * Use `app.api('My API').websocket().channel(name)` to avoid ambiguity when
3390
3458
  * multiple APIs define the same channel.
3391
3459
  *
3392
- * @param channel The channel name to send to, as defined in the AsyncAPI specification.
3460
+ * Use `sendAsync()` if you need to await the result.
3461
+ *
3393
3462
  * @param message The message payload to send. Will be encoded according to the channel's `contentType`.
3463
+ * If omitted, a payload is generated from the channel's message (see `args.messageId`).
3394
3464
  * @param args Optional configuration including a result callback and retry settings.
3465
+ * @returns This `WebsocketChannelRouter` instance for chaining.
3395
3466
  * @throws Error if the channel is found in more than one API, or if no matching channel exists.
3396
3467
  *
3397
3468
  * @example
3398
- * // Safe when exactly one API defines this channel
3399
3469
  * import { app, every } from 'mokapi'
3400
3470
  * export default function() {
3401
3471
  * every('10s', () => {
3402
- * app.websocket().send('/updates', { text: 'ping' })
3472
+ * app.api('Chat API').websocket()
3473
+ * .channel('/updates')
3474
+ * .send({ text: 'ping' })
3403
3475
  * })
3404
3476
  * }
3477
+ */
3478
+ send(message?: any, args?: WebsocketSendArgs): WebsocketChannelRouter
3479
+
3480
+ /**
3481
+ * Sends a message to all clients currently connected to this channel asynchronously.
3482
+ *
3483
+ * Use this when you need to await the send, for example to continue
3484
+ * only after the `result` callback has been invoked.
3485
+ *
3486
+ * The arguments are read when you call the method, so changing the message afterwards doesn't affect what is sent. Invalid arguments throw an error at the call. Client errors reject the returned promise. An error thrown in the `result` callback also rejects it.
3487
+ *
3488
+ * @param message The message payload to send. Will be encoded according to the channel's `contentType`.
3489
+ * If omitted, a payload is generated from the channel's message (see `args.messageId`).
3490
+ * @param args Optional configuration including a result callback and retry settings.
3491
+ * @returns A `Promise` resolving to this `WebsocketChannelRouter` instance for chaining.
3405
3492
  *
3406
3493
  * @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
- * })
3494
+ * import { app } from 'mokapi'
3495
+ * export default async function() {
3496
+ * await app.api('Chat API').websocket()
3497
+ * .channel('/updates')
3498
+ * .sendAsync({ text: 'ping' }, {
3499
+ * result: (r) => console.log(`Sent to ${r.channel}`)
3500
+ * })
3413
3501
  * }
3414
3502
  */
3415
- send(message: any, args?: WebsocketSendArgs): void
3503
+ sendAsync(message?: any, args?: WebsocketSendArgs): Promise<WebsocketChannelRouter>
3416
3504
  }
3417
3505
 
3418
3506
  /**
3419
- * Optional configuration for `send()` on `WebsocketRouter` and `WebsocketChannelRouter`.
3507
+ * Optional configuration for `send()` and `sendAsync()` on `WebsocketRouter` and `WebsocketChannelRouter`.
3420
3508
  *
3421
3509
  * Use `result` to inspect the outcome of a send operation.
3422
3510
  * Use `retry` to configure retry behavior when the channel is not yet available.
@@ -3427,6 +3515,18 @@ export interface WebsocketChannelRouter {
3427
3515
  * })
3428
3516
  */
3429
3517
  export interface WebsocketSendArgs {
3518
+ /**
3519
+ * The key of the message under `channels.<channel>.messages` in the AsyncAPI specification.
3520
+ *
3521
+ * Without a message, the payload is generated from the selected message. With a message,
3522
+ * the message is validated against the selected message and an invalid message throws an error.
3523
+ * An unknown `messageId` throws an error.
3524
+ *
3525
+ * @example
3526
+ * app.websocket().send('/chat', undefined, { messageId: 'serverMessage' })
3527
+ */
3528
+ messageId?: string;
3529
+
3430
3530
  /**
3431
3531
  * Callback invoked after the message has been successfully sent
3432
3532
  * to all connected clients on the channel.
@@ -3460,7 +3560,7 @@ export interface WebsocketSendArgs {
3460
3560
  }
3461
3561
 
3462
3562
  /**
3463
- * The result of a `send()` operation on a WebSocket channel.
3563
+ * The result of a `send()` or `sendAsync()` operation on a WebSocket channel.
3464
3564
  *
3465
3565
  * Contains the API, channel, and message that was sent.
3466
3566
  * Received via the `result` callback in `WebsocketSendArgs`.
package/types/kafka.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  /**
2
2
  * Sends one or more messages to a Kafka topic.
3
+ *
4
+ * Arguments are read when you call the function. Invalid arguments throw an error at the call. Client errors reject the returned promise.
3
5
  * https://mokapi.io/docs/javascript-api/mokapi-kafka/produce
4
6
  * @param args - Produce arguments. If omitted, a random message is written to a random topic.
5
7
  * @returns Information about the written messages.
@@ -139,6 +141,12 @@ export interface ProduceArgs {
139
141
  * Represents a Kafka message to write.
140
142
  */
141
143
  export interface Message {
144
+ /**
145
+ * The key of the message under `channels.<channel>.messages` in the AsyncAPI specification.
146
+ * Selects the message used to generate the value and key. An unknown `messageId` throws an error.
147
+ */
148
+ messageId?: string;
149
+
142
150
  /** Kafka partition index. If not specified, the partition is chosen based on the key. */
143
151
  partition?: number;
144
152
 
package/types/mqtt.d.ts CHANGED
@@ -15,6 +15,8 @@ export function publish(args?: PublishArgs): PublishResult;
15
15
 
16
16
  /**
17
17
  * Sends a single message to a MQTT topic asynchronously.
18
+ *
19
+ * Arguments are read when you call the function. Invalid arguments throw an error at the call. Client errors reject the returned promise.
18
20
  * https://mokapi.io/docs/javascript-api/mokapi-mqtt/publishAsync
19
21
  * @param args - PublishArgs object contains MQTT publish arguments.
20
22
  * @returns The publish result.
@@ -46,8 +48,14 @@ export interface PublishArgs {
46
48
  /** MQTT topic name. If not specified, message will be written to a random topic. */
47
49
  topic?: string
48
50
 
51
+ /**
52
+ * The key of the message under `channels.<channel>.messages` in the AsyncAPI specification.
53
+ * Selects the message used to generate the value. An unknown `messageId` throws an error.
54
+ */
55
+ messageId?: string;
56
+
49
57
  /** MQTT message value. If not specified, a random value will be generated based on the topic configuration. */
50
- value: string;
58
+ value?: string;
51
59
 
52
60
  /** Whether the message should be retained */
53
61
  retain?: boolean