go-mokapi 0.19.0 → 0.20.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.
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "go-mokapi",
3
- "version": "0.19.0",
3
+ "version": "0.20.0",
4
4
  "description": "Easy and flexible API mocking",
5
5
  "homepage": "https://mokapi.io",
6
6
  "repository": {
package/types/faker.d.ts CHANGED
@@ -36,7 +36,7 @@ export function findByName(name: string): Node;
36
36
  /**
37
37
  * The name of the root node in the faker tree.
38
38
  */
39
- export const Root_Name = "root";
39
+ export const ROOT_NAME = "root";
40
40
 
41
41
  /**
42
42
  * Represents a node in the faker tree.
@@ -63,7 +63,54 @@ export interface Node {
63
63
  /**
64
64
  * The child nodes of this node.
65
65
  */
66
- children: Node[];
66
+ children: Array<Node | AddNode>;
67
+
68
+ /**
69
+ * Generates a fake value based on the given request.
70
+ *
71
+ * @param r r - The request describing the value to generate.
72
+ * @returns A generated fake value.
73
+ *
74
+ * @example
75
+ * export default function() {
76
+ * const frequencyItems = ['never', 'daily', 'weekly', 'monthly', 'yearly']
77
+ * const node = findByName(ROOT_NAME)
78
+ * node.children.unshift({
79
+ * name: 'Frequency',
80
+ * attributes: [ 'frequency' ]
81
+ * fake: (r) => {
82
+ * return frequencyItems[Math.floor(Math.random()*frequencyItems.length)]
83
+ * }
84
+ * })
85
+ * console.log(fake({ properties: { frequency } }))
86
+ * // Expected output: an object with a frequency attribute containing a random value of frequency
87
+ * }
88
+ */
89
+ fake: (r: Request) => any;
90
+ }
91
+
92
+ export interface AddNode {
93
+ /**
94
+ * The unique name of the node.
95
+ */
96
+ name: string;
97
+
98
+ /**
99
+ * Attributes associated with this node.
100
+ * These will be used for matching path or schema properties.
101
+ */
102
+ attributes?: string[];
103
+
104
+ /**
105
+ * A weight that determines how likely this node is to be selected,
106
+ * if multiple nodes match.
107
+ */
108
+ weight?: number;
109
+
110
+ /**
111
+ * The child nodes of this node.
112
+ */
113
+ children?: Array<Node | AddNode>;
67
114
 
68
115
  /**
69
116
  * Generates a fake value based on the given request.
package/types/index.d.ts CHANGED
@@ -108,10 +108,6 @@ export interface EventHandler {
108
108
  smtp: SmtpEventHandler;
109
109
  }
110
110
 
111
- export type ChangeResult = boolean | undefined;
112
-
113
- export type EventHandlerResult = ChangeResult | Promise<ChangeResult>;
114
-
115
111
  /**
116
112
  * HttpEventHandler is a function that is executed when an HTTP event is triggered.
117
113
  * https://mokapi.io/docs/javascript-api/mokapi/eventhandler/httpeventhandler
@@ -126,7 +122,7 @@ export type EventHandlerResult = ChangeResult | Promise<ChangeResult>;
126
122
  * })
127
123
  * }
128
124
  */
129
- export type HttpEventHandler = (request: HttpRequest, response: HttpResponse) => EventHandlerResult;
125
+ export type HttpEventHandler = (request: HttpRequest, response: HttpResponse) => void;
130
126
 
131
127
  /**
132
128
  * HttpRequest is an object used by HttpEventHandler that contains request-specific
@@ -208,7 +204,7 @@ export interface Url {
208
204
  * })
209
205
  * }
210
206
  */
211
- export type KafkaEventHandler = (message: KafkaEventMessage) => EventHandlerResult;
207
+ export type KafkaEventHandler = (message: KafkaEventMessage) => void;
212
208
 
213
209
  /**
214
210
  * KafkaEventMessage is an object used by KafkaEventHandler that contains Kafka-specific message data.
@@ -247,7 +243,7 @@ export interface KafkaEventMessage {
247
243
  * })
248
244
  * }
249
245
  */
250
- export type LdapEventHandler = (request: LdapSearchRequest, response: LdapSearchResponse) => EventHandlerResult;
246
+ export type LdapEventHandler = (request: LdapSearchRequest, response: LdapSearchResponse) => void;
251
247
 
252
248
  /**
253
249
  * LdapSearchRequest is an object used by LdapEventHandler that contains request-specific data.
@@ -347,7 +343,7 @@ export enum LdapResultStatus {
347
343
  SizeLimitExceeded = 4,
348
344
  }
349
345
 
350
- export type SmtpEventHandler = (record: SmtpEventMessage) => EventHandlerResult;
346
+ export type SmtpEventHandler = (record: SmtpEventMessage) => void;
351
347
 
352
348
  export interface SmtpEventMessage {
353
349
  server: string;
@@ -460,4 +456,57 @@ export interface JSONObject {
460
456
  [key: string]: JSONValue;
461
457
  }
462
458
 
463
- export const RFC3339 = "RFC3339";
459
+ /**
460
+ * Specifies the date-time format defined in [RFC 3339](https://datatracker.ietf.org/doc/html/rfc3339).
461
+ * This constant can be used when defining or validating datetime strings.
462
+ *
463
+ * @example
464
+ * const date = new Date().toISOString()
465
+ * if (isValidDate(date, RFC3339)) {
466
+ * // do something
467
+ * }
468
+ */
469
+ export const RFC3339 = "RFC3339";
470
+
471
+ /**
472
+ * Applies a patch object to a target object. Only properties that are explicitly defined in the patch
473
+ * are applied. This includes nested objects. Properties marked with `Delete` will be removed.
474
+ *
475
+ * This function is especially useful when working with generated mock data in Mokapi that you want to override
476
+ * or refine with specific values.
477
+ *
478
+ * https://mokapi.io/docs/javascript-api/mokapi/patch
479
+ *
480
+ * @param target The original object or value to be patched.
481
+ * @param patch The patch object or value. Only defined values are applied; undefined values are ignored. Use `Delete` to remove fields.
482
+ * @returns A new object or value with the patch applied.
483
+ *
484
+ * @example
485
+ * const result = patch({ name: "foo", age: 42 }, { name: "bar" })
486
+ * // result: { name: "bar", age: 42 }
487
+ *
488
+ * @example
489
+ * const result = patch({ name: "foo", meta: { version: 1 } }, { meta: { version: 2 } })
490
+ * // result: { name: "foo", meta: { version: 2 } }
491
+ *
492
+ * @example
493
+ * const result = patch({ name: "foo", age: 42 }, { age: Delete })
494
+ * // result: { name: "foo" }
495
+ */
496
+ export function patch(target: any, patch: any): any;
497
+
498
+ /**
499
+ * Special marker used with the `patch` function to indicate a property should be removed.
500
+ *
501
+ * When used as a value inside a patch object, the corresponding property will be deleted
502
+ * from the result.
503
+ *
504
+ * This is useful when refining or overriding mock data in a script while keeping validation logic intact.
505
+ *
506
+ * https://mokapi.io/docs/javascript-api/mokapi/patch#delete
507
+ *
508
+ * @example
509
+ * const result = patch({ name: "foo", age: 42 }, { age: Delete })
510
+ * // result: { name: "foo" }
511
+ */
512
+ export const Delete: unique symbol;