go-mokapi 0.44.0 → 0.48.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
@@ -1,9 +1,11 @@
1
1
  <p align="center">
2
2
  <a href="https://mokapi.io">
3
- <img src="https://raw.githubusercontent.com/marle3003/mokapi/refs/heads/main/logo.svg" alt="Mokapi" title="Mokapi" width="300" />
3
+ <img src="https://raw.githubusercontent.com/marle3003/mokapi/refs/heads/main/logo.svg" alt="Mokapi - Open Source Mock API Server" width="300" />
4
4
  </a>
5
5
  </p>
6
- <h3 align="center">Mock APIs Across Protocols. Test Faster. Ship Better.</h3>
6
+
7
+ # The Open-Source Mock API Tool Across Protocols
8
+
7
9
  <p align="center">
8
10
  <a href="https://www.npmjs.com/package/go-mokapi"><img src="https://img.shields.io/npm/v/go-mokapi.svg" alt="npm version"></a>
9
11
  <a href="https://github.com/marle3003/mokapi/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="License"></a>
@@ -12,9 +14,8 @@
12
14
 
13
15
  ## What is Mokapi?
14
16
 
15
- Mokapi is an open-source API mocking tool that lets you develop and test without waiting
16
- for backends. Mock REST APIs, Kafka topics, LDAP directories, and SMTP servers using
17
- OpenAPI and AsyncAPI specifications.
17
+ Mokapi is an open-source [mock API](https://mokapi.io) tool for development and testing without backend dependencies. Easily mock
18
+ REST APIs, Kafka topics, LDAP directories, and SMTP servers using OpenAPI or AsyncAPI.
18
19
 
19
20
  Perfect for:
20
21
  - Frontend developers building UIs before backends exist
@@ -24,13 +25,13 @@ Perfect for:
24
25
 
25
26
  ## Quick Start
26
27
 
27
- Try Instantly
28
+ Try the **mock API** instantly:
28
29
 
29
30
  ```
30
31
  npx go-mokapi https://petstore3.swagger.io/api/v3/openapi.json
31
32
  ```
32
33
 
33
- Then test your mock:
34
+ Then test your local **mock API**:
34
35
 
35
36
  ```
36
37
  curl http://localhost/api/v3/pet/1 -H 'Accept: application/json'
@@ -49,15 +50,15 @@ Check other installation methods [here](https://mokapi.io/docs/get-started/insta
49
50
 
50
51
  ## Key Features
51
52
 
52
- ### Multi-Protocol Support
53
+ ### Multi-Protocol Mock API Support
53
54
  Mock HTTP/HTTPS, Apache Kafka, LDAP, and SMTP — all from a single tool.
54
55
 
55
56
  ### Specification-Driven
56
- Uses OpenAPI and AsyncAPI specs as the source of truth. Your mocks stay aligned with your API contracts.
57
+ Uses OpenAPI and AsyncAPI specs as the source of truth. Your **mock API** stays aligned with your API contracts.
57
58
 
58
59
  ### Dynamic Behavior with JavaScript
59
60
 
60
- Control responses, simulate errors, add delays, or create complex workflows using embedded JavaScript:
61
+ Control responses, simulate errors, add delays, or create complex workflows in your **mock API** using embedded JavaScript:
61
62
  ```javascript
62
63
  import { on } from 'mokapi'
63
64
 
@@ -76,7 +77,7 @@ export default function() {
76
77
  ```
77
78
 
78
79
  ### Everything as Code
79
- Version control your mocks alongside your code. Run them in CI/CD pipelines. No UI configuration required.
80
+ Version control your **mock API** alongside your code. Run them in CI/CD pipelines. No UI configuration required.
80
81
 
81
82
  ### Configuration Patching
82
83
  Override parts of your OpenAPI spec without modifying the original file. Perfect for testing different scenarios.
@@ -106,8 +107,8 @@ Validate that your requests and responses match your OpenAPI specification. Catc
106
107
 
107
108
  Explore tutorials that walk you through mocking different protocols and scenarios:
108
109
 
109
- - [Get started with REST API](https://mokapi.io/resources/tutorials/get-started-with-rest-api)
110
- Mock a REST API using OpenAPI specification
110
+ - [Get started with REST API Mocking](https://mokapi.io/resources/tutorials/get-started-with-rest-api)
111
+ Deploy a REST **mock API** using OpenAPI specification
111
112
 
112
113
  - [Mock Kafka with AsyncAPI](https://mokapi.io/resources/tutorials/get-started-with-kafka)
113
114
  Simulate Kafka topics and validate message producers
@@ -148,6 +149,7 @@ MIT License - see [LICENSE](https://github.com/marle3003/mokapi/blob/main/LICENS
148
149
  - Website: [mokapi.io](https://mokapi.io)
149
150
  - GitHub: [github.com/marle3003/mokapi](https://github.com/marle3003/mokapi)
150
151
  - NPM Package: [npmjs.com/package/go-mokapi](https://npmjs.com/package/go-mokapi)
152
+ - Docker Image: [https://hub.docker.com/r/mokapi/mokapi](https://hub.docker.com/r/mokapi/mokapi)
151
153
  - Documentation: [mokapi.io/docs](https://mokapi.io/docs)
152
154
  - Tutorials: [mokapi.io/resources/tutorials](https://mokapi.io/resources/tutorials)
153
155
  - Blog: [mokapi.io/resources/blogs](https://mokapi.io/resources/blogs)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "go-mokapi",
3
- "version": "0.44.0",
3
+ "version": "0.48.0",
4
4
  "description": "Easy and flexible API mocking",
5
5
  "homepage": "https://mokapi.io",
6
6
  "repository": {
@@ -20,13 +20,16 @@
20
20
  "@go-mokapi/win32-x64": "*"
21
21
  },
22
22
  "keywords": [
23
- "mock",
24
- "api",
25
- "http",
26
- "kafka",
27
- "ldap",
28
- "smtp",
29
- "imap"
23
+ "mock api",
24
+ "api mock",
25
+ "api mocking",
26
+ "kafka mock",
27
+ "ldap mock",
28
+ "smtp mock",
29
+ "imap mock",
30
+ "http mock",
31
+ "openapi",
32
+ "asyncapi"
30
33
  ],
31
34
  "author": "Marcel Lehmann",
32
35
  "license": "MIT",
package/types/index.d.ts CHANGED
@@ -12,6 +12,8 @@
12
12
  import "./faker";
13
13
  import "./global";
14
14
  import "./http";
15
+ import "./kafka";
16
+ import "./mqtt";
15
17
  import "./mustache";
16
18
  import "./yaml";
17
19
  import "./encoding";
@@ -115,6 +117,7 @@ export type Interval = string;
115
117
  export interface EventHandler {
116
118
  http: HttpEventHandler;
117
119
  kafka: KafkaEventHandler;
120
+ mqtt: MqttEventHandler;
118
121
  ldap: LdapEventHandler;
119
122
  smtp: SmtpEventHandler;
120
123
  }
@@ -256,7 +259,7 @@ export interface Url {
256
259
  * export default function() {
257
260
  * on('kafka', function(msg) {
258
261
  * // add header 'foo' to every Kafka message
259
- * msg.headers = { foo: 'bar' }
262
+ * message.headers['correlationId'] = 'abc-123'
260
263
  * })
261
264
  * }
262
265
  */
@@ -267,17 +270,67 @@ export type KafkaEventHandler = (message: KafkaEventMessage) => void | Promise<v
267
270
  * https://mokapi.io/docs/javascript-api/mokapi/eventhandler/KafkaEventMessage
268
271
  */
269
272
  export interface KafkaEventMessage {
270
- /** Kafka offset of the kafka message */
273
+ /** The name of the API, matching the `info.title` field in the AsyncAPI specification */
274
+ readonly api: string;
275
+
276
+ /**
277
+ * The name of the Kafka topic where this message originates.
278
+ */
279
+ readonly topic: string;
280
+
281
+ /**
282
+ * The partition index within the Kafka topic.
283
+ */
284
+ readonly partition: string;
285
+
286
+ /**
287
+ * The unique sequential offset assigned to the message within its partition.
288
+ */
271
289
  readonly offset: number;
272
290
 
273
- /** Kafka message key */
291
+ /**
292
+ * The key of the Kafka message, used for partitioning and identification.
293
+ */
274
294
  key: string;
275
295
 
276
- /** Kafka message value */
296
+ /** The Kafka message value */
277
297
  value: string;
278
298
 
279
- /** Kafka message headers */
299
+ /** Key-value pairs representing optional Kafka message headers. */
280
300
  headers: { [name: string]: string } | null;
301
+
302
+ /**
303
+ * The identifier of the schema used to validate or serialize this message payload.
304
+ *
305
+ * @remarks
306
+ * * This property is **only set** automatically if the schema ID is embedded within the
307
+ * message payload itself (matching `SchemaIdLocation: "payload"`).
308
+ * * If the ID is transmitted via message metadata instead (matching `SchemaIdLocation: "header"`),
309
+ * it will **not** populate this field and must instead be extracted manually from the {@link headers} object.
310
+ */
311
+ schemaId: number;
312
+ }
313
+
314
+ export type MqttEventHandler = (message: MqttEventMessage) => void | Promise<void>;
315
+
316
+ /**
317
+ * KafkaEventMessage is an object used by KafkaEventHandler that contains Kafka-specific message data.
318
+ * https://mokapi.io/docs/javascript-api/mokapi/eventhandler/KafkaEventMessage
319
+ */
320
+ export interface MqttEventMessage {
321
+ /** The name of the API, matching the `info.title` field in the AsyncAPI specification */
322
+ readonly api: string;
323
+
324
+ /**
325
+ * The name of the MQTT topic where this message originates.
326
+ */
327
+ readonly topic: string;
328
+
329
+ /** Specifies whether message should be retained */
330
+ retain: boolean;
331
+
332
+ /** MQTT message value */
333
+ value: string;
281
334
  }
282
335
 
283
336
  /**
@@ -517,6 +570,10 @@ export interface TypedEventArgs {
517
570
  * Arguments for Kafka event handlers.
518
571
  */
519
572
  kafka: KafkaEventArgs;
573
+ /**
574
+ * Arguments for MQTT event handlers.
575
+ */
576
+ mqtt: MqttEventArgs;
520
577
  /**
521
578
  * Arguments for LDAP event handlers.
522
579
  */
@@ -563,6 +620,24 @@ export interface KafkaEventArgs extends EventArgs {
563
620
  track?: boolean | ((message: KafkaEventMessage) => boolean);
564
621
  }
565
622
 
623
+ /**
624
+ * Configuration options for MQTT event handlers.
625
+ *
626
+ * These arguments control execution behavior such as
627
+ * priority, tagging, and dashboard tracking.
628
+ */
629
+ export interface MqttEventArgs extends EventArgs {
630
+ /**
631
+ * Controls whether this event handler is tracked in the dashboard.
632
+ *
633
+ * - true: always track this handler
634
+ * - false: never track this handler
635
+ * - undefined: Mokapi determines tracking automatically based on
636
+ * whether the message was modified or acknowledged by the handler
637
+ */
638
+ track?: boolean | ((message: MqttEventMessage) => boolean);
639
+ }
640
+
566
641
  /**
567
642
  * Configuration options for LDAP event handlers.
568
643
  *
@@ -791,7 +866,7 @@ export interface SharedMemory {
791
866
  *
792
867
  * // Retrieve the current counter value
793
868
  * const count = mokapi.shared.get("counter")
794
- * mokapi.log(`Current counter: ${count}`)
869
+ * console.log(`Current counter: ${count}`)
795
870
  * ```
796
871
  */
797
872
  export const shared: SharedMemory;
package/types/kafka.d.ts CHANGED
@@ -165,32 +165,36 @@ export interface ProduceResult {
165
165
  }
166
166
 
167
167
  /**
168
- * Contains information of the written Kafka message.
168
+ * Represents the outcome of producing a Kafka message in Mokapi.
169
+ *
170
+ * Provides details about the target topic location, message payloads,
171
+ * and schema validation outcomes.
172
+ *
169
173
  * https://mokapi.io/docs/javascript-api/mokapi-kafka/kafkamessageresult
170
174
  */
171
175
  export interface MessageResult {
172
176
  /**
173
- * Kafka partition index in which the message was written.
177
+ * The zero-based index of the Kafka partition where the message was written.
174
178
  */
175
179
  readonly partition: number;
176
180
 
177
181
  /**
178
- * Kafka offset of the written message.
182
+ * The unique sequential identifier (offset) assigned to the message within its partition.
179
183
  */
180
184
  readonly offset: number;
181
185
 
182
186
  /**
183
- * Kafka written message key.
187
+ * The key of the written Kafka message, typically used for partitioning.
184
188
  */
185
189
  readonly key: string;
186
190
 
187
191
  /**
188
- * Kafka written message value.
192
+ * The main payload data of the written Kafka message.
189
193
  */
190
194
  readonly value: string;
191
195
 
192
196
  /**
193
- * Kafka written message headers.
197
+ * Key-value pairs representing the metadata headers attached to the Kafka message.
194
198
  */
195
199
  readonly headers: { [name: string]: string };
196
200
  }
@@ -0,0 +1,125 @@
1
+ /**
2
+ * Sends a single message to a MQTT topic.
3
+ * https://mokapi.io/docs/javascript-api/mokapi-mqtt/publish
4
+ * @param args - PublishArgs object contains MQTT publish arguments.
5
+ * @returns The publish result.
6
+ * @example
7
+ * export default function() {
8
+ * publish({
9
+ * topic: 'foo',
10
+ * value: JSON.stringify({ foo: bar })
11
+ * });
12
+ * }
13
+ */
14
+ export function produce(args?: PublishArgs): PublishResult;
15
+
16
+ /**
17
+ * Sends a single message to a MQTT topic asynchronously.
18
+ * https://mokapi.io/docs/javascript-api/mokapi-mqtt/publishAsync
19
+ * @param args - PublishArgs object contains MQTT publish arguments.
20
+ * @returns The publish result.
21
+ * @example
22
+ * export default function() {
23
+ * publish({
24
+ * topic: 'foo',
25
+ * value: JSON.stringify({ foo: bar })
26
+ * });
27
+ * }
28
+ */
29
+ export function publishAsync(args?: PublishArgs): Promise<PublishResult>;
30
+
31
+ /**
32
+ * Contains publish-specific arguments.
33
+ * https://mokapi.io/docs/javascript-api/mokapi-mqtt/publishargs
34
+ * @example
35
+ * export default function() {
36
+ * const res = publish({
37
+ * topic: 'foo',
38
+ * value: `{"foo": "bar"}`
39
+ * });
40
+ * }
41
+ */
42
+ export interface PublishArgs {
43
+ /** MQTT cluster name. Used when topic name is not unique. */
44
+ cluster?: string;
45
+
46
+ /** MQTT topic name. If not specified, message will be written to a random topic. */
47
+
48
+ /** MQTT message value. If not specified, a random value will be generated based on the topic configuration. */
49
+ value: string;
50
+
51
+ /**
52
+ * The retry option is used if script is executed before Kafka topic is set up.
53
+ */
54
+ retry?: ProduceRetry;
55
+ }
56
+
57
+ /**
58
+ * Contains information of the written Kafka message.
59
+ * https://mokapi.io/docs/javascript-api/mokapi-kafka/produceresult
60
+ * @example
61
+ * export default function() {
62
+ * const res = produce({ topic: 'foo' })
63
+ * console.log(`new kafka message written with offset: ${res.offset}`)
64
+ * }
65
+ */
66
+ export interface PublishResult {
67
+ /** Name of the Kafka cluster where the message was written. */
68
+ readonly cluster: string;
69
+
70
+ /** Kafka topic name where the message was written. */
71
+ readonly topic: string;
72
+
73
+ /** The value of the written message. */
74
+ readonly value: string;
75
+ }
76
+
77
+ /**
78
+ * The retry option can be used to customize the configuration of the retry mechanism.
79
+ */
80
+ export interface ProduceRetry {
81
+ /**
82
+ * Maximum wait time for a retry
83
+ * MaxRetryTime number express the wait time in milliseconds.
84
+ * MaxRetryTime string is a possibly signed sequence of
85
+ * decimal numbers, each with optional fraction and a unit suffix,
86
+ * such as "300ms", "-1.5h" or "2h45m".
87
+ * Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
88
+ * @default 30000ms
89
+ * @example
90
+ * export default function() {
91
+ * produce({ topic: 'foo', messages: [{ value: 'value-1' }], retry: { maxRetryTime: '30s' } })
92
+ * }
93
+ */
94
+ maxRetryTime: string | number;
95
+
96
+ /**
97
+ * Initial value used to calculate the wait time
98
+ * InitialRetryTime number express the wait time in milliseconds.
99
+ * InitialRetryTime string is a possibly signed sequence of
100
+ * decimal numbers, each with optional fraction and a unit suffix,
101
+ * such as "300ms", "-1.5h" or "2h45m".
102
+ * Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
103
+ * @default 200ms
104
+ * @example
105
+ * export default function() {
106
+ * produce({ topic: 'foo', messages: [{ value: 'value-1' }], retry: { initialRetryTime: '2s' } })
107
+ * }
108
+ */
109
+ initialRetryTime: string | number;
110
+
111
+ /**
112
+ * Factor for increasing the wait time for next retry.
113
+ * 1st retry: 200ms
114
+ * 2nd retry: 4 * 200ms = 800ms
115
+ * 3th retry: 4 * 800ms = 3200ms
116
+ * @default 4
117
+ */
118
+ factor: number;
119
+
120
+ /**
121
+ * Max number of retries
122
+ * @default 5
123
+ */
124
+ retries: number;
125
+ }