go-mokapi 0.29.0 → 0.30.1

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.29.0",
3
+ "version": "0.30.1",
4
4
  "description": "Easy and flexible API mocking",
5
5
  "homepage": "https://mokapi.io",
6
6
  "repository": {
package/types/http.d.ts CHANGED
@@ -86,11 +86,83 @@ export function del(url: string, body?: JSONValue, args?: Args): Response;
86
86
  */
87
87
  export function options(url: string, body?: JSONValue, args?: Args): Response;
88
88
 
89
- export function fetch(url: string, args: FetchOptions): Promise<Response>
89
+ /**
90
+ * Sends an HTTP request and returns a Promise resolving to the response.
91
+ *
92
+ * @param {string} url - The URL to request.
93
+ * @param {FetchOptions} opts - Optional fetch configuration.
94
+ * @returns {Promise<Response>} A Promise that resolves with the response.
95
+ *
96
+ * @example
97
+ * // Simple GET request
98
+ * const res = await fetch('https://foo.bar/api');
99
+ * const data = await res.json();
100
+ *
101
+ * @example
102
+ * // POST request with JSON body
103
+ * const res = await fetch('https://foo.bar/api', {
104
+ * method: 'POST',
105
+ * headers: { 'Content-Type': 'application/json' },
106
+ * body: { name: 'Alice' }
107
+ * });
108
+ *
109
+ * @example
110
+ * // Request with timeout and redirect settings
111
+ * const res = await fetch('https://foo.bar/api', {
112
+ * timeout: '5s',
113
+ * maxRedirects: 2
114
+ * });
115
+ */
116
+ export function fetch(url: string, opts?: FetchOptions): Promise<Response>
90
117
 
118
+ /**
119
+ * Options for the {@link fetch} function.
120
+ */
91
121
  export interface FetchOptions {
92
- method: string
93
- body: any
122
+ /**
123
+ * HTTP method to use for the request.
124
+ * @default "GET"
125
+ * @example
126
+ * const res = await fetch(url, { method: 'POST' });
127
+ */
128
+ method?: string;
129
+
130
+ /**
131
+ * The body of the request, such as a string or object.
132
+ * @example
133
+ * const res = await fetch(url, { body: JSON.stringify({ name: 'Alice' }) });
134
+ */
135
+ body?: any;
136
+
137
+ /**
138
+ * Request headers.
139
+ * @example
140
+ * const res = await fetch(url, {
141
+ * headers: { 'Authorization': 'Bearer token123' }
142
+ * });
143
+ */
144
+ headers?: { [name: string]: string };
145
+
146
+ /**
147
+ * The number of redirects to follow for this request.
148
+ * @default 5
149
+ * @example
150
+ * const res = await fetch(url, { maxRedirects: 1 });
151
+ */
152
+ maxRedirects?: number;
153
+
154
+ /**
155
+ * Maximum time to wait for the request to complete. Default
156
+ * timeout is 60 seconds ("60s"). The type can also be a number, in which
157
+ * case Mokapi interprets it as milliseconds
158
+ * @default "60s"
159
+ * @example
160
+ * const res = get(url, { timeout: '5m' })
161
+ * @example
162
+ * // Timeout as milliseconds
163
+ * const res = await fetch(url, { timeout: 3000 });
164
+ */
165
+ timeout?: number | string;
94
166
  }
95
167
 
96
168
  /**
@@ -103,8 +175,8 @@ export interface Args {
103
175
  /**
104
176
  * The number of redirects to follow for this request.
105
177
  * @default 5
106
- **/
107
- maxRedirects?: number
178
+ */
179
+ maxRedirects?: number;
108
180
  /**
109
181
  * Maximum time to wait for the request to complete. Default
110
182
  * timeout is 60 seconds ("60s"). The type can also be a number, in which
@@ -112,7 +184,7 @@ export interface Args {
112
184
  * @example
113
185
  * const res = get(url, { timeout: '5m' })
114
186
  */
115
- timeout?: number | string
187
+ timeout?: number | string;
116
188
  }
117
189
 
118
190
  /**
package/types/index.d.ts CHANGED
@@ -29,7 +29,7 @@ import "./mail";
29
29
  * })
30
30
  * }
31
31
  */
32
- export function on<T extends keyof EventHandler>(event: T, handler: EventHandler[T], args?: EventArgs): void | Promise<void>;
32
+ export function on<T extends keyof EventHandler>(event: T, handler: EventHandler[T], args?: EventArgs): void;
33
33
 
34
34
  /**
35
35
  * Schedules a new periodic job with interval.
@@ -122,7 +122,7 @@ export interface EventHandler {
122
122
  * })
123
123
  * }
124
124
  */
125
- export type HttpEventHandler = (request: HttpRequest, response: HttpResponse) => void;
125
+ export type HttpEventHandler = (request: HttpRequest, response: HttpResponse) => void | Promise<void>;
126
126
 
127
127
  /**
128
128
  * HttpRequest is an object used by HttpEventHandler that contains request-specific
@@ -133,7 +133,7 @@ export interface HttpRequest {
133
133
  /**
134
134
  * Request method.
135
135
  * @example GET
136
- * */
136
+ */
137
137
  readonly method: string;
138
138
 
