go-mokapi 0.49.0 → 0.51.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -128,7 +128,7 @@ Explore tutorials that walk you through mocking different protocols and scenario
128
128
  - [CI/CD Integration with GitHub Actions](https://mokapi.io/resources/tutorials/running-mokapi-in-a-ci-cd-pipeline)\
129
129
  Run Mokapi in automated test pipelines
130
130
 
131
- > More at [mokapi.io/resources](https://mokapi.io/resources)
131
+ More at [mokapi.io/resources](https://mokapi.io/resources)
132
132
 
133
133
  ## Documentation
134
134
 
@@ -144,7 +144,7 @@ Explore tutorials that walk you through mocking different protocols and scenario
144
144
 
145
145
  If Mokapi saves you time, consider buying me a coffee. It helps keep the project going.
146
146
 
147
- <a href="https://www.buymeacoffee.com/mokapi" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>
147
+ [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/mokapi) <a href="https://www.buymeacoffee.com/mokapi" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 30px !important;width: 174px !important;" ></a>
148
148
 
149
149
  ## Merch
150
150
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "go-mokapi",
3
- "version": "0.49.0",
3
+ "version": "0.51.0",
4
4
  "description": "Easy and flexible API mocking",
5
5
  "homepage": "https://mokapi.io",
6
6
  "repository": {
package/types/index.d.ts CHANGED
@@ -227,7 +227,63 @@ export interface HttpResponse {
227
227
  * }
228
228
  * })
229
229
  */
230
- rebuild: (statusCode?: number, contentType?: string) => void;
230
+ rebuild(statusCode?: number, contentType?: string): void;
231
+
232
+ /**
233
+ * A request-scoped key-value store for passing data between handlers
234
+ * registered on the same route.
235
+ *
236
+ * Use `context` to share state between `use()` middleware and method handlers
237
+ * without relying on external variables. Values are only available for the
238
+ * lifetime of the current request.
239
+ *
240
+ * @example
241
+ * import { app } from 'mokapi'
242
+ * export default function() {
243
+ * app.api('Petstore').http()
244
+ * .route('/pets/{petId}')
245
+ * .use((req, res) => {
246
+ * res.context.user = parseToken(req.header['Authorization'])
247
+ * })
248
+ * .get((req, res) => {
249
+ * res.data = { id: req.path.petId, owner: res.context.user.name }
250
+ * })
251
+ * }
252
+ */
253
+ context: Record<string, any>
254
+
255
+ /**
256
+ * Stops the handler pipeline for the current route.
257
+ *
258
+ * By default, all handlers registered on a route run in order.
259
+ * Calling `stopPropagation()` prevents any subsequent handlers
260
+ * on the same route from being executed.
261
+ *
262
+ * This is useful for guard logic such as authentication checks,
263
+ * where further processing should be skipped if a condition is not met.
264
+ *
265
+ * @example
266
+ * import { app } from 'mokapi'
267
+ * export default function() {
268
+ * app.api('Petstore').http()
269
+ * .route('/pets/{petId}')
270
+ * .use((req, res) => {
271
+ * if (!req.header['Authorization']) {
272
+ * res.statusCode = 401
273
+ * res.stopPropagation() // get/delete will not run
274
+ * return
275
+ * }
276
+ * res.context.user = parseToken(req.header['Authorization'])
277
+ * })
278
+ * .get((req, res) => {
279
+ * res.data = { id: req.path.petId, owner: res.context.user.name }
280
+ * })
281
+ * .delete((req, res) => {
282
+ * res.statusCode = 204
283
+ * })
284
+ * }
285
+ */
286
+ stopPropagation(): void
231
287
  }
232
288
 
233
289
  /**
@@ -334,44 +390,82 @@ export interface MqttEventMessage {
334
390
  value: string;
335
391
  }
336
392
 
337
- export type WebsocketEventHandler = (message: WebsocketEventMessage) => void | Promise<void>;
338
-
339
393
  /**
340
- * WebsocketEventMessage is an object passed to a WebSocket event handler whenever
341
- * a message is received from a client. It provides access to the message payload,
342
- * connection metadata, and methods to send messages back.
394
+ * Event handler function passed to `on('websocket', handler)`. Called for every
395
+ * WebSocket lifecycle event — connect, message, and close — on any channel
396
+ * defined in the AsyncAPI specification.
397
+ *
398
+ * Use the `event.type` field to distinguish between event types and access
399
+ * type-specific fields like `message` and `reply`.
400
+ *
401
+ * Async handlers are supported — return a `Promise` if you need to perform
402
+ * asynchronous work before responding.
343
403
  *
344
404
  * @example
345
- * // Simple request/reply
405
+ * // Handle all event types in one handler
346
406
  * import { on } from 'mokapi'
347
407
  * export default function() {
348
408
  * on('websocket', function(event) {
349
- * event.reply({ text: 'pong' })
409
+ * if (event.type === 'connect') {
410
+ * event.client.send({ text: 'welcome!' })
411
+ * }
412
+ * if (event.type === 'message') {
413
+ * event.reply({ text: 'pong' })
414
+ * }
415
+ * if (event.type === 'close') {
416
+ * console.log(`connection closed by ${event.closedBy}: ${event.reason}`)
417
+ * }
350
418
  * })
351
419
  * }
352
420
  *
353
421
  * @example
354
- * // Broadcast to all connected clients (e.g. chat)
422
+ * // Async handler
355
423
  * import { on } from 'mokapi'
356
424
  * export default function() {
357
- * on('websocket', function(event) {
358
- * event.broadcast({ from: event.client.query['userId'], text: event.message.text })
425
+ * on('websocket', async function(event) {
426
+ * if (event.type === 'message') {
427
+ * await someAsyncOperation()
428
+ * event.reply({ text: 'done' })
429
+ * }
359
430
  * })
360
431
  * }
361
432
  *
433
+ * @see https://mokapi.io/docs/javascript-api/mokapi/eventhandler/WebsocketEventHandler
434
+ */
435
+ export type WebsocketEventHandler = (message: WebsocketEvent) => void | Promise<void>;
436
+
437
+ export type WebsocketEvent = WebsocketEventConnect | WebsocketEventMessage | WebsocketEventClose;
438
+
439
+ /**
440
+ * Base interface shared by all WebSocket events. Use the `type` field to
441
+ * discriminate between connect, message, and close events.
442
+ *
362
443
  * @example
363
- * // Store clients and send later
364
444
  * import { on } from 'mokapi'
365
- * const clients: WebsocketClient[] = []
366
445
  * export default function() {
367
446
  * on('websocket', function(event) {
368
- * clients.push(event.client)
447
+ * if (event.type === 'connect') {
448
+ * event.broadcast({ text: 'a new user joined' })
449
+ * }
450
+ * if (event.type === 'message') {
451
+ * event.reply({ text: 'pong' })
452
+ * }
453
+ * if (event.type === 'close') {
454
+ * console.log(`client disconnected: ${event.reason}`)
455
+ * }
369
456
  * })
370
457
  * }
371
458
  *
372
- * @see https://mokapi.io/docs/javascript-api/mokapi/eventhandler/WebsocketEventMessage
459
+ * @see https://mokapi.io/docs/javascript-api/mokapi/eventhandler/WebsocketEvent
373
460
  */
374
- export interface WebsocketEventMessage {
461
+ export interface WebsocketEventBase {
462
+ /**
463
+ * Discriminates between the three WebSocket event types.
464
+ * Use this in a conditional to access type-specific fields like
465
+ * `message` and `reply` (only on `'message'`) or `reason` (only on `'close'`).
466
+ */
467
+ readonly type: 'connect' | 'message' | 'close'
468
+
375
469
  /**
376
470
  * The name of the API, matching the `info.title` field in the AsyncAPI specification.
377
471
  *
@@ -404,6 +498,69 @@ export interface WebsocketEventMessage {
404
498
  */
405
499
  readonly client: WebsocketClient;
406
500
 
501
+ /**
502
+ * Sends a message to all clients currently connected to this channel,
503
+ * including the sender. Shorthand for looping over `event.channel.clients`.
504
+ *
505
+ * @param message - The message payload to send to every connected client.
506
+ *
507
+ * @example
508
+ * // Broadcast a chat message to everyone
509
+ * on('websocket', function(event) {
510
+ * event.broadcast({ from: event.client.query['userId'], text: event.message.text })
511
+ * })
512
+ *
513
+ * @example
514
+ * // Broadcast excluding the sender
515
+ * on('websocket', function(event) {
516
+ * for (const client of event.channel.clients) {
517
+ * if (client.remoteAddr !== event.client.remoteAddr) {
518
+ * client.send(event.message)
519
+ * }
520
+ * }
521
+ * })
522
+ */
523
+ broadcast(message: any): void;
524
+ }
525
+
526
+ /**
527
+ * WebsocketEventMessage is an object passed to a WebSocket event handler whenever
528
+ * a message is received from a client. It provides access to the message payload,
529
+ * connection metadata, and methods to send messages back.
530
+ *
531
+ * @example
532
+ * // Simple request/reply
533
+ * import { on } from 'mokapi'
534
+ * export default function() {
535
+ * on('websocket', function(event) {
536
+ * event.reply({ text: 'pong' })
537
+ * })
538
+ * }
539
+ *
540
+ * @example
541
+ * // Broadcast to all connected clients (e.g. chat)
542
+ * import { on } from 'mokapi'
543
+ * export default function() {
544
+ * on('websocket', function(event) {
545
+ * event.broadcast({ from: event.client.query['userId'], text: event.message.text })
546
+ * })
547
+ * }
548
+ *
549
+ * @example
550
+ * // Store clients and send later
551
+ * import { on } from 'mokapi'
552
+ * const clients: WebsocketClient[] = []
553
+ * export default function() {
554
+ * on('websocket', function(event) {
555
+ * clients.push(event.client)
556
+ * })
557
+ * }
558
+ *
559
+ * @see https://mokapi.io/docs/javascript-api/mokapi/eventhandler/WebsocketEventMessage
560
+ */
561
+ export interface WebsocketEventMessage extends WebsocketEventBase {
562
+ readonly type: 'message'
563
+
407
564
  /**
408
565
  * The decoded message payload. The type depends on the `contentType`
409
566
  * defined in the AsyncAPI specification — typically an object for
@@ -433,30 +590,100 @@ export interface WebsocketEventMessage {
433
590
  * })
434
591
  */
435
592
  reply(message: any): void;
593
+ }
594
+
595
+ /**
596
+ * Fired once when a client establishes a WebSocket connection.
597
+ * Use this to send a welcome message to the connecting client or
598
+ * notify all other clients that someone joined.
599
+ *
600
+ * @example
601
+ * // Send a welcome message to the connecting client
602
+ * import { on } from 'mokapi'
603
+ * export default function() {
604
+ * on('websocket', function(event) {
605
+ * if (event.type === 'connect') {
606
+ * event.client.send({ text: 'welcome!' })
607
+ * }
608
+ * })
609
+ * }
610
+ *
611
+ * @example
612
+ * // Notify all existing clients that someone joined
613
+ * import { on } from 'mokapi'
614
+ * export default function() {
615
+ * on('websocket', function(event) {
616
+ * if (event.type === 'connect') {
617
+ * event.broadcast({ text: `${event.client.query['userId']} has joined` })
618
+ * }
619
+ * })
620
+ * }
621
+ *
622
+ * @see https://mokapi.io/docs/javascript-api/mokapi/eventhandler/WebsocketEventConnect
623
+ */
624
+ export interface WebsocketEventConnect extends WebsocketEventBase {
625
+ readonly type: 'connect'
626
+ }
627
+
628
+ /**
629
+ * Fired when a WebSocket connection is closed, either by the client or the server.
630
+ * Use `closedBy` to determine who initiated the close and `reason` for the explanation.
631
+ *
632
+ * @example
633
+ * // Log when a client disconnects
634
+ * import { on } from 'mokapi'
635
+ * export default function() {
636
+ * on('websocket', function(event) {
637
+ * if (event.type === 'close') {
638
+ * console.log(`connection closed by ${event.closedBy}: ${event.reason}`)
639
+ * }
640
+ * })
641
+ * }
642
+ *
643
+ * @example
644
+ * // Notify remaining clients when someone leaves
645
+ * import { on } from 'mokapi'
646
+ * export default function() {
647
+ * on('websocket', function(event) {
648
+ * if (event.type === 'close') {
649
+ * event.broadcast({ text: `${event.client.query['userId']} has left` })
650
+ * }
651
+ * })
652
+ * }
653
+ *
654
+ * @see https://mokapi.io/docs/javascript-api/mokapi/eventhandler/WebsocketEventClose
655
+ */
656
+ export interface WebsocketEventClose extends WebsocketEventBase {
657
+ readonly type: 'close'
436
658
 
437
659
  /**
438
- * Sends a message to all clients currently connected to this channel,
439
- * including the sender. Shorthand for looping over `event.channel.clients`.
440
- *
441
- * @param message - The message payload to send to every connected client.
660
+ * The reason the connection was closed. Maps to the WebSocket close frame
661
+ * reason string. Empty if no reason was provided.
442
662
  *
443
663
  * @example
444
- * // Broadcast a chat message to everyone
445
664
  * on('websocket', function(event) {
446
- * event.broadcast({ from: event.client.query['userId'], text: event.message.text })
665
+ * if (event.type === 'close') {
666
+ * console.log(event.reason) // e.g. "going away"
667
+ * }
447
668
  * })
669
+ */
670
+ readonly reason: string
671
+
672
+ /**
673
+ * Indicates who initiated the close — either the client or the server.
448
674
  *
449
675
  * @example
450
- * // Broadcast excluding the sender
451
676
  * on('websocket', function(event) {
452
- * for (const client of event.channel.clients) {
453
- * if (client.remoteAddr !== event.client.remoteAddr) {
454
- * client.send(event.message)
677
+ * if (event.type === 'close') {
678
+ * if (event.closedBy === 'client') {
679
+ * console.log('client disconnected')
680
+ * } else {
681
+ * console.log('server closed the connection')
455
682
  * }
456
683
  * }
457
684
  * })
458
685
  */
459
- broadcast(message: any): void;
686
+ readonly closedBy: 'client' | 'server'
460
687
  }
461
688
 
462
689
  /**
@@ -579,6 +806,18 @@ export interface WebsocketChannel {
579
806
  */
580
807
  readonly name: string;
581
808
 
809
+ /**
810
+ * The parameter values extracted from the channel address for this
811
+ * specific channel instance, keyed by parameter name.
812
+ *
813
+ * @example
814
+ * // channel address: /doc/{roomId}
815
+ * on('websocket', function(event) {
816
+ * console.log(event.channel.params['roomId']) // e.g. "room-1"
817
+ * })
818
+ */
819
+ readonly params: Record<string, string>;
820
+
582
821
  /**
583
822
  * All clients currently connected to this channel. Use this for
584
823
  * custom fan-out logic beyond what `broadcast` provides — for
@@ -916,7 +1155,7 @@ export interface WebsocketEventArgs extends EventArgs {
916
1155
  * - undefined: Mokapi determines tracking automatically based on
917
1156
  * whether the message was modified or acknowledged by the handler
918
1157
  */
919
- track?: boolean | ((message: WebsocketEventMessage) => boolean);
1158
+ track?: boolean | ((message: WebsocketEvent) => boolean);
920
1159
  }
921
1160
 
922
1161
  /**
@@ -1151,3 +1390,501 @@ export interface SharedMemory {
1151
1390
  * ```
1152
1391
  */
1153
1392
  export const shared: SharedMemory;
1393
+
1394
+ /**
1395
+ * The central application object for registering HTTP (and future protocol) handlers
1396
+ * in a structured, route-oriented style.
1397
+ *
1398
+ * `app` is a singleton — the same instance is returned every time it is accessed.
1399
+ * Use `app.api()` to scope handlers to a specific API by its OpenAPI `info.title`,
1400
+ * which is required when multiple APIs share the same path.
1401
+ *
1402
+ * @example
1403
+ * // Without API scope — matches all APIs with this path
1404
+ * import { app } from 'mokapi'
1405
+ * export default function() {
1406
+ * app.http().get('/pets', (req, res) => {
1407
+ * res.data = [{ name: 'Bello' }]
1408
+ * })
1409
+ * }
1410
+ *
1411
+ * @example
1412
+ * // With API scope — only matches the 'Petstore' API
1413
+ * import { app } from 'mokapi'
1414
+ * export default function() {
1415
+ * app.api('Petstore').http().get('/pets', (req, res) => {
1416
+ * res.data = [{ name: 'Bello' }]
1417
+ * })
1418
+ * }
1419
+ */
1420
+ export const app: App
1421
+
1422
+ /**
1423
+ * The top-level application interface providing access to protocol routers
1424
+ * and API scoping.
1425
+ *
1426
+ * Use `api()` to narrow handlers to a specific API by its OpenAPI `info.title`.
1427
+ * Use `http()` directly for handlers that apply across all APIs.
1428
+ */
1429
+ export interface App {
1430
+ /**
1431
+ * Creates or returns a scoped context for the given API title.
1432
+ *
1433
+ * The title must match the `info.title` field in the OpenAPI specification.
1434
+ * Scoping is required when multiple APIs define the same path, to avoid
1435
+ * a handler running for unintended APIs.
1436
+ *
1437
+ * @param title The API title as defined in `info.title` of the OpenAPI spec.
1438
+ * @returns An `ApiScope` bound to the given API title.
1439
+ *
1440
+ * @example
1441
+ * import { app } from 'mokapi'
1442
+ * export default function() {
1443
+ * const petstore = app.api('Petstore')
1444
+ * petstore.http().get('/pets', (req, res) => {
1445
+ * res.data = [{ name: 'Bello' }]
1446
+ * })
1447
+ *
1448
+ * const zoo = app.api('Zoo API')
1449
+ * zoo.http().get('/pets', (req, res) => {
1450
+ * res.data = [{ name: 'Simba' }]
1451
+ * })
1452
+ * }
1453
+ */
1454
+ api(title: string): ApiScope
1455
+
1456
+ /**
1457
+ * Returns an `HttpRouter` not bound to any specific API.
1458
+ * Handlers registered here will run for all APIs that match
1459
+ * the given path and method.
1460
+ *
1461
+ * Use this for cross-cutting concerns such as logging, shared headers,
1462
+ * or common error responses.
1463
+ *
1464
+ * @returns An `HttpRouter` with no API scope.
1465
+ *
1466
+ * @example
1467
+ * import { app } from 'mokapi'
1468
+ * export default function() {
1469
+ * app.http().get('/pets', (req, res) => {
1470
+ * res.headers['X-Mock'] = 'true'
1471
+ * })
1472
+ * }
1473
+ */
1474
+ http(): HttpRouter
1475
+ }
1476
+
1477
+ /**
1478
+ * A scoped application context bound to a specific API by its `info.title`.
1479
+ *
1480
+ * Obtain an `ApiScope` via `app.api('My API')`. All handlers registered
1481
+ * through this scope will only run for requests matched to the named API.
1482
+ *
1483
+ * @example
1484
+ * import { app } from 'mokapi'
1485
+ * export default function() {
1486
+ * const petstore = app.api('Petstore')
1487
+ * petstore.http().route('/pets')
1488
+ * .get((req, res) => { res.data = [] })
1489
+ * .post((req, res) => { res.statusCode = 201 })
1490
+ * }
1491
+ */
1492
+ export interface ApiScope {
1493
+ /**
1494
+ * Returns an `HttpRouter` scoped to this API.
1495
+ *
1496
+ * Handlers registered on this router will only run for HTTP requests
1497
+ * that are matched to the API identified by this scope's title.
1498
+ *
1499
+ * @returns An `HttpRouter` bound to this API scope.
1500
+ *
1501
+ * @example
1502
+ * import { app } from 'mokapi'
1503
+ * export default function() {
1504
+ * app.api('Petstore').http().get('/pets', (req, res) => {
1505
+ * res.data = [{ name: 'Bello' }]
1506
+ * })
1507
+ * }
1508
+ */
1509
+ http(): HttpRouter
1510
+ }
1511
+
1512
+ /**
1513
+ * Registers and organizes HTTP event handlers by path, operation, and method.
1514
+ *
1515
+ * Obtain an `HttpRouter` via `app.http()` or `app.api('My API').http()`.
1516
+ *
1517
+ * Two styles are supported:
1518
+ * - **Shorthand**: `router.get(path, handler)` — registers a handler directly for a path and method.
1519
+ * - **Chained**: `router.route(path).get(handler).post(handler)` — groups multiple methods under one path.
1520
+ *
1521
+ * Paths must match the path template defined in the OpenAPI specification,
1522
+ * not the absolute server URL. For example, use `/pets/{petId}`, not `/v1/pets/123`.
1523
+ *
1524
+ * When multiple APIs share the same path, use `app.api()` to scope the router
1525
+ * to a specific API title to avoid unintended matches.
1526
+ *
1527
+ * All handlers registered on the same path and method run in registration order
1528
+ * (or by `priority` if specified). The last handler to write wins.
1529
+ *
1530
+ * @example
1531
+ * // Shorthand style
1532
+ * import { app } from 'mokapi'
1533
+ * export default function() {
1534
+ * const router = app.api('Petstore').http()
1535
+ * router.get('/pets', (req, res) => { res.data = [] })
1536
+ * router.post('/pets', (req, res) => { res.statusCode = 201 })
1537
+ * }
1538
+ *
1539
+ * @example
1540
+ * // Chained style
1541
+ * import { app } from 'mokapi'
1542
+ * export default function() {
1543
+ * app.api('Petstore').http()
1544
+ * .route('/pets')
1545
+ * .get((req, res) => { res.data = [] })
1546
+ * .post((req, res) => { res.statusCode = 201 })
1547
+ * }
1548
+ */
1549
+ export interface HttpRouter {
1550
+
1551
+ /**
1552
+ * Registers a middleware handler that runs for all methods.
1553
+ *
1554
+ * Middleware is useful for shared logic that should execute regardless
1555
+ * of the HTTP method — for example, authentication checks, setting up
1556
+ * `res.ctx`, or adding common response headers.
1557
+ *
1558
+ * Middleware handlers run in registration order relative to other handlers
1559
+ * on the same route. Use `opts.priority` to control ordering explicitly.
1560
+ *
1561
+ * @param handler The middleware handler to invoke.
1562
+ * @param opts Optional handler configuration such as priority and tracking.
1563
+ * @returns This `HttpRoute` instance for chaining.
1564
+ *
1565
+ * @example
1566
+ * router
1567
+ * .use((req, res) => {
1568
+ * const token = req.header['Authorization']
1569
+ * if (!token) {
1570
+ * res.statusCode = 401
1571
+ * return
1572
+ * }
1573
+ * res.ctx.user = parseToken(token)
1574
+ * })
1575
+ * .get((req, res) => {
1576
+ * res.data = { id: req.path.petId, owner: res.ctx.user.name }
1577
+ * })
1578
+ */
1579
+ use(handler: HttpEventHandler, opts?: HttpEventArgs): this
1580
+
1581
+ /**
1582
+ * Returns an `HttpRoute` for the given OpenAPI path template.
1583
+ *
1584
+ * Use this to register multiple method handlers under the same path
1585
+ * using method chaining, or to attach middleware via `.use()`.
1586
+ *
1587
+ * The path must match the path template in the OpenAPI specification,
1588
+ * e.g. `/pets/{petId}`, not the absolute URL.
1589
+ *
1590
+ * @param path The OpenAPI path template, e.g. `/pets/{petId}`.
1591
+ * @returns An `HttpRoute` for the given path.
1592
+ *
1593
+ * @example
1594
+ * router.route('/pets/{petId}')
1595
+ * .use((req, res) => {
1596
+ * if (!req.header['Authorization']) {
1597
+ * res.statusCode = 401
1598
+ * res.stopPropagation() // get/delete will not run
1599
+ * return
1600
+ * }
1601
+ * res.context.user = parseToken(req.header['Authorization'])
1602
+ * })
1603
+ * .get((req, res) => {
1604
+ * res.data = { id: req.path.petId, owner: res.context.user.name }
1605
+ * })
1606
+ * .delete((req, res) => {
1607
+ * res.statusCode = 204
1608
+ * })
1609
+ */
1610
+ route(path: string): HttpRoute
1611
+
1612
+ /**
1613
+ * Returns an `HttpRoute` matched by the given `operationId` as defined
1614
+ * in the OpenAPI specification.
1615
+ *
1616
+ * Use this as an alternative to `route()` when you prefer to reference
1617
+ * operations by name rather than by path template.
1618
+ *
1619
+ * @param operationId The `operationId` defined in the OpenAPI operation.
1620
+ * @returns An `HttpRoute` for the matching operation.
1621
+ *
1622
+ * @example
1623
+ * import { app } from 'mokapi'
1624
+ * export default function() {
1625
+ * app.api('Petstore').http()
1626
+ * .operation('listPets')
1627
+ * .get((req, res) => { res.data = [] })
1628
+ * }
1629
+ */
1630
+ operation(operationId: string): HttpRoute
1631
+
1632
+ /**
1633
+ * Narrows this router to a specific API by title.
1634
+ *
1635
+ * This is an alternative to `app.api(title).http()` for cases where
1636
+ * the router is already obtained and you want to further scope it.
1637
+ *
1638
+ * @param title The API title as defined in `info.title` of the OpenAPI spec.
1639
+ * @returns A new `HttpRouter` scoped to the given API title.
1640
+ *
1641
+ * @example
1642
+ * import { app } from 'mokapi'
1643
+ * export default function() {
1644
+ * app.http().api('Petstore').get('/pets', (req, res) => {
1645
+ * res.data = []
1646
+ * })
1647
+ * }
1648
+ */
1649
+ api(title: string): HttpRouter
1650
+
1651
+ /**
1652
+ * Registers a handler for `GET` requests to the given path.
1653
+ * @param path The OpenAPI path template, e.g. `/pets/{petId}`.
1654
+ * @param handler The event handler to invoke.
1655
+ * @param opts Optional handler configuration such as priority and tracking.
1656
+ *
1657
+ * @example
1658
+ * app.http().get('/pets', (req, res) => { res.data = [] })
1659
+ */
1660
+ get(path: string, handler: HttpEventHandler, opts?: HttpEventArgs): void
1661
+
1662
+ /**
1663
+ * Registers a handler for `POST` requests to the given path.
1664
+ * @param path The OpenAPI path template.
1665
+ * @param handler The event handler to invoke.
1666
+ * @param opts Optional handler configuration such as priority and tracking.
1667
+ *
1668
+ * @example
1669
+ * app.http().post('/pets', (req, res) => { res.statusCode = 201 })
1670
+ */
1671
+ post(path: string, handler: HttpEventHandler, opts?: HttpEventArgs): void
1672
+
1673
+ /**
1674
+ * Registers a handler for `PUT` requests to the given path.
1675
+ * @param path The OpenAPI path template.
1676
+ * @param handler The event handler to invoke.
1677
+ * @param opts Optional handler configuration such as priority and tracking.
1678
+ */
1679
+ put(path: string, handler: HttpEventHandler, opts?: HttpEventArgs): void
1680
+
1681
+ /**
1682
+ * Registers a handler for `PATCH` requests to the given path.
1683
+ * @param path The OpenAPI path template.
1684
+ * @param handler The event handler to invoke.
1685
+ * @param opts Optional handler configuration such as priority and tracking.
1686
+ */
1687
+ patch(path: string, handler: HttpEventHandler, opts?: HttpEventArgs): void
1688
+
1689
+ /**
1690
+ * Registers a handler for `DELETE` requests to the given path.
1691
+ * @param path The OpenAPI path template.
1692
+ * @param handler The event handler to invoke.
1693
+ * @param opts Optional handler configuration such as priority and tracking.
1694
+ */
1695
+ delete(path: string, handler: HttpEventHandler, opts?: HttpEventArgs): void
1696
+
1697
+ /**
1698
+ * Registers a handler for `HEAD` requests to the given path.
1699
+ * @param path The OpenAPI path template.
1700
+ * @param handler The event handler to invoke.
1701
+ * @param opts Optional handler configuration such as priority and tracking.
1702
+ */
1703
+ head(path: string, handler: HttpEventHandler, opts?: HttpEventArgs): void
1704
+
1705
+ /**
1706
+ * Registers a handler for `OPTIONS` requests to the given path.
1707
+ * @param path The OpenAPI path template.
1708
+ * @param handler The event handler to invoke.
1709
+ * @param opts Optional handler configuration such as priority and tracking.
1710
+ */
1711
+ options(path: string, handler: HttpEventHandler, opts?: HttpEventArgs): void
1712
+
1713
+ /**
1714
+ * Registers a handler for `TRACE` requests to the given path.
1715
+ * @param path The OpenAPI path template.
1716
+ * @param handler The event handler to invoke.
1717
+ * @param opts Optional handler configuration such as priority and tracking.
1718
+ */
1719
+ trace(path: string, handler: HttpEventHandler, opts?: HttpEventArgs): void
1720
+
1721
+ /**
1722
+ * Registers a handler for `QUERY` requests to the given path.
1723
+ * @param path The OpenAPI path template.
1724
+ * @param handler The event handler to invoke.
1725
+ * @param opts Optional handler configuration such as priority and tracking.
1726
+ */
1727
+ query(path: string, handler: HttpEventHandler, opts?: HttpEventArgs): void
1728
+
1729
+ /** Allows registering a handler for any HTTP method, including custom ones.
1730
+ * @example
1731
+ * app.http().purge('/cache', (req, res) => { res.statusCode = 200 })
1732
+ */
1733
+ [method: string]: any
1734
+ }
1735
+
1736
+ /**
1737
+ * Represents a single route (path or operation) within an `HttpRouter`,
1738
+ * allowing multiple method handlers and middleware to be registered
1739
+ * via method chaining.
1740
+ *
1741
+ * Obtain an `HttpRoute` via `router.route(path)` or `router.operation(operationId)`.
1742
+ *
1743
+ * All methods return `this`, enabling fluent chaining:
1744
+ * ```ts
1745
+ * router.route('/pets')
1746
+ * .use(authMiddleware)
1747
+ * .get(listHandler)
1748
+ * .post(createHandler)
1749
+ * ```
1750
+ *
1751
+ * Handlers registered on the same route run in registration order
1752
+ * (or by `priority`). The last handler to write a value wins.
1753
+ *
1754
+ * Middleware registered via `use()` runs for all methods on this route
1755
+ * and is well suited for shared logic such as authentication,
1756
+ * request context setup, or response enrichment.
1757
+ *
1758
+ * @example
1759
+ * import { app } from 'mokapi'
1760
+ * export default function() {
1761
+ * app.api('Petstore').http()
1762
+ * .route('/pets/{petId}')
1763
+ * .use((req, res) => {
1764
+ * // Runs before get and delete — set up shared context
1765
+ * res.ctx.requestedAt = Date.now()
1766
+ * })
1767
+ * .get((req, res) => {
1768
+ * res.data = { id: req.path.petId, requestedAt: res.ctx.requestedAt }
1769
+ * })
1770
+ * .delete((req, res) => {
1771
+ * res.statusCode = 204
1772
+ * })
1773
+ * }
1774
+ */
1775
+ export interface HttpRoute {
1776
+
1777
+ /**
1778
+ * Registers a middleware handler that runs for all methods on this route.
1779
+ *
1780
+ * Middleware is useful for shared logic that should execute regardless
1781
+ * of the HTTP method — for example, authentication checks, setting up
1782
+ * `res.ctx`, or adding common response headers.
1783
+ *
1784
+ * Middleware handlers run in registration order relative to other handlers
1785
+ * on the same route. Use `opts.priority` to control ordering explicitly.
1786
+ *
1787
+ * @param handler The middleware handler to invoke.
1788
+ * @param opts Optional handler configuration such as priority and tracking.
1789
+ * @returns This `HttpRoute` instance for chaining.
1790
+ *
1791
+ * @example
1792
+ * router.route('/pets/{petId}')
1793
+ * .use((req, res) => {
1794
+ * const token = req.header['Authorization']
1795
+ * if (!token) {
1796
+ * res.statusCode = 401
1797
+ * return
1798
+ * }
1799
+ * res.ctx.user = parseToken(token)
1800
+ * })
1801
+ * .get((req, res) => {
1802
+ * res.data = { id: req.path.petId, owner: res.ctx.user.name }
1803
+ * })
1804
+ */
1805
+ use(handler: HttpEventHandler, opts?: HttpEventArgs): this
1806
+
1807
+ /**
1808
+ * Registers a handler for `GET` requests on this route.
1809
+ * @param handler The event handler to invoke.
1810
+ * @param opts Optional handler configuration such as priority and tracking.
1811
+ * @returns This `HttpRoute` instance for chaining.
1812
+ *
1813
+ * @example
1814
+ * router.route('/pets').get((req, res) => { res.data = [] })
1815
+ */
1816
+ get(handler: HttpEventHandler, opts?: HttpEventArgs): this
1817
+
1818
+ /**
1819
+ * Registers a handler for `POST` requests on this route.
1820
+ * @param handler The event handler to invoke.
1821
+ * @param opts Optional handler configuration such as priority and tracking.
1822
+ * @returns This `HttpRoute` instance for chaining.
1823
+ *
1824
+ * @example
1825
+ * router.route('/pets').post((req, res) => { res.statusCode = 201 })
1826
+ */
1827
+ post(handler: HttpEventHandler, opts?: HttpEventArgs): this
1828
+
1829
+ /**
1830
+ * Registers a handler for `PUT` requests on this route.
1831
+ * @param handler The event handler to invoke.
1832
+ * @param opts Optional handler configuration such as priority and tracking.
1833
+ * @returns This `HttpRoute` instance for chaining.
1834
+ */
1835
+ put(handler: HttpEventHandler, opts?: HttpEventArgs): this
1836
+
1837
+ /**
1838
+ * Registers a handler for `PATCH` requests on this route.
1839
+ * @param handler The event handler to invoke.
1840
+ * @param opts Optional handler configuration such as priority and tracking.
1841
+ * @returns This `HttpRoute` instance for chaining.
1842
+ */
1843
+ patch(handler: HttpEventHandler, opts?: HttpEventArgs): this
1844
+
1845
+ /**
1846
+ * Registers a handler for `DELETE` requests on this route.
1847
+ * @param handler The event handler to invoke.
1848
+ * @param opts Optional handler configuration such as priority and tracking.
1849
+ * @returns This `HttpRoute` instance for chaining.
1850
+ */
1851
+ delete(handler: HttpEventHandler, opts?: HttpEventArgs): this
1852
+
1853
+ /**
1854
+ * Registers a handler for `HEAD` requests on this route.
1855
+ * @param handler The event handler to invoke.
1856
+ * @param opts Optional handler configuration such as priority and tracking.
1857
+ * @returns This `HttpRoute` instance for chaining.
1858
+ */
1859
+ head(handler: HttpEventHandler, opts?: HttpEventArgs): this
1860
+
1861
+ /**
1862
+ * Registers a handler for `OPTIONS` requests on this route.
1863
+ * @param handler The event handler to invoke.
1864
+ * @param opts Optional handler configuration such as priority and tracking.
1865
+ * @returns This `HttpRoute` instance for chaining.
1866
+ */
1867
+ options(handler: HttpEventHandler, opts?: HttpEventArgs): this
1868
+
1869
+ /**
1870
+ * Registers a handler for `TRACE` requests on this route.
1871
+ * @param handler The event handler to invoke.
1872
+ * @param opts Optional handler configuration such as priority and tracking.
1873
+ * @returns This `HttpRoute` instance for chaining.
1874
+ */
1875
+ trace(handler: HttpEventHandler, opts?: HttpEventArgs): this
1876
+
1877
+ /**
1878
+ * Registers a handler for `QUERY` requests on this route.
1879
+ * @param handler The event handler to invoke.
1880
+ * @param opts Optional handler configuration such as priority and tracking.
1881
+ * @returns This `HttpRoute` instance for chaining.
1882
+ */
1883
+ query(handler: HttpEventHandler, opts?: HttpEventArgs): this
1884
+
1885
+ /** Allows registering a handler for any HTTP method, including custom ones such as `PURGE` or `SEARCH`.
1886
+ * @example
1887
+ * router.route('/cache').purge((req, res) => { res.statusCode = 200 })
1888
+ */
1889
+ [method: string]: any
1890
+ }
package/types/kafka.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { JSONValue } from ".";
2
2
 
3
3
  /**
4
- * Sends a single message to a Kafka topic.
4
+ * Sends messages to a Kafka topic.
5
5
  * https://mokapi.io/docs/javascript-api/mokapi-kafka/produce
6
6
  * @param args - ProduceArgs object contains Kafka produce arguments.
7
7
  * @returns The produce result.
@@ -22,7 +22,7 @@ import { JSONValue } from ".";
22
22
  export function produce(args?: ProduceArgs): ProduceResult;
23
23
 
24
24
  /**
25
- * Sends a single message to a Kafka topic asynchronously.
25
+ * Sends messages to a Kafka topic asynchronously.
26
26
  * https://mokapi.io/docs/javascript-api/mokapi-kafka/produce
27
27
  * @param args - ProduceArgs object contains Kafka produce arguments.
28
28
  * @returns The produce result.