go-mokapi 0.56.0 → 0.58.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.56.0",
3
+ "version": "0.58.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.56.0",
16
- "@go-mokapi/darwin-x64": "0.56.0",
17
- "@go-mokapi/linux-arm64": "0.56.0",
18
- "@go-mokapi/linux-x64": "0.56.0",
19
- "@go-mokapi/win32-arm64": "0.56.0",
20
- "@go-mokapi/win32-x64": "0.56.0"
15
+ "@go-mokapi/darwin-arm64": "0.58.0",
16
+ "@go-mokapi/darwin-x64": "0.58.0",
17
+ "@go-mokapi/linux-arm64": "0.58.0",
18
+ "@go-mokapi/linux-x64": "0.58.0",
19
+ "@go-mokapi/win32-arm64": "0.58.0",
20
+ "@go-mokapi/win32-x64": "0.58.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
@@ -91,12 +91,31 @@ export function date(args?: DateArgs): string;
91
91
 
92
92
  /**
93
93
  * Suspends the execution for the specified duration.
94
+ * It blocks all handlers of the script file, use {@link delay} to delay only the calling handler.
94
95
  * https://mokapi.io/docs/javascript-api/mokapi/sleep
95
96
  * @param time Duration in milliseconds or duration as string with unit.
96
97
  * Valid time units are `ns`, `us` (or `µs`), `ms`, `s`, `m`, `h`
97
98
  */
98
99
  export function sleep(time: number | string): void;
99
100
 
101
+ /**
102
+ * Delays the calling handler for the specified duration without blocking other handlers.
103
+ * The handler must be `async` and `await` the returned `Promise`.
104
+ * https://mokapi.io/docs/javascript-api/mokapi/delay
105
+ * @param time Duration in milliseconds or duration as string with unit.
106
+ * Valid time units are `ns`, `us` (or `µs`), `ms`, `s`, `m`, `h`.
107
+ * A number with a fraction is truncated to whole milliseconds,
108
+ * zero or a negative number resolves right away.
109
+ * @returns A `Promise` that resolves after the given time.
110
+ * @example
111
+ * export default function() {
112
+ * on('http', async (request, response) => {
113
+ * await delay('500ms')
114
+ * })
115
+ * }
116
+ */
117
+ export function delay(time: number | string): Promise<void>;
118
+
100
119
  /**
101
120
  * Specifies the interval of a periodic job.
102
121
  * Interval string is a possibly signed sequence of decimal numbers, each with an optional
@@ -278,25 +297,26 @@ export interface HttpResponse {
278
297
  }
279
298
 
280
299
  /**
281
- * Represents an URL
300
+ * Represents the URL of an HTTP request.
301
+ * https://mokapi.io/docs/javascript-api/mokapi/eventhandler/httprequest
282
302
  */
283
303
  export interface Url {
284
- /** URL scheme. */
304
+ /** URL scheme: `https` if the request was received over TLS, otherwise `http`. */
285
305
  readonly scheme: string;
286
306
 
287
- /** URL host. */
307
+ /** Host name from the Host header of the request, without the port. */
288
308
  readonly host: string;
289
309
 
290
- /** URL port */
310
+ /** Port from the Host header. If missing, the port of the request URL, else 443 (https) or 80 (http). */
291
311
  readonly port: number;
292
312
 
293
- /** URL path. */
313
+ /** Decoded request path, e.g. `/pets/my pet` for `/pets/my%20pet`. */
294
314
  readonly path: string;
295
315
 
296
- /** URL query string. */
316
+ /** Raw query string without the leading `?`, e.g. `limit=10&offset=0`. */
297
317
  readonly query: string;
298
318
 
299
- /** Returns a string representing this Url object. */
319
+ /** Returns scheme, host, path and query, e.g. `http://localhost/pets?limit=10`. The port is not included. */
300
320
  toString(): string;
301
321
  }
302
322
 
