go-mokapi 0.51.2 → 0.53.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/types/kafka.d.ts CHANGED
@@ -1,43 +1,39 @@
1
- import { JSONValue } from ".";
2
-
3
1
  /**
4
- * Sends messages to a Kafka topic.
2
+ * Sends one or more messages to a Kafka topic.
5
3
  * https://mokapi.io/docs/javascript-api/mokapi-kafka/produce
6
- * @param args - ProduceArgs object contains Kafka produce arguments.
7
- * @returns The produce result.
4
+ * @param args - Produce arguments. If omitted, a random message is written to a random topic.
5
+ * @returns Information about the written messages.
8
6
  * @example
7
+ * import { produce } from 'mokapi/kafka'
8
+ *
9
9
  * export default function() {
10
10
  * const res = produce({
11
11
  * topic: 'foo',
12
12
  * messages: [
13
- * {
14
- * key: 'foo-1',
15
- * data: { foo: 'bar' }
16
- * }
13
+ * { key: 'foo-1', data: { foo: 'bar' } }
17
14
  * ]
18
- * });
19
- * console.log(`new kafka message written with offset: ${res.offset}`)
15
+ * })
16
+ * console.log(`new kafka message written with offset: ${res.messages[0].offset}`)
20
17
  * }
21
18
  */
22
19
  export function produce(args?: ProduceArgs): ProduceResult;
23
20
 
24
21
  /**
25
- * Sends messages to a Kafka topic asynchronously.
22
+ * Sends one or more messages to a Kafka topic asynchronously.
26
23
  * https://mokapi.io/docs/javascript-api/mokapi-kafka/produce
27
- * @param args - ProduceArgs object contains Kafka produce arguments.
28
- * @returns The produce result.
24
+ * @param args - Produce arguments. If omitted, a random message is written to a random topic.
25
+ * @returns A promise resolving to information about the written messages.
29
26
  * @example
30
- * export default function() {
31
- * const res = produce({
27
+ * import { produceAsync } from 'mokapi/kafka'
28
+ *
29
+ * export default async function() {
30
+ * const res = await produceAsync({
32
31
  * topic: 'foo',
33
32
  * messages: [
34
- * {
35
- * key: 'foo-1',
36
- * data: { foo: 'bar' }
37
- * }
33
+ * { key: 'foo-1', data: { foo: 'bar' } }
38
34
  * ]
39
- * });
40
- * console.log(`new kafka message written with offset: ${res.offset}`)
35
+ * })
36
+ * console.log(`new kafka message written with offset: ${res.messages[0].offset}`)
41
37
  * }
42
38
  */
43
39
  export function produceAsync(args?: ProduceArgs): Promise<ProduceResult>;
@@ -47,204 +43,241 @@ export function produceAsync(args?: ProduceArgs): Promise<ProduceResult>;
47
43
  * https://mokapi.io/docs/javascript-api/mokapi-kafka/produceargs
48
44
  * @example
49
45
  * export default function() {
50
- * const res = produce({
46
+ * produce({
51
47
  * topic: 'foo',
52
48
  * messages: [
53
- * {
54
- * key: 'foo-1',
55
- * data: { foo: 'bar-1' }
56
- * },
57
- * {
58
- * key: 'foo-2',
59
- * data: { foo: 'bar-2' }
60
- * }
49
+ * { key: 'foo-1', data: { foo: 'bar-1' } },
50
+ * { key: 'foo-2', data: { foo: 'bar-2' } }
61
51
  * ]
62
- * });
52
+ * })
63
53
  * }
64
54
  */