139
139
  /** Represents a parsed URL. */
@@ -162,6 +162,9 @@ export interface HttpRequest {
162
162
 
163
163
  /** OperationId defined in OpenAPI */
164
164
  readonly operationId: string;
165
+
166
+ /** Returns a string representing this HttpRequest object. */
167
+ toString(): string
165
168
  }
166
169
 
167
170
  /**
@@ -170,7 +173,7 @@ export interface HttpRequest {
170
173
  */
171
174
  export interface HttpResponse {
172
175
  /** Object contains header parameters specified by OpenAPI header parameters. */
173
- headers: { [key: string]: string };
176
+ headers: { [key: string]: any };
174
177
 
175
178
  /** Specifies the http status used to select the OpenAPI response definition. */
176
179
  statusCode: number;
@@ -192,11 +195,17 @@ export interface Url {
192
195
  /** URL host. */
193
196
  readonly host: string;
194
197
 
198
+ /** URL port */
199
+ readonly port: number;
200
+
195
201
  /** URL path. */
196
202
  readonly path: string;
197
203
 
198
204
  /** URL query string. */
199
205
  readonly query: string;
206
+
207
+ /** Returns a string representing this Url object. */
208
+ toString(): string;
200
209
  }
201
210
 
202
211
  /**
@@ -417,12 +426,6 @@ export type DateLayout =
417
426
  *
418
427
  * Use this to customize how the event appears in the dashboard or to control tracking.
419
428
  *
420
- * @property tags Optional key-value pairs used to label the event in the dashboard.
421
- *
422
- * @property track Set to `true` to enable tracking of this event handler in the dashboard.
423
- * If omitted, Mokapi automatically checks whether the response object has
424
- * been modified and tracks the handler only if a change is detected.
425
- *
426
429
  * @example
427
430
  * export default function() {
428
431
  * on('http', function(req, res) {
@@ -435,7 +438,7 @@ export type DateLayout =
435
438
  */
436
439
  export interface EventArgs {
437
440
  /**
438
- * Adds or overrides existing tags used in dashboard
441
+ * Adds or overrides existing tags used to label the event in dashboard
439
442
  */
440
443
  tags?: { [key: string]: string };
441
444
 
@@ -448,7 +451,7 @@ export interface EventArgs {
448
451
  }
449
452
 
450
453
  /**
451
- * cheduledEventArgs is an object used by every and cron function.
454
+ * ScheduledEventHandler is an object used by every and cron function.
452
455
  * https://mokapi.io/docs/javascript-api/mokapi/eventhandler/scheduledeventargs
453
456
  * @example
454
457
  * export default function() {
@@ -549,7 +552,7 @@ export interface SharedMemory {
549
552
  * @param key The key to retrieve.
550
553
  * @returns The stored value, or `undefined` if not found.
551
554
  */
552
- get<T = any>(key: string): T | undefined
555
+ get(key: string): any;
553
556
 
554
557
  /**
555
558
  * Sets a value for the given key.
@@ -557,7 +560,7 @@ export interface SharedMemory {
557
560
  * @param key The key to store the value under.
558
561
  * @param value The value to store.
559
562
  */
560
- set<T = any>(key: string, value: T): void
563
+ set(key: string, value: any): void;
561
564
 
562
565
  /**
563
566
  * Updates a value atomically using an updater function.
@@ -573,32 +576,32 @@ export interface SharedMemory {
573
576
  * @param updater Function that receives the current value and returns the new value.
574
577
  * @returns The new value after update.
575
578
  */
576
- update<T = any>(key: string, updater: (value: T | undefined) => T): T
579
+ update<T = any>(key: string, updater: (value: T | undefined) => T): T;
577
580
 
578
581
  /**
579
582
  * Checks if the given key exists in shared memory.
580
583
  * @param key The key to check.
581
584
  * @returns `true` if the key exists, otherwise `false`.
582
585
  */
583
- has(key: string): boolean
586
+ has(key: string): boolean;
584
587
 
585
588
  /**
586
589
  * Removes the specified key and its value from shared memory.
587
590
  * @param key The key to remove.
588
591
  */
589
- delete(key: string): void
592
+ delete(key: string): void;
590
593
 
591
594
  /**
592
595
  * Removes all stored entries from shared memory.
593
596
  * Use with caution — this clears all shared state.
594
597
  */
595
- clear(): void
598
+ clear(): void;
596
599
 
597
600
  /**
598
601
  * Returns a list of all stored keys.
599
602
  * @returns An array of key names.
600
603
  */
601
- keys(): string[]
604
+ keys(): string[];
602
605
 
603
606
  /**
604
607
  * Creates or returns a namespaced shared memory store.
@@ -613,7 +616,7 @@ export interface SharedMemory {
613
616
  * @param name The namespace identifier.
614
617
  * @returns A `SharedMemory` object scoped to the given namespace.
615
618
  */
616
- namespace(name: string): SharedMemory
619
+ namespace(name: string): SharedMemory;
617
620
  }
618
621
 
619
622
  /**
@@ -637,4 +640,4 @@ export interface SharedMemory {
637
640
  * mokapi.log(`Current counter: ${count}`)
638
641
  * ```
639
642
  */
640
- export const shared: SharedMemory
643
+ export const shared: SharedMemory;