Skip to content

Commit 63f477e

Browse files
committed
update Mokapi typings to v0.20.0
1 parent 0046363 commit 63f477e

File tree

13 files changed

+391
-159
lines changed

13 files changed

+391
-159
lines changed

types/mokapi/encoding.d.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export interface Base64Encoding {
2+
/**
3+
* Encodes a string value to its equivalent string representation that is encoded with base-64.
4+
* https://mokapi.io/docs/javascript-api/mokapi-encoding/base64-encode
5+
* @param input input - The input string to base64 encode.
6+
* @returns The base64 encoding of the input string.
7+
* @example
8+
* export default function() {
9+
* console.log(`The base64 encoding of 'hello world' is: ${base.encode('hello world')}`)
10+
* }
11+
*/
12+
encode(input: string | ArrayBuffer): string;
13+
14+
/**
15+
* Decodes a string value that is encoded with base-64 to its equivalent unencoded string representation.
16+
* @param input input - The base64 input string to decode.
17+
* @returns The base64 decoded version of the input string.
18+
* @example
19+
* export default function() {
20+
* console.log(`The base64 decoded string of 'aGVsbG8gd29ybGQ=' is: ${base.decode('aGVsbG8gd29ybGQ=')}`)
21+
* }
22+
*/
23+
decode(input: string): string;
24+
}
25+
26+
export const base64: Base64Encoding;
27+
28+

types/mokapi/faker.d.ts

Lines changed: 102 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,116 +13,160 @@
1313
export function fake(schema: Schema | JSONSchema): any;
1414

1515
/**
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+
*
1821
* @example
1922
* export default function() {
2023
* 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+
* })
2230
* console.log(fake({type: 'string'}))
31+
* // Expected output: foobar
2332
* }
2433
*/
25-
export function findByName(name: string): Tree;
34+
export function findByName(name: string): Node;
2635

2736
/**
28-
* The name of the root faker tree
37+
* The name of the root node in the faker tree.
2938
*/
30-
export const RootName = "Faker";
39+
export const ROOT_NAME = "root";
3140

3241
/**
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.
3444
*/
35-
export interface Tree {
45+
export interface Node {
3646
/**
37-
* Gets the name of the tree node
47+
* The unique name of the node.
3848
*/
3949
name: string;
4050

4151
/**
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.
4454
*/
45-
append: (node: Tree | CustomTree) => void;
55+
attributes: string[];
4656

4757
/**
48-
* Inserts a Tree objects at a specified index position
49-
* @param index index - The zero-based index position of the insertion.
50-
* @param node node - The tree node to insert
58+
* A weight that determines how likely this node is to be selected,
59+
* if multiple nodes match.
5160
*/
52-
insert: (index: number, node: Tree | CustomTree) => void;
61+
weight: number;
5362

5463
/**
55-
* Removes a Tree node at the specific index position
56-
* @param index index - The zero-based index position to remove.
64+
* The child nodes of this node.
5765
*/
58-
removeAt: (index: number) => void;
66+
children: Array<Node | AddNode>;
5967

6068
/**
61-
* Removes a Tree node with the given name
62-
* @param name name - The name of a node to remove
63-
*/
64-
remove: (name: string) => void;
65-
}
66-
67-
/**
68-
* The CustomTree object represents a custom node in the faker tree
69-
*/
70-
export interface CustomTree {
71-
/**
72-
* Gets the name of the custom tree node
73-
*/
74-
name: string;
75-
76-
/**
77-
* Tests whether the tree node supports the request.
78-
* @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+
*
7974
* @example
8075
* export default function() {
8176
* const frequencyItems = ['never', 'daily', 'weekly', 'monthly', 'yearly']
82-
* const node = findByName('Strings')
83-
* node.append({
77+
* const node = findByName(ROOT_NAME)
78+
* node.children.unshift({
8479
* name: 'Frequency',
85-
* test: (r) => { return r.lastName() === 'frequency' },
80+
* attributes: [ 'frequency' ]
8681
* fake: (r) => {
8782
* return frequencyItems[Math.floor(Math.random()*frequencyItems.length)]
8883
* }
8984
* })
90-
* 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
9187
* }
9288
*/
93-
test: (r: Request) => boolean;
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>;
94114

95115
/**
96-
* Gets a new fake value
97-
* @param request request - Request for a new fake value
116+
* Generates a fake value based on the given request.
117+
*
118+
* @param r r - The request describing the value to generate.
119+
* @returns A generated fake value.
120+
*
98121
* @example
99122
* export default function() {
100123
* const frequencyItems = ['never', 'daily', 'weekly', 'monthly', 'yearly']
101-
* const node = findByName('Strings')
102-
* node.append({
124+
* const node = findByName(ROOT_NAME)
125+
* node.children.unshift({
103126
* name: 'Frequency',
104-
* test: (r) => { return r.lastName() === 'frequency' },
127+
* attributes: [ 'frequency' ]
105128
* fake: (r) => {
106129
* return frequencyItems[Math.floor(Math.random()*frequencyItems.length)]
107130
* }
108131
* })
109-
* return fake({ type: 'string' })
132+
* console.log(fake({ properties: { frequency } }))
133+
* // Expected output: an object with a frequency attribute containing a random value of frequency
110134
* }
111135
*/
112136
fake: (r: Request) => any;
113137
}
114138

139+
/**
140+
* Represents the input provided to the `fake` function of a {@link Node}.
141+
*/
115142
export interface Request {
116-
path: PathElement[];
143+
/**
144+
* The path to the value to generate.
145+
* This may include paths from an API endpoint or the structure of a nested object.
146+
*/
147+
path: string[];
117148

118-
last: () => PathElement;
119-
lastName: () => string;
120-
lastSchema: () => JSONSchema;
149+
/**
150+
* The {@link JSONSchema} definition that describes the value to generate.
151+
*/
152+
schema: JSONSchema;
153+
154+
/**
155+
* A shared context holding values generated by other nodes.
156+
* This allows for interdependent data generation — for example, creating an email
157+
* address using a previously generated first and last name.
158+
*/
159+
context: Context
121160
}
122161

123-
export interface PathElement {
124-
name: string;
125-
schema: JSONSchema;
162+
/**
163+
* A context map holding values generated during the faker tree evaluation.
164+
*/
165+
export interface Context {
166+
/**
167+
* A map of names to previously generated values.
168+
*/
169+
values: { [name: string]: any }
126170
}
127171

128172
/**
@@ -171,7 +215,7 @@ export interface JSONSchema {
171215
maximum?: number;
172216

173217
/**
174-
* Restricts the number to a exclusive maximum number
218+
* Restricts the number to an exclusive maximum number
175219
*/
176220
exclusiveMaximum?: number;
177221

@@ -181,7 +225,7 @@ export interface JSONSchema {
181225
minimum?: number;
182226

183227
/**
184-
* Restricts the number to a exclusive minimum number
228+
* Restricts the number to an exclusive minimum number
185229
*/
186230
exclusiveMinimum?: number;
187231

@@ -348,4 +392,4 @@ export interface Schema {
348392

349393
/** Specifies whether all items in an array must be unique. */
350394
uniqueItems?: boolean;
351-
}
395+
}

types/mokapi/global.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ declare global {
1616
* });
1717
* }
1818
*/
19-
function open(filePath: string, args?: Args): string;
19+
function open(filePath: string, args?: Args): any;
2020

