go-mokapi 0.17.3 → 0.18.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.17.3",
3
+ "version": "0.18.0",
4
4
  "description": "Easy and flexible API mocking",
5
5
  "homepage": "https://mokapi.io",
6
6
  "repository": {
package/types/faker.d.ts CHANGED
@@ -13,109 +13,113 @@
13
13
  export function fake(schema: Schema | JSONSchema): any;
14
14
 
15
15
  /**
16
- * Gets the tree node with the given name
17
- * @param name name - tree node's name
16
+ * Retrieves a node from the faker tree by its name.
17
+ *
18
+ * @param name name - The name of the node to find.
19
+ * @returns The matching {@link Node} from the faker tree.
20
+ *
18
21
  * @example
19
22
  * export default function() {
20
23
  * const root = findByName(RootName)
21
- * root.insert(0, { name: 'foo', test: () => { return true }, fake: () => { return 'foobar' } })
24
+ * root.children.unshift({
25
+ * name: 'foo',
26
+ * fake: () => {
27
+ * return 'foobar'
28
+ * }
29
+ * })
22
30
  * console.log(fake({type: 'string'}))
31
+ * // Expected output: foobar
23
32
  * }
24
33
  */
25
- export function findByName(name: string): Tree;
34
+ export function findByName(name: string): Node;
26
35
 
27
36
  /**
28
- * The name of the root faker tree
37
+ * The name of the root node in the faker tree.
29
38
  */
30
- export const RootName = "Faker";
39
+ export const Root_Name = "root";
31
40
 
32
41
  /**
33
- * The Tree object represents a node in the faker tree
42
+ * Represents a node in the faker tree.
43
+ * Nodes can have children and can generate fake values based on a request.
34
44
  */
35
- export interface Tree {
45
+ export interface Node {
36
46
  /**
37
- * Gets the name of the tree node
47
+ * The unique name of the node.
38
48
  */
39
49
  name: string;
40
50
 
41
51
  /**
42
- * Inserts a Tree objects after the last child of this tree.
43
- * @param node node - A Tree node to insert after the last child.
52
+ * Attributes associated with this node.
53
+ * These will be used for matching path or schema properties.
44
54
  */
45
- append: (node: Tree | CustomTree) => void;
55
+ attributes: string[];
46
56
 
47
57
  /**
48
- * Removes a Tree node at the specific index position
49
- * @param index index - The zero-based index position to remove.
58
+ * A weight that determines how likely this node is to be selected,
59
+ * if multiple nodes match.
50
60
  */
51
- removeAt: (index: number) => void;
61
+ weight: number;
52
62
 
53
63
  /**
54
- * Removes a Tree node with the given name
55
- * @param name name - The name of a node to remove
64
+ * The child nodes of this node.
56
65
  */
57
- remove: (name: string) => void;
58
- }
66
+ children: Node[];
59
67
 
60
- /**
61
- * The CustomTree object represents a custom node in the faker tree
62
- */
63
- export interface CustomTree {
64
68
  /**
65
- * Gets the name of the custom tree node
66
- */
67
- name: string;
68
-
69
- /**
70
- * Tests whether the tree node supports the request.
71
- * @param request request - Request for a new fake value
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
+ *
72
74
  * @example
73
75
  * export default function() {
74
76
  * const frequencyItems = ['never', 'daily', 'weekly', 'monthly', 'yearly']
75
- * const node = findByName('Strings')
76
- * node.append({
77
+ * const node = findByName(ROOT_NAME)
78
+ * node.children.unshift({
77
79
  * name: 'Frequency',
78
- * test: (r) => { return r.lastName() === 'frequency' },
80
+ * attributes: [ 'frequency' ]
79
81
  * fake: (r) => {
80
82
  * return frequencyItems[Math.floor(Math.random()*frequencyItems.length)]
81
83
  * }
82
84
  * })
83
- * return fake({ type: 'string' })
84
- * }
85
- */
86
- test: (r: Request) => boolean;
87
-
88
- /**
89
- * Gets a new fake value
90
- * @param request request - Request for a new fake value
91
- * @example
92
- * export default function() {
93
- * const frequencyItems = ['never', 'daily', 'weekly', 'monthly', 'yearly']
94
- * const node = findByName('Strings')
95
- * node.append({
96
- * name: 'Frequency',
97
- * test: (r) => { return r.lastName() === 'frequency' },
98
- * fake: (r) => {
99
- * return frequencyItems[Math.floor(Math.random()*frequencyItems.length)]
100
- * }
101
- * })
102
- * return fake({ type: 'string' })
85
+ * console.log(fake({ properties: { frequency } }))
86
+ * // Expected output: an object with a frequency attribute containing a random value of frequency
103
87
  * }
104
88
  */
105
89
  fake: (r: Request) => any;
106
90
  }
107
91
 
92
+ /**
93
+ * Represents the input provided to the `fake` function of a {@link Node}.
94
+ */
108
95
  export interface Request {
109
- path: PathElement[];
96
+ /**
97
+ * The path to the value to generate.
98
+ * This may include paths from an API endpoint or the structure of a nested object.
99
+ */
100
+ path: string[];
101
+
102
+ /**
103
+ * The {@link JSONSchema} definition that describes the value to generate.
104
+ */
105
+ schema: JSONSchema;
110
106
 
111
- last: () => PathElement;
112
- lastName: () => string;
113
- lastSchema: () => JSONSchema;
107
+ /**
108
+ * A shared context holding values generated by other nodes.
109
+ * This allows for interdependent data generation — for example, creating an email
110
+ * address using a previously generated first and last name.
111
+ */
112
+ context: Context
114
113
  }
115
114
 
116
- export interface PathElement {
117
- name: string;
118
- schema: JSONSchema;
115
+ /**
116
+ * A context map holding values generated during the faker tree evaluation.
117
+ */
118
+ export interface Context {
119
+ /**
120
+ * A map of names to previously generated values.
121
+ */
122
+ values: { [name: string]: any }
119
123
  }
120
124
 
121
125
  /**
@@ -164,7 +168,7 @@ export interface JSONSchema {
164
168
  maximum?: number;
165
169
 
166
170
  /**
167
- * Restricts the number to a exclusive maximum number
171
+ * Restricts the number to an exclusive maximum number
168
172
  */
169
173
  exclusiveMaximum?: number;
170
174
 
@@ -174,7 +178,7 @@ export interface JSONSchema {
174
178
  minimum?: number;
175
179
 
176
180
  /**
177
- * Restricts the number to a exclusive minimum number
181
+ * Restricts the number to an exclusive minimum number
178
182
  */
179
183
  exclusiveMinimum?: number;
180
184
 
package/types/index.d.ts CHANGED
@@ -108,6 +108,10 @@ export interface EventHandler {
108
108
  smtp: SmtpEventHandler;
109
109
  }
110
110
 
111
+ export type ChangeResult = boolean | void;
112
+
113
+ export type EventHandlerResult = ChangeResult | Promise<ChangeResult>;
114
+
111
115
  /**
112
116
  * HttpEventHandler is a function that is executed when an HTTP event is triggered.
113
117
  * https://mokapi.io/docs/javascript-api/mokapi/eventhandler/httpeventhandler
@@ -122,7 +126,7 @@ export interface EventHandler {
122
126
  * })
123
127
  * }
124
128
  */
125
- export type HttpEventHandler = (request: HttpRequest, response: HttpResponse) => boolean | Promise<boolean>;
129
+ export type HttpEventHandler = (request: HttpRequest, response: HttpResponse) => EventHandlerResult;
126
130
 
127
131
  /**
128
132
  * HttpRequest is an object used by HttpEventHandler that contains request-specific
@@ -136,7 +140,7 @@ export interface HttpRequest {
136
140
  /** Represents a parsed URL. */
137
141
  readonly url: Url;
138
142
 
139
- /** Body contains request body specified by OpenAPI request body. */
143
+ /** Body contains the request body specified by OpenAPI request body. */
140
144
  readonly body: any;
141
145
 
142
146
  /** Object contains path parameters specified by OpenAPI path parameters. */
@@ -204,7 +208,7 @@ export interface Url {
204
208
  * })
205
209
  * }
206
210
  */
207
- export type KafkaEventHandler = (message: KafkaEventMessage) => boolean | Promise<boolean>;
211
+ export type KafkaEventHandler = (message: KafkaEventMessage) => EventHandlerResult;
208
212
 
209
213
  /**
210
214
  * KafkaEventMessage is an object used by KafkaEventHandler that contains Kafka-specific message data.
@@ -243,7 +247,7 @@ export interface KafkaEventMessage {
243
247
  * })
244
248
  * }
245
249
  */
246
- export type LdapEventHandler = (request: LdapSearchRequest, response: LdapSearchResponse) => boolean | Promise<boolean>;
250
+ export type LdapEventHandler = (request: LdapSearchRequest, response: LdapSearchResponse) => EventHandlerResult;
247
251
 
248
252
  /**
249
253
  * LdapSearchRequest is an object used by LdapEventHandler that contains request-specific data.
@@ -343,7 +347,7 @@ export enum LdapResultStatus {
343
347
  SizeLimitExceeded = 4,
344
348
  }
345
349
 
346
- export type SmtpEventHandler = (record: SmtpEventMessage) => boolean | Promise<boolean>;
350
+ export type SmtpEventHandler = (record: SmtpEventMessage) => EventHandlerResult;
347
351
 
348
352
  export interface SmtpEventMessage {
349
353
  server: string;