65
55
  export interface ProduceArgs {
66
- /** Kafka cluster name. Used when topic name is not unique. */
56
+ /**
57
+ * Kafka cluster name. Only required if the topic name exists in more than one cluster.
58
+ */
67
59
  cluster?: string;
68
60
 
69
- /** Kafka topic name. If not specified, message will be written to a random topic. */
61
+ /**
62
+ * Kafka topic name. If not specified, the message is written to a random topic.
63
+ *
64
+ * The name can also be a concrete instance of a parameterized channel address,
65
+ * e.g. `shipments.eu.updates` for the address `shipments.{region}.updates`.
66
+ * If that topic does not exist yet, set {@link allowAutoTopicCreation} to create it.
67
+ */
70
68
  topic?: string;
71
69
 
72
- /** Kafka partition index. If not specified, the message will be written to any partition */
70
+ /**
71
+ * Kafka partition index. If not specified, the partition is chosen based on the key.
72
+ * Shorthand for a single message; prefer {@link messages}.
73
+ */
73
74
  partition?: number;
74
75
 
75
- /** Kafka message key. If not specified, a random key will be generated based on the topic configuration. */
76
- key?: JSONValue;
76
+ /**
77
+ * Kafka message key. If not specified, a random key is generated based on the topic configuration.
78
+ * Shorthand for a single message; prefer {@link messages}.
79
+ */
80
+ key?: any;
77
81
 
78
- /** Kafka message value. If not specified, a random value will be generated based on the topic configuration. */
79
- value?: JSONValue;
82
+ /**
83
+ * Kafka message value. If not specified, a random value is generated based on the topic configuration.
84
+ * Shorthand for a single message; prefer {@link messages}.
85
+ */
86
+ value?: any;
80
87
 
81
- /** Kafka message headers. */
82
- headers?: { [name: string]: JSONValue };
88
+ /**
89
+ * Kafka message headers.
90
+ * Shorthand for a single message; prefer {@link messages}.
91
+ */
92
+ headers?: { [name: string]: any };
83
93
 
84
94
  /**
85
- * An array of Kafka messages
95
+ * Messages to write. Each message can define its own partition, key, value and headers.
86
96
  * @see Message
87
97
  * @example
88
98
  * export default function() {
89
- * const res = produce({
99
+ * produce({
90
100
  * topic: 'foo',
91
101
  * messages: [
92
- * {
93
- * key: 'foo-1',
94
- * data: { foo: 'bar-1' }
95
- * },
96
- * {
97
- * key: 'foo-2',
98
- * data: { foo: 'bar-2' }
99
- * }
102
+ * { key: 'foo-1', data: { foo: 'bar-1' } },
103
+ * { key: 'foo-2', data: { foo: 'bar-2' } }
100
104
  * ]
101
- * });
105
+ * })
102
106
  * }
103
107
  */
104
108
  messages?: Message[];
105
109
 
106
110
  /**
107
- * The retry option is used if script is executed before Kafka topic is set up.
111
+ * Creates the topic if it does not exist yet.
112
+ *
113
+ * The topic is only created if its name matches a channel address defined in the
114
+ * AsyncAPI specification, including parameterized addresses such as
115
+ * `shipments.{region}.updates`, and the channel is available on the target server.
116
+ * Parameter values must satisfy the parameter's `enum`, if defined.
117
+ * @default false
118
+ * @example
119
+ * export default function() {
120
+ * produce({
121
+ * topic: 'shipments.eu.updates',
122
+ * allowAutoTopicCreation: true,
123
+ * messages: [
124
+ * { key: 'c9bf9e57-1685-4c89-bafb-ff5af830be8a', data: { status: 'IN_TRANSIT' } }
125
+ * ]
126
+ * })
127
+ * }
128
+ */
129
+ allowAutoTopicCreation?: boolean;
130
+
131
+ /**
132
+ * Retries producing if the script runs before the Kafka topic is set up.
133
+ * @see ProduceRetry
108
134
  */
109
135
  retry?: ProduceRetry;
110
136
  }
111
137
 
112
138
  /**
113
- * Represents a Kafka message
139
+ * Represents a Kafka message to write.
114
140
  */
115
141
  export interface Message {
116
- /** Kafka partition index. If not specified, the message will be written to any partition */
142
+ /** Kafka partition index. If not specified, the partition is chosen based on the key. */
117
143
  partition?: number;
118
144
 
119
- /** Kafka message key. If not specified, a random key will be generated based on the topic configuration. */
120
- key?: JSONValue;
145
+ /** Kafka message key. If not specified, a random key is generated based on the topic configuration. */
146
+ key?: any;
121
147
 
122
- /** Kafka message value. If data and value are not specified, a random value will be generated based on the topic configuration. */
123
- data?: JSONValue;
148
+ /**
149
+ * Kafka message value, validated against the message schema of the topic.
150
+ * If neither `data` nor `value` is specified, a random value is generated based on the topic configuration.
151
+ */
152
+ data?: any;
124
153
 
125
- /** Kafka message value not validating against schema. If data and value are not specified, a random value will be generated based on the topic configuration. */
154
+ /**
155
+ * Raw Kafka message value, written without schema validation.
156
+ * Use it to simulate invalid messages. If neither `data` nor `value` is specified,
157
+ * a random value is generated based on the topic configuration.
158
+ */
126
159
  value?: string | number | boolean | null;
127
160
 
128
161
  /** Kafka message headers. */
129
- headers?: { [name: string]: JSONValue };
162
+ headers?: { [name: string]: any };
130
163
  }