2121
interface Args {
2222
/**
23-
* By default contents of the file are read as string, but with binary the file will be read as binary.
23+
* By default, contents of the file are read as string, but with binary the file will be read as binary.
2424
*/
25-
as: "binary" | "string";
25+
as: "binary" | "string" | "resolved";
2626
}
27-
}
27+
}

types/mokapi/http.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,4 @@ export interface Response {
117117
* res.json()
118118
*/
119119
json(): JSONValue;
120-
}
120+
}

types/mokapi/index.d.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
/**
22
* Mokapi JavaScript API
3-
* https://mokapi.io/docs/guides/get-started/welcome
3+
* https://mokapi.io/docs/guides/welcome
44
*/
55

66
import "./faker";
77
import "./global";
88
import "./http";
99
import "./mustache";
1010
import "./yaml";
11+
import "./encoding";
12+
import "./mail";
1113

1214
/**
1315
* Attaches an event handler for the given event.
@@ -93,7 +95,7 @@ export function sleep(time: number | string): void;
9395

9496
/**
9597
* Specifies the interval of a periodic job.
96-
* Interval string is a possibly signed sequence of decimal numbers, each with optional
98+
* Interval string is a possibly signed sequence of decimal numbers, each with an optional
9799
* fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m".
98100
* Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
99101
*/
@@ -120,7 +122,7 @@ export interface EventHandler {
120122
* })
121123
* }
122124
*/
123-
export type HttpEventHandler = (request: HttpRequest, response: HttpResponse) => boolean;
125+
export type HttpEventHandler = (request: HttpRequest, response: HttpResponse) => void | Promise<void>;
124126

125127
/**
126128
* HttpRequest is an object used by HttpEventHandler that contains request-specific
@@ -134,7 +136,7 @@ export interface HttpRequest {
134136
/** Represents a parsed URL. */
135137
readonly url: Url;
136138

137-
/** Body contains request body specified by OpenAPI request body. */
139+
/** Body contains the request body specified by OpenAPI request body. */
138140
readonly body: any;
139141

140142
/** Object contains path parameters specified by OpenAPI path parameters. */
@@ -202,7 +204,7 @@ export interface Url {
202204
* })
203205
* }
204206
*/
205-
export type KafkaEventHandler = (message: KafkaEventMessage) => boolean;
207+
export type KafkaEventHandler = (message: KafkaEventMessage) => void | Promise<void>;
206208

207209
/**
208210
* KafkaEventMessage is an object used by KafkaEventHandler that contains Kafka-specific message data.
@@ -241,7 +243,7 @@ export interface KafkaEventMessage {
241243
* })
242244
* }
243245
*/
244-
export type LdapEventHandler = (request: LdapSearchRequest, response: LdapSearchResponse) => boolean;
246+
export type LdapEventHandler = (request: LdapSearchRequest, response: LdapSearchResponse) => void | Promise<void>;
245247

246248
/**
247249
* LdapSearchRequest is an object used by LdapEventHandler that contains request-specific data.
@@ -341,7 +343,7 @@ export enum LdapResultStatus {
341343
SizeLimitExceeded = 4,
342344
}
343345

344-
export type SmtpEventHandler = (record: SmtpEventMessage) => boolean;
346+
export type SmtpEventHandler = (record: SmtpEventMessage) => void | Promise<void>;
345347

346348
export interface SmtpEventMessage {
347349
server: string;
@@ -379,7 +381,7 @@ export interface DateArgs {
379381
/**
380382
* The format of the textual representation, default is RFC3339
381383
*/
382-
layout?: DateLayout;
384+
layout?: DateLayout | string;
383385

384386
/**
385387
* The timestamp of the date, default is current UTC time
@@ -455,3 +457,7 @@ export interface JSONObject {
455457
}
456458

457459
export const RFC3339 = "RFC3339";
460+
461+
export function patch(target: any, patch: any): any;
462+
463+
export const Delete: unique symbol;

types/mokapi/kafka.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,4 @@ export interface ProduceRetry {
243243
* @default 5
244244
*/
245245
retries: number;
246-
}
246+
}

0 commit comments

Comments
 (0)