@@ -2246,6 +2266,8 @@ export interface KafkaRouter {
2246
2266
  * If neither `value` nor `data` is set on a message, Mokapi generates
2247
2267
  * a random valid value based on the AsyncAPI schema definition.
2248
2268
  *
2269
+ * 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.
2270
+ *
2249
2271
  * @param topic The Kafka topic name to produce to.
2250
2272
  * @param message One or more messages to produce.
2251
2273
  * @param opts Optional configuration including a result callback and retry settings.
@@ -2345,6 +2367,8 @@ export interface KafkaTopic {
2345
2367
  * If neither `value` nor `data` is set on a message, Mokapi generates
2346
2368
  * a random valid value based on the AsyncAPI schema definition.
2347
2369
  *
2370
+ * 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.
2371
+ *
2348
2372
  * @param message One or more messages to produce.
2349
2373
  * @param opts Optional configuration including a result callback and retry settings.
2350
2374
  * @returns A `Promise` resolving to this `KafkaTopic` instance for chaining.
@@ -2400,6 +2424,22 @@ export interface KafkaTopic {
2400
2424
  * })
2401
2425
  */
2402
2426
  export interface KafkaProduceMessage {
2427
+ /**
2428
+ * The key of the message under `channels.<channel>.messages` in the AsyncAPI specification.
2429
+ *
2430
+ * Selects the message used to generate the value and, if no `key` is set, the key
2431
+ * (`bindings.kafka.key`). With `data`, the data is validated against this message only.
2432
+ * An unknown `messageId` throws an error. Without `messageId`, Mokapi chooses a matching
2433
+ * message of the topic.
2434
+ *
2435
+ * @example
2436
+ * app.kafka().produce('orders', [
2437
+ * { messageId: 'orderCreated' },
2438
+ * { messageId: 'orderDeleted', key: 'order-1', value: null } // tombstone
2439
+ * ])
2440
+ */
2441
+ messageId?: string;
2442
+
2403
2443
  /**
2404
2444
  * The zero-based partition index to write the message to.
2405
2445
  * If not specified, Mokapi selects a partition automatically
@@ -2814,6 +2854,8 @@ export interface MqttRouter {
2814
2854
  * If neither `value` nor `data` is set, Mokapi generates a random valid
2815
2855
  * value based on the AsyncAPI schema definition for the topic.
2816
2856
  *
2857
+ * 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.
2858
+ *
2817
2859
  * @param topic The MQTT topic name to publish to.
2818
2860
  * @param message The message to publish.
2819
2861
  * @param opts Optional configuration including a result callback and retry settings.
@@ -2909,6 +2951,8 @@ export interface MqttTopicRouter {
2909
2951
  * If neither `value` nor `data` is set, Mokapi generates a random valid
2910
2952
  * value based on the AsyncAPI schema definition for this topic.
2911
2953
  *
2954
+ * 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.
2955
+ *
2912
2956
  * @param message The message to publish.
2913
2957
  * @param opts Optional configuration including a result callback and retry settings.
2914
2958
  * @returns A `Promise` resolving to this `MqttTopicRouter` instance for chaining.
@@ -2949,6 +2993,18 @@ export interface MqttTopicRouter {
2949
2993
  * app.mqtt().topic('sensors/temperature').publish({})
2950
2994
  */
2951
2995
  export interface MqttPublishMessage {
2996
+ /**
2997
+ * The key of the message under `channels.<channel>.messages` in the AsyncAPI specification.
2998
+ *
2999
+ * Selects the message used to generate the payload or to encode and validate `data`.
3000
+ * An unknown `messageId` throws an error. Without `messageId`, Mokapi chooses a message
3001
+ * of the topic.
3002
+ *
3003
+ * @example
3004
+ * app.mqtt().publish('sensors/events', { messageId: 'alarm' })
3005
+ */
3006
+ messageId?: string;
3007
+
2952
3008
  /**
2953
3009
  * Structured data to be encoded and validated according to the
2954
3010
  * AsyncAPI schema definition for this topic.
@@ -3262,10 +3318,14 @@ export interface WebsocketRouter {
3262
3318
  * Use `app.api('My API').websocket().send()` to avoid ambiguity when
3263
3319
  * multiple APIs define the same channel.
3264
3320
  *
3321
+ * Use `sendAsync()` if you need to await the result.
3322
+ *
3265
3323
  * @param channel The channel name to send to, as defined in the AsyncAPI specification.
3266
3324
  * @param message The message payload to send. Will be encoded according to the channel's `contentType`.
3325
+ * If omitted, a payload is generated from the channel's message (see `args.messageId`).
3267
3326
  * @param args Optional configuration including a result callback and retry settings.
3268
3327
  * @throws Error if the channel is found in more than one API, or if no matching channel exists.
3328
+ * @returns This `WebsocketRouter` instance for chaining.
3269
3329
  *
3270
3330
  * @example
3271
3331
  * // Safe when exactly one API defines this channel
@@ -3285,7 +3345,36 @@ export interface WebsocketRouter {
3285
3345
  * })
3286
3346
  * }
3287
3347
  */
3288
- send(channel: string, message: any, args?: WebsocketSendArgs): void
3348
+ send(channel: string, message?: any, args?: WebsocketSendArgs): WebsocketRouter
3349
+
3350
+ /**
3351
+ * Sends a message to all clients currently connected to the given channel asynchronously.
3352
+ *
3353
+ * Use this when you need to await the send, for example to continue
3354
+ * only after the `result` callback has been invoked.
3355
+ *
3356
+ * When no API scope is set, Mokapi searches all APIs for the given channel name.
3357
+ * An error is thrown if the channel is found in more than one API or in none.
3358
+ * Use `app.api('My API').websocket().sendAsync()` to avoid ambiguity when
3359
+ * multiple APIs define the same channel.
3360
+ *
3361
+ * 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.
3362
+ *
3363
+ * @param channel The channel name to send to, as defined in the AsyncAPI specification.
3364
+ * @param message The message payload to send. Will be encoded according to the channel's `contentType`.
3365
+ * If omitted, a payload is generated from the channel's message (see `args.messageId`).
3366
+ * @param args Optional configuration including a result callback and retry settings.
3367
+ * @returns A `Promise` resolving to this `WebsocketRouter` instance for chaining.
3368
+ *
3369
+ * @example
3370
+ * import { app } from 'mokapi'
3371
+ * export default async function() {
3372
+ * await app.api('Chat API').websocket().sendAsync('/updates', { text: 'ping' }, {
3373
+ * result: (r) => console.log(`Sent to ${r.channel}`)
3374
+ * })
3375
+ * }
3376
+ */
3377
+ sendAsync(channel: string, message?: any, args?: WebsocketSendArgs): Promise<WebsocketRouter>
3289
3378
  }
3290
3379
 
3291
3380
  /**
@@ -3379,44 +3468,63 @@ export interface WebsocketChannelRouter {
3379
3468
  close(handler: WebsocketCloseEventHandler, opts?: WebsocketEventArgs): WebsocketChannelRouter
3380
3469
 
3381
3470
  /**
3382
- * Sends a message to all clients currently connected to the given channel.
3471
+ * Sends a message to all clients currently connected to this channel.
3383
3472
  *
3384
3473
  * Use this for server-initiated pushes outside of an event handler —
3385
3474
  * for example, from a scheduled job or in response to an external event.
3386
3475
  *
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
3476
+ * When no API scope is set, Mokapi searches all APIs for this channel name.
3477
+ * Use `app.api('My API').websocket().channel(name)` to avoid ambiguity when
3390
3478
  * multiple APIs define the same channel.
3391
3479
  *
3392
- * @param channel The channel name to send to, as defined in the AsyncAPI specification.
3480
+ * Use `sendAsync()` if you need to await the result.
3481
+ *
3393
3482
  * @param message The message payload to send. Will be encoded according to the channel's `contentType`.
3483
+ * If omitted, a payload is generated from the channel's message (see `args.messageId`).
3394
3484
  * @param args Optional configuration including a result callback and retry settings.
3485
+ * @returns This `WebsocketChannelRouter` instance for chaining.
3395
3486
  * @throws Error if the channel is found in more than one API, or if no matching channel exists.
3396
3487
  *
3397
3488
  * @example
3398
- * // Safe when exactly one API defines this channel
3399
3489
  * import { app, every } from 'mokapi'
3400
3490
  * export default function() {
3401
3491
  * every('10s', () => {
3402
- * app.websocket().send('/updates', { text: 'ping' })
3492
+ * app.api('Chat API').websocket()
3493
+ * .channel('/updates')
3494
+ * .send({ text: 'ping' })
3403
3495
  * })
3404
3496
  * }
3497
+ */
3498
+ send(message?: any, args?: WebsocketSendArgs): WebsocketChannelRouter
3499
+
3500
+ /**
3501
+ * Sends a message to all clients currently connected to this channel asynchronously.
3502
+ *
3503
+ * Use this when you need to await the send, for example to continue
3504
+ * only after the `result` callback has been invoked.
3505
+ *
3506
+ * 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.
3507
+ *
3508
+ * @param message The message payload to send. Will be encoded according to the channel's `contentType`.
3509
+ * If omitted, a payload is generated from the channel's message (see `args.messageId`).
3510
+ * @param args Optional configuration including a result callback and retry settings.
3511
+ * @returns A `Promise` resolving to this `WebsocketChannelRouter` instance for chaining.
3405
3512
  *
3406
3513
  * @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
- * })
3514
+ * import { app } from 'mokapi'
3515
+ * export default async function() {
3516
+ * await app.api('Chat API').websocket()
3517
+ * .channel('/updates')
3518
+ * .sendAsync({ text: 'ping' }, {
3519
+ * result: (r) => console.log(`Sent to ${r.channel}`)
3520
+ * })
3413
3521
  * }
3414
3522
  */
3415
- send(message: any, args?: WebsocketSendArgs): void
3523
+ sendAsync(message?: any, args?: WebsocketSendArgs): Promise<WebsocketChannelRouter>
3416
3524
  }
3417
3525
 
3418
3526
  /**
3419
- * Optional configuration for `send()` on `WebsocketRouter` and `WebsocketChannelRouter`.
3527
+ * Optional configuration for `send()` and `sendAsync()` on `WebsocketRouter` and `WebsocketChannelRouter`.
3420
3528
  *
3421
3529
  * Use `result` to inspect the outcome of a send operation.
3422
3530
  * Use `retry` to configure retry behavior when the channel is not yet available.
@@ -3427,6 +3535,18 @@ export interface WebsocketChannelRouter {
3427
3535
  * })
3428
3536
  */
3429
3537
  export interface WebsocketSendArgs {
3538
+ /**
3539
+ * The key of the message under `channels.<channel>.messages` in the AsyncAPI specification.
3540
+ *
3541
+ * Without a message, the payload is generated from the selected message. With a message,
3542
+ * the message is validated against the selected message and an invalid message throws an error.
3543
+ * An unknown `messageId` throws an error.
3544
+ *
3545
+ * @example
3546
+ * app.websocket().send('/chat', undefined, { messageId: 'serverMessage' })
3547
+ */
3548
+ messageId?: string;
3549
+
3430
3550
  /**
3431
3551
  * Callback invoked after the message has been successfully sent
3432
3552
  * to all connected clients on the channel.
@@ -3460,7 +3580,7 @@ export interface WebsocketSendArgs {
3460
3580
  }
3461
3581
 
3462
3582
  /**
3463
- * The result of a `send()` operation on a WebSocket channel.
3583
+ * The result of a `send()` or `sendAsync()` operation on a WebSocket channel.
3464
3584
  *
3465
3585
  * Contains the API, channel, and message that was sent.
3466
3586
  * 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