131
164
 
132
165
  /**
133
- * Contains information of the written Kafka message.
166
+ * Contains information about the written Kafka messages.
134
167
  * https://mokapi.io/docs/javascript-api/mokapi-kafka/produceresult
135
168
  * @example
136
169
  * export default function() {
137
170
  * const res = produce({ topic: 'foo' })
138
- * console.log(`new kafka message written with offset: ${res.offset}`)
171
+ * for (const msg of res.messages) {
172
+ * console.log(`written to partition ${msg.partition} with offset ${msg.offset}`)
173
+ * }
139
174
  * }
140
175
  */
141
176
  export interface ProduceResult {
142
- /** Name of the Kafka cluster where the message was written. */
177
+ /** Name of the Kafka cluster where the messages were written. */
143
178
  readonly cluster: string;
144
179
 
145
- /** Kafka topic name where the message was written. */
180
+ /** Name of the Kafka topic where the messages were written. */
146
181
  readonly topic: string;
147
182
 
148
- // ** List of produced Kafka messages */
149
- messages: MessageResult[];
183
+ /** The written messages, in the same order as in {@link ProduceArgs.messages}. */
184
+ readonly messages: readonly MessageResult[];
150
185
 
151
- /** Kafka partition where the message was written. */
186
+ /**
187
+ * Partition of the first written message.
188
+ * @deprecated Use `messages[i].partition` instead.
189
+ */
152
190
  readonly partition: number;
153
191
 
154
- /** The offset of the written message. */
192
+ /**
193
+ * Offset of the first written message.
194
+ * @deprecated Use `messages[i].offset` instead.
195
+ */
155
196
  readonly offset: number;
156
197
 
157
- /** The key of the written message, */
198
+ /**
199
+ * Key of the first written message.
200
+ * @deprecated Use `messages[i].key` instead.
201
+ */
158
202
  readonly key: string;
159
203
 
160
- /** The value of the written message. */
204
+ /**
205
+ * Value of the first written message.
206
+ * @deprecated Use `messages[i].value` instead.
207
+ */
161
208
  readonly value: string;
162
209
 
163
- /** The headers of the written message */
210
+ /**
211
+ * Headers of the first written message.
212
+ * @deprecated Use `messages[i].headers` instead.
213
+ */
164
214
  readonly headers: { [name: string]: string };
165
215
  }
166
216
 
167
217
  /**
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
- *
218
+ * Represents a single written Kafka message.
173
219
  * https://mokapi.io/docs/javascript-api/mokapi-kafka/kafkamessageresult
174
220
  */
175
221
  export interface MessageResult {
176
- /**
177
- * The zero-based index of the Kafka partition where the message was written.
178
- */
222
+ /** Zero-based index of the partition where the message was written. */
179
223
  readonly partition: number;
180
224
 
181
- /**
182
- * The unique sequential identifier (offset) assigned to the message within its partition.
183
- */
225
+ /** Offset assigned to the message within its partition. */
184
226
  readonly offset: number;
185
227
 
186
- /**
187
- * The key of the written Kafka message, typically used for partitioning.
188
- */
228
+ /** Key of the written message. */
189
229
  readonly key: string;
190
230
 
191
- /**
192
- * The main payload data of the written Kafka message.
193
- */
231
+ /** Value of the written message. */
194
232
  readonly value: string;
195
233
 
196
- /**
197
- * Key-value pairs representing the metadata headers attached to the Kafka message.
198
- */
234
+ /** Headers of the written message. */
199
235
  readonly headers: { [name: string]: string };
200
236
  }
201
237
 
202
238
  /**
203
- * The retry option can be used to customize the configuration of the retry mechanism.
239
+ * A duration, either as a number of milliseconds or as a string with unit suffix,
240
+ * such as `"300ms"`, `"1.5s"` or `"2m30s"`.
241
+ * Valid units are `ns`, `us` (or `µs`), `ms`, `s`, `m`, `h`.
242
+ */
243
+ export type Duration = string | number;
244
+
245
+ /**
246
+ * Configures the retry mechanism used when the topic is not available yet.
247
+ * The wait time grows exponentially: `initialRetryTime * factor^(n-1)`,
248
+ * capped at {@link maxRetryTime}.
249
+ * @example
250
+ * export default function() {
251
+ * produce({
252
+ * topic: 'foo',
253
+ * messages: [{ value: 'value-1' }],
254
+ * retry: { initialRetryTime: '500ms', retries: 10 }
255
+ * })
256
+ * }
204
257
  */
205
258
  export interface ProduceRetry {
206
259
  /**
207
- * Maximum wait time for a retry
208
- * MaxRetryTime number express the wait time in milliseconds.
209
- * MaxRetryTime string is a possibly signed sequence of
210
- * decimal numbers, each with optional fraction and a unit suffix,
211
- * such as "300ms", "-1.5h" or "2h45m".
212
- * Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
213
- * @default 30000ms
214
- * @example
215
- * export default function() {
216
- * produce({ topic: 'foo', messages: [{ value: 'value-1' }], retry: { maxRetryTime: '30s' } })
217
- * }
260
+ * Maximum wait time for a single retry.
261
+ * @default '30s'
218
262
  */
219
- maxRetryTime: string | number;
263
+ maxRetryTime?: Duration;
220
264
 
221
265
  /**
222
- * Initial value used to calculate the wait time
223
- * InitialRetryTime number express the wait time in milliseconds.
224
- * InitialRetryTime string is a possibly signed sequence of
225
- * decimal numbers, each with optional fraction and a unit suffix,
226
- * such as "300ms", "-1.5h" or "2h45m".
227
- * Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
228
- * @default 200ms
229
- * @example
230
- * export default function() {
231
- * produce({ topic: 'foo', messages: [{ value: 'value-1' }], retry: { initialRetryTime: '2s' } })
232
- * }
266
+ * Wait time before the first retry.
267
+ * @default '200ms'
233
268
  */
234
- initialRetryTime: string | number;
269
+ initialRetryTime?: Duration;
235
270
 
236
271
  /**
237
- * Factor for increasing the wait time for next retry.
238
- * 1st retry: 200ms
239
- * 2nd retry: 4 * 200ms = 800ms
240
- * 3th retry: 4 * 800ms = 3200ms
272
+ * Factor by which the wait time increases with each retry.
273
+ * With the defaults: 1st retry 200ms, 2nd 800ms, 3rd 3200ms.
241
274
  * @default 4
242
275
  */
243
- factor: number;
276
+ factor?: number;
244
277
 
245
278
  /**
246
- * Max number of retries
279
+ * Maximum number of retries.
247
280
  * @default 5
248
281
  */
249
- retries: number;
282
+ retries?: number;
250
283
  }
@@ -1,5 +1,3 @@
1
- import { JSONValue } from ".";
2
-
3
1
  /**
4
2
  * Renders the given mustache template with the given data.
5
3
  * https://mokapi.io/docs/javascript-api/mokapi-mustache/render
@@ -22,7 +20,7 @@ import { JSONValue } from ".";
22
20
  */
23
21
  export function render(template: string, data: Scope): string;
24
22
 
25
- export type Scope = null | Scope[] | ScopeObject | JSONValue;
23
+ export type Scope = null | Scope[] | ScopeObject | any;
26
24
 
27
25
  /**
28
26
  * Object representable with JSON.
package/types/yaml.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- import { JSONValue } from ".";
2
-
3
1
  /**
4
2
  * Parses a YAML string, constructing the JavaScript value or object described by the string.
5
3
  * https://mokapi.io/docs/javascript-api/mokapi-yaml/parse
@@ -35,4 +33,4 @@ export function parse(text: string): any;
35
33
  * })
36
34
  * }
37
35
  */
38
- export function stringify(value: JSONValue): string;
36
+ export function stringify(value: any